ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.8
Committed: Tue Dec 22 10:18:44 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.7: +22 -16 lines
Log Message:
improved portal code

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