ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.37
Committed: Fri Dec 18 11:56:10 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.36: +28 -21 lines
Log Message:
created new ogl driver with direct geometry rendering

File Contents

# User Rev Content
1 gregl 3.23 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 gregl 3.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Holodeck display process.
9     */
10    
11     #include "rholo.h"
12     #include "rhdisp.h"
13     #include "rhdriver.h"
14     #include "selcall.h"
15 gregl 3.10 #include <ctype.h>
16 gregl 3.1
17 gregl 3.12 #ifndef VIEWHISTLEN
18 gregl 3.16 #define VIEWHISTLEN 4 /* number of remembered views */
19 gregl 3.12 #endif
20    
21 gwlarson 3.36 #ifndef FSIZDEF
22     #define FSIZDEF 0.125 /* default focus frame size */
23     #endif
24    
25 gregl 3.1 HOLO *hdlist[HDMAX+1]; /* global holodeck list */
26    
27 gwlarson 3.37 char *hdofn[HDMAX+1]; /* holodeck section octree list */
28    
29 gregl 3.10 char cmdlist[DC_NCMDS][8] = DC_INIT;
30    
31 gregl 3.1 int imm_mode = 0; /* bundles are being delivered immediately */
32    
33 gregl 3.20 int do_outside = 0; /* render from outside sections */
34    
35 gwlarson 3.25 double eyesepdist = 1; /* eye separation distance */
36    
37 gregl 3.1 char *progname; /* global argv[0] */
38    
39 gregl 3.10 FILE *sstdin, *sstdout; /* server's standard input and output */
40 gregl 3.1
41 gregl 3.21 #ifdef DEBUG
42     #include <sys/types.h>
43     extern time_t time();
44     static time_t tmodesw;
45     static time_t timm, tadd;
46     static long nimmrays, naddrays;
47     #endif
48    
49 gregl 3.10 #define RDY_SRV 01
50     #define RDY_DEV 02
51     #define RDY_SIN 04
52 gregl 3.1
53 gregl 3.10
54 gregl 3.1 main(argc, argv)
55     int argc;
56     char *argv[];
57     {
58 gregl 3.10 extern int eputs();
59 gregl 3.3 int rdy, inp, res = 0, pause = 0;
60 gregl 3.1
61     progname = argv[0];
62 gregl 3.10 if (argc < 3)
63 gregl 3.1 error(USER, "bad command line arguments");
64     /* open our device */
65     dev_open(argv[1]);
66 gregl 3.10 /* open server process i/o */
67     sstdout = fdopen(atoi(argv[2]), "w");
68     if (argc < 4 || (inp = atoi(argv[3])) < 0)
69     sstdin = NULL;
70     else
71     sstdin = fdopen(inp, "r");
72     /* set command error vector */
73     erract[COMMAND].pf = eputs;
74 gregl 3.21 #ifdef DEBUG
75     tmodesw = time(NULL);
76     #endif
77 gregl 3.1 /* enter main loop */
78     do {
79     rdy = disp_wait();
80 gregl 3.19 if (rdy & RDY_SRV) { /* process server result */
81     res = serv_result();
82     if (pause && res != DS_SHUTDOWN) {
83     serv_request(DR_ATTEN, 0, NULL);
84     while ((res = serv_result()) != DS_ACKNOW &&
85     res != DS_SHUTDOWN)
86     ;
87     }
88     }
89 gregl 3.10 if (rdy & RDY_DEV) { /* user input from driver */
90 gregl 3.3 inp = dev_input();
91 gregl 3.13 if (inp & DFL(DC_SETVIEW))
92     new_view(&odev.v);
93 gwlarson 3.37 else if (inp & DFL(DC_LASTVIEW))
94     new_view(NULL);
95     if (inp & DFL(DC_REDRAW))
96     imm_mode = beam_sync(1) > 0;
97 gregl 3.13 if (inp & DFL(DC_GETVIEW))
98 gregl 3.10 printview();
99 gwlarson 3.36 if (inp & DFL(DC_FOCUS))
100     set_focus(odev_args);
101 gregl 3.15 if (inp & DFL(DC_KILL)) {
102 gregl 3.14 serv_request(DR_KILL, 0, NULL);
103 gregl 3.15 pause = 0;
104     }
105 gregl 3.14 if (inp & DFL(DC_CLOBBER))
106     serv_request(DR_CLOBBER, 0, NULL);
107 gregl 3.15 if (inp & DFL(DC_RESTART)) {
108 gregl 3.14 serv_request(DR_RESTART, 0, NULL);
109 gregl 3.15 pause = 0;
110     }
111 gwlarson 3.32 if (inp & DFL(DC_RESUME)) {
112     serv_request(DR_NOOP, 0, NULL);
113     pause = 0;
114     }
115     if (inp & DFL(DC_PAUSE))
116     pause = 1;
117 gregl 3.13 if (inp & DFL(DC_QUIT))
118     serv_request(DR_SHUTDOWN, 0, NULL);
119 gregl 3.3 }
120 gwlarson 3.34 if (rdy & RDY_SIN && !imm_mode) /* user input from sstdin */
121 gregl 3.10 switch (usr_input()) {
122     case DC_PAUSE:
123     pause = 1;
124     break;
125     case DC_RESUME:
126     serv_request(DR_NOOP, 0, NULL);
127 gregl 3.15 /* fall through */
128     case DC_KILL:
129     case DC_RESTART:
130 gregl 3.10 pause = 0;
131     break;
132     }
133 gregl 3.1 } while (res != DS_SHUTDOWN);
134 gregl 3.21 #ifdef DEBUG
135     if (timm && nimmrays)
136     fprintf(stderr,
137     "%s: %.1f rays recalled/second (%ld rays total)\n",
138     progname, (double)nimmrays/timm, nimmrays);
139     if (tadd && naddrays)
140     fprintf(stderr,
141     "%s: %.1f rays calculated/second (%ld rays total)\n",
142     progname, (double)naddrays/tadd, naddrays);
143     #endif
144 gregl 3.1 /* all done */
145     quit(0);
146     }
147    
148    
149     int
150     disp_wait() /* wait for more input */
151     {
152     fd_set readset, errset;
153     int flgs;
154     int n;
155     register int i;
156     /* see if we can avoid select call */
157 gwlarson 3.30 flgs = 0; /* flag what's ready already */
158 gregl 3.1 if (imm_mode || stdin->_cnt > 0)
159 gwlarson 3.30 flgs |= RDY_SRV;
160 gregl 3.10 if (sstdin != NULL && sstdin->_cnt > 0)
161 gwlarson 3.30 flgs |= RDY_SIN;
162     if (odev.inpready)
163     flgs |= RDY_DEV;
164     if (flgs) /* got something? */
165     return(flgs);
166     if (dev_flush()) /* else flush output & check keyboard+mouse */
167 gregl 3.4 return(RDY_DEV);
168 gwlarson 3.30 /* if nothing, we need to call select */
169 gregl 3.1 FD_ZERO(&readset); FD_ZERO(&errset);
170     FD_SET(0, &readset);
171     FD_SET(0, &errset);
172     FD_SET(odev.ifd, &readset);
173     FD_SET(odev.ifd, &errset);
174 gregl 3.10 n = odev.ifd+1;
175     if (sstdin != NULL) {
176     FD_SET(fileno(sstdin), &readset);
177 gregl 3.23 FD_SET(fileno(sstdin), &errset);
178 gregl 3.10 if (fileno(sstdin) >= n)
179     n = fileno(sstdin) + 1;
180     }
181 gregl 3.1 n = select(n, &readset, NULL, &errset, NULL);
182     if (n < 0) {
183     if (errno == EINTR)
184     return(0);
185     error(SYSTEM, "select call failure in disp_wait");
186     }
187     if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset))
188 gregl 3.4 flgs |= RDY_SRV;
189 gregl 3.1 if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset))
190 gregl 3.4 flgs |= RDY_DEV;
191 gregl 3.23 if (sstdin != NULL && (FD_ISSET(fileno(sstdin), &readset) ||
192     FD_ISSET(fileno(sstdin), &errset)))
193 gregl 3.10 flgs |= RDY_SIN;
194 gregl 3.1 return(flgs);
195     }
196    
197    
198 gwlarson 3.37 add_holo(hdg, ofn) /* register a new holodeck section */
199 gregl 3.1 HDGRID *hdg;
200 gwlarson 3.37 char *ofn;
201 gregl 3.1 {
202     VIEW nv;
203 gregl 3.20 double d;
204 gregl 3.1 register int hd;
205    
206     for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++)
207     ;
208     if (hd >= HDMAX)
209     error(INTERNAL, "too many holodeck sections in add_holo");
210     hdlist[hd] = (HOLO *)malloc(sizeof(HOLO));
211     if (hdlist[hd] == NULL)
212     error(SYSTEM, "out of memory in add_holo");
213     bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID));
214     hdcompgrid(hdlist[hd]);
215 gwlarson 3.37 hdofn[hd] = savestr(ofn);
216 gregl 3.1 if (hd)
217     return;
218     /* set initial viewpoint */
219     copystruct(&nv, &odev.v);
220     VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5);
221     VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5);
222     VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5);
223     fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]);
224     VCOPY(nv.vup, hdlist[0]->xv[2]);
225 gregl 3.20 if (do_outside) {
226     normalize(nv.vdir);
227     d = VLEN(hdlist[0]->xv[1]);
228     d += VLEN(hdlist[0]->xv[2]);
229     VSUM(nv.vp, nv.vp, nv.vdir, -d);
230     }
231 gregl 3.1 new_view(&nv);
232     }
233    
234    
235     disp_bundle(p) /* display a ray bundle */
236     register PACKHEAD *p;
237     {
238     GCOORD gc[2];
239     FVECT ro, rd, wp;
240     double d;
241     register int i;
242     /* get beam coordinates */
243     if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL)
244     error(INTERNAL, "bad holodeck number in disp_bundle");
245     if (!hdbcoord(gc, hdlist[p->hd], p->bi))
246     error(INTERNAL, "bad beam index in disp_bundle");
247     /* display each ray */
248     for (i = p->nr; i--; ) {
249     hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r);
250     d = hddepth(hdlist[p->hd], packra(p)[i].d);
251 gwlarson 3.29 if (d < .99*FHUGE) {
252     VSUM(wp, ro, rd, d); /* might be behind viewpoint */
253     dev_value(packra(p)[i].v, rd, wp);
254     } else
255     dev_value(packra(p)[i].v, rd, NULL);
256 gregl 3.1 }
257 gregl 3.21 #ifdef DEBUG
258     if (imm_mode) nimmrays += p->nr;
259     else naddrays += p->nr;
260     #endif
261 gregl 3.1 }
262    
263    
264     new_view(v) /* change view parameters */
265 gregl 3.12 register VIEW *v;
266 gregl 3.1 {
267 gregl 3.12 static VIEW viewhist[VIEWHISTLEN];
268     static unsigned nhist;
269 gwlarson 3.24 VIEW *dv;
270 gwlarson 3.37 int i, res[2], *slist;
271 gregl 3.1 char *err;
272 gregl 3.12 /* restore previous view? */
273     if (v == NULL) {
274 gregl 3.16 if (nhist > 1) /* get one before last setting */
275     nhist--;
276     else /* else go to end of list */
277     while (nhist < VIEWHISTLEN && viewhist[nhist].type)
278     nhist++;
279 gregl 3.12 v = viewhist + ((nhist-1)%VIEWHISTLEN);
280 gregl 3.17 } else
281     again:
282     if ((err = setview(v)) != NULL) {
283 gregl 3.12 error(COMMAND, err);
284     return;
285     }
286 gwlarson 3.37 if (!dev_view(v)) /* notify display driver */
287     goto again;
288 gregl 3.12 if (v->type == VT_PAR) {
289     error(COMMAND, "cannot handle parallel views");
290     return;
291     }
292 gwlarson 3.37 beam_init(odev.firstuse); /* compute new beam set */
293     for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) {
294     if ((slist = beam_view(dv, res[0], res[1])) == NULL) {
295 gwlarson 3.24 if (!nhist) {
296     error(COMMAND, "invalid starting view");
297     return;
298     }
299     copystruct(v, viewhist + ((nhist-1)%VIEWHISTLEN));
300 gwlarson 3.37 goto again; /* poss. overloading dev_section()? */
301     }
302     DCHECK(*slist < 0, WARNING, "no visible sections in new_view");
303     while (*slist >= 0)
304     dev_section(hdofn[*slist++]);
305     }
306     dev_section(NULL); /* end section list */
307     dev_flush(); /* update display */
308 gwlarson 3.34 /* update server */
309 gwlarson 3.37 imm_mode = beam_sync(odev.firstuse) > 0;
310 gregl 3.12 /* record new view */
311     if (v < viewhist || v >= viewhist+VIEWHISTLEN) {
312     copystruct(viewhist + (nhist%VIEWHISTLEN), v);
313     nhist++;
314     }
315 gregl 3.10 }
316    
317    
318 gwlarson 3.36 set_focus(args) /* set focus frame */
319     char *args;
320     {
321     double hcent, vcent, hsiz, vsiz;
322     VIEW *dv, vwfocus;
323     int i, res[2];
324    
325     i = sscanf(args, "%lf %lf %lf %lf", &hcent, &vcent, &hsiz, &vsiz);
326     if (i < 2 || hcent < 0 || hcent > 1 || vcent < 0 || vcent > 1) {
327     beam_init(0); /* restore view */
328     for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++)
329     beam_view(dv, res[0], res[1]);
330     beam_sync(0); /* update server */
331     return;
332     }
333     if (i < 4 || hsiz <= hcent || hsiz > 1 || vsiz <= vcent || vsiz > 1)
334     hsiz = vsiz = FSIZDEF; /* gave center only */
335     else {
336     hsiz -= hcent; hcent += 0.5*hsiz; /* gave min and max */
337     vsiz -= vcent; vcent += 0.5*vsiz;
338     }
339     beam_init(0); /* add basic views */
340     for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++)
341     beam_view(dv, res[0]>>4, res[1]>>4);
342     copystruct(&vwfocus, &odev.v); /* add focus view */
343     switch (odev.v.type) {
344     case VT_PER:
345     vwfocus.horiz = 2.*180./PI*atan(
346     hsiz * tan(PI/180./2.*odev.v.horiz) );
347     vwfocus.vert = 2.*180./PI*atan(
348     vsiz * tan(PI/180./2.*odev.v.vert) );
349     break;
350     case VT_PAR:
351     case VT_ANG:
352     vwfocus.horiz = hsiz * odev.v.horiz;
353     vwfocus.vert = vsiz * odev.v.vert;
354     break;
355     case VT_HEM:
356     vwfocus.horiz = 2.*180./PI*asin(
357     hsiz * sin(PI/180./2.*odev.v.horiz) );
358     vwfocus.vert = 2.*180./PI*asin(
359     vsiz * sin(PI/180./2.*odev.v.vert) );
360     break;
361     case VT_CYL:
362     vwfocus.horiz = hsiz * odev.v.horiz;
363     vwfocus.vert = 2.*180./PI*atan(
364     vsiz * tan(PI/180./2.*odev.v.vert) );
365     break;
366     default:
367     error(INTERNAL, "bad view type in set_focus");
368     }
369     vwfocus.hoff = (odev.v.hoff + hcent - 0.5)/hsiz;
370     vwfocus.voff = (odev.v.voff + vcent - 0.5)/vsiz;
371     setview(&vwfocus);
372     beam_view(&vwfocus, (int)(3*odev.hres*hsiz)+100,
373     (int)(3*odev.vres*vsiz)+100);
374     beam_sync(0); /* update server */
375     }
376    
377    
378 gregl 3.10 int
379     usr_input() /* get user input and process it */
380     {
381     VIEW vparams;
382 gregl 3.23 char cmd[256];
383 gregl 3.10 register char *args;
384 gregl 3.11 register int i;
385 gregl 3.10
386 gregl 3.23 if (fgets(cmd, sizeof(cmd), sstdin) == NULL) {
387     fclose(sstdin);
388     sstdin = NULL;
389 gwlarson 3.26 return(-1);
390 gregl 3.23 }
391 gwlarson 3.31 if (*cmd == '\n')
392 gwlarson 3.26 return(DC_RESUME);
393 gregl 3.10 for (args = cmd; *args && !isspace(*args); args++)
394     ;
395     while (isspace(*args))
396     *args++ = '\0';
397 gregl 3.11 if (*args && args[i=strlen(args)-1] == '\n')
398     args[i] = '\0';
399     for (i = 0; i < DC_NCMDS; i++)
400     if (!strcmp(cmd, cmdlist[i]))
401 gregl 3.10 break;
402 gregl 3.11 if (i >= DC_NCMDS) {
403 gwlarson 3.26 dev_auxcom(cmd, args);
404 gregl 3.10 return(-1);
405     }
406 gregl 3.11 switch (i) {
407 gregl 3.10 case DC_SETVIEW: /* set the view */
408     copystruct(&vparams, &odev.v);
409     if (!sscanview(&vparams, args))
410     error(COMMAND, "missing view options");
411     else
412     new_view(&vparams);
413     break;
414     case DC_GETVIEW: /* print the current view */
415     printview();
416 gregl 3.12 break;
417     case DC_LASTVIEW: /* restore previous view */
418     new_view(NULL);
419 gwlarson 3.36 break;
420     case DC_FOCUS: /* set focus frame */
421     set_focus(args);
422 gregl 3.10 break;
423     case DC_PAUSE: /* pause the current calculation */
424     case DC_RESUME: /* resume the calculation */
425     /* handled in main() */
426 gregl 3.13 break;
427     case DC_REDRAW: /* redraw from server */
428 gwlarson 3.24 imm_mode = beam_sync(1) > 0;
429 gregl 3.22 dev_clear();
430 gregl 3.14 break;
431     case DC_KILL: /* kill rtrace process(es) */
432     serv_request(DR_KILL, 0, NULL);
433     break;
434     case DC_CLOBBER: /* clobber holodeck */
435     serv_request(DR_CLOBBER, 0, NULL);
436     break;
437     case DC_RESTART: /* restart rtrace */
438     serv_request(DR_RESTART, 0, NULL);
439 gregl 3.10 break;
440     case DC_QUIT: /* quit request */
441     serv_request(DR_SHUTDOWN, 0, NULL);
442     break;
443     default:
444     error(CONSISTENCY, "bad command id in usr_input");
445     }
446 gregl 3.11 return(i);
447 gregl 3.10 }
448    
449    
450     printview() /* print our current view to server stdout */
451     {
452     fputs(VIEWSTR, sstdout);
453     fprintview(&odev.v, sstdout);
454     fputc('\n', sstdout);
455     fflush(sstdout);
456 gregl 3.1 }
457    
458    
459     int
460     serv_result() /* get next server result and process it */
461     {
462     static char *buf = NULL;
463     static int bufsiz = 0;
464     MSGHEAD msg;
465     int n;
466     /* read message header */
467     if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1)
468     goto readerr;
469     if (msg.nbytes > 0) { /* get the message body */
470     if (msg.nbytes > bufsiz) {
471     if (buf == NULL)
472     buf = (char *)malloc(bufsiz=msg.nbytes);
473     else
474     buf = (char *)realloc(buf, bufsiz=msg.nbytes);
475     if (buf == NULL)
476     error(SYSTEM, "out of memory in serv_result");
477     }
478     if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes)
479     goto readerr;
480     }
481     switch (msg.type) { /* process results */
482     case DS_BUNDLE:
483     if (msg.nbytes < sizeof(PACKHEAD) ||
484     msg.nbytes != packsiz(((PACKHEAD *)buf)->nr))
485     error(INTERNAL, "bad display packet from server");
486     disp_bundle((PACKHEAD *)buf);
487     break;
488     case DS_ADDHOLO:
489 gwlarson 3.37 if (msg.nbytes <= sizeof(HDGRID))
490 gregl 3.1 error(INTERNAL, "bad holodeck record from server");
491 gwlarson 3.37 add_holo((HDGRID *)buf, buf+sizeof(HDGRID));
492 gregl 3.20 break;
493     case DS_OUTSECT:
494     do_outside = 1;
495 gwlarson 3.28 goto noargs;
496 gwlarson 3.25 case DS_EYESEP:
497     if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY)
498     error(INTERNAL, "bad eye separation from server");
499 gregl 3.1 break;
500     case DS_STARTIMM:
501     case DS_ENDIMM:
502 gregl 3.21 #ifdef DEBUG
503 gwlarson 3.35 {
504 gregl 3.21 time_t tnow = time(NULL);
505 gwlarson 3.35 if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw;
506     else timm += tnow - tmodesw;
507 gregl 3.21 tmodesw = tnow;
508     }
509     #endif
510 gwlarson 3.34 if (!(imm_mode = msg.type==DS_STARTIMM))
511     dev_flush();
512 gwlarson 3.28 goto noargs;
513 gregl 3.1 case DS_ACKNOW:
514     case DS_SHUTDOWN:
515 gwlarson 3.28 goto noargs;
516 gregl 3.1 default:
517     error(INTERNAL, "unrecognized result from server process");
518     }
519     return(msg.type); /* return message type */
520 gwlarson 3.28 noargs:
521     if (msg.nbytes) {
522     sprintf(errmsg, "unexpected body with server message %d",
523     msg.type);
524     error(INTERNAL, errmsg);
525     }
526     return(msg.type);
527 gregl 3.1 readerr:
528     if (feof(stdin))
529     error(SYSTEM, "server process died");
530     error(SYSTEM, "error reading from server process");
531     }
532    
533    
534     serv_request(type, nbytes, p) /* send a request to the server process */
535     int type, nbytes;
536     char *p;
537     {
538     MSGHEAD msg;
539     int m;
540 gwlarson 3.33 /* consistency checks */
541 gwlarson 3.37 DCHECK(nbytes < 0 || nbytes > 0 & p == NULL,
542     CONSISTENCY, "bad buffer handed to serv_request");
543 gregl 3.1 /* get server's attention for big request */
544     if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) {
545     serv_request(DR_ATTEN, 0, NULL);
546     while ((m = serv_result()) != DS_ACKNOW)
547     if (m == DS_SHUTDOWN) /* the bugger quit on us */
548     quit(0);
549     }
550     msg.type = type; /* write and flush the message */
551     msg.nbytes = nbytes;
552     fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout);
553     if (nbytes > 0)
554     fwrite(p, 1, nbytes, stdout);
555     if (fflush(stdout) < 0)
556     error(SYSTEM, "write error in serv_request");
557 gregl 3.2 }
558    
559    
560     eputs(s) /* put error message to stderr */
561     register char *s;
562     {
563     static int midline = 0;
564    
565     if (!*s)
566     return;
567     if (!midline++) { /* prepend line with program name */
568     fputs(progname, stderr);
569     fputs(": ", stderr);
570     }
571     fputs(s, stderr);
572     if (s[strlen(s)-1] == '\n') {
573     fflush(stderr);
574     midline = 0;
575     }
576 gregl 3.1 }
577    
578    
579     quit(code) /* clean up and exit */
580     int code;
581     {
582     if (odev.v.type)
583     dev_close();
584     exit(code);
585     }