ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_glx.c
Revision: 3.26
Committed: Mon Dec 28 17:58:45 1998 UTC (25 years, 3 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.25: +3 -5 lines
Log Message:
New insertion routine
New Culling routine based on insertion algorithm
Adapted old insertion code: now used by picking
Point location code returns on-vertex,on-edge, or in-triangle
Added on_edge case for subdivision
implemented  unordered sets
Removed deletion from quadtree- added set compression to replace functionality

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