ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.20
Committed: Sun Jan 4 18:37:10 1998 UTC (26 years, 2 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.19: +12 -0 lines
Log Message:
added motion outside section when OBSTRUCTIONS=True

File Contents

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