ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.29
Committed: Tue Aug 11 07:51:19 1998 UTC (26 years, 3 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.28: +5 -2 lines
Log Message:
changed dev_value() interface so NULL is passed for infinite points

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