ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_glx.c
Revision: 3.22
Committed: Tue Aug 25 18:11:17 1998 UTC (25 years, 8 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.21: +72 -67 lines
Log Message:
bug fixes and improved interactive drawing

File Contents

# User Rev Content
1 gwlarson 3.18 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 gregl 3.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * OpenGL GLX driver for holodeck display.
9 gwlarson 3.19 * Based on old GLX driver using cones.
10     *
11     * Define symbol STEREO for stereo viewing.
12     * Define symbol DOBJ for display object viewing.
13 gregl 3.1 */
14    
15 gwlarson 3.19 #ifdef NOSTEREO
16     #ifdef STEREO
17     #undef STEREO
18     #else
19     #undef NOSTEREO
20     #endif
21     #endif
22    
23 gregl 3.1 #include "standard.h"
24 gwlarson 3.19 #include "rhd_sample.h"
25 gregl 3.1
26 gwlarson 3.19 #include <sys/types.h>
27     #include <GL/glx.h>
28     #include <GL/glu.h>
29     #ifdef STEREO
30     #include <X11/extensions/SGIStereo.h>
31     #endif
32     #ifdef DOBJ
33     #include "rhdobj.h"
34     #endif
35 gregl 3.1
36 gwlarson 3.19 #include "x11icon.h"
37 gregl 3.1
38 gregl 3.10 #ifndef RAYQLEN
39 gwlarson 3.19 #define RAYQLEN 250 /* max. rays to queue before flush */
40 gregl 3.10 #endif
41    
42 gregl 3.1 #ifndef FEQ
43     #define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
44     #endif
45    
46 gregl 3.2 #define GAMMA 1.4 /* default gamma correction */
47 gregl 3.1
48     #define MOVPCT 7 /* percent distance to move /frame */
49     #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
50     #define MOVDEG (-5) /* degrees to orbit CW/down /frame */
51     #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
52    
53 gwlarson 3.19 #ifndef TARGETFPS
54     #define TARGETFPS 4.0 /* target frames/sec during motion */
55     #endif
56    
57 gregl 3.1 #define MINWIDTH 480 /* minimum graphics window width */
58     #define MINHEIGHT 400 /* minimum graphics window height */
59    
60     #define VIEWDIST 356 /* assumed viewing distance (mm) */
61    
62     #define BORWIDTH 5 /* border width */
63    
64 gwlarson 3.19 #define setstereobuf(bid) (glXWaitGL(), \
65     XSGISetStereoBuffer(ourdisplay, gwind, bid), \
66     glXWaitX())
67    
68 gregl 3.1 #define ourscreen DefaultScreen(ourdisplay)
69     #define ourroot RootWindow(ourdisplay,ourscreen)
70     #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
71     ButtonPressMask|ButtonReleaseMask)
72    
73     #define levptr(etype) ((etype *)&currentevent)
74    
75     struct driver odev; /* global device driver structure */
76    
77 gwlarson 3.19 static VIEW vwright; /* right eye view */
78    
79     static int rayqleft = 0; /* rays left to queue before flush */
80    
81 gregl 3.1 static XEvent currentevent; /* current event */
82    
83     static int mapped = 0; /* window is mapped? */
84     static unsigned long ourblack=0, ourwhite=~0;
85    
86     static Display *ourdisplay = NULL; /* our display */
87     static XVisualInfo *ourvinf; /* our visual information */
88     static Window gwind = 0; /* our graphics window */
89     static GLXContext gctx; /* our GLX context */
90    
91     static double pwidth, pheight; /* pixel dimensions (mm) */
92    
93 gwlarson 3.19 static double mindpth, maxdpth; /* min. and max. depth */
94 gregl 3.1
95 gwlarson 3.19 double dev_zmin, dev_zmax; /* fore and aft clipping plane dist. */
96 gregl 3.4
97 gregl 3.1 static int inpresflags; /* input result flags */
98    
99     static int headlocked = 0; /* lock vertical motion */
100    
101 gwlarson 3.19 static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
102     setglpersp(), getmove(), fixwindow(), mytmflags();
103 gregl 3.1
104 gwlarson 3.19 #ifdef STEREO
105     static int pushright(), popright();
106     #endif
107 gregl 3.1
108 gwlarson 3.19 extern time_t time();
109    
110    
111     dev_open(id) /* initialize GLX driver */
112 gregl 3.1 char *id;
113     {
114     extern char *getenv();
115 gregl 3.15 static RGBPRIMS myprims = STDPRIMS;
116 gregl 3.1 static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
117     GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
118 gregl 3.4 GLX_DEPTH_SIZE,15, None};
119 gregl 3.15 char *ev;
120 gregl 3.1 double gamval = GAMMA;
121 gregl 3.15 RGBPRIMP dpri = stdprims;
122 gregl 3.1 XSetWindowAttributes ourwinattr;
123     XWMHints ourxwmhints;
124     XSizeHints oursizhints;
125 gwlarson 3.19 /* check for unsupported stereo */
126     #ifdef NOSTEREO
127 gwlarson 3.22 error(INTERNAL, "stereo display driver unavailable");
128 gwlarson 3.19 #endif
129 gregl 3.1 /* open display server */
130     ourdisplay = XOpenDisplay(NULL);
131     if (ourdisplay == NULL)
132     error(USER, "cannot open X-windows; DISPLAY variable set?\n");
133 gwlarson 3.19 #ifdef STEREO
134     switch (XSGIQueryStereoMode(ourdisplay, ourroot)) {
135     case STEREO_TOP:
136     case STEREO_BOTTOM:
137     break;
138     case STEREO_OFF:
139     error(USER,
140     "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
141     case X_STEREO_UNSUPPORTED:
142     error(USER, "stereo mode not supported on this screen");
143     default:
144     error(INTERNAL, "unknown stereo mode");
145     }
146     #endif
147 gregl 3.1 /* find a usable visual */
148     ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
149     if (ourvinf == NULL)
150     error(USER, "no suitable visuals available");
151     /* get a context */
152     gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
153     /* set gamma and tone mapping */
154 gregl 3.15 if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
155     || (ev = getenv("DISPLAY_GAMMA")) != NULL)
156     gamval = atof(ev);
157     if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
158     sscanf(ev, "%f %f %f %f %f %f %f %f",
159     &myprims[RED][CIEX],&myprims[RED][CIEY],
160     &myprims[GRN][CIEX],&myprims[GRN][CIEY],
161     &myprims[BLU][CIEX],&myprims[BLU][CIEY],
162     &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
163     dpri = myprims;
164     if (tmInit(mytmflags(), dpri, gamval) == NULL)
165 gregl 3.1 error(SYSTEM, "not enough memory in dev_open");
166     /* open window */
167     ourwinattr.background_pixel = ourblack;
168     ourwinattr.border_pixel = ourblack;
169     ourwinattr.event_mask = ourmask;
170     /* this is stupid */
171     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
172     ourvinf->visual, AllocNone);
173     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
174     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
175 gwlarson 3.19 #ifdef STEREO
176     (DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2,
177     #else
178 gregl 3.1 DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
179 gwlarson 3.19 #endif
180 gregl 3.1 BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
181     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
182     if (gwind == 0)
183     error(SYSTEM, "cannot create window\n");
184     XStoreName(ourdisplay, gwind, id);
185     /* set window manager hints */
186     ourxwmhints.flags = InputHint|IconPixmapHint;
187     ourxwmhints.input = True;
188     ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
189     gwind, x11icon_bits, x11icon_width, x11icon_height);
190     XSetWMHints(ourdisplay, gwind, &ourxwmhints);
191     oursizhints.min_width = MINWIDTH;
192 gwlarson 3.19 #ifdef STEREO
193     oursizhints.min_height = MINHEIGHT/2;
194     oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH;
195     oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) -
196     2*BORWIDTH)/2;
197     oursizhints.flags = PMinSize|PMaxSize;
198     #else
199 gregl 3.1 oursizhints.min_height = MINHEIGHT;
200     oursizhints.flags = PMinSize;
201 gwlarson 3.19 #endif
202 gregl 3.1 XSetNormalHints(ourdisplay, gwind, &oursizhints);
203 gregl 3.2 /* set GLX context */
204     glXMakeCurrent(ourdisplay, gwind, gctx);
205     glEnable(GL_DEPTH_TEST);
206     glDepthFunc(GL_LEQUAL);
207 gwlarson 3.19 glShadeModel(GL_SMOOTH);
208 gregl 3.2 glDisable(GL_DITHER);
209 gregl 3.4 glDisable(GL_CULL_FACE);
210 gregl 3.1 /* figure out sensible view */
211     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
212     DisplayWidth(ourdisplay, ourscreen);
213     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
214     DisplayHeight(ourdisplay, ourscreen);
215 gwlarson 3.19 #ifdef STEREO
216     pheight *= 2.;
217     setstereobuf(STEREO_BUFFER_LEFT);
218     #endif
219 gregl 3.1 copystruct(&odev.v, &stdview);
220     odev.v.type = VT_PER;
221 gregl 3.2 /* map the window */
222     XMapWindow(ourdisplay, gwind);
223     dev_input(); /* sets size and view angles */
224 gwlarson 3.19 /* allocate our samples */
225     if (!smInit(DisplayWidth(ourdisplay,ourscreen) *
226     DisplayHeight(ourdisplay,ourscreen) / 10))
227 gregl 3.2 error(SYSTEM, "insufficient memory for value storage");
228 gwlarson 3.19 mindpth = FHUGE; maxdpth = FTINY;
229 gregl 3.1 odev.name = id;
230     odev.ifd = ConnectionNumber(ourdisplay);
231     }
232    
233    
234     dev_close() /* close our display and free resources */
235     {
236 gwlarson 3.19 smInit(0);
237     #ifdef DOBJ
238     dobj_cleanup();
239     #endif
240 gregl 3.1 glXMakeCurrent(ourdisplay, None, NULL);
241     glXDestroyContext(ourdisplay, gctx);
242     XDestroyWindow(ourdisplay, gwind);
243     gwind = 0;
244     XCloseDisplay(ourdisplay);
245     ourdisplay = NULL;
246     tmDone(NULL);
247     odev.v.type = 0;
248     odev.hres = odev.vres = 0;
249     odev.ifd = -1;
250     }
251    
252    
253 gwlarson 3.19 dev_clear() /* clear our representation */
254 gregl 3.14 {
255 gwlarson 3.19 smInit(rsL.max_samp);
256     wipeclean();
257 gregl 3.14 rayqleft = 0; /* hold off update */
258     }
259    
260    
261 gregl 3.1 int
262     dev_view(nv) /* assign new driver view */
263     register VIEW *nv;
264     {
265 gwlarson 3.19 double d;
266    
267     if (nv->type != VT_PER || /* check view legality */
268 gregl 3.4 nv->horiz > 160. || nv->vert > 160.) {
269 gregl 3.1 error(COMMAND, "illegal view type/angle");
270 gregl 3.4 nv->type = odev.v.type;
271 gregl 3.1 nv->horiz = odev.v.horiz;
272     nv->vert = odev.v.vert;
273     return(0);
274     }
275 gwlarson 3.22 if (nv != &odev.v) {
276     /* resize window? */
277     if (!FEQ(nv->horiz,odev.v.horiz) ||
278     !FEQ(nv->vert,odev.v.vert)) {
279     int dw = DisplayWidth(ourdisplay,ourscreen);
280     int dh = DisplayHeight(ourdisplay,ourscreen);
281 gregl 3.1
282 gwlarson 3.22 dw -= 25; /* for window frame */
283     dh -= 50;
284 gwlarson 3.19 #ifdef STEREO
285 gwlarson 3.22 dh /= 2;
286 gwlarson 3.19 #endif
287 gwlarson 3.22 odev.hres = 2.*VIEWDIST/pwidth *
288     tan(PI/180./2.*nv->horiz);
289     odev.vres = 2.*VIEWDIST/pheight *
290     tan(PI/180./2.*nv->vert);
291     if (odev.hres > dw) {
292     odev.vres = dw * odev.vres / odev.hres;
293     odev.hres = dw;
294     }
295     if (odev.vres > dh) {
296     odev.hres = dh * odev.hres / odev.vres;
297     odev.vres = dh;
298     }
299     XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
300     dev_input(); /* get resize event */
301 gregl 3.1 }
302 gwlarson 3.22 copystruct(&odev.v, nv); /* setview() already called */
303 gwlarson 3.19 #ifdef STEREO
304 gwlarson 3.22 copystruct(&vwright, nv);
305     d = eyesepdist / sqrt(nv->hn2);
306     VSUM(vwright.vp, nv->vp, nv->hvec, d);
307     /* setview(&vwright); -- Unnecessary */
308 gwlarson 3.19 #endif
309 gwlarson 3.22 }
310 gwlarson 3.19 wipeclean();
311 gregl 3.1 return(1);
312 gwlarson 3.18 }
313    
314    
315     dev_auxcom(cmd, args) /* process an auxiliary command */
316     char *cmd, *args;
317     {
318 gwlarson 3.19 #ifdef DOBJ
319     if (dobj_command(cmd, args) >= 0)
320     return;
321     #endif
322 gwlarson 3.18 sprintf(errmsg, "%s: unknown command", cmd);
323     error(COMMAND, errmsg);
324 gregl 3.1 }
325    
326    
327 gwlarson 3.17 VIEW *
328     dev_auxview(n, hvres) /* return nth auxiliary view */
329     int n;
330     int hvres[2];
331     {
332     hvres[0] = odev.hres; hvres[1] = odev.vres;
333 gwlarson 3.19 if (n == 0)
334     return(&odev.v);
335     #ifdef STEREO
336     if (n == 1)
337     return(&vwright);
338     #endif
339     return(NULL);
340 gwlarson 3.17 }
341    
342    
343 gregl 3.1 int
344     dev_input() /* get X11 input */
345     {
346     inpresflags = 0;
347    
348     do
349     getevent();
350    
351 gwlarson 3.19 while (XPending(ourdisplay) > 0);
352 gregl 3.1
353 gwlarson 3.19 odev.inpready = 0;
354    
355 gregl 3.1 return(inpresflags);
356     }
357    
358    
359 gwlarson 3.19 dev_value(c, d, p) /* add a pixel value to our mesh */
360     COLR c;
361     FVECT d, p;
362     {
363     double depth;
364     #ifdef DOBJ
365     if (dobj_lightsamp != NULL) { /* in light source sampling */
366     (*dobj_lightsamp)(c, d, p);
367     return;
368     }
369     #endif
370 gwlarson 3.22 if (p != NULL) { /* add depth to our range */
371 gwlarson 3.19 depth = (p[0] - odev.v.vp[0])*d[0] +
372     (p[1] - odev.v.vp[1])*d[1] +
373     (p[2] - odev.v.vp[2])*d[2];
374     if (depth > FTINY) {
375     if (depth < mindpth)
376     mindpth = depth;
377     if (depth > maxdpth)
378     maxdpth = depth;
379     }
380     }
381 gwlarson 3.22 smNewSamp(c, d, p); /* add to display representation */
382 gwlarson 3.19 if (!--rayqleft)
383     dev_flush(); /* flush output */
384     }
385    
386    
387 gregl 3.4 int
388     dev_flush() /* flush output */
389 gregl 3.1 {
390 gwlarson 3.22 if (mapped) {
391 gwlarson 3.19 #ifdef STEREO
392 gwlarson 3.22 pushright(); /* update right eye */
393     glClear(GL_DEPTH_BUFFER_BIT);
394     smUpdate(&vwright, 100);
395 gwlarson 3.19 #ifdef DOBJ
396 gwlarson 3.22 dobj_render(); /* usually in foreground */
397 gwlarson 3.19 #endif
398 gwlarson 3.22 popright(); /* update left eye */
399     glClear(GL_DEPTH_BUFFER_BIT);
400 gwlarson 3.19 #endif
401 gwlarson 3.22 smUpdate(&odev.v, 100);
402     checkglerr("rendering mesh");
403 gwlarson 3.19 #ifdef DOBJ
404 gwlarson 3.22 dobj_render();
405 gwlarson 3.19 #endif
406 gwlarson 3.22 glFlush(); /* flush OGL */
407     }
408 gregl 3.10 rayqleft = RAYQLEN;
409 gwlarson 3.19 /* flush X11 and return # pending */
410     return(odev.inpready = XPending(ourdisplay));
411 gregl 3.1 }
412    
413    
414 gwlarson 3.19 checkglerr(where) /* check for GL or GLU error */
415     char *where;
416 gregl 3.1 {
417 gwlarson 3.19 register GLenum errcode;
418    
419     while ((errcode = glGetError()) != GL_NO_ERROR) {
420     sprintf(errmsg, "OpenGL error %s: %s",
421     where, gluErrorString(errcode));
422     error(WARNING, errmsg);
423 gregl 3.1 }
424     }
425    
426    
427 gwlarson 3.19 #ifdef STEREO
428     static
429     pushright() /* push on right view */
430     {
431     double d;
432    
433     setstereobuf(STEREO_BUFFER_RIGHT);
434     glMatrixMode(GL_MODELVIEW);
435     glPushMatrix();
436     d = -eyesepdist / sqrt(odev.v.hn2);
437     glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]);
438     }
439    
440    
441     static
442     popright() /* pop off right view */
443     {
444     glMatrixMode(GL_MODELVIEW);
445     glPopMatrix();
446     setstereobuf(STEREO_BUFFER_LEFT);
447     }
448     #endif
449    
450    
451 gregl 3.1 static int
452     mytmflags() /* figure out tone mapping flags */
453     {
454     extern char *progname;
455     register char *cp, *tail;
456     /* find basic name */
457     for (cp = tail = progname; *cp; cp++)
458     if (*cp == '/')
459     tail = cp+1;
460     for (cp = tail; *cp && *cp != '.'; cp++)
461     ;
462 gwlarson 3.19 if (cp > tail && cp[-1] == 'h')
463     return(TM_F_HUMAN|TM_F_NOSTDERR);
464     else
465 gregl 3.10 return(TM_F_CAMERA|TM_F_NOSTDERR);
466 gregl 3.1 }
467    
468    
469     static
470     getevent() /* get next event */
471     {
472     XNextEvent(ourdisplay, levptr(XEvent));
473     switch (levptr(XEvent)->type) {
474     case ConfigureNotify:
475     resizewindow(levptr(XConfigureEvent));
476     break;
477     case UnmapNotify:
478     mapped = 0;
479     break;
480     case MapNotify:
481     mapped = 1;
482     break;
483     case Expose:
484     fixwindow(levptr(XExposeEvent));
485     break;
486     case KeyPress:
487     getkey(levptr(XKeyPressedEvent));
488     break;
489     case ButtonPress:
490     getmove(levptr(XButtonPressedEvent));
491     break;
492     }
493     }
494    
495    
496     static
497     draw3dline(wp) /* draw 3d line in world coordinates */
498     register FVECT wp[2];
499     {
500     glVertex3d(wp[0][0], wp[0][1], wp[0][2]);
501     glVertex3d(wp[1][0], wp[1][1], wp[1][2]);
502     }
503    
504    
505     static
506 gwlarson 3.22 draw_grids(fore) /* draw holodeck section grids */
507     int fore;
508 gregl 3.1 {
509 gwlarson 3.19 if (!mapped)
510 gregl 3.1 return;
511 gwlarson 3.22 if (fore)
512     glColor4ub(0, 255, 255, 0);
513     else
514     glColor4ub(0, 0, 0, 0);
515 gregl 3.4 glBegin(GL_LINES); /* draw each grid line */
516 gregl 3.1 gridlines(draw3dline);
517     glEnd();
518     }
519    
520    
521     static
522     moveview(dx, dy, mov, orb) /* move our view */
523     int dx, dy, mov, orb;
524     {
525     VIEW nv;
526 gwlarson 3.19 FVECT odir, v1, wip;
527 gregl 3.1 double d;
528     register int li;
529     /* start with old view */
530     copystruct(&nv, &odev.v);
531 gwlarson 3.22 /* orient our motion */
532 gwlarson 3.19 if (viewray(v1, odir, &odev.v,
533     (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
534     return(0); /* outside view */
535 gwlarson 3.22 if (mov | orb) { /* moving relative to geometry */
536 gwlarson 3.19 #ifdef DOBJ
537 gwlarson 3.21 d = dobj_trace(NULL, v1, odir); /* check objects */
538 gwlarson 3.22 /* check holodeck */
539     if ((li = smFindSamp(v1, odir)) >= 0) {
540 gwlarson 3.19 VCOPY(wip, rsL.wp[li]);
541 gwlarson 3.22 if (d < .99*FHUGE && d*d <= dist2(v1, wip))
542     li = -1; /* object is closer */
543     } else if (d >= .99*FHUGE)
544     return(0); /* nothing visible */
545     if (li < 0)
546     VSUM(wip, v1, odir, d); /* else get object point */
547 gwlarson 3.19 #else
548     if ((li = smFindSamp(v1, odir)) < 0)
549 gregl 3.1 return(0); /* not on window */
550 gwlarson 3.19 VCOPY(wip, rsL.wp[li]);
551     #endif
552 gwlarson 3.22 VSUM(odir, wip, odev.v.vp, -1.);
553     } else /* panning with constant viewpoint */
554 gwlarson 3.19 VCOPY(nv.vdir, odir);
555 gregl 3.1 if (orb && mov) { /* orbit left/right */
556     spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
557 gwlarson 3.19 VSUM(nv.vp, wip, odir, -1.);
558 gregl 3.1 spinvector(nv.vdir, nv.vdir, nv.vup, d);
559     } else if (orb) { /* orbit up/down */
560     fcross(v1, odir, nv.vup);
561     if (normalize(v1) == 0.)
562     return(0);
563     spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
564 gwlarson 3.19 VSUM(nv.vp, wip, odir, -1.);
565 gregl 3.1 spinvector(nv.vdir, nv.vdir, v1, d);
566     } else if (mov) { /* move forward/backward */
567     d = MOVPCT/100. * mov;
568     VSUM(nv.vp, nv.vp, odir, d);
569     }
570     if (!mov ^ !orb && headlocked) { /* restore head height */
571     VSUM(v1, odev.v.vp, nv.vp, -1.);
572 gwlarson 3.22 d = DOT(v1, nv.vup);
573 gregl 3.1 VSUM(nv.vp, nv.vp, odev.v.vup, d);
574     }
575     if (setview(&nv) != NULL)
576     return(0); /* illegal view */
577     dev_view(&nv);
578     inpresflags |= DFL(DC_SETVIEW);
579     return(1);
580     }
581    
582    
583     static
584     getmove(ebut) /* get view change */
585     XButtonPressedEvent *ebut;
586     {
587     int movdir = MOVDIR(ebut->button);
588     int movorb = MOVORB(ebut->state);
589 gwlarson 3.19 int qlevel = 99;
590     time_t lasttime, thistime;
591     int nframes;
592 gregl 3.1 Window rootw, childw;
593     int rootx, rooty, wx, wy;
594     unsigned int statemask;
595    
596     XNoOp(ourdisplay);
597    
598 gwlarson 3.19 lasttime = time(0); nframes = 0;
599 gregl 3.1 while (!XCheckMaskEvent(ourdisplay,
600     ButtonReleaseMask, levptr(XEvent))) {
601    
602     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
603     &rootx, &rooty, &wx, &wy, &statemask))
604     break; /* on another screen */
605    
606 gwlarson 3.22 draw_grids(0); /* clear old grid lines */
607 gregl 3.1 if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
608     sleep(1);
609 gwlarson 3.19 lasttime++;
610 gregl 3.1 continue;
611     }
612 gwlarson 3.19 #ifdef STEREO
613     pushright();
614 gwlarson 3.22 draw_grids(1);
615 gwlarson 3.19 smUpdate(&vwright, qlevel);
616     #ifdef DOBJ
617     dobj_render();
618     #endif
619     popright();
620     #endif
621 gwlarson 3.22 draw_grids(1);
622 gwlarson 3.19 smUpdate(&odev.v, qlevel);
623     #ifdef DOBJ
624     dobj_render();
625     #endif
626 gregl 3.4 glFlush();
627 gwlarson 3.19 checkglerr("moving view");
628     nframes++;
629     thistime = time(0);
630     if (thistime - lasttime >= 3 ||
631     nframes > (int)(3*3*TARGETFPS)) {
632     qlevel = thistime<=lasttime ? 1000 :
633     (int)((double)nframes/(thistime-lasttime)
634     / TARGETFPS * qlevel + 0.5);
635     lasttime = thistime; nframes = 0;
636     if (qlevel > 99) {
637     if (qlevel > 300) { /* put on the brakes */
638     sleep(1);
639     lasttime++;
640     }
641     qlevel = 99;
642     } else if (qlevel < 1)
643     qlevel = 1;
644     }
645 gregl 3.1 }
646     if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
647     movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
648     wx = levptr(XButtonReleasedEvent)->x;
649     wy = levptr(XButtonReleasedEvent)->y;
650     moveview(wx, odev.vres-1-wy, movdir, movorb);
651     }
652 gwlarson 3.19 }
653 gregl 3.4
654 gwlarson 3.19
655     static
656     setglpersp(vp) /* set perspective view in GL */
657     register VIEW *vp;
658     {
659     double d, xmin, xmax, ymin, ymax;
660    
661     if (mindpth >= maxdpth) {
662     dev_zmin = 0.1;
663     dev_zmax = 100.;
664     } else {
665     dev_zmin = 0.5*mindpth;
666     dev_zmax = 1.5*maxdpth;
667     if (dev_zmin > dev_zmax/100.)
668     dev_zmin = dev_zmax/100.;
669     }
670     if (odev.v.vfore > FTINY)
671     dev_zmin = odev.v.vfore;
672     if (odev.v.vaft > FTINY)
673     dev_zmax = odev.v.vaft;
674     if (dev_zmin < dev_zmax/5000.)
675     dev_zmin = dev_zmax/5000.;
676     xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
677     xmin = -xmax;
678     d = odev.v.hoff * (xmax - xmin);
679     xmin += d; xmax += d;
680     ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
681     ymin = -ymax;
682     d = odev.v.voff * (ymax - ymin);
683     ymin += d; ymax += d;
684     /* set view matrix */
685     glMatrixMode(GL_PROJECTION);
686     glLoadIdentity();
687     glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
688     gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
689     odev.v.vp[0] + odev.v.vdir[0],
690     odev.v.vp[1] + odev.v.vdir[1],
691     odev.v.vp[2] + odev.v.vdir[2],
692     odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
693 gregl 3.1 }
694    
695    
696     static
697 gwlarson 3.19 wipeclean() /* prepare for redraw */
698     {
699 gwlarson 3.22 /* clear depth buffer */
700 gwlarson 3.19 #ifdef STEREO
701     setstereobuf(STEREO_BUFFER_RIGHT);
702     glClear(GL_DEPTH_BUFFER_BIT);
703     setstereobuf(STEREO_BUFFER_LEFT);
704     #endif
705     glClear(GL_DEPTH_BUFFER_BIT);
706 gwlarson 3.22 smClean(); /* reset drawing routines */
707     setglpersp(&odev.v); /* reset view & clipping planes */
708 gwlarson 3.19 }
709    
710    
711     static
712 gregl 3.1 getkey(ekey) /* get input key */
713     register XKeyPressedEvent *ekey;
714     {
715     int n;
716     char buf[8];
717    
718     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
719     if (n != 1)
720     return;
721     switch (buf[0]) {
722     case 'h': /* turn on height motion lock */
723     headlocked = 1;
724     return;
725     case 'H': /* turn off height motion lock */
726     headlocked = 0;
727     return;
728     case 'l': /* retrieve last view */
729     inpresflags |= DFL(DC_LASTVIEW);
730     return;
731     case 'p': /* pause computation */
732     inpresflags |= DFL(DC_PAUSE);
733     return;
734     case 'v': /* spit out view */
735     inpresflags |= DFL(DC_GETVIEW);
736     return;
737     case '\n':
738     case '\r': /* resume computation */
739     inpresflags |= DFL(DC_RESUME);
740     return;
741     case CTRL('R'): /* redraw screen */
742 gwlarson 3.19 wipeclean();
743 gregl 3.1 return;
744     case CTRL('L'): /* refresh from server */
745     if (inpresflags & DFL(DC_REDRAW))
746     return;
747     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
748 gwlarson 3.19 glDisable(GL_DEPTH_TEST);
749 gwlarson 3.22 draw_grids(1);
750 gwlarson 3.19 #ifdef STEREO
751     pushright();
752     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
753 gwlarson 3.22 draw_grids(1);
754 gwlarson 3.19 popright();
755     #endif
756     glEnable(GL_DEPTH_TEST);
757 gregl 3.1 glFlush();
758 gwlarson 3.19 smInit(rsL.max_samp); /* get rid of old values */
759 gregl 3.1 inpresflags |= DFL(DC_REDRAW); /* resend values from server */
760 gwlarson 3.22 setglpersp(&odev.v); /* reset clipping planes */
761 gregl 3.13 rayqleft = 0; /* hold off update */
762 gregl 3.1 return;
763     case 'K': /* kill rtrace process(es) */
764     inpresflags |= DFL(DC_KILL);
765     break;
766     case 'R': /* restart rtrace */
767     inpresflags |= DFL(DC_RESTART);
768     break;
769     case 'C': /* clobber holodeck */
770     inpresflags |= DFL(DC_CLOBBER);
771     break;
772     case 'q': /* quit the program */
773     inpresflags |= DFL(DC_QUIT);
774     return;
775     default:
776     XBell(ourdisplay, 0);
777     return;
778     }
779     }
780    
781    
782     static
783     fixwindow(eexp) /* repair damage to window */
784     register XExposeEvent *eexp;
785     {
786 gwlarson 3.19 if (odev.hres == 0 | odev.vres == 0) { /* first exposure */
787 gregl 3.2 resizewindow((XConfigureEvent *)eexp);
788 gwlarson 3.19 return;
789     }
790     if (eexp->count) /* wait for final exposure */
791     return;
792     wipeclean(); /* clear depth */
793 gregl 3.1 }
794    
795    
796     static
797     resizewindow(ersz) /* resize window */
798     register XConfigureEvent *ersz;
799     {
800 gregl 3.6 glViewport(0, 0, ersz->width, ersz->height);
801    
802 gregl 3.1 if (ersz->width == odev.hres && ersz->height == odev.vres)
803     return;
804    
805     odev.hres = ersz->width;
806     odev.vres = ersz->height;
807    
808     odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
809     odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
810    
811     inpresflags |= DFL(DC_SETVIEW);
812     }