ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.4
Committed: Mon Dec 21 15:10:13 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.3: +5 -9 lines
Log Message:
fixed problem with initial drawing

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