ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.22
Committed: Tue Jan 6 05:26:20 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.21: +1 -0 lines
Log Message:
created dev_clear() routine to clear device ray cache

File Contents

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