ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.3
Committed: Mon Dec 21 11:42:38 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.2: +62 -29 lines
Log Message:
got portals working with or without alpha buffer

File Contents

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