ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.6
Committed: Tue Nov 25 10:27:47 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.5: +2 -4 lines
Log Message:
bug fixes and minor improvements

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
16 HOLO *hdlist[HDMAX+1]; /* global holodeck list */
17
18 int imm_mode = 0; /* bundles are being delivered immediately */
19
20 char *progname; /* global argv[0] */
21
22 #define RDY_SRV 01
23 #define RDY_DEV 02
24
25
26 main(argc, argv)
27 int argc;
28 char *argv[];
29 {
30 int rdy, inp, res = 0, pause = 0;
31
32 progname = argv[0];
33 if (argc != 2)
34 error(USER, "bad command line arguments");
35 /* open our device */
36 dev_open(argv[1]);
37 /* enter main loop */
38 do {
39 rdy = disp_wait();
40 if (rdy & RDY_DEV) { /* get user input */
41 inp = dev_input();
42 if (inp & DEV_NEWVIEW)
43 new_view(&odev.v);
44 if (inp & DEV_SHUTDOWN)
45 serv_request(DR_SHUTDOWN, 0, NULL);
46 if (inp & DEV_REDRAW) {
47 imm_mode = 1; /* preempt updates */
48 beam_sync();
49 }
50 if (inp & DEV_WAIT)
51 pause = 1;
52 if (inp & DEV_RESUME) {
53 serv_request(DR_NOOP, 0, NULL);
54 pause = 0;
55 }
56 }
57 if (rdy & RDY_SRV) { /* get server result */
58 res = serv_result();
59 if (pause && res != DS_SHUTDOWN) {
60 serv_request(DR_ATTEN, 0, NULL);
61 while ((res = serv_result()) != DS_ACKNOW &&
62 res != DS_SHUTDOWN)
63 ;
64 }
65 }
66 } while (res != DS_SHUTDOWN);
67 /* all done */
68 quit(0);
69 }
70
71
72 int
73 disp_wait() /* wait for more input */
74 {
75 fd_set readset, errset;
76 int flgs;
77 int n;
78 register int i;
79 /* see if we can avoid select call */
80 if (imm_mode || stdin->_cnt > 0)
81 return(RDY_SRV);
82 if (dev_flush())
83 return(RDY_DEV);
84 /* make the call */
85 FD_ZERO(&readset); FD_ZERO(&errset);
86 FD_SET(0, &readset);
87 FD_SET(0, &errset);
88 FD_SET(odev.ifd, &readset);
89 FD_SET(odev.ifd, &errset);
90 n = odev.ifd + 1;
91 n = select(n, &readset, NULL, &errset, NULL);
92 if (n < 0) {
93 if (errno == EINTR)
94 return(0);
95 error(SYSTEM, "select call failure in disp_wait");
96 }
97 flgs = 0; /* flag what's ready */
98 if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset))
99 flgs |= RDY_SRV;
100 if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset))
101 flgs |= RDY_DEV;
102 return(flgs);
103 }
104
105
106 add_holo(hdg) /* register a new holodeck section */
107 HDGRID *hdg;
108 {
109 VIEW nv;
110 register int hd;
111
112 for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++)
113 ;
114 if (hd >= HDMAX)
115 error(INTERNAL, "too many holodeck sections in add_holo");
116 hdlist[hd] = (HOLO *)malloc(sizeof(HOLO));
117 if (hdlist[hd] == NULL)
118 error(SYSTEM, "out of memory in add_holo");
119 bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID));
120 hdcompgrid(hdlist[hd]);
121 if (hd)
122 return;
123 /* set initial viewpoint */
124 copystruct(&nv, &odev.v);
125 VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5);
126 VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5);
127 VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5);
128 fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]);
129 VCOPY(nv.vup, hdlist[0]->xv[2]);
130 new_view(&nv);
131 }
132
133
134 disp_bundle(p) /* display a ray bundle */
135 register PACKHEAD *p;
136 {
137 GCOORD gc[2];
138 FVECT ro, rd, wp;
139 double d;
140 register int i;
141 /* get beam coordinates */
142 if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL)
143 error(INTERNAL, "bad holodeck number in disp_bundle");
144 if (!hdbcoord(gc, hdlist[p->hd], p->bi))
145 error(INTERNAL, "bad beam index in disp_bundle");
146 /* display each ray */
147 for (i = p->nr; i--; ) {
148 hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r);
149 d = hddepth(hdlist[p->hd], packra(p)[i].d);
150 VSUM(wp, ro, rd, d); /* might be behind viewpoint */
151 dev_value(packra(p)[i].v, wp);
152 }
153 }
154
155
156 new_view(v) /* change view parameters */
157 VIEW *v;
158 {
159 char *err;
160
161 if ((err = setview(v)) != NULL)
162 error(INTERNAL, err);
163 dev_view(v); /* update display driver */
164 dev_flush(); /* update screen */
165 beam_view(v); /* update beam list */
166 }
167
168
169 int
170 serv_result() /* get next server result and process it */
171 {
172 static char *buf = NULL;
173 static int bufsiz = 0;
174 MSGHEAD msg;
175 int n;
176 /* read message header */
177 if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1)
178 goto readerr;
179 if (msg.nbytes > 0) { /* get the message body */
180 if (msg.nbytes > bufsiz) {
181 if (buf == NULL)
182 buf = (char *)malloc(bufsiz=msg.nbytes);
183 else
184 buf = (char *)realloc(buf, bufsiz=msg.nbytes);
185 if (buf == NULL)
186 error(SYSTEM, "out of memory in serv_result");
187 }
188 if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes)
189 goto readerr;
190 }
191 switch (msg.type) { /* process results */
192 case DS_BUNDLE:
193 if (msg.nbytes < sizeof(PACKHEAD) ||
194 msg.nbytes != packsiz(((PACKHEAD *)buf)->nr))
195 error(INTERNAL, "bad display packet from server");
196 disp_bundle((PACKHEAD *)buf);
197 break;
198 case DS_ADDHOLO:
199 if (msg.nbytes != sizeof(HDGRID))
200 error(INTERNAL, "bad holodeck record from server");
201 add_holo((HDGRID *)buf);
202 break;
203 case DS_STARTIMM:
204 case DS_ENDIMM:
205 imm_mode = msg.type==DS_STARTIMM;
206 /* fall through */
207 case DS_ACKNOW:
208 case DS_SHUTDOWN:
209 if (msg.nbytes) {
210 sprintf(errmsg,
211 "unexpected body with server message %d",
212 msg.type);
213 error(INTERNAL, errmsg);
214 }
215 break;
216 default:
217 error(INTERNAL, "unrecognized result from server process");
218 }
219 return(msg.type); /* return message type */
220 readerr:
221 if (feof(stdin))
222 error(SYSTEM, "server process died");
223 error(SYSTEM, "error reading from server process");
224 }
225
226
227 serv_request(type, nbytes, p) /* send a request to the server process */
228 int type, nbytes;
229 char *p;
230 {
231 MSGHEAD msg;
232 int m;
233 /* get server's attention for big request */
234 if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) {
235 serv_request(DR_ATTEN, 0, NULL);
236 while ((m = serv_result()) != DS_ACKNOW)
237 if (m == DS_SHUTDOWN) /* the bugger quit on us */
238 quit(0);
239 }
240 msg.type = type; /* write and flush the message */
241 msg.nbytes = nbytes;
242 fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout);
243 if (nbytes > 0)
244 fwrite(p, 1, nbytes, stdout);
245 if (fflush(stdout) < 0)
246 error(SYSTEM, "write error in serv_request");
247 }
248
249
250 eputs(s) /* put error message to stderr */
251 register char *s;
252 {
253 static int midline = 0;
254
255 if (!*s)
256 return;
257 if (!midline++) { /* prepend line with program name */
258 fputs(progname, stderr);
259 fputs(": ", stderr);
260 }
261 fputs(s, stderr);
262 if (s[strlen(s)-1] == '\n') {
263 fflush(stderr);
264 midline = 0;
265 }
266 }
267
268
269 quit(code) /* clean up and exit */
270 int code;
271 {
272 if (odev.v.type)
273 dev_close();
274 exit(code);
275 }