ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.11
Committed: Wed Dec 30 17:13:20 1998 UTC (25 years, 3 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.10: +9 -17 lines
Log Message:
fixed redraw when window is remapped
increased number of samples allocated
turned off incremental updates by default (RAYQLEN)

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