ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.19
Committed: Sat Feb 22 02:07:24 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.18: +18 -8 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 greg 3.19 static const char RCSid[] = "$Id$";
3 gwlarson 3.1 #endif
4     /*
5     * OpenGL driver for holodeck display.
6     * Based on GLX driver using T-mesh.
7     *
8     * Define symbol STEREO for stereo viewing.
9     * Define symbol DOBJ for display object viewing.
10     */
11    
12     #ifdef NOSTEREO
13     #ifdef STEREO
14     #undef STEREO
15     #else
16     #undef NOSTEREO
17     #endif
18     #endif
19    
20     #include "standard.h"
21    
22     #include <GL/glx.h>
23     #include <GL/glu.h>
24     #ifdef STEREO
25     #include <X11/extensions/SGIStereo.h>
26     #endif
27 greg 3.19 #include <time.h>
28 gwlarson 3.1
29     #include "rhd_odraw.h"
30     #ifdef DOBJ
31     #include "rhdobj.h"
32     #endif
33    
34     #include "x11icon.h"
35    
36     #ifndef RAYQLEN
37 gwlarson 3.11 #define RAYQLEN 0 /* max. rays to queue before flush */
38 gwlarson 3.1 #endif
39 gwlarson 3.15 #ifndef MINWIDTH
40     #define MINWIDTH 480 /* minimum graphics window width */
41     #define MINHEIGHT 400 /* minimum graphics window height */
42     #endif
43     #ifndef VIEWDIST
44     #define VIEWDIST 356 /* assumed viewing distance (mm) */
45     #endif
46     #ifndef BORWIDTH
47     #define BORWIDTH 5 /* border width */
48     #endif
49 gwlarson 3.1
50     #ifndef FEQ
51     #define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
52     #endif
53    
54 gwlarson 3.7 #define VWHEADLOCK 01 /* head position is locked flag */
55     #define VWPERSP 02 /* perspective view is set */
56     #define VWORTHO 04 /* orthographic view is set */
57     #define VWCHANGE 010 /* view has changed */
58     #define VWSTEADY 020 /* view is now steady */
59     #define VWMAPPED 040 /* window is mapped */
60    
61 gwlarson 3.1 #define GAMMA 1.4 /* default gamma correction */
62    
63     #define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask))
64    
65     #define MOVPCT 7 /* percent distance to move /frame */
66     #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
67     #define MOVDEG (-5) /* degrees to orbit CW/down /frame */
68     #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
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 gwlarson 3.10 glPixelStorei(GL_PACK_ALIGNMENT, 1);
235     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
236 gwlarson 3.1 /* figure out sensible view */
237     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
238     DisplayWidth(ourdisplay, ourscreen);
239     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
240     DisplayHeight(ourdisplay, ourscreen);
241     #ifdef STEREO
242     pheight *= 2.;
243     setstereobuf(STEREO_BUFFER_LEFT);
244     #endif
245     checkglerr("setting rendering parameters");
246     copystruct(&odev.v, &stdview);
247     odev.v.type = VT_PER;
248 gwlarson 3.7 viewflags = VWSTEADY; /* view starts static */
249 gwlarson 3.1 /* map the window */
250     XMapWindow(ourdisplay, gwind);
251     dev_input(); /* sets size and view angles */
252     if (!odInit(DisplayWidth(ourdisplay,ourscreen) *
253 gwlarson 3.11 DisplayHeight(ourdisplay,ourscreen) / 3))
254 gwlarson 3.1 error(SYSTEM, "insufficient memory for value storage");
255     odev.name = id;
256     odev.firstuse = 1; /* can't recycle samples */
257     odev.ifd = ConnectionNumber(ourdisplay);
258     }
259    
260    
261     dev_close() /* close our display and free resources */
262     {
263     #ifdef DOBJ
264     dobj_cleanup();
265     #endif
266 gwlarson 3.2 freedepth();
267     gmEndGeom();
268     gmEndPortal();
269 gwlarson 3.1 odDone();
270     glXMakeCurrent(ourdisplay, None, NULL);
271     glXDestroyContext(ourdisplay, gctx);
272     XDestroyWindow(ourdisplay, gwind);
273     gwind = 0;
274     XCloseDisplay(ourdisplay);
275     ourdisplay = NULL;
276     tmDone(NULL);
277     odev.v.type = 0;
278     odev.hres = odev.vres = 0;
279     odev.ifd = -1;
280     }
281    
282    
283     dev_clear() /* clear our representation */
284     {
285 gwlarson 3.10 viewflags |= VWCHANGE; /* pretend our view has changed */
286     wipeclean(); /* clean off display and samples */
287 gwlarson 3.12 dev_flush(); /* redraw geometry & get depth */
288 gwlarson 3.1 rayqleft = 0; /* hold off update */
289     }
290    
291    
292     int
293     dev_view(nv) /* assign new driver view */
294     register VIEW *nv;
295     {
296     double d;
297    
298     if (nv->type != VT_PER || /* check view legality */
299     nv->horiz > 160. || nv->vert > 160.) {
300     error(COMMAND, "illegal view type/angle");
301     nv->type = odev.v.type;
302     nv->horiz = odev.v.horiz;
303     nv->vert = odev.v.vert;
304     return(0);
305     }
306     if (nv != &odev.v) {
307     /* resize window? */
308     if (!FEQ(nv->horiz,odev.v.horiz) ||
309     !FEQ(nv->vert,odev.v.vert)) {
310     int dw = DisplayWidth(ourdisplay,ourscreen);
311     int dh = DisplayHeight(ourdisplay,ourscreen);
312    
313     dw -= 25; /* for window frame */
314     dh -= 50;
315     #ifdef STEREO
316     dh /= 2;
317     #endif
318     odev.hres = 2.*VIEWDIST/pwidth *
319     tan(PI/180./2.*nv->horiz);
320     odev.vres = 2.*VIEWDIST/pheight *
321     tan(PI/180./2.*nv->vert);
322     if (odev.hres > dw) {
323     odev.vres = dw * odev.vres / odev.hres;
324     odev.hres = dw;
325     }
326     if (odev.vres > dh) {
327     odev.hres = dh * odev.hres / odev.vres;
328     odev.vres = dh;
329     }
330     XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
331     dev_input(); /* get resize event */
332     }
333     copystruct(&odev.v, nv); /* setview() already called */
334 gwlarson 3.14 viewflags |= VWCHANGE;
335     }
336 gwlarson 3.1 #ifdef STEREO
337 gwlarson 3.14 copystruct(&vwright, nv);
338     d = eyesepdist / sqrt(nv->hn2);
339     VSUM(vwright.vp, nv->vp, nv->hvec, d);
340     /* setview(&vwright); -- Unnecessary */
341 gwlarson 3.1 #endif
342     wipeclean();
343     return(1);
344     }
345    
346    
347 gwlarson 3.2 dev_section(gfn, pfn) /* add octree for geometry rendering */
348     char *gfn, *pfn;
349 gwlarson 3.1 {
350     extern char *index();
351     char *cp;
352    
353 gwlarson 3.2 if (gfn == NULL) {
354     gmEndGeom();
355     gmEndPortal();
356 gwlarson 3.4 wipeclean(); /* new geometry, so redraw it */
357 gwlarson 3.2 return;
358     }
359     if (access(gfn, R_OK) == 0)
360     gmNewGeom(gfn);
361 gwlarson 3.1 #ifdef DEBUG
362     else {
363 gwlarson 3.2 sprintf(errmsg, "cannot load octree \"%s\"", gfn);
364 gwlarson 3.1 error(WARNING, errmsg);
365     }
366     #endif
367 gwlarson 3.2 if (pfn != NULL)
368     gmNewPortal(pfn);
369 gwlarson 3.1 }
370    
371    
372     dev_auxcom(cmd, args) /* process an auxiliary command */
373     char *cmd, *args;
374     {
375     #ifdef DOBJ
376 gwlarson 3.12 int vischange;
377    
378     if ((vischange = dobj_command(cmd, args)) >= 0) {
379     if (vischange) {
380     imm_mode = beam_sync(1) > 0;
381     dev_clear();
382     }
383 gwlarson 3.1 return;
384 gwlarson 3.12 }
385 gwlarson 3.1 #endif
386     sprintf(errmsg, "%s: unknown command", cmd);
387     error(COMMAND, errmsg);
388     }
389    
390    
391     VIEW *
392     dev_auxview(n, hvres) /* return nth auxiliary view */
393     int n;
394     int hvres[2];
395     {
396     hvres[0] = odev.hres; hvres[1] = odev.vres;
397     if (n == 0)
398     return(&odev.v);
399     #ifdef STEREO
400     if (n == 1)
401     return(&vwright);
402     #endif
403     return(NULL);
404     }
405    
406    
407     int
408     dev_input() /* get X11 input */
409     {
410     inpresflags = 0;
411    
412     do
413     getevent();
414    
415     while (XPending(ourdisplay) > 0);
416    
417     odev.inpready = 0;
418    
419     return(inpresflags);
420     }
421    
422    
423     dev_value(c, d, p) /* add a pixel value to our texture */
424     COLR c;
425     FVECT d, p;
426     {
427     #ifdef DOBJ
428     if (dobj_lightsamp != NULL) { /* in light source sampling */
429     (*dobj_lightsamp)(c, d, p);
430     return;
431     }
432     #endif
433     odSample(c, d, p); /* add to display representation */
434     if (!--rayqleft)
435     dev_flush(); /* flush output */
436     }
437    
438    
439     int
440     dev_flush() /* flush output as appropriate */
441     {
442     int ndrawn;
443    
444 gwlarson 3.7 if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) {
445 gwlarson 3.1 #ifdef STEREO
446     pushright(); /* draw right eye */
447 gwlarson 3.3 ndrawn = gmDrawGeom();
448 gwlarson 3.1 #ifdef DOBJ
449 gwlarson 3.3 ndrawn += dobj_render();
450 gwlarson 3.1 #endif
451     checkglerr("rendering right eye");
452     popright(); /* draw left eye */
453     #endif
454 gwlarson 3.3 ndrawn = gmDrawGeom();
455 gwlarson 3.1 #ifdef DOBJ
456     ndrawn += dobj_render();
457     #endif
458 gwlarson 3.6 glXSwapBuffers(ourdisplay, gwind);
459 gwlarson 3.1 checkglerr("rendering base view");
460     }
461 gwlarson 3.7 if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) ==
462     (VWMAPPED|VWSTEADY|VWPERSP)) {
463     /* first time after steady */
464     if (ndrawn)
465     xferdepth(); /* transfer and clear depth */
466     setglortho(); /* set orthographic view */
467    
468     }
469     if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) ==
470     (VWMAPPED|VWSTEADY|VWORTHO)) {
471     /* else update cones */
472 gwlarson 3.1 #ifdef STEREO
473 gwlarson 3.7 pushright();
474     odUpdate(1); /* draw right eye */
475     popright();
476 gwlarson 3.1 #endif
477 gwlarson 3.7 odUpdate(0); /* draw left eye */
478     glFlush(); /* flush OpenGL */
479     }
480 gwlarson 3.1 rayqleft = RAYQLEN;
481     /* flush X11 and return # pending */
482     return(odev.inpready = XPending(ourdisplay));
483     }
484    
485    
486     checkglerr(where) /* check for GL or GLU error */
487     char *where;
488     {
489     register GLenum errcode;
490    
491     while ((errcode = glGetError()) != GL_NO_ERROR) {
492     sprintf(errmsg, "OpenGL error %s: %s",
493     where, gluErrorString(errcode));
494     error(WARNING, errmsg);
495     }
496     }
497    
498    
499     static
500     xferdepth() /* load and clear depth buffer */
501     {
502     register GLfloat *dbp;
503 gwlarson 3.9 register GLubyte *pbuf;
504 gwlarson 3.1
505 gwlarson 3.3 if (depthbuffer == NULL) { /* allocate private depth buffer */
506 gwlarson 3.1 #ifdef STEREO
507     depthright = (GLfloat *)malloc(
508     odev.hres*odev.vres*sizeof(GLfloat));
509     #endif
510     depthbuffer = (GLfloat *)malloc(
511     odev.hres*odev.vres*sizeof(GLfloat));
512 gwlarson 3.5 CHECK(depthbuffer==NULL, SYSTEM, "out of memory in xferdepth");
513 gwlarson 3.1 }
514 gwlarson 3.3 /* allocate alpha buffer for portals */
515     if (gmPortals)
516 gwlarson 3.10 pbuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte));
517 gwlarson 3.3 else
518 gwlarson 3.9 pbuf = NULL;
519 gwlarson 3.1 #ifdef STEREO
520 gwlarson 3.12 pushright();
521 gwlarson 3.1 glReadPixels(0, 0, odev.hres, odev.vres,
522     GL_DEPTH_COMPONENT, GL_FLOAT, depthright);
523 gwlarson 3.9 if (pbuf != NULL) {
524     glClear(GL_COLOR_BUFFER_BIT);
525     gmDrawPortals(0xff, -1, -1, -1);
526 gwlarson 3.3 glReadPixels(0, 0, odev.hres, odev.vres,
527 gwlarson 3.10 GL_RED, GL_UNSIGNED_BYTE, pbuf);
528 gwlarson 3.9 }
529 gwlarson 3.3 for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; )
530 gwlarson 3.10 if (pbuf != NULL && pbuf[dbp-depthright]&0x40)
531 gwlarson 3.3 *dbp = FHUGE;
532     else
533     *dbp = mapdepth(*dbp);
534     glClear(GL_DEPTH_BUFFER_BIT);
535 gwlarson 3.1 odDepthMap(1, depthright);
536 gwlarson 3.12 popright();
537 gwlarson 3.1 #endif
538 gwlarson 3.2 /* read back depth buffer */
539 gwlarson 3.1 glReadPixels(0, 0, odev.hres, odev.vres,
540     GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer);
541 gwlarson 3.9 if (pbuf != NULL) {
542     glClear(GL_COLOR_BUFFER_BIT); /* find portals */
543     gmDrawPortals(0xff, -1, -1, -1);
544 gwlarson 3.2 glReadPixels(0, 0, odev.hres, odev.vres,
545 gwlarson 3.10 GL_RED, GL_UNSIGNED_BYTE, pbuf);
546     #ifdef DEBUG
547     glXSwapBuffers(ourdisplay, gwind);
548     #endif
549 gwlarson 3.9 }
550 gwlarson 3.1 for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; )
551 gwlarson 3.10 if (pbuf != NULL && pbuf[dbp-depthbuffer]&0x40)
552 gwlarson 3.2 *dbp = FHUGE;
553     else
554     *dbp = mapdepth(*dbp);
555 gwlarson 3.3 glClear(GL_DEPTH_BUFFER_BIT); /* clear system depth buffer */
556 gwlarson 3.1 odDepthMap(0, depthbuffer); /* transfer depth data */
557 gwlarson 3.9 if (pbuf != NULL)
558 greg 3.19 free((void *)pbuf); /* free our portal buffer */
559 gwlarson 3.1 }
560    
561    
562 gwlarson 3.2 static
563     freedepth() /* free recorded depth buffer */
564     {
565     if (depthbuffer == NULL)
566     return;
567     #ifdef STEREO
568     odDepthMap(1, NULL);
569 greg 3.19 free((void *)depthright);
570 gwlarson 3.2 depthright = NULL;
571     #endif
572     odDepthMap(0, NULL);
573 greg 3.19 free((void *)depthbuffer);
574 gwlarson 3.2 depthbuffer = NULL;
575     }
576    
577    
578 gwlarson 3.1 static double
579     getdistance(dx, dy, direc) /* distance from fore plane along view ray */
580     int dx, dy;
581     FVECT direc;
582     {
583     GLfloat gldepth;
584     double dist;
585    
586     if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres)
587     return(FHUGE);
588     if (depthbuffer != NULL)
589     dist = depthbuffer[dy*odev.hres + dx];
590     else {
591     glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT,
592     GL_FLOAT, &gldepth);
593 gwlarson 3.3 dist = mapdepth(gldepth);
594 gwlarson 3.1 }
595     if (dist >= .99*FHUGE)
596     return(FHUGE);
597     return((dist-odev.v.vfore)/DOT(direc,odev.v.vdir));
598     }
599    
600    
601     #ifdef STEREO
602     static
603     pushright() /* push on right view & buffer */
604     {
605     double d;
606    
607     setstereobuf(STEREO_BUFFER_RIGHT);
608 gwlarson 3.7 if (viewflags & VWPERSP) {
609 gwlarson 3.1 glMatrixMode(GL_MODELVIEW);
610     glPushMatrix();
611     d = -eyesepdist / sqrt(odev.v.hn2);
612     glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1],
613     d*odev.v.hvec[2]);
614     checkglerr("setting right view");
615     }
616     }
617    
618    
619     static
620     popright() /* pop off right view & buffer */
621     {
622 gwlarson 3.7 if (viewflags & VWPERSP) {
623 gwlarson 3.1 glMatrixMode(GL_MODELVIEW);
624     glPopMatrix();
625     }
626     setstereobuf(STEREO_BUFFER_LEFT);
627     }
628     #endif
629    
630    
631     static int
632     mytmflags() /* figure out tone mapping flags */
633     {
634     extern char *progname;
635     register char *cp, *tail;
636     /* find basic name */
637     for (cp = tail = progname; *cp; cp++)
638     if (*cp == '/')
639     tail = cp+1;
640     for (cp = tail; *cp && *cp != '.'; cp++)
641     ;
642     #ifdef DEBUG
643     if (cp > tail && cp[-1] == 'h')
644     return(TM_F_HUMAN);
645     else
646     return(TM_F_CAMERA);
647     #else
648     if (cp > tail && cp[-1] == 'h')
649     return(TM_F_HUMAN|TM_F_NOSTDERR);
650     else
651     return(TM_F_CAMERA|TM_F_NOSTDERR);
652     #endif
653     }
654    
655    
656     static
657     getevent() /* get next event */
658     {
659     XNextEvent(ourdisplay, levptr(XEvent));
660     switch (levptr(XEvent)->type) {
661     case ConfigureNotify:
662     resizewindow(levptr(XConfigureEvent));
663     break;
664     case UnmapNotify:
665 gwlarson 3.7 viewflags &= ~VWMAPPED;
666 gwlarson 3.1 break;
667     case MapNotify:
668 gwlarson 3.2 odRemap(0);
669 gwlarson 3.7 viewflags |= VWMAPPED;
670 gwlarson 3.1 break;
671     case Expose:
672     fixwindow(levptr(XExposeEvent));
673     break;
674     case KeyPress:
675     getkey(levptr(XKeyPressedEvent));
676     break;
677     case ButtonPress:
678     if (FRAMESTATE(levptr(XButtonPressedEvent)->state))
679     getframe(levptr(XButtonPressedEvent));
680     else
681     getmove(levptr(XButtonPressedEvent));
682     break;
683     }
684     }
685    
686    
687     static
688     draw3dline(wp) /* draw 3d line in world coordinates */
689     register FVECT wp[2];
690     {
691     glVertex3d(wp[0][0], wp[0][1], wp[0][2]);
692     glVertex3d(wp[1][0], wp[1][1], wp[1][2]);
693     }
694    
695    
696     static
697     draw_grids(fore) /* draw holodeck section grids */
698     int fore;
699     {
700     glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT);
701     glDisable(GL_LIGHTING);
702     if (fore)
703 gwlarson 3.8 glColor3ub(4, 250, 250);
704 gwlarson 3.1 else
705     glColor3ub(0, 0, 0);
706     glBegin(GL_LINES); /* draw each grid line */
707     gridlines(draw3dline);
708     glEnd();
709     checkglerr("drawing grid lines");
710     glPopAttrib();
711     }
712    
713    
714     static
715     moveview(dx, dy, mov, orb) /* move our view */
716     int dx, dy, mov, orb;
717     {
718     VIEW nv;
719     FVECT odir, v1, wip;
720     double d, d1;
721     register int li;
722     /* start with old view */
723     copystruct(&nv, &odev.v);
724     /* orient our motion */
725     if (viewray(v1, odir, &odev.v,
726     (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
727     return(0); /* outside view */
728     if (mov | orb) { /* moving relative to geometry */
729     d = getdistance(dx, dy, odir); /* distance from front plane */
730     #ifdef DOBJ
731     d1 = dobj_trace(NULL, v1, odir);
732     if (d1 < d)
733     d = d1;
734     #endif
735     if (d >= .99*FHUGE)
736     d = 0.5*(dev_zmax+dev_zmin); /* just guess */
737     VSUM(wip, v1, odir, d);
738     VSUB(odir, wip, odev.v.vp);
739     } else /* panning with constant viewpoint */
740     VCOPY(nv.vdir, odir);
741     if (orb && mov) { /* orbit left/right */
742     spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
743     VSUM(nv.vp, wip, odir, -1.);
744     spinvector(nv.vdir, nv.vdir, nv.vup, d);
745     } else if (orb) { /* orbit up/down */
746     fcross(v1, odir, nv.vup);
747     if (normalize(v1) == 0.)
748     return(0);
749     spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
750     VSUM(nv.vp, wip, odir, -1.);
751     spinvector(nv.vdir, nv.vdir, v1, d);
752     } else if (mov) { /* move forward/backward */
753     d = MOVPCT/100. * mov;
754     VSUM(nv.vp, nv.vp, odir, d);
755     }
756 gwlarson 3.7 if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */
757 gwlarson 3.1 VSUM(v1, odev.v.vp, nv.vp, -1.);
758     d = DOT(v1, nv.vup);
759     VSUM(nv.vp, nv.vp, odev.v.vup, d);
760     }
761     if (setview(&nv) != NULL)
762     return(0); /* illegal view */
763     dev_view(&nv);
764 gwlarson 3.2 inpresflags |= DFL(DC_SETVIEW);
765 gwlarson 3.1 return(1);
766     }
767    
768    
769     static
770     getframe(ebut) /* get focus frame */
771     XButtonPressedEvent *ebut;
772     {
773     int startx = ebut->x, starty = ebut->y;
774     int endx, endy;
775    
776     XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent));
777     endx = levptr(XButtonReleasedEvent)->x;
778     endy = levptr(XButtonReleasedEvent)->y;
779     if (endx == startx | endy == starty) {
780     XBell(ourdisplay, 0);
781     return;
782     }
783     if (endx < startx) {register int c = endx; endx = startx; startx = c;}
784     if (endy < starty) {register int c = endy; endy = starty; starty = c;}
785     sprintf(odev_args, "%.3f %.3f %.3f %.3f",
786     (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres,
787     (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres);
788     inpresflags |= DFL(DC_FOCUS);
789     }
790    
791    
792     static
793 greg 3.19 waitabit() /* pause a moment */
794     {
795     struct timespec ts;
796     ts.tv_sec = 0;
797     ts.tv_nsec = 5000000;
798     nanosleep(&ts, NULL);
799     }
800    
801    
802     static
803 gwlarson 3.1 getmove(ebut) /* get view change */
804     XButtonPressedEvent *ebut;
805     {
806     int movdir = MOVDIR(ebut->button);
807     int movorb = MOVORB(ebut->state);
808 gwlarson 3.2 int ndrawn;
809 gwlarson 3.1 Window rootw, childw;
810     int rootx, rooty, wx, wy;
811     unsigned int statemask;
812    
813     XNoOp(ourdisplay); /* makes sure we're not idle */
814    
815 gwlarson 3.7 viewflags &= ~VWSTEADY; /* flag moving view */
816 gwlarson 3.11 setglpersp(); /* start us off in perspective */
817 gwlarson 3.1 while (!XCheckMaskEvent(ourdisplay,
818     ButtonReleaseMask, levptr(XEvent))) {
819 greg 3.19 /* pause so as not to move too fast */
820     waitabit();
821 gwlarson 3.1 /* get cursor position */
822     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
823     &rootx, &rooty, &wx, &wy, &statemask))
824     break; /* on another screen */
825     /* compute view motion */
826     if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
827     sleep(1);
828     continue; /* cursor in bad place */
829     }
830     draw_grids(1); /* redraw grid */
831     #ifdef STEREO
832     pushright();
833     draw_grids(1);
834 gwlarson 3.3 ndrawn = gmDrawGeom();
835 gwlarson 3.1 #ifdef DOBJ
836 gwlarson 3.3 ndrawn += dobj_render();
837 gwlarson 3.1 #endif
838     popright();
839     #endif
840     /* redraw octrees */
841 gwlarson 3.3 ndrawn = gmDrawGeom();
842 gwlarson 3.1 #ifdef DOBJ
843 gwlarson 3.2 ndrawn += dobj_render(); /* redraw objects */
844 gwlarson 3.1 #endif
845 gwlarson 3.6 glXSwapBuffers(ourdisplay, gwind);
846     if (!ndrawn)
847 gwlarson 3.2 sleep(1); /* for reasonable interaction */
848 gwlarson 3.1 }
849     if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
850     movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
851     wx = levptr(XButtonReleasedEvent)->x;
852     wy = levptr(XButtonReleasedEvent)->y;
853     moveview(wx, odev.vres-1-wy, movdir, movorb);
854     }
855 gwlarson 3.7 viewflags |= VWSTEADY; /* done goofing around */
856 gwlarson 3.1 }
857    
858    
859     static
860 gwlarson 3.11 setglpersp() /* set perspective view in GL */
861 gwlarson 3.1 {
862     double d, xmin, xmax, ymin, ymax;
863     GLfloat vec[4];
864     double depthlim[2];
865     /* set depth limits */
866 gwlarson 3.2 gmDepthLimit(depthlim, odev.v.vp, odev.v.vdir);
867 gwlarson 3.1 if (depthlim[0] >= depthlim[1]) {
868     dev_zmin = 1.;
869     dev_zmax = 100.;
870     } else {
871     dev_zmin = 0.5*depthlim[0];
872 gwlarson 3.13 dev_zmax = 1.25*depthlim[1];
873 gwlarson 3.1 if (dev_zmin > dev_zmax/5.)
874     dev_zmin = dev_zmax/5.;
875     }
876     if (odev.v.vfore > FTINY)
877     dev_zmin = odev.v.vfore;
878     if (odev.v.vaft > FTINY)
879     dev_zmax = odev.v.vaft;
880 gwlarson 3.18 if (dev_zmin*500. < dev_zmax)
881 greg 3.19 dev_zmin = dev_zmax/500.;
882 gwlarson 3.1 setzrat();
883     xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
884     xmin = -xmax;
885     d = odev.v.hoff * (xmax - xmin);
886     xmin += d; xmax += d;
887     ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
888     ymin = -ymax;
889     d = odev.v.voff * (ymax - ymin);
890     ymin += d; ymax += d;
891     /* set view matrix */
892     glMatrixMode(GL_PROJECTION);
893     glLoadIdentity();
894     glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
895     gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
896     odev.v.vp[0] + odev.v.vdir[0],
897     odev.v.vp[1] + odev.v.vdir[1],
898     odev.v.vp[2] + odev.v.vdir[2],
899     odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
900     checkglerr("setting perspective view");
901     vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.;
902     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec);
903     vec[0] = -odev.v.vdir[0];
904     vec[1] = -odev.v.vdir[1];
905     vec[2] = -odev.v.vdir[2];
906     vec[3] = 0.;
907     glLightfv(GL_LIGHT0, GL_POSITION, vec);
908     vec[0] = vec[1] = vec[2] = .7; vec[3] = 1.;
909     glLightfv(GL_LIGHT0, GL_SPECULAR, vec);
910     glLightfv(GL_LIGHT0, GL_DIFFUSE, vec);
911     vec[0] = vec[1] = vec[2] = .3; vec[3] = 1.;
912     glLightfv(GL_LIGHT0, GL_AMBIENT, vec);
913     glEnable(GL_LIGHT0);
914     glEnable(GL_LIGHTING); /* light our GL objects */
915     glShadeModel(GL_SMOOTH);
916 gwlarson 3.7 viewflags &= ~VWORTHO;
917     viewflags |= VWPERSP;
918 gwlarson 3.1 }
919    
920    
921     static
922     setglortho() /* set up orthographic view for cone drawing */
923     {
924 gwlarson 3.6 glDrawBuffer(GL_FRONT); /* use single-buffer mode */
925 gwlarson 3.1 /* set view matrix */
926     glMatrixMode(GL_PROJECTION);
927     glLoadIdentity();
928     glOrtho(0., (double)odev.hres, 0., (double)odev.vres,
929     0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH));
930     checkglerr("setting orthographic view");
931     glDisable(GL_LIGHTING); /* cones are constant color */
932     glShadeModel(GL_FLAT);
933 gwlarson 3.7 viewflags &= ~VWPERSP;
934     viewflags |= VWORTHO;
935 gwlarson 3.1 }
936    
937    
938     static
939     wipeclean() /* prepare for redraw */
940     {
941 gwlarson 3.6 glDrawBuffer(GL_BACK); /* use double-buffer mode */
942 gwlarson 3.9 glReadBuffer(GL_BACK);
943 gwlarson 3.6 /* clear buffers */
944 gwlarson 3.1 #ifdef STEREO
945     setstereobuf(STEREO_BUFFER_RIGHT);
946 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
947 gwlarson 3.1 setstereobuf(STEREO_BUFFER_LEFT);
948     #endif
949 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
950     freedepth();
951 gwlarson 3.7 if ((viewflags&(VWCHANGE|VWSTEADY)) ==
952     (VWCHANGE|VWSTEADY)) { /* clear samples if new */
953 gwlarson 3.1 odClean();
954 gwlarson 3.7 viewflags &= ~VWCHANGE; /* change noted */
955 gwlarson 3.10 } else if (viewflags & VWSTEADY)
956     odRedrawAll();
957 gwlarson 3.17 setglpersp(); /* reset view & clipping planes */
958 gwlarson 3.1 }
959    
960    
961     static
962     getkey(ekey) /* get input key */
963     register XKeyPressedEvent *ekey;
964     {
965     Window rootw, childw;
966     int rootx, rooty, wx, wy;
967     unsigned int statemask;
968     int n;
969     char buf[8];
970    
971     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
972     if (n != 1)
973     return;
974     switch (buf[0]) {
975     case 'h': /* turn on height motion lock */
976 gwlarson 3.7 viewflags |= VWHEADLOCK;
977 gwlarson 3.1 return;
978     case 'H': /* turn off height motion lock */
979 gwlarson 3.7 viewflags &= ~VWHEADLOCK;
980 gwlarson 3.1 return;
981     case 'l': /* retrieve last view */
982     inpresflags |= DFL(DC_LASTVIEW);
983     return;
984     case 'p': /* pause computation */
985     inpresflags |= DFL(DC_PAUSE);
986     return;
987     case 'v': /* spit out view */
988     inpresflags |= DFL(DC_GETVIEW);
989     return;
990     case 'f': /* frame view position */
991     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
992     &rootx, &rooty, &wx, &wy, &statemask))
993     return; /* on another screen */
994     sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres,
995     1.-(wy+.5)/odev.vres);
996     inpresflags |= DFL(DC_FOCUS);
997     return;
998     case 'F': /* unfocus */
999     odev_args[0] = '\0';
1000     inpresflags |= DFL(DC_FOCUS);
1001     return;
1002     case '\n':
1003     case '\r': /* resume computation */
1004     inpresflags |= DFL(DC_RESUME);
1005     return;
1006     case CTRL('R'): /* redraw screen */
1007 gwlarson 3.10 odRemap(0); /* new tone mapping */
1008 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT);
1009 gwlarson 3.1 #ifdef STEREO
1010     setstereobuf(STEREO_BUFFER_RIGHT);
1011 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT);
1012 gwlarson 3.1 setstereobuf(STEREO_BUFFER_LEFT);
1013     #endif
1014     return;
1015     case CTRL('L'): /* refresh from server */
1016     if (inpresflags & DFL(DC_REDRAW))
1017 gwlarson 3.12 return; /* already called */
1018 gwlarson 3.1 XRaiseWindow(ourdisplay, gwind);
1019 gwlarson 3.11 XFlush(ourdisplay); /* raise up window */
1020 gwlarson 3.10 sleep(1); /* wait for restacking */
1021 gwlarson 3.11 dev_clear(); /* clear buffer and samples */
1022 gwlarson 3.10 odRemap(1); /* start fresh histogram */
1023 gwlarson 3.1 inpresflags |= DFL(DC_REDRAW); /* resend values from server */
1024     return;
1025     case 'K': /* kill rtrace process(es) */
1026     inpresflags |= DFL(DC_KILL);
1027     break;
1028     case 'R': /* restart rtrace */
1029     inpresflags |= DFL(DC_RESTART);
1030     break;
1031     case 'C': /* clobber holodeck */
1032     inpresflags |= DFL(DC_CLOBBER);
1033     break;
1034     case 'q': /* quit the program */
1035     inpresflags |= DFL(DC_QUIT);
1036     return;
1037     default:
1038     XBell(ourdisplay, 0);
1039     return;
1040     }
1041     }
1042    
1043    
1044     static
1045     fixwindow(eexp) /* repair damage to window */
1046     register XExposeEvent *eexp;
1047     {
1048     int xmin, ymin, xmax, ymax;
1049    
1050     if (odev.hres == 0 | odev.vres == 0) { /* first exposure */
1051     resizewindow((XConfigureEvent *)eexp);
1052     return;
1053     }
1054     xmin = eexp->x; xmax = eexp->x + eexp->width;
1055     ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
1056 gwlarson 3.10
1057     if (xmin <= 0 && xmax >= odev.hres-1 &&
1058     ymin <= 0 && ymax >= odev.vres) {
1059     DCHECK(eexp->count, WARNING, "multiple clear in fixwindow");
1060 gwlarson 3.11 wipeclean(); /* make sure we're go */
1061 gwlarson 3.10 return;
1062     }
1063 gwlarson 3.1 /* clear portion of depth */
1064 gwlarson 3.2 glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1065 gwlarson 3.1 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1066     glDepthFunc(GL_ALWAYS);
1067     glBegin(GL_POLYGON);
1068     glVertex3i(xmin, ymin, OMAXDEPTH);
1069     glVertex3i(xmax, ymin, OMAXDEPTH);
1070     glVertex3i(xmax, ymax, OMAXDEPTH);
1071     glVertex3i(xmin, ymax, OMAXDEPTH);
1072     glEnd();
1073     #ifdef STEREO
1074     setstereobuf(STEREO_BUFFER_RIGHT);
1075     glBegin(GL_POLYGON);
1076     glVertex3i(xmin, ymin, OMAXDEPTH);
1077     glVertex3i(xmax, ymin, OMAXDEPTH);
1078     glVertex3i(xmax, ymax, OMAXDEPTH);
1079     glVertex3i(xmin, ymax, OMAXDEPTH);
1080     glEnd();
1081     odRedraw(1, xmin, ymin, xmax, ymax);
1082     setstereobuf(STEREO_BUFFER_LEFT);
1083     #endif
1084 gwlarson 3.2 glPopAttrib();
1085 gwlarson 3.1 odRedraw(0, xmin, ymin, xmax, ymax);
1086     }
1087    
1088    
1089     static
1090     resizewindow(ersz) /* resize window */
1091     register XConfigureEvent *ersz;
1092     {
1093     glViewport(0, 0, ersz->width, ersz->height);
1094    
1095     if (ersz->width == odev.hres && ersz->height == odev.vres)
1096     return;
1097    
1098     odev.hres = ersz->width;
1099     odev.vres = ersz->height;
1100    
1101     odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
1102     odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
1103    
1104 gwlarson 3.2 inpresflags |= DFL(DC_SETVIEW);
1105 gwlarson 3.7 viewflags |= VWCHANGE;
1106 gwlarson 3.1 }