ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.34
Committed: Thu Nov 5 14:27:02 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.33: +6 -4 lines
Log Message:
made animations from standard input work

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