ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.40
Committed: Thu Mar 4 10:14:52 1999 UTC (25 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.39: +4 -1 lines
Log Message:
added wait for initialization in disp_wait()
changed return value of beam_view()

File Contents

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