ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdobj.c
Revision: 3.17
Committed: Tue Mar 30 20:40:04 2004 UTC (20 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 3.16: +2 -3 lines
Log Message:
ANSI-discovered fixes thanks to Schorsh

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rhdobj.c,v 3.16 2004/01/01 11:21:55 schorsch Exp $";
3 #endif
4 /*
5 * Routines for loading and displaying Radiance objects in rholo with GLX.
6 */
7
8 #include <string.h>
9 #include <ctype.h>
10
11 #include "radogl.h"
12 #include "tonemap.h"
13 #include "rhdisp.h"
14 #include "rhdriver.h"
15 #include "rhdobj.h"
16 #include "rtprocess.h"
17
18 extern FILE *sstdout; /* user standard output */
19
20 char rhdcmd[DO_NCMDS][8] = DO_INIT; /* user command list */
21
22 /* pointer to function to get lights */
23 void (*dobj_lightsamp)(COLR clr, FVECT direc, FVECT pos) = NULL;
24
25 #define AVGREFL 0.5 /* assumed average reflectance */
26
27 #define MAXAC 512 /* maximum number of args */
28
29 #ifndef MINTHRESH
30 #define MINTHRESH 5.0 /* source threshold w.r.t. mean */
31 #endif
32
33 #ifndef NALT
34 #define NALT 11 /* # sampling altitude angles */
35 #endif
36 #ifndef NAZI
37 #define NAZI ((int)(PI/2.*NALT+.5))
38 #endif
39
40 typedef struct dlights {
41 struct dlights *next; /* next in lighting set list */
42 FVECT lcent; /* computed lighting center */
43 double ravg; /* harmonic mean free range radius */
44 TMbright larb; /* average reflected brightness */
45 COLOR lamb; /* local ambient value */
46 short nl; /* number of lights in this set */
47 struct lsource {
48 FVECT direc; /* source direction */
49 double omega; /* source solid angle */
50 COLOR val; /* source color */
51 } li[MAXLIGHTS]; /* light sources */
52 } DLIGHTS; /* a light source set */
53
54 typedef struct dobject {
55 struct dobject *next; /* next object in list */
56 char name[64]; /* object name */
57 FVECT center; /* orig. object center */
58 RREAL radius; /* orig. object radius */
59 int listid; /* GL display list identifier */
60 int nlists; /* number of lists allocated */
61 SUBPROC rtp; /* associated rtrace process */
62 DLIGHTS *ol; /* object lights */
63 FULLXF xfb; /* coordinate transform */
64 short drawcode; /* drawing code */
65 short xfac; /* transform argument count */
66 char *xfav[MAXAC+1]; /* transform args */
67 } DOBJECT; /* a displayable object */
68
69 static DLIGHTS *dlightsets; /* lighting sets */
70 static DOBJECT *dobjects; /* display object list */
71 static DOBJECT *curobj; /* current (last referred) object */
72 static int lastxfac; /* last number of transform args */
73 static char *lastxfav[MAXAC+1]; /* saved transform arguments */
74
75 #define getdcent(c,op) multp3(c,(op)->center,(op)->xfb.f.xfm)
76 #define getdrad(op) ((op)->radius*(op)->xfb.f.sca)
77
78 #define RTARGC 8
79 static char *rtargv[RTARGC+1] = {"rtrace", "-h-", "-w-", "-fdd",
80 "-x", "1", "-oL"};
81
82 static struct {
83 int nsamp; /* number of ray samples */
84 COLOR val; /* value (sum) */
85 } ssamp[NALT][NAZI]; /* current sphere samples */
86
87 #define curname (curobj==NULL ? (char *)NULL : curobj->name)
88
89 static DOBJECT *getdobj(char *nm);
90 static int freedobj(DOBJECT *op);
91 static int savedxf(DOBJECT *op);
92 static void ssph_sample(COLR clr, FVECT direc, FVECT pos);
93 static void ssph_direc(FVECT direc, int alt, int azi);
94 static int ssph_neigh(int sp[2], int next);
95 static int ssph_compute(void);
96 static int getdlights(DOBJECT *op, int force);
97 static void cmderror(int cn, char *err);
98
99
100 static DOBJECT *
101 getdobj( /* get object from list by name */
102 char *nm
103 )
104 {
105 register DOBJECT *op;
106
107 if (nm == NULL)
108 return(NULL);
109 if (nm == curname)
110 return(curobj);
111 for (op = dobjects; op != NULL; op = op->next)
112 if (!strcmp(op->name, nm))
113 break;
114 return(op);
115 }
116
117
118 static int
119 freedobj( /* free resources and memory assoc. with op */
120 register DOBJECT *op
121 )
122 {
123 int foundlink = 0;
124 DOBJECT ohead;
125 register DOBJECT *opl;
126
127 if (op == NULL)
128 return(0);
129 ohead.next = dobjects;
130 for (opl = &ohead; opl->next != NULL; opl = opl->next) {
131 if (opl->next == op && (opl->next = op->next) == NULL)
132 break;
133 foundlink += opl->next->listid == op->listid;
134 }
135 dobjects = ohead.next;
136 if (!foundlink) {
137 glDeleteLists(op->listid, op->nlists);
138 close_process(&(op->rtp));
139 }
140 while (op->xfac)
141 freestr(op->xfav[--op->xfac]);
142 free((void *)op);
143 return(1);
144 }
145
146
147 static int
148 savedxf( /* save transform for display object */
149 register DOBJECT *op
150 )
151 {
152 /* free old */
153 while (lastxfac)
154 freestr(lastxfav[--lastxfac]);
155 /* nothing to save? */
156 if (op == NULL) {
157 lastxfav[0] = NULL;
158 return(0);
159 }
160 /* else save new */
161 for (lastxfac = 0; lastxfac < op->xfac; lastxfac++)
162 lastxfav[lastxfac] = savestr(op->xfav[lastxfac]);
163 lastxfav[lastxfac] = NULL;
164 return(1);
165 }
166
167
168 static void
169 ssph_sample( /* add sample to current source sphere */
170 COLR clr,
171 FVECT direc,
172 FVECT pos
173 )
174 {
175 COLOR col;
176 double d;
177 register int alt, azi;
178
179 if (dlightsets == NULL)
180 return;
181 if (pos == NULL)
182 d = FHUGE; /* sample is at infinity */
183 else if ((d = (pos[0] - dlightsets->lcent[0])*direc[0] +
184 (pos[1] - dlightsets->lcent[1])*direc[1] +
185 (pos[2] - dlightsets->lcent[2])*direc[2]) > FTINY)
186 dlightsets->ravg += 1./d;
187 else
188 return; /* sample is behind us */
189 alt = NALT*(1.-FTINY)*(.5*(1.+FTINY) + direc[2]*.5);
190 azi = NAZI*(1.-FTINY)*(.5*(1.+FTINY) + .5/PI*atan2(direc[1],direc[0]));
191 colr_color(col, clr);
192 addcolor(ssamp[alt][azi].val, col);
193 ssamp[alt][azi].nsamp++;
194 }
195
196
197 static void
198 ssph_direc( /* compute sphere sampling direction */
199 FVECT direc,
200 int alt,
201 int azi
202 )
203 {
204 double phi, d;
205
206 direc[2] = 2./NALT*(alt+.5) - 1.;
207 d = sqrt(1. - direc[2]*direc[2]);
208 phi = 2.*PI/NAZI*(azi+.5) - PI;
209 direc[0] = d*cos(phi);
210 direc[1] = d*sin(phi);
211 }
212
213
214 static int
215 ssph_neigh( /* neighbor counter on sphere */
216 register int sp[2],
217 int next
218 )
219 {
220 static short nneigh = 0; /* neighbor count */
221 static short neighlist[NAZI+6][2]; /* neighbor list (0 is home) */
222 register int i;
223
224 if (next) {
225 if (nneigh <= 0)
226 return(0);
227 sp[0] = neighlist[--nneigh][0];
228 sp[1] = neighlist[nneigh][1];
229 return(1);
230 }
231 if ((sp[0] < 0) | (sp[0] >= NALT) | (sp[1] < 0) | (sp[1] >= NAZI))
232 return(nneigh=0);
233 neighlist[0][0] = sp[0]; neighlist[0][1] = sp[1];
234 nneigh = 1;
235 if (sp[0] == 0) {
236 neighlist[nneigh][0] = 1;
237 neighlist[nneigh++][1] = (sp[1]+1)%NAZI;
238 neighlist[nneigh][0] = 1;
239 neighlist[nneigh++][1] = sp[1];
240 neighlist[nneigh][0] = 1;
241 neighlist[nneigh++][1] = (sp[1]+(NAZI-1))%NAZI;
242 for (i = 0; i < NAZI; i++)
243 if (i != sp[1]) {
244 neighlist[nneigh][0] = 0;
245 neighlist[nneigh++][1] = i;
246 }
247 } else if (sp[0] == NALT-1) {
248 neighlist[nneigh][0] = NALT-2;
249 neighlist[nneigh++][1] = (sp[1]+1)%NAZI;
250 neighlist[nneigh][0] = NALT-2;
251 neighlist[nneigh++][1] = sp[1];
252 neighlist[nneigh][0] = NALT-2;
253 neighlist[nneigh++][1] = (sp[1]+(NAZI-1))%NAZI;
254 for (i = 0; i < NAZI; i++)
255 if (i != sp[1]) {
256 neighlist[nneigh][0] = NALT-1;
257 neighlist[nneigh++][1] = i;
258 }
259 } else {
260 neighlist[nneigh][0] = sp[0]-1;
261 neighlist[nneigh++][1] = (sp[1]+1)%NAZI;
262 neighlist[nneigh][0] = sp[0]-1;
263 neighlist[nneigh++][1] = sp[1];
264 neighlist[nneigh][0] = sp[0]-1;
265 neighlist[nneigh++][1] = (sp[1]+(NAZI-1))%NAZI;
266 neighlist[nneigh][0] = sp[0];
267 neighlist[nneigh++][1] = (sp[1]+1)%NAZI;
268 neighlist[nneigh][0] = sp[0];
269 neighlist[nneigh++][1] = (sp[1]+(NAZI-1))%NAZI;
270 neighlist[nneigh][0] = sp[0]+1;
271 neighlist[nneigh++][1] = (sp[1]+1)%NAZI;
272 neighlist[nneigh][0] = sp[0]+1;
273 neighlist[nneigh++][1] = sp[1];
274 neighlist[nneigh][0] = sp[0]+1;
275 neighlist[nneigh++][1] = (sp[1]+(NAZI-1))%NAZI;
276 }
277 return(nneigh);
278 }
279
280
281 static int
282 ssph_compute(void) /* compute source set from sphere samples */
283 {
284 int ncells, nsamps;
285 COLOR csum;
286 FVECT v;
287 double d, thresh, maxbr;
288 int maxalt, maxazi, spos[2];
289 register int alt, azi;
290 register struct lsource *ls;
291 /* count & average sampled cells */
292 setcolor(csum, 0., 0., 0.);
293 ncells = nsamps = 0;
294 for (alt = 0; alt < NALT; alt++)
295 for (azi = 0; azi < NAZI; azi++)
296 if (ssamp[alt][azi].nsamp) {
297 if (ssamp[alt][azi].nsamp > 1) {
298 d = 1.0/ssamp[alt][azi].nsamp;
299 scalecolor(ssamp[alt][azi].val, d);
300 }
301 addcolor(csum, ssamp[alt][azi].val);
302 nsamps += ssamp[alt][azi].nsamp;
303 ncells++;
304 }
305 if ((dlightsets == NULL) | (ncells < NALT*NAZI/4)) {
306 ncells = 0;
307 goto done;
308 }
309 /* harmonic mean distance */
310 if (dlightsets->ravg > FTINY)
311 dlightsets->ravg = nsamps / dlightsets->ravg;
312 else
313 dlightsets->ravg = FHUGE;
314 /* light source threshold */
315 thresh = MINTHRESH*bright(csum)/ncells;
316 if (thresh <= FTINY) {
317 ncells = 0;
318 goto done;
319 }
320 /* avg. reflected brightness */
321 d = AVGREFL / (double)ncells;
322 scalecolor(csum, d);
323 if (tmCvColors(&dlightsets->larb, TM_NOCHROM, &csum, 1) != TM_E_OK)
324 error(CONSISTENCY, "tone mapping problem in ssph_compute");
325 /* greedy light source clustering */
326 while (dlightsets->nl < MAXLIGHTS) {
327 maxbr = 0.; /* find brightest cell */
328 for (alt = 0; alt < NALT; alt++)
329 for (azi = 0; azi < NAZI; azi++)
330 if ((d = bright(ssamp[alt][azi].val)) > maxbr) {
331 maxalt = alt; maxazi = azi;
332 maxbr = d;
333 }
334 if (maxbr < thresh) /* below threshold? */
335 break;
336 ls = dlightsets->li + dlightsets->nl++;
337 spos[0] = maxalt; spos[1] = maxazi; /* cluster */
338 for (ssph_neigh(spos, 0); ssph_neigh(spos, 1); ) {
339 alt = spos[0]; azi = spos[1];
340 if ((d = bright(ssamp[alt][azi].val)) < .75*thresh)
341 continue; /* too dim */
342 ssph_direc(v, alt, azi); /* else add it in */
343 VSUM(ls->direc, ls->direc, v, d);
344 ls->omega += 1.;
345 addcolor(ls->val, ssamp[alt][azi].val);
346 /* remove from list */
347 setcolor(ssamp[alt][azi].val, 0., 0., 0.);
348 ssamp[alt][azi].nsamp = 0;
349 }
350 d = 1./ls->omega; /* avg. brightness */
351 scalecolor(ls->val, d);
352 ls->omega *= 4.*PI/(NALT*NAZI); /* solid angle */
353 normalize(ls->direc); /* direction */
354 }
355 /* compute ambient remainder */
356 for (alt = 0; alt < NALT; alt++)
357 for (azi = 0; azi < NAZI; azi++)
358 if (ssamp[alt][azi].nsamp)
359 addcolor(dlightsets->lamb, ssamp[alt][azi].val);
360 d = 1.0/ncells;
361 scalecolor(dlightsets->lamb, d);
362 done: /* clear sphere sample array */
363 memset((void *)ssamp, '\0', sizeof(ssamp));
364 return(ncells);
365 }
366
367
368 static int
369 getdlights( /* get lights for display object */
370 register DOBJECT *op,
371 int force
372 )
373 {
374 double d2, mind2 = FHUGE*FHUGE;
375 FVECT ocent;
376 VIEW cvw;
377 register DLIGHTS *dl;
378
379 op->ol = NULL;
380 if (op->drawcode != DO_LIGHT)
381 return(0);
382 /* check for usable light set */
383 getdcent(ocent, op);
384 for (dl = dlightsets; dl != NULL; dl = dl->next)
385 if ((d2 = dist2(dl->lcent, ocent)) < mind2) {
386 op->ol = dl;
387 mind2 = d2;
388 }
389 /* the following is heuristic */
390 d2 = 2.*getdrad(op); d2 *= d2;
391 if ((dl = op->ol) != NULL && (mind2 < 0.0625*dl->ravg*dl->ravg ||
392 mind2 < 4.*getdrad(op)*getdrad(op)))
393 return(1);
394 if (!force)
395 return(0);
396 /* need to compute new light set */
397 cvw = stdview;
398 cvw.type = VT_PER;
399 VCOPY(cvw.vp, ocent);
400 cvw.vup[0] = 1.; cvw.vup[1] = cvw.vup[2] = 0.;
401 cvw.horiz = 90; cvw.vert = 90.;
402 beam_init(1); /* query beams through center */
403 cvw.vdir[0] = cvw.vdir[1] = 0.; cvw.vdir[2] = 1.;
404 setview(&cvw); beam_view(&cvw, 0, 0);
405 cvw.vdir[0] = cvw.vdir[1] = 0.; cvw.vdir[2] = -1.;
406 setview(&cvw); beam_view(&cvw, 0, 0);
407 cvw.vup[0] = cvw.vup[1] = 0.; cvw.vup[2] = 1.;
408 cvw.vdir[0] = cvw.vdir[2] = 0.; cvw.vdir[1] = 1.;
409 setview(&cvw); beam_view(&cvw, 0, 0);
410 cvw.vdir[0] = cvw.vdir[2] = 0.; cvw.vdir[1] = -1.;
411 setview(&cvw); beam_view(&cvw, 0, 0);
412 cvw.vdir[1] = cvw.vdir[2] = 0.; cvw.vdir[0] = 1.;
413 setview(&cvw); beam_view(&cvw, 0, 0);
414 cvw.vdir[1] = cvw.vdir[2] = 0.; cvw.vdir[0] = -1.;
415 setview(&cvw); beam_view(&cvw, 0, 0);
416 /* allocate new light set */
417 dl = (DLIGHTS *)calloc(1, sizeof(DLIGHTS));
418 if (dl == NULL)
419 goto memerr;
420 VCOPY(dl->lcent, ocent);
421 /* push onto our light set list */
422 dl->next = dlightsets;
423 dlightsets = dl;
424 dobj_lightsamp = ssph_sample; /* get beams from server */
425 imm_mode = beam_sync(-1) > 0;
426 while (imm_mode)
427 if (serv_result() == DS_SHUTDOWN)
428 quit(0);
429 if (!ssph_compute()) { /* compute light sources from sphere */
430 dlightsets = dl->next;
431 free((void *)dl);
432 return(0);
433 }
434 op->ol = dl;
435 return(1);
436 memerr:
437 error(SYSTEM, "out of memory in getdlights");
438 return 0; /* pro forma return */
439 }
440
441
442 static void
443 cmderror( /* report command error */
444 int cn,
445 char *err
446 )
447 {
448 sprintf(errmsg, "%s: %s", rhdcmd[cn], err);
449 error(COMMAND, errmsg);
450 }
451
452
453 extern int
454 dobj_command( /* run object display command */
455 char *cmd,
456 register char *args
457 )
458 {
459 int somechange = 0;
460 int cn, na;
461 register int nn;
462 char *alist[MAXAC+1], *nm;
463 /* find command */
464 for (cn = 0; cn < DO_NCMDS; cn++)
465 if (!strcmp(cmd, rhdcmd[cn]))
466 break;
467 if (cn >= DO_NCMDS)
468 return(-1); /* not in our list */
469 /* make argument list */
470 for (na = 0; *args; na++) {
471 if (na > MAXAC)
472 goto toomany;
473 alist[na] = args;
474 while (*args && !isspace(*args))
475 args++;
476 while (isspace(*args))
477 *args++ = '\0';
478 }
479 alist[na] = NULL;
480 /* execute command */
481 switch (cn) {
482 case DO_LOAD: /* load an octree */
483 if (na == 1)
484 dobj_load(alist[0], alist[0]);
485 else if (na == 2)
486 dobj_load(alist[0], alist[1]);
487 else {
488 cmderror(cn, "need octree [name]");
489 return(0);
490 }
491 break;
492 case DO_UNLOAD: /* clear an object */
493 if (na > 1) goto toomany;
494 if (na && alist[0][0] == '*')
495 somechange += dobj_cleanup();
496 else
497 somechange += dobj_unload(na ? alist[0] : curname);
498 break;
499 case DO_XFORM: /* transform object */
500 case DO_MOVE:
501 if (na && alist[0][0] != '-') {
502 nm = alist[0]; nn = 1;
503 } else {
504 nm = curname; nn = 0;
505 }
506 if (cn == DO_MOVE && nn >= na) {
507 cmderror(cn, "missing transform");
508 return(0);
509 }
510 somechange += dobj_xform(nm, cn==DO_MOVE, na-nn, alist+nn);
511 break;
512 case DO_UNMOVE: /* undo last transform */
513 somechange += dobj_unmove();
514 break;
515 case DO_OBJECT: /* print object statistics */
516 if (dobj_putstats(na ? alist[0] : curname, sstdout))
517 if (na && alist[0][0] != '*' && (curobj == NULL ||
518 strcmp(alist[0], curobj->name)))
519 savedxf(curobj = getdobj(alist[0]));
520 break;
521 case DO_DUP: /* duplicate object */
522 for (nn = 0; nn < na; nn++)
523 if (alist[nn][0] == '-')
524 break;
525 switch (nn) {
526 case 0:
527 cmderror(cn, "need new object name");
528 return(0);
529 case 1:
530 nm = curname;
531 break;
532 case 2:
533 nm = alist[0];
534 break;
535 default:
536 goto toomany;
537 }
538 if (!dobj_dup(nm, alist[nn-1]))
539 break;
540 if (na > nn)
541 somechange += dobj_xform(curname, 1, na-nn, alist+nn);
542 else
543 curobj->drawcode = DO_HIDE;
544 savedxf(curobj);
545 break;
546 case DO_SHOW: /* change rendering option */
547 case DO_LIGHT:
548 case DO_HIDE:
549 if (na > 1) goto toomany;
550 dobj_lighting(na ? alist[0] : curname, cn);
551 somechange++;
552 break;
553 default:
554 error(CONSISTENCY, "bad command id in dobj_command");
555 }
556 return(somechange);
557 toomany:
558 cmderror(cn, "too many arguments");
559 return(-1);
560 }
561
562
563 extern int
564 dobj_load( /* create/load an octree object */
565 char *oct,
566 char *nam
567 )
568 {
569 char *fpp, fpath[128];
570 register DOBJECT *op;
571 /* check arguments */
572 if (oct == NULL) {
573 error(COMMAND, "missing octree");
574 return(0);
575 }
576 if (nam == NULL) {
577 error(COMMAND, "missing name");
578 return(0);
579 }
580 if ((*nam == '*') | (*nam == '-')) {
581 error(COMMAND, "illegal name");
582 return(0);
583 }
584 if (getdobj(nam) != NULL) {
585 error(COMMAND, "name already taken (clear first)");
586 return(0);
587 }
588 /* get octree path */
589 if ((fpp = getpath(oct, getrlibpath(), R_OK)) == NULL) {
590 sprintf(errmsg, "cannot find octree \"%s\"", oct);
591 error(COMMAND, errmsg);
592 return(0);
593 }
594 strcpy(fpath, fpp);
595 op = (DOBJECT *)malloc(sizeof(DOBJECT));
596 if (op == NULL)
597 error(SYSTEM, "out of memory in dobj_load");
598 /* set struct fields */
599 strcpy(op->name, nam);
600 op->ol = NULL;
601 op->drawcode = DO_HIDE;
602 setident4(op->xfb.f.xfm); op->xfb.f.sca = 1.;
603 setident4(op->xfb.b.xfm); op->xfb.b.sca = 1.;
604 op->xfav[op->xfac=0] = NULL;
605 /* load octree into display list */
606 dolights = 0;
607 domats = 1;
608 op->listid = rgl_octlist(fpath, op->center, &op->radius, &op->nlists);
609 /* start rtrace */
610 rtargv[RTARGC-1] = fpath;
611 rtargv[RTARGC] = NULL;
612 open_process(&(op->rtp), rtargv);
613 /* insert into main list */
614 op->next = dobjects;
615 curobj = dobjects = op;
616 savedxf(NULL);
617 return(1);
618 }
619
620
621 extern int
622 dobj_unload( /* free the named object */
623 char *nam
624 )
625 {
626 register DOBJECT *op;
627
628 if ((op = getdobj(nam)) == NULL) {
629 error(COMMAND, "no object");
630 return(0);
631 }
632 freedobj(op);
633 savedxf(curobj = NULL);
634 return(1);
635 }
636
637
638 extern int
639 dobj_cleanup(void) /* free all resources */
640 {
641 register DLIGHTS *lp;
642
643 while (dobjects != NULL)
644 freedobj(dobjects);
645 savedxf(curobj = NULL);
646 while ((lp = dlightsets) != NULL) {
647 dlightsets = lp->next;
648 free((void *)lp);
649 }
650 return(1);
651 }
652
653
654 extern int
655 dobj_xform( /* set/add transform for nam */
656 char *nam,
657 int rel,
658 int ac,
659 char **av
660 )
661 {
662 register DOBJECT *op;
663 FVECT cent;
664 double rad;
665 char scoord[16];
666 int i;
667
668 if ((op = getdobj(nam)) == NULL) {
669 error(COMMAND, "no object");
670 return(0);
671 }
672 if (rel)
673 rel = op->xfac + 8;
674 if (ac + rel > MAXAC) {
675 error(COMMAND, "too many transform arguments");
676 return(0);
677 }
678 savedxf(curobj = op); /* remember current transform */
679 if (rel && ac == 4 && !strcmp(av[0], "-t"))
680 rel = -1; /* don't move for translate */
681 else {
682 getdcent(cent, op); /* don't move if near orig. */
683 rad = getdrad(op);
684 if (DOT(cent,cent) < rad*rad)
685 rel = -1;
686 }
687 if (!rel) { /* remove old transform */
688 while (op->xfac)
689 freestr(op->xfav[--op->xfac]);
690 } else if (rel > 0) { /* relative move */
691 op->xfav[op->xfac++] = savestr("-t");
692 for (i = 0; i < 3; i++) {
693 sprintf(scoord, "%.4e", -cent[i]);
694 op->xfav[op->xfac++] = savestr(scoord);
695 }
696 }
697 while (ac--)
698 op->xfav[op->xfac++] = savestr(*av++);
699 if (rel > 0) { /* move back */
700 op->xfav[op->xfac++] = savestr("-t");
701 for (i = 0; i < 3; i++) {
702 sprintf(scoord, "%.4e", cent[i]);
703 op->xfav[op->xfac++] = savestr(scoord);
704 }
705 }
706 op->xfav[op->xfac] = NULL;
707 if (fullxf(&op->xfb, op->xfac, op->xfav) != op->xfac) {
708 error(COMMAND, "bad transform arguments");
709 dobj_unmove();
710 savedxf(op); /* save current transform instead */
711 return(0);
712 }
713 /* don't know local lights anymore */
714 getdlights(op, 0);
715 return(1);
716 }
717
718
719 extern int
720 dobj_putstats( /* put out statistics for nam */
721 char *nam,
722 FILE *fp
723 )
724 {
725 FVECT ocent;
726 register DOBJECT *op;
727 register int i;
728
729 if (nam == NULL) {
730 error(COMMAND, "no current object");
731 return(0);
732 }
733 if (nam[0] == '*') {
734 i = 0;
735 for (op = dobjects; op != NULL; op = op->next)
736 i += dobj_putstats(op->name, fp);
737 return(i);
738 }
739 if ((op = getdobj(nam)) == NULL) {
740 error(COMMAND, "unknown object");
741 return(0);
742 }
743 getdcent(ocent, op);
744 fprintf(fp, "%s: %s, center [%g %g %g], radius %g", op->name,
745 op->drawcode==DO_HIDE ? "hidden" :
746 op->drawcode==DO_LIGHT && op->ol!=NULL ? "lighted" :
747 "shown",
748 ocent[0],ocent[1],ocent[2], getdrad(op));
749 if (op->xfac)
750 fputs(", (xform", fp);
751 for (i = 0; i < op->xfac; i++) {
752 putc(' ', fp);
753 fputs(op->xfav[i], fp);
754 }
755 if (op->xfac)
756 fputc(')', fp);
757 fputc('\n', fp);
758 return(1);
759 }
760
761
762 extern int
763 dobj_unmove(void) /* undo last transform change */
764 {
765 int txfac;
766 char *txfav[MAXAC+1];
767
768 if (curobj == NULL) {
769 error(COMMAND, "no current object");
770 return(0);
771 }
772 /* hold last transform */
773 memcpy((void *)txfav, (void *)lastxfav,
774 (txfac=lastxfac)*sizeof(char *));
775 /* save this transform */
776 memcpy((void *)lastxfav, (void *)curobj->xfav,
777 (lastxfac=curobj->xfac)*sizeof(char *));
778 /* copy back last transform */
779 memcpy((void *)curobj->xfav, (void *)txfav,
780 (curobj->xfac=txfac)*sizeof(char *));
781 /* set matrices */
782 fullxf(&curobj->xfb, curobj->xfac, curobj->xfav);
783 /* don't know local lights anymore */
784 getdlights(curobj, 0);
785 return(1);
786 }
787
788
789 extern int
790 dobj_dup( /* duplicate object oldnm as nam */
791 char *oldnm,
792 char *nam
793 )
794 {
795 register DOBJECT *op, *opdup;
796 /* check arguments */
797 if ((op = getdobj(oldnm)) == NULL) {
798 error(COMMAND, "no object");
799 return(0);
800 }
801 if (nam == NULL) {
802 error(COMMAND, "missing name");
803 return(0);
804 }
805 if ((*nam == '*') | (*nam == '-')) {
806 error(COMMAND, "illegal name");
807 return(0);
808 }
809 if (getdobj(nam) != NULL) {
810 error(COMMAND, "name already taken (clear first)");
811 return(0);
812 }
813 /* allocate and copy struct */
814 opdup = (DOBJECT *)malloc(sizeof(DOBJECT));
815 if (opdup == NULL)
816 error(SYSTEM, "out of memory in dobj_dup");
817 *opdup = *op;
818 /* rename */
819 strcpy(opdup->name, nam);
820 /* get our own copy of transform */
821 for (opdup->xfac = 0; opdup->xfac < op->xfac; opdup->xfac++)
822 opdup->xfav[opdup->xfac] = savestr(op->xfav[opdup->xfac]);
823 opdup->xfav[opdup->xfac] = NULL;
824 /* insert it into our list */
825 opdup->next = dobjects;
826 curobj = dobjects = opdup;
827 return(1);
828 }
829
830
831 extern int
832 dobj_lighting( /* set up lighting for display object */
833 char *nam,
834 int cn
835 )
836 {
837 int i, res[2];
838 VIEW *dv;
839 register DOBJECT *op;
840
841 if (nam == NULL) {
842 error(COMMAND, "no current object");
843 return(0);
844 }
845 if (nam[0] == '*') {
846 for (op = dobjects; op != NULL; op = op->next)
847 if ((op->drawcode = cn) == DO_LIGHT)
848 getdlights(op, 1);
849 else
850 op->ol = NULL;
851 } else if ((op = getdobj(nam)) == NULL) {
852 error(COMMAND, "unknown object");
853 return(0);
854 } else if ((op->drawcode = cn) == DO_LIGHT) {
855 if (!getdlights(op, 1))
856 error(COMMAND, "insufficient samples to light object");
857 } else
858 op->ol = NULL;
859
860 if (dobj_lightsamp != NULL) { /* restore beam set */
861 dobj_lightsamp = NULL;
862 beam_init(1);
863 for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++)
864 beam_view(dv, res[0], res[1]);
865 beam_sync(1); /* update server */
866 }
867 return 0; /* XXX not sure if this is the right value */
868 }
869
870
871 extern double
872 dobj_trace( /* check for ray intersection with object(s) */
873 char nm[],
874 FVECT rorg,
875 FVECT rdir
876 )
877 {
878 register DOBJECT *op;
879 FVECT xorg, xdir;
880 double darr[6];
881 /* check each visible object? */
882 if (nm == NULL || *nm == '*') {
883 double dist, mindist = 1.01*FHUGE;
884
885 if (nm != NULL) nm[0] = '\0';
886 for (op = dobjects; op != NULL; op = op->next) {
887 if (op->drawcode == DO_HIDE)
888 continue;
889 dist = dobj_trace(op->name, rorg, rdir);
890 if (dist < mindist) {
891 if (nm != NULL) strcpy(nm, op->name);
892 mindist = dist;
893 }
894 }
895 return(mindist);
896 }
897 /* else check particular object */
898 if ((op = getdobj(nm)) == NULL) {
899 error(COMMAND, "unknown object");
900 return(FHUGE);
901 }
902 if (op->xfac) { /* put ray in local coordinates */
903 multp3(xorg, rorg, op->xfb.b.xfm);
904 multv3(xdir, rdir, op->xfb.b.xfm);
905 VCOPY(darr, xorg); VCOPY(darr+3, xdir);
906 } else {
907 VCOPY(darr, rorg); VCOPY(darr+3, rdir);
908 }
909 /* trace it */
910 if (process(&(op->rtp), (char *)darr, (char *)darr, sizeof(double),
911 6*sizeof(double)) != sizeof(double))
912 error(SYSTEM, "rtrace communication error");
913 /* return distance */
914 if (darr[0] >= .99*FHUGE)
915 return(FHUGE);
916 return(darr[0]*op->xfb.f.sca);
917 }
918
919
920 extern int
921 dobj_render(void) /* render our objects in OpenGL */
922 {
923 int nrendered = 0;
924 GLboolean normalizing;
925 GLfloat vec[4];
926 FVECT v1;
927 register DOBJECT *op;
928 register int i;
929 /* anything to render? */
930 for (op = dobjects; op != NULL; op = op->next)
931 if (op->drawcode != DO_HIDE)
932 break;
933 if (op == NULL)
934 return(0);
935 /* set up general rendering params */
936 glGetBooleanv(GL_NORMALIZE, &normalizing);
937 glPushAttrib(GL_LIGHTING_BIT|GL_TRANSFORM_BIT|GL_ENABLE_BIT|
938 GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_POLYGON_BIT);
939 glDepthFunc(GL_LESS);
940 glEnable(GL_DEPTH_TEST);
941 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
942 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
943 glShadeModel(GL_SMOOTH);
944 glFrontFace(GL_CCW);
945 glDisable(GL_CULL_FACE);
946 for (i = MAXLIGHTS; i--; )
947 glDisable(glightid[i]);
948 glEnable(GL_LIGHTING);
949 rgl_checkerr("setting rendering modes in dobj_render");
950 /* render each object */
951 for (op = dobjects; op != NULL; op = op->next) {
952 if (op->drawcode == DO_HIDE)
953 continue;
954 /* set up lighting */
955 if (op->drawcode == DO_LIGHT && op->ol != NULL) {
956 BYTE pval;
957 double expval, d;
958 /* use computed sources */
959 if (tmMapPixels(&pval, &op->ol->larb, TM_NOCHROM, 1)
960 != TM_E_OK)
961 error(CONSISTENCY, "dobj_render w/o tone map");
962 expval = pval * (WHTEFFICACY/256.) /
963 tmLuminance(op->ol->larb);
964 vec[0] = expval * colval(op->ol->lamb,RED);
965 vec[1] = expval * colval(op->ol->lamb,GRN);
966 vec[2] = expval * colval(op->ol->lamb,BLU);
967 vec[3] = 1.;
968 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec);
969 for (i = op->ol->nl; i--; ) {
970 VCOPY(vec, op->ol->li[i].direc);
971 vec[3] = 0.;
972 glLightfv(glightid[i], GL_POSITION, vec);
973 d = expval * op->ol->li[i].omega;
974 vec[0] = d * colval(op->ol->li[i].val,RED);
975 vec[1] = d * colval(op->ol->li[i].val,GRN);
976 vec[2] = d * colval(op->ol->li[i].val,BLU);
977 vec[3] = 1.;
978 glLightfv(glightid[i], GL_SPECULAR, vec);
979 glLightfv(glightid[i], GL_DIFFUSE, vec);
980 vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.;
981 glLightfv(glightid[i], GL_AMBIENT, vec);
982 glEnable(glightid[i]);
983 }
984 } else { /* fake lighting */
985 vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.;
986 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec);
987 getdcent(v1, op);
988 VSUB(v1, odev.v.vp, v1);
989 if (normalize(v1) <= getdrad(op)) {
990 vec[0] = -odev.v.vdir[0];
991 vec[1] = -odev.v.vdir[1];
992 vec[2] = -odev.v.vdir[2];
993 } else
994 VCOPY(vec, v1);
995 vec[3] = 0.;
996 glLightfv(GL_LIGHT0, GL_POSITION, vec);
997 vec[0] = vec[1] = vec[2] = .7; vec[3] = 1.;
998 glLightfv(GL_LIGHT0, GL_SPECULAR, vec);
999 glLightfv(GL_LIGHT0, GL_DIFFUSE, vec);
1000 vec[0] = vec[1] = vec[2] = .3; vec[3] = 1.;
1001 glLightfv(GL_LIGHT0, GL_AMBIENT, vec);
1002 glEnable(GL_LIGHT0);
1003 }
1004 /* set up object transform */
1005 if (op->xfac) {
1006 if (!normalizing && (op->xfb.f.sca < 1.-FTINY) |
1007 (op->xfb.f.sca > 1.+FTINY))
1008 glEnable(GL_NORMALIZE);
1009 glMatrixMode(GL_MODELVIEW);
1010 glPushMatrix();
1011 /* matrix order works out to same */
1012 #ifdef SMLFLT
1013 glMultMatrixf((GLfloat *)op->xfb.f.xfm);
1014 #else
1015 glMultMatrixd((GLdouble *)op->xfb.f.xfm);
1016 #endif
1017 }
1018 /* render the display list */
1019 glCallList(op->listid);
1020 nrendered++;
1021 /* restore matrix */
1022 if (op->xfac) {
1023 glMatrixMode(GL_MODELVIEW);
1024 glPopMatrix();
1025 if (!normalizing)
1026 glDisable(GL_NORMALIZE);
1027 }
1028 /* restore lighting */
1029 if (op->drawcode == DO_LIGHT && op->ol != NULL)
1030 for (i = op->ol->nl; i--; )
1031 glDisable(glightid[i]);
1032 else
1033 glDisable(GL_LIGHT0);
1034 /* check errors */
1035 }
1036 glPopAttrib(); /* restore rendering params */
1037 rgl_checkerr("rendering objects in dobj_render");
1038 return(nrendered);
1039 }