ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.31
Committed: Thu Sep 6 00:07:43 2012 UTC (11 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R2P1
Changes since 3.30: +4 -5 lines
Log Message:
Created geodesic() function for vector rotation along great circles

File Contents

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