ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.36
Committed: Thu Dec 10 10:45:54 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.35: +69 -0 lines
Log Message:
created "frame" command to focus calculation

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