ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_glx.c
Revision: 3.23
Committed: Thu Aug 27 19:33:05 1998 UTC (25 years, 7 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.22: +19 -13 lines
Log Message:
improved grid drawing in stereo mode

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