ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.9
Committed: Tue Dec 22 12:59:17 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.8: +20 -56 lines
Log Message:
changed portal code to write and read back buffer

File Contents

# User Rev Content
1 gwlarson 3.1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * OpenGL driver for holodeck display.
9     * Based on GLX driver using T-mesh.
10     *
11     * Define symbol STEREO for stereo viewing.
12     * Define symbol DOBJ for display object viewing.
13     */
14    
15     #ifdef NOSTEREO
16     #ifdef STEREO
17     #undef STEREO
18     #else
19     #undef NOSTEREO
20     #endif
21     #endif
22    
23     #include "standard.h"
24    
25     #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    
32     #include "rhd_odraw.h"
33     #ifdef DOBJ
34     #include "rhdobj.h"
35     #endif
36    
37     #include "x11icon.h"
38    
39     #ifndef RAYQLEN
40 gwlarson 3.6 #define RAYQLEN 50000 /* max. rays to queue before flush */
41 gwlarson 3.1 #endif
42    
43     #ifndef FEQ
44     #define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
45     #endif
46    
47 gwlarson 3.7 #define VWHEADLOCK 01 /* head position is locked flag */
48     #define VWPERSP 02 /* perspective view is set */
49     #define VWORTHO 04 /* orthographic view is set */
50     #define VWCHANGE 010 /* view has changed */
51     #define VWSTEADY 020 /* view is now steady */
52     #define VWMAPPED 040 /* window is mapped */
53    
54 gwlarson 3.1 #define GAMMA 1.4 /* default gamma correction */
55    
56     #define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask))
57    
58     #define MOVPCT 7 /* percent distance to move /frame */
59     #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
60     #define MOVDEG (-5) /* degrees to orbit CW/down /frame */
61     #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
62    
63     #define MINWIDTH 480 /* minimum graphics window width */
64     #define MINHEIGHT 400 /* minimum graphics window height */
65    
66     #define VIEWDIST 356 /* assumed viewing distance (mm) */
67    
68     #define BORWIDTH 5 /* border width */
69    
70     #define setstereobuf(bid) (glXWaitGL(), \
71     XSGISetStereoBuffer(ourdisplay, gwind, bid), \
72     glXWaitX())
73    
74     #define ourscreen DefaultScreen(ourdisplay)
75     #define ourroot RootWindow(ourdisplay,ourscreen)
76     #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
77     ButtonPressMask|ButtonReleaseMask)
78    
79     #define levptr(etype) ((etype *)&currentevent)
80    
81     struct driver odev; /* global device driver structure */
82    
83     char odev_args[64]; /* command arguments */
84    
85     static GLfloat *depthbuffer = NULL; /* depth buffer */
86    
87     #ifdef STEREO
88     static VIEW vwright; /* right eye view */
89     static GLfloat *depthright = NULL; /* right depth buffer */
90     #endif
91    
92     static int rayqleft = 0; /* rays left to queue before flush */
93    
94     static XEvent currentevent; /* current event */
95    
96     static unsigned long ourblack=0, ourwhite=~0;
97    
98     static Display *ourdisplay = NULL; /* our display */
99     static XVisualInfo *ourvinf; /* our visual information */
100     static Window gwind = 0; /* our graphics window */
101     static GLXContext gctx; /* our GLX context */
102    
103     static double pwidth, pheight; /* pixel dimensions (mm) */
104    
105     static double dev_zmin, dev_zmax; /* fore and aft clipping plane dist. */
106     static double dev_zrat; /* (1. - dev_zmin/dev_zmax) */
107    
108     #define setzrat() (dev_zrat = 1. - dev_zmin/dev_zmax)
109     #define mapdepth(d) ((d)>0.9995 ? FHUGE : dev_zmin/(1.-(d)*dev_zrat))
110    
111     static int inpresflags; /* input result flags */
112    
113 gwlarson 3.7 static int viewflags; /* what's happening with view */
114 gwlarson 3.1
115     static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
116 gwlarson 3.2 xferdepth(), freedepth(), setglortho(),
117 gwlarson 3.1 setglpersp(), getframe(), getmove(), fixwindow(), mytmflags();
118    
119     static double getdistance();
120    
121     #ifdef STEREO
122     static int pushright(), popright();
123     #endif
124    
125 gwlarson 3.2 extern int gmPortals; /* GL portal list id */
126    
127 gwlarson 3.1 extern time_t time();
128    
129    
130     dev_open(id) /* initialize GLX driver */
131     char *id;
132     {
133     extern char *getenv();
134     static RGBPRIMS myprims = STDPRIMS;
135 gwlarson 3.6 static int atlBest[] = {GLX_RGBA, GLX_DOUBLEBUFFER,
136     GLX_RED_SIZE,8, GLX_GREEN_SIZE,8,
137     GLX_BLUE_SIZE,8, GLX_DEPTH_SIZE,15, None};
138     static int atlOK[] = {GLX_RGBA, GLX_DOUBLEBUFFER,
139     GLX_RED_SIZE,4, GLX_GREEN_SIZE,4,
140     GLX_BLUE_SIZE,4, GLX_DEPTH_SIZE,15, None};
141 gwlarson 3.1 char *ev;
142     double gamval = GAMMA;
143     RGBPRIMP dpri = stdprims;
144     XSetWindowAttributes ourwinattr;
145     XWMHints ourxwmhints;
146     XSizeHints oursizhints;
147     /* check for unsupported stereo */
148     #ifdef NOSTEREO
149     error(INTERNAL, "stereo display driver unavailable");
150     #endif
151     /* open display server */
152     ourdisplay = XOpenDisplay(NULL);
153 gwlarson 3.5 CHECK(ourdisplay==NULL, USER,
154     "cannot open X-windows; DISPLAY variable set?");
155 gwlarson 3.1 #ifdef STEREO
156     switch (XSGIQueryStereoMode(ourdisplay, ourroot)) {
157     case STEREO_TOP:
158     case STEREO_BOTTOM:
159     break;
160     case STEREO_OFF:
161     error(USER,
162 gwlarson 3.5 "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
163 gwlarson 3.1 case X_STEREO_UNSUPPORTED:
164     error(USER, "stereo mode not supported on this screen");
165     default:
166     error(INTERNAL, "unknown stereo mode");
167     }
168     #endif
169     /* find a usable visual */
170     ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
171 gwlarson 3.6 if (ourvinf == NULL)
172     ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK);
173 gwlarson 3.5 CHECK(ourvinf==NULL, USER, "no suitable visuals available");
174 gwlarson 3.1 /* get a context */
175     gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
176     /* set gamma and tone mapping */
177     if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
178     || (ev = getenv("DISPLAY_GAMMA")) != NULL)
179     gamval = atof(ev);
180     if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
181     sscanf(ev, "%f %f %f %f %f %f %f %f",
182     &myprims[RED][CIEX],&myprims[RED][CIEY],
183     &myprims[GRN][CIEX],&myprims[GRN][CIEY],
184     &myprims[BLU][CIEX],&myprims[BLU][CIEY],
185     &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
186     dpri = myprims;
187     if (tmInit(mytmflags(), dpri, gamval) == NULL)
188     error(SYSTEM, "not enough memory in dev_open");
189     /* open window */
190     ourwinattr.background_pixel = ourblack;
191     ourwinattr.border_pixel = ourblack;
192     ourwinattr.event_mask = ourmask;
193     /* this is stupid */
194     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
195     ourvinf->visual, AllocNone);
196     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
197     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
198     #ifdef STEREO
199     (DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2,
200     #else
201     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
202     #endif
203     BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
204     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
205 gwlarson 3.5 CHECK(gwind==0, SYSTEM, "cannot create window");
206 gwlarson 3.1 XStoreName(ourdisplay, gwind, id);
207     /* set window manager hints */
208     ourxwmhints.flags = InputHint|IconPixmapHint;
209     ourxwmhints.input = True;
210     ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
211     gwind, x11icon_bits, x11icon_width, x11icon_height);
212     XSetWMHints(ourdisplay, gwind, &ourxwmhints);
213     oursizhints.min_width = MINWIDTH;
214     #ifdef STEREO
215     oursizhints.min_height = MINHEIGHT/2;
216     oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH;
217     oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) -
218     2*BORWIDTH)/2;
219     oursizhints.flags = PMinSize|PMaxSize;
220     #else
221     oursizhints.min_height = MINHEIGHT;
222     oursizhints.flags = PMinSize;
223     #endif
224     XSetNormalHints(ourdisplay, gwind, &oursizhints);
225     /* set GLX context */
226     glXMakeCurrent(ourdisplay, gwind, gctx);
227     glEnable(GL_DEPTH_TEST);
228     glDepthFunc(GL_LEQUAL);
229 gwlarson 3.9 glClearColor(0, 0, 0, 0);
230 gwlarson 3.1 glFrontFace(GL_CCW);
231     glDisable(GL_CULL_FACE);
232     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
233     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
234     /* figure out sensible view */
235     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
236     DisplayWidth(ourdisplay, ourscreen);
237     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
238     DisplayHeight(ourdisplay, ourscreen);
239     #ifdef STEREO
240     pheight *= 2.;
241     setstereobuf(STEREO_BUFFER_LEFT);
242     #endif
243     checkglerr("setting rendering parameters");
244     copystruct(&odev.v, &stdview);
245     odev.v.type = VT_PER;
246 gwlarson 3.7 viewflags = VWSTEADY; /* view starts static */
247 gwlarson 3.1 /* map the window */
248     XMapWindow(ourdisplay, gwind);
249     dev_input(); /* sets size and view angles */
250     if (!odInit(DisplayWidth(ourdisplay,ourscreen) *
251 gwlarson 3.4 DisplayHeight(ourdisplay,ourscreen) / 4))
252 gwlarson 3.1 error(SYSTEM, "insufficient memory for value storage");
253     odev.name = id;
254     odev.firstuse = 1; /* can't recycle samples */
255     odev.ifd = ConnectionNumber(ourdisplay);
256     }
257    
258    
259     dev_close() /* close our display and free resources */
260     {
261     #ifdef DOBJ
262     dobj_cleanup();
263     #endif
264 gwlarson 3.2 freedepth();
265     gmEndGeom();
266     gmEndPortal();
267 gwlarson 3.1 odDone();
268     glXMakeCurrent(ourdisplay, None, NULL);
269     glXDestroyContext(ourdisplay, gctx);
270     XDestroyWindow(ourdisplay, gwind);
271     gwind = 0;
272     XCloseDisplay(ourdisplay);
273     ourdisplay = NULL;
274     tmDone(NULL);
275     odev.v.type = 0;
276     odev.hres = odev.vres = 0;
277     odev.ifd = -1;
278     }
279    
280    
281     dev_clear() /* clear our representation */
282     {
283     wipeclean();
284     rayqleft = 0; /* hold off update */
285     }
286    
287    
288     int
289     dev_view(nv) /* assign new driver view */
290     register VIEW *nv;
291     {
292     double d;
293    
294     if (nv->type != VT_PER || /* check view legality */
295     nv->horiz > 160. || nv->vert > 160.) {
296     error(COMMAND, "illegal view type/angle");
297     nv->type = odev.v.type;
298     nv->horiz = odev.v.horiz;
299     nv->vert = odev.v.vert;
300     return(0);
301     }
302     if (nv != &odev.v) {
303     /* resize window? */
304     if (!FEQ(nv->horiz,odev.v.horiz) ||
305     !FEQ(nv->vert,odev.v.vert)) {
306     int dw = DisplayWidth(ourdisplay,ourscreen);
307     int dh = DisplayHeight(ourdisplay,ourscreen);
308    
309     dw -= 25; /* for window frame */
310     dh -= 50;
311     #ifdef STEREO
312     dh /= 2;
313     #endif
314     odev.hres = 2.*VIEWDIST/pwidth *
315     tan(PI/180./2.*nv->horiz);
316     odev.vres = 2.*VIEWDIST/pheight *
317     tan(PI/180./2.*nv->vert);
318     if (odev.hres > dw) {
319     odev.vres = dw * odev.vres / odev.hres;
320     odev.hres = dw;
321     }
322     if (odev.vres > dh) {
323     odev.hres = dh * odev.hres / odev.vres;
324     odev.vres = dh;
325     }
326     XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
327     dev_input(); /* get resize event */
328     }
329     copystruct(&odev.v, nv); /* setview() already called */
330     #ifdef STEREO
331     copystruct(&vwright, nv);
332     d = eyesepdist / sqrt(nv->hn2);
333     VSUM(vwright.vp, nv->vp, nv->hvec, d);
334     /* setview(&vwright); -- Unnecessary */
335     #endif
336 gwlarson 3.7 viewflags |= VWCHANGE;
337 gwlarson 3.6 }
338 gwlarson 3.1 wipeclean();
339     return(1);
340     }
341    
342    
343 gwlarson 3.2 dev_section(gfn, pfn) /* add octree for geometry rendering */
344     char *gfn, *pfn;
345 gwlarson 3.1 {
346     extern char *index();
347     char *cp;
348    
349 gwlarson 3.2 if (gfn == NULL) {
350     gmEndGeom();
351     gmEndPortal();
352 gwlarson 3.4 wipeclean(); /* new geometry, so redraw it */
353 gwlarson 3.2 return;
354     }
355     if (access(gfn, R_OK) == 0)
356     gmNewGeom(gfn);
357 gwlarson 3.1 #ifdef DEBUG
358     else {
359 gwlarson 3.2 sprintf(errmsg, "cannot load octree \"%s\"", gfn);
360 gwlarson 3.1 error(WARNING, errmsg);
361     }
362     #endif
363 gwlarson 3.2 if (pfn != NULL)
364     gmNewPortal(pfn);
365 gwlarson 3.1 }
366    
367    
368     dev_auxcom(cmd, args) /* process an auxiliary command */
369     char *cmd, *args;
370     {
371     #ifdef DOBJ
372     if (dobj_command(cmd, args) >= 0)
373     return;
374     #endif
375     sprintf(errmsg, "%s: unknown command", cmd);
376     error(COMMAND, errmsg);
377     }
378    
379    
380     VIEW *
381     dev_auxview(n, hvres) /* return nth auxiliary view */
382     int n;
383     int hvres[2];
384     {
385     hvres[0] = odev.hres; hvres[1] = odev.vres;
386     if (n == 0)
387     return(&odev.v);
388     #ifdef STEREO
389     if (n == 1)
390     return(&vwright);
391     #endif
392     return(NULL);
393     }
394    
395    
396     int
397     dev_input() /* get X11 input */
398     {
399     inpresflags = 0;
400    
401     do
402     getevent();
403    
404     while (XPending(ourdisplay) > 0);
405    
406     odev.inpready = 0;
407    
408     return(inpresflags);
409     }
410    
411    
412     dev_value(c, d, p) /* add a pixel value to our texture */
413     COLR c;
414     FVECT d, p;
415     {
416     #ifdef DOBJ
417     if (dobj_lightsamp != NULL) { /* in light source sampling */
418     (*dobj_lightsamp)(c, d, p);
419     return;
420     }
421     #endif
422     odSample(c, d, p); /* add to display representation */
423     if (!--rayqleft)
424     dev_flush(); /* flush output */
425     }
426    
427    
428     int
429     dev_flush() /* flush output as appropriate */
430     {
431     int ndrawn;
432    
433 gwlarson 3.7 if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) {
434 gwlarson 3.1 #ifdef STEREO
435     pushright(); /* draw right eye */
436 gwlarson 3.3 ndrawn = gmDrawGeom();
437 gwlarson 3.1 #ifdef DOBJ
438 gwlarson 3.3 ndrawn += dobj_render();
439 gwlarson 3.1 #endif
440     checkglerr("rendering right eye");
441     popright(); /* draw left eye */
442     #endif
443 gwlarson 3.3 ndrawn = gmDrawGeom();
444 gwlarson 3.1 #ifdef DOBJ
445     ndrawn += dobj_render();
446     #endif
447 gwlarson 3.6 glXSwapBuffers(ourdisplay, gwind);
448 gwlarson 3.1 checkglerr("rendering base view");
449     }
450 gwlarson 3.7 if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) ==
451     (VWMAPPED|VWSTEADY|VWPERSP)) {
452     /* first time after steady */
453     if (ndrawn)
454     xferdepth(); /* transfer and clear depth */
455     setglortho(); /* set orthographic view */
456    
457     }
458     if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) ==
459     (VWMAPPED|VWSTEADY|VWORTHO)) {
460     /* else update cones */
461 gwlarson 3.1 #ifdef STEREO
462 gwlarson 3.7 pushright();
463     odUpdate(1); /* draw right eye */
464     popright();
465 gwlarson 3.1 #endif
466 gwlarson 3.7 odUpdate(0); /* draw left eye */
467     glFlush(); /* flush OpenGL */
468     }
469 gwlarson 3.1 rayqleft = RAYQLEN;
470     /* flush X11 and return # pending */
471     return(odev.inpready = XPending(ourdisplay));
472     }
473    
474    
475     checkglerr(where) /* check for GL or GLU error */
476     char *where;
477     {
478     register GLenum errcode;
479    
480     while ((errcode = glGetError()) != GL_NO_ERROR) {
481     sprintf(errmsg, "OpenGL error %s: %s",
482     where, gluErrorString(errcode));
483     error(WARNING, errmsg);
484     }
485     }
486    
487    
488     static
489     xferdepth() /* load and clear depth buffer */
490     {
491     register GLfloat *dbp;
492 gwlarson 3.9 register GLubyte *pbuf;
493 gwlarson 3.1
494 gwlarson 3.3 if (depthbuffer == NULL) { /* allocate private depth buffer */
495 gwlarson 3.1 #ifdef STEREO
496     depthright = (GLfloat *)malloc(
497     odev.hres*odev.vres*sizeof(GLfloat));
498     #endif
499     depthbuffer = (GLfloat *)malloc(
500     odev.hres*odev.vres*sizeof(GLfloat));
501 gwlarson 3.5 CHECK(depthbuffer==NULL, SYSTEM, "out of memory in xferdepth");
502 gwlarson 3.1 }
503 gwlarson 3.3 /* allocate alpha buffer for portals */
504     if (gmPortals)
505 gwlarson 3.9 pbuf = (GLubyte *)malloc(odev.hres*odev.vres *
506     (4*sizeof(GLubyte)));
507 gwlarson 3.3 else
508 gwlarson 3.9 pbuf = NULL;
509 gwlarson 3.1 #ifdef STEREO
510     setstereobuf(STEREO_BUFFER_RIGHT);
511     glReadPixels(0, 0, odev.hres, odev.vres,
512     GL_DEPTH_COMPONENT, GL_FLOAT, depthright);
513 gwlarson 3.9 if (pbuf != NULL) {
514     glClear(GL_COLOR_BUFFER_BIT);
515     gmDrawPortals(0xff, -1, -1, -1);
516 gwlarson 3.3 glReadPixels(0, 0, odev.hres, odev.vres,
517 gwlarson 3.9 GL_RGBA, GL_UNSIGNED_BYTE, pbuf);
518     }
519 gwlarson 3.3 for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; )
520 gwlarson 3.9 if (pbuf != NULL && pbuf[4*(dbp-depthright)]&0x40)
521 gwlarson 3.3 *dbp = FHUGE;
522     else
523     *dbp = mapdepth(*dbp);
524     glClear(GL_DEPTH_BUFFER_BIT);
525 gwlarson 3.1 setstereobuf(STEREO_BUFFER_LEFT);
526     odDepthMap(1, depthright);
527     #endif
528 gwlarson 3.2 /* read back depth buffer */
529 gwlarson 3.1 glReadPixels(0, 0, odev.hres, odev.vres,
530     GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer);
531 gwlarson 3.9 if (pbuf != NULL) {
532     glClear(GL_COLOR_BUFFER_BIT); /* find portals */
533     gmDrawPortals(0xff, -1, -1, -1);
534 gwlarson 3.2 glReadPixels(0, 0, odev.hres, odev.vres,
535 gwlarson 3.9 GL_RGBA, GL_UNSIGNED_BYTE, pbuf);
536     }
537 gwlarson 3.1 for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; )
538 gwlarson 3.9 if (pbuf != NULL && pbuf[4*(dbp-depthbuffer)]&0x40)
539 gwlarson 3.2 *dbp = FHUGE;
540     else
541     *dbp = mapdepth(*dbp);
542 gwlarson 3.3 glClear(GL_DEPTH_BUFFER_BIT); /* clear system depth buffer */
543 gwlarson 3.1 odDepthMap(0, depthbuffer); /* transfer depth data */
544 gwlarson 3.9 if (pbuf != NULL)
545     free((char *)pbuf); /* free our portal buffer */
546 gwlarson 3.1 }
547    
548    
549 gwlarson 3.2 static
550     freedepth() /* free recorded depth buffer */
551     {
552     if (depthbuffer == NULL)
553     return;
554     #ifdef STEREO
555     odDepthMap(1, NULL);
556     free((char *)depthright);
557     depthright = NULL;
558     #endif
559     odDepthMap(0, NULL);
560     free((char *)depthbuffer);
561     depthbuffer = NULL;
562     }
563    
564    
565 gwlarson 3.1 static double
566     getdistance(dx, dy, direc) /* distance from fore plane along view ray */
567     int dx, dy;
568     FVECT direc;
569     {
570     GLfloat gldepth;
571     double dist;
572    
573     if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres)
574     return(FHUGE);
575     if (depthbuffer != NULL)
576     dist = depthbuffer[dy*odev.hres + dx];
577     else {
578     glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT,
579     GL_FLOAT, &gldepth);
580 gwlarson 3.3 dist = mapdepth(gldepth);
581 gwlarson 3.1 }
582     if (dist >= .99*FHUGE)
583     return(FHUGE);
584     return((dist-odev.v.vfore)/DOT(direc,odev.v.vdir));
585     }
586    
587    
588     #ifdef STEREO
589     static
590     pushright() /* push on right view & buffer */
591     {
592     double d;
593    
594     setstereobuf(STEREO_BUFFER_RIGHT);
595 gwlarson 3.7 if (viewflags & VWPERSP) {
596 gwlarson 3.1 glMatrixMode(GL_MODELVIEW);
597     glPushMatrix();
598     d = -eyesepdist / sqrt(odev.v.hn2);
599     glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1],
600     d*odev.v.hvec[2]);
601     checkglerr("setting right view");
602     }
603     }
604    
605    
606     static
607     popright() /* pop off right view & buffer */
608     {
609 gwlarson 3.7 if (viewflags & VWPERSP) {
610 gwlarson 3.1 glMatrixMode(GL_MODELVIEW);
611     glPopMatrix();
612     }
613     setstereobuf(STEREO_BUFFER_LEFT);
614     }
615     #endif
616    
617    
618     static int
619     mytmflags() /* figure out tone mapping flags */
620     {
621     extern char *progname;
622     register char *cp, *tail;
623     /* find basic name */
624     for (cp = tail = progname; *cp; cp++)
625     if (*cp == '/')
626     tail = cp+1;
627     for (cp = tail; *cp && *cp != '.'; cp++)
628     ;
629     #ifdef DEBUG
630     if (cp > tail && cp[-1] == 'h')
631     return(TM_F_HUMAN);
632     else
633     return(TM_F_CAMERA);
634     #else
635     if (cp > tail && cp[-1] == 'h')
636     return(TM_F_HUMAN|TM_F_NOSTDERR);
637     else
638     return(TM_F_CAMERA|TM_F_NOSTDERR);
639     #endif
640     }
641    
642    
643     static
644     getevent() /* get next event */
645     {
646     XNextEvent(ourdisplay, levptr(XEvent));
647     switch (levptr(XEvent)->type) {
648     case ConfigureNotify:
649     resizewindow(levptr(XConfigureEvent));
650     break;
651     case UnmapNotify:
652 gwlarson 3.7 viewflags &= ~VWMAPPED;
653 gwlarson 3.1 break;
654     case MapNotify:
655 gwlarson 3.2 odRemap(0);
656 gwlarson 3.7 viewflags |= VWMAPPED;
657 gwlarson 3.1 break;
658     case Expose:
659     fixwindow(levptr(XExposeEvent));
660     break;
661     case KeyPress:
662     getkey(levptr(XKeyPressedEvent));
663     break;
664     case ButtonPress:
665     if (FRAMESTATE(levptr(XButtonPressedEvent)->state))
666     getframe(levptr(XButtonPressedEvent));
667     else
668     getmove(levptr(XButtonPressedEvent));
669     break;
670     }
671     }
672    
673    
674     static
675     draw3dline(wp) /* draw 3d line in world coordinates */
676     register FVECT wp[2];
677     {
678     glVertex3d(wp[0][0], wp[0][1], wp[0][2]);
679     glVertex3d(wp[1][0], wp[1][1], wp[1][2]);
680     }
681    
682    
683     static
684     draw_grids(fore) /* draw holodeck section grids */
685     int fore;
686     {
687     glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT);
688     glDisable(GL_LIGHTING);
689     if (fore)
690 gwlarson 3.8 glColor3ub(4, 250, 250);
691 gwlarson 3.1 else
692     glColor3ub(0, 0, 0);
693     glBegin(GL_LINES); /* draw each grid line */
694     gridlines(draw3dline);
695     glEnd();
696     checkglerr("drawing grid lines");
697     glPopAttrib();
698     }
699    
700    
701     static
702     moveview(dx, dy, mov, orb) /* move our view */
703     int dx, dy, mov, orb;
704     {
705     VIEW nv;
706     FVECT odir, v1, wip;
707     double d, d1;
708     register int li;
709     /* start with old view */
710     copystruct(&nv, &odev.v);
711     /* orient our motion */
712     if (viewray(v1, odir, &odev.v,
713     (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
714     return(0); /* outside view */
715     if (mov | orb) { /* moving relative to geometry */
716     d = getdistance(dx, dy, odir); /* distance from front plane */
717     #ifdef DOBJ
718     d1 = dobj_trace(NULL, v1, odir);
719     if (d1 < d)
720     d = d1;
721     #endif
722     if (d >= .99*FHUGE)
723     d = 0.5*(dev_zmax+dev_zmin); /* just guess */
724     VSUM(wip, v1, odir, d);
725     VSUB(odir, wip, odev.v.vp);
726     } else /* panning with constant viewpoint */
727     VCOPY(nv.vdir, odir);
728     if (orb && mov) { /* orbit left/right */
729     spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
730     VSUM(nv.vp, wip, odir, -1.);
731     spinvector(nv.vdir, nv.vdir, nv.vup, d);
732     } else if (orb) { /* orbit up/down */
733     fcross(v1, odir, nv.vup);
734     if (normalize(v1) == 0.)
735     return(0);
736     spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
737     VSUM(nv.vp, wip, odir, -1.);
738     spinvector(nv.vdir, nv.vdir, v1, d);
739     } else if (mov) { /* move forward/backward */
740     d = MOVPCT/100. * mov;
741     VSUM(nv.vp, nv.vp, odir, d);
742     }
743 gwlarson 3.7 if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */
744 gwlarson 3.1 VSUM(v1, odev.v.vp, nv.vp, -1.);
745     d = DOT(v1, nv.vup);
746     VSUM(nv.vp, nv.vp, odev.v.vup, d);
747     }
748     if (setview(&nv) != NULL)
749     return(0); /* illegal view */
750     dev_view(&nv);
751 gwlarson 3.2 inpresflags |= DFL(DC_SETVIEW);
752 gwlarson 3.1 return(1);
753     }
754    
755    
756     static
757     getframe(ebut) /* get focus frame */
758     XButtonPressedEvent *ebut;
759     {
760     int startx = ebut->x, starty = ebut->y;
761     int endx, endy;
762    
763     XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent));
764     endx = levptr(XButtonReleasedEvent)->x;
765     endy = levptr(XButtonReleasedEvent)->y;
766     if (endx == startx | endy == starty) {
767     XBell(ourdisplay, 0);
768     return;
769     }
770     if (endx < startx) {register int c = endx; endx = startx; startx = c;}
771     if (endy < starty) {register int c = endy; endy = starty; starty = c;}
772     sprintf(odev_args, "%.3f %.3f %.3f %.3f",
773     (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres,
774     (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres);
775     inpresflags |= DFL(DC_FOCUS);
776     }
777    
778    
779     static
780     getmove(ebut) /* get view change */
781     XButtonPressedEvent *ebut;
782     {
783     int movdir = MOVDIR(ebut->button);
784     int movorb = MOVORB(ebut->state);
785 gwlarson 3.2 int ndrawn;
786 gwlarson 3.1 Window rootw, childw;
787     int rootx, rooty, wx, wy;
788     unsigned int statemask;
789    
790     XNoOp(ourdisplay); /* makes sure we're not idle */
791    
792 gwlarson 3.7 viewflags &= ~VWSTEADY; /* flag moving view */
793 gwlarson 3.1 setglpersp(&odev.v); /* start us off in perspective */
794     while (!XCheckMaskEvent(ourdisplay,
795     ButtonReleaseMask, levptr(XEvent))) {
796     /* get cursor position */
797     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
798     &rootx, &rooty, &wx, &wy, &statemask))
799     break; /* on another screen */
800     /* compute view motion */
801     if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
802     sleep(1);
803     continue; /* cursor in bad place */
804     }
805     draw_grids(1); /* redraw grid */
806     #ifdef STEREO
807     pushright();
808     draw_grids(1);
809 gwlarson 3.3 ndrawn = gmDrawGeom();
810 gwlarson 3.1 #ifdef DOBJ
811 gwlarson 3.3 ndrawn += dobj_render();
812 gwlarson 3.1 #endif
813     popright();
814     #endif
815     /* redraw octrees */
816 gwlarson 3.3 ndrawn = gmDrawGeom();
817 gwlarson 3.1 #ifdef DOBJ
818 gwlarson 3.2 ndrawn += dobj_render(); /* redraw objects */
819 gwlarson 3.1 #endif
820 gwlarson 3.6 glXSwapBuffers(ourdisplay, gwind);
821     if (!ndrawn)
822 gwlarson 3.2 sleep(1); /* for reasonable interaction */
823 gwlarson 3.1 }
824     if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
825     movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
826     wx = levptr(XButtonReleasedEvent)->x;
827     wy = levptr(XButtonReleasedEvent)->y;
828     moveview(wx, odev.vres-1-wy, movdir, movorb);
829     }
830 gwlarson 3.7 viewflags |= VWSTEADY; /* done goofing around */
831 gwlarson 3.1 }
832    
833    
834     static
835     setglpersp(vp) /* set perspective view in GL */
836     register VIEW *vp;
837     {
838     double d, xmin, xmax, ymin, ymax;
839     GLfloat vec[4];
840     double depthlim[2];
841     /* set depth limits */
842 gwlarson 3.2 gmDepthLimit(depthlim, odev.v.vp, odev.v.vdir);
843 gwlarson 3.1 if (depthlim[0] >= depthlim[1]) {
844     dev_zmin = 1.;
845     dev_zmax = 100.;
846     } else {
847     dev_zmin = 0.5*depthlim[0];
848 gwlarson 3.2 dev_zmax = 1.75*depthlim[1];
849 gwlarson 3.1 if (dev_zmin > dev_zmax/5.)
850     dev_zmin = dev_zmax/5.;
851     }
852     if (odev.v.vfore > FTINY)
853     dev_zmin = odev.v.vfore;
854     if (odev.v.vaft > FTINY)
855     dev_zmax = odev.v.vaft;
856     if (dev_zmin < dev_zmax/100.)
857     dev_zmin = dev_zmax/100.;
858     setzrat();
859     xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
860     xmin = -xmax;
861     d = odev.v.hoff * (xmax - xmin);
862     xmin += d; xmax += d;
863     ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
864     ymin = -ymax;
865     d = odev.v.voff * (ymax - ymin);
866     ymin += d; ymax += d;
867     /* set view matrix */
868     glMatrixMode(GL_PROJECTION);
869     glLoadIdentity();
870     glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
871     gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
872     odev.v.vp[0] + odev.v.vdir[0],
873     odev.v.vp[1] + odev.v.vdir[1],
874     odev.v.vp[2] + odev.v.vdir[2],
875     odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
876     checkglerr("setting perspective view");
877     vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.;
878     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec);
879     vec[0] = -odev.v.vdir[0];
880     vec[1] = -odev.v.vdir[1];
881     vec[2] = -odev.v.vdir[2];
882     vec[3] = 0.;
883     glLightfv(GL_LIGHT0, GL_POSITION, vec);
884     vec[0] = vec[1] = vec[2] = .7; vec[3] = 1.;
885     glLightfv(GL_LIGHT0, GL_SPECULAR, vec);
886     glLightfv(GL_LIGHT0, GL_DIFFUSE, vec);
887     vec[0] = vec[1] = vec[2] = .3; vec[3] = 1.;
888     glLightfv(GL_LIGHT0, GL_AMBIENT, vec);
889     glEnable(GL_LIGHT0);
890     glEnable(GL_LIGHTING); /* light our GL objects */
891     glShadeModel(GL_SMOOTH);
892 gwlarson 3.7 viewflags &= ~VWORTHO;
893     viewflags |= VWPERSP;
894 gwlarson 3.1 }
895    
896    
897     static
898     setglortho() /* set up orthographic view for cone drawing */
899     {
900 gwlarson 3.6 glDrawBuffer(GL_FRONT); /* use single-buffer mode */
901 gwlarson 3.1 /* set view matrix */
902     glMatrixMode(GL_PROJECTION);
903     glLoadIdentity();
904     glOrtho(0., (double)odev.hres, 0., (double)odev.vres,
905     0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH));
906     checkglerr("setting orthographic view");
907     glDisable(GL_LIGHTING); /* cones are constant color */
908     glShadeModel(GL_FLAT);
909 gwlarson 3.7 viewflags &= ~VWPERSP;
910     viewflags |= VWORTHO;
911 gwlarson 3.1 }
912    
913    
914     static
915     wipeclean() /* prepare for redraw */
916     {
917 gwlarson 3.6 glDrawBuffer(GL_BACK); /* use double-buffer mode */
918 gwlarson 3.9 glReadBuffer(GL_BACK);
919 gwlarson 3.6 /* clear buffers */
920 gwlarson 3.1 #ifdef STEREO
921     setstereobuf(STEREO_BUFFER_RIGHT);
922 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
923 gwlarson 3.1 setstereobuf(STEREO_BUFFER_LEFT);
924     #endif
925 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
926     freedepth();
927 gwlarson 3.7 if ((viewflags&(VWCHANGE|VWSTEADY)) ==
928     (VWCHANGE|VWSTEADY)) { /* clear samples if new */
929 gwlarson 3.1 odClean();
930 gwlarson 3.7 viewflags &= ~VWCHANGE; /* change noted */
931     }
932 gwlarson 3.1 setglpersp(&odev.v); /* reset view & clipping planes */
933     }
934    
935    
936     static
937     getkey(ekey) /* get input key */
938     register XKeyPressedEvent *ekey;
939     {
940     Window rootw, childw;
941     int rootx, rooty, wx, wy;
942     unsigned int statemask;
943     int n;
944     char buf[8];
945    
946     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
947     if (n != 1)
948     return;
949     switch (buf[0]) {
950     case 'h': /* turn on height motion lock */
951 gwlarson 3.7 viewflags |= VWHEADLOCK;
952 gwlarson 3.1 return;
953     case 'H': /* turn off height motion lock */
954 gwlarson 3.7 viewflags &= ~VWHEADLOCK;
955 gwlarson 3.1 return;
956     case 'l': /* retrieve last view */
957     inpresflags |= DFL(DC_LASTVIEW);
958     return;
959     case 'p': /* pause computation */
960     inpresflags |= DFL(DC_PAUSE);
961     return;
962     case 'v': /* spit out view */
963     inpresflags |= DFL(DC_GETVIEW);
964     return;
965     case 'f': /* frame view position */
966     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
967     &rootx, &rooty, &wx, &wy, &statemask))
968     return; /* on another screen */
969     sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres,
970     1.-(wy+.5)/odev.vres);
971     inpresflags |= DFL(DC_FOCUS);
972     return;
973     case 'F': /* unfocus */
974     odev_args[0] = '\0';
975     inpresflags |= DFL(DC_FOCUS);
976     return;
977     case '\n':
978     case '\r': /* resume computation */
979     inpresflags |= DFL(DC_RESUME);
980     return;
981     case CTRL('R'): /* redraw screen */
982 gwlarson 3.2 odRemap(0);
983 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT);
984 gwlarson 3.1 #ifdef STEREO
985     setstereobuf(STEREO_BUFFER_RIGHT);
986 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT);
987 gwlarson 3.1 setstereobuf(STEREO_BUFFER_LEFT);
988     #endif
989     return;
990     case CTRL('L'): /* refresh from server */
991     if (inpresflags & DFL(DC_REDRAW))
992     return;
993     XRaiseWindow(ourdisplay, gwind);
994     XFlush(ourdisplay);
995     sleep(1);
996     wipeclean(); /* fresh display */
997 gwlarson 3.2 odRemap(1); /* fresh tone mapping */
998 gwlarson 3.1 dev_flush(); /* draw octrees */
999     inpresflags |= DFL(DC_REDRAW); /* resend values from server */
1000     rayqleft = 0; /* hold off update */
1001     return;
1002     case 'K': /* kill rtrace process(es) */
1003     inpresflags |= DFL(DC_KILL);
1004     break;
1005     case 'R': /* restart rtrace */
1006     inpresflags |= DFL(DC_RESTART);
1007     break;
1008     case 'C': /* clobber holodeck */
1009     inpresflags |= DFL(DC_CLOBBER);
1010     break;
1011     case 'q': /* quit the program */
1012     inpresflags |= DFL(DC_QUIT);
1013     return;
1014     default:
1015     XBell(ourdisplay, 0);
1016     return;
1017     }
1018     }
1019    
1020    
1021     static
1022     fixwindow(eexp) /* repair damage to window */
1023     register XExposeEvent *eexp;
1024     {
1025     int xmin, ymin, xmax, ymax;
1026    
1027     if (odev.hres == 0 | odev.vres == 0) { /* first exposure */
1028     resizewindow((XConfigureEvent *)eexp);
1029     return;
1030     }
1031     xmin = eexp->x; xmax = eexp->x + eexp->width;
1032     ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
1033     /* clear portion of depth */
1034 gwlarson 3.2 glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1035 gwlarson 3.1 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1036     glDepthFunc(GL_ALWAYS);
1037     glBegin(GL_POLYGON);
1038     glVertex3i(xmin, ymin, OMAXDEPTH);
1039     glVertex3i(xmax, ymin, OMAXDEPTH);
1040     glVertex3i(xmax, ymax, OMAXDEPTH);
1041     glVertex3i(xmin, ymax, OMAXDEPTH);
1042     glEnd();
1043     #ifdef STEREO
1044     setstereobuf(STEREO_BUFFER_RIGHT);
1045     glBegin(GL_POLYGON);
1046     glVertex3i(xmin, ymin, OMAXDEPTH);
1047     glVertex3i(xmax, ymin, OMAXDEPTH);
1048     glVertex3i(xmax, ymax, OMAXDEPTH);
1049     glVertex3i(xmin, ymax, OMAXDEPTH);
1050     glEnd();
1051     odRedraw(1, xmin, ymin, xmax, ymax);
1052     setstereobuf(STEREO_BUFFER_LEFT);
1053     #endif
1054 gwlarson 3.2 glPopAttrib();
1055 gwlarson 3.1 odRedraw(0, xmin, ymin, xmax, ymax);
1056     }
1057    
1058    
1059     static
1060     resizewindow(ersz) /* resize window */
1061     register XConfigureEvent *ersz;
1062     {
1063     glViewport(0, 0, ersz->width, ersz->height);
1064    
1065     if (ersz->width == odev.hres && ersz->height == odev.vres)
1066     return;
1067    
1068     odev.hres = ersz->width;
1069     odev.vres = ersz->height;
1070    
1071     odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
1072     odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
1073    
1074 gwlarson 3.2 inpresflags |= DFL(DC_SETVIEW);
1075 gwlarson 3.7 viewflags |= VWCHANGE;
1076 gwlarson 3.1 }