ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.30
Committed: Wed Jun 7 17:52:04 2006 UTC (17 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R1, rad4R0, rad3R8, rad3R9
Changes since 3.29: +9 -3 lines
Log Message:
Eliminated some compiler warnings.

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 schorsch 3.30 static const char RCSid[] = "$Id: rhd_ogl.c,v 3.29 2006/05/23 13:01:07 greg 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     fcross(v1, odir, nv.vup);
800     if (normalize(v1) == 0.)
801     return(0);
802     spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
803     VSUM(nv.vp, wip, odir, -1.);
804     spinvector(nv.vdir, nv.vdir, v1, d);
805     } else if (mov) { /* move forward/backward */
806     d = MOVPCT/100. * mov;
807     VSUM(nv.vp, nv.vp, odir, d);
808     }
809 gwlarson 3.7 if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */
810 gwlarson 3.1 VSUM(v1, odev.v.vp, nv.vp, -1.);
811     d = DOT(v1, nv.vup);
812     VSUM(nv.vp, nv.vp, odev.v.vup, d);
813     }
814     if (setview(&nv) != NULL)
815     return(0); /* illegal view */
816     dev_view(&nv);
817 gwlarson 3.2 inpresflags |= DFL(DC_SETVIEW);
818 gwlarson 3.1 return(1);
819     }
820    
821    
822 schorsch 3.24 static void
823     getframe( /* get focus frame */
824     XButtonPressedEvent *ebut
825     )
826 gwlarson 3.1 {
827     int startx = ebut->x, starty = ebut->y;
828 greg 3.27 int endx, endy, midx, midy;
829     FVECT odir, v1;
830 schorsch 3.30 double d;
831     #ifdef DOBJ
832     double d1;
833     #endif
834 greg 3.27 /* get mouse drag */
835 gwlarson 3.1 XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent));
836     endx = levptr(XButtonReleasedEvent)->x;
837     endy = levptr(XButtonReleasedEvent)->y;
838 greg 3.27 midx = (startx + endx) >> 1;
839     midy = (starty + endy) >> 1;
840     /* set focus distance */
841     if (viewray(v1, odir, &odev.v,
842     (midx+.5)/odev.hres, (midy+.5)/odev.vres) < -FTINY)
843     return;
844     d = getdistance(midx, midy, odir); /* distance from front plane */
845     #ifdef DOBJ
846     d1 = dobj_trace(NULL, v1, odir);
847     if (d1 < d)
848     d = d1;
849     #endif
850     if (d < .99*FHUGE)
851     odev.v.vdist = d + sqrt(dist2(v1, odev.v.vp));
852     /* set frame for rendering */
853     if ((endx == startx) | (endy == starty))
854 gwlarson 3.1 return;
855     if (endx < startx) {register int c = endx; endx = startx; startx = c;}
856     if (endy < starty) {register int c = endy; endy = starty; starty = c;}
857     sprintf(odev_args, "%.3f %.3f %.3f %.3f",
858     (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres,
859     (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres);
860     inpresflags |= DFL(DC_FOCUS);
861     }
862    
863    
864 schorsch 3.24 static void
865     waitabit(void) /* pause a moment */
866 greg 3.19 {
867     struct timespec ts;
868     ts.tv_sec = 0;
869 greg 3.23 ts.tv_nsec = 100000000L;
870 greg 3.19 nanosleep(&ts, NULL);
871     }
872    
873    
874 schorsch 3.24 static void
875     getmove( /* get view change */
876     XButtonPressedEvent *ebut
877     )
878 gwlarson 3.1 {
879     int movdir = MOVDIR(ebut->button);
880     int movorb = MOVORB(ebut->state);
881 gwlarson 3.2 int ndrawn;
882 gwlarson 3.1 Window rootw, childw;
883     int rootx, rooty, wx, wy;
884     unsigned int statemask;
885    
886     XNoOp(ourdisplay); /* makes sure we're not idle */
887    
888 gwlarson 3.7 viewflags &= ~VWSTEADY; /* flag moving view */
889 gwlarson 3.11 setglpersp(); /* start us off in perspective */
890 gwlarson 3.1 while (!XCheckMaskEvent(ourdisplay,
891     ButtonReleaseMask, levptr(XEvent))) {
892 greg 3.19 /* pause so as not to move too fast */
893     waitabit();
894 gwlarson 3.1 /* get cursor position */
895     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
896     &rootx, &rooty, &wx, &wy, &statemask))
897     break; /* on another screen */
898     /* compute view motion */
899     if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
900     sleep(1);
901     continue; /* cursor in bad place */
902     }
903     draw_grids(1); /* redraw grid */
904     #ifdef STEREO
905     pushright();
906     draw_grids(1);
907 gwlarson 3.3 ndrawn = gmDrawGeom();
908 gwlarson 3.1 #ifdef DOBJ
909 gwlarson 3.3 ndrawn += dobj_render();
910 gwlarson 3.1 #endif
911     popright();
912     #endif
913     /* redraw octrees */
914 gwlarson 3.3 ndrawn = gmDrawGeom();
915 gwlarson 3.1 #ifdef DOBJ
916 gwlarson 3.2 ndrawn += dobj_render(); /* redraw objects */
917 gwlarson 3.1 #endif
918 gwlarson 3.6 glXSwapBuffers(ourdisplay, gwind);
919     if (!ndrawn)
920 gwlarson 3.2 sleep(1); /* for reasonable interaction */
921 gwlarson 3.1 }
922     if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
923     movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
924     wx = levptr(XButtonReleasedEvent)->x;
925     wy = levptr(XButtonReleasedEvent)->y;
926     moveview(wx, odev.vres-1-wy, movdir, movorb);
927     }
928 gwlarson 3.7 viewflags |= VWSTEADY; /* done goofing around */
929 gwlarson 3.1 }
930    
931    
932 schorsch 3.24 static void
933     setglpersp(void) /* set perspective view in GL */
934 gwlarson 3.1 {
935     double d, xmin, xmax, ymin, ymax;
936     GLfloat vec[4];
937     double depthlim[2];
938     /* set depth limits */
939 gwlarson 3.2 gmDepthLimit(depthlim, odev.v.vp, odev.v.vdir);
940 gwlarson 3.1 if (depthlim[0] >= depthlim[1]) {
941     dev_zmin = 1.;
942     dev_zmax = 100.;
943     } else {
944     dev_zmin = 0.5*depthlim[0];
945 gwlarson 3.13 dev_zmax = 1.25*depthlim[1];
946 gwlarson 3.1 if (dev_zmin > dev_zmax/5.)
947     dev_zmin = dev_zmax/5.;
948     }
949     if (odev.v.vfore > FTINY)
950     dev_zmin = odev.v.vfore;
951     if (odev.v.vaft > FTINY)
952     dev_zmax = odev.v.vaft;
953 gwlarson 3.18 if (dev_zmin*500. < dev_zmax)
954 greg 3.19 dev_zmin = dev_zmax/500.;
955 gwlarson 3.1 setzrat();
956     xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
957     xmin = -xmax;
958     d = odev.v.hoff * (xmax - xmin);
959     xmin += d; xmax += d;
960     ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
961     ymin = -ymax;
962     d = odev.v.voff * (ymax - ymin);
963     ymin += d; ymax += d;
964     /* set view matrix */
965     glMatrixMode(GL_PROJECTION);
966     glLoadIdentity();
967     glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
968     gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
969     odev.v.vp[0] + odev.v.vdir[0],
970     odev.v.vp[1] + odev.v.vdir[1],
971     odev.v.vp[2] + odev.v.vdir[2],
972     odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
973     checkglerr("setting perspective view");
974     vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.;
975     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec);
976     vec[0] = -odev.v.vdir[0];
977     vec[1] = -odev.v.vdir[1];
978     vec[2] = -odev.v.vdir[2];
979     vec[3] = 0.;
980     glLightfv(GL_LIGHT0, GL_POSITION, vec);
981     vec[0] = vec[1] = vec[2] = .7; vec[3] = 1.;
982     glLightfv(GL_LIGHT0, GL_SPECULAR, vec);
983     glLightfv(GL_LIGHT0, GL_DIFFUSE, vec);
984     vec[0] = vec[1] = vec[2] = .3; vec[3] = 1.;
985     glLightfv(GL_LIGHT0, GL_AMBIENT, vec);
986     glEnable(GL_LIGHT0);
987     glEnable(GL_LIGHTING); /* light our GL objects */
988     glShadeModel(GL_SMOOTH);
989 gwlarson 3.7 viewflags &= ~VWORTHO;
990     viewflags |= VWPERSP;
991 gwlarson 3.1 }
992    
993    
994 schorsch 3.24 static void
995     setglortho(void) /* set up orthographic view for cone drawing */
996 gwlarson 3.1 {
997 gwlarson 3.6 glDrawBuffer(GL_FRONT); /* use single-buffer mode */
998 gwlarson 3.1 /* set view matrix */
999     glMatrixMode(GL_PROJECTION);
1000     glLoadIdentity();
1001     glOrtho(0., (double)odev.hres, 0., (double)odev.vres,
1002     0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH));
1003     checkglerr("setting orthographic view");
1004     glDisable(GL_LIGHTING); /* cones are constant color */
1005     glShadeModel(GL_FLAT);
1006 gwlarson 3.7 viewflags &= ~VWPERSP;
1007     viewflags |= VWORTHO;
1008 gwlarson 3.1 }
1009    
1010    
1011 schorsch 3.24 static void
1012     wipeclean(void) /* prepare for redraw */
1013 gwlarson 3.1 {
1014 gwlarson 3.6 glDrawBuffer(GL_BACK); /* use double-buffer mode */
1015 gwlarson 3.9 glReadBuffer(GL_BACK);
1016 gwlarson 3.6 /* clear buffers */
1017 gwlarson 3.1 #ifdef STEREO
1018     setstereobuf(STEREO_BUFFER_RIGHT);
1019 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1020 gwlarson 3.1 setstereobuf(STEREO_BUFFER_LEFT);
1021     #endif
1022 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1023     freedepth();
1024 gwlarson 3.7 if ((viewflags&(VWCHANGE|VWSTEADY)) ==
1025     (VWCHANGE|VWSTEADY)) { /* clear samples if new */
1026 gwlarson 3.1 odClean();
1027 gwlarson 3.7 viewflags &= ~VWCHANGE; /* change noted */
1028 gwlarson 3.10 } else if (viewflags & VWSTEADY)
1029     odRedrawAll();
1030 gwlarson 3.17 setglpersp(); /* reset view & clipping planes */
1031 gwlarson 3.1 }
1032    
1033    
1034 schorsch 3.24 static void
1035     getkey( /* get input key */
1036     register XKeyPressedEvent *ekey
1037     )
1038 gwlarson 3.1 {
1039     Window rootw, childw;
1040     int rootx, rooty, wx, wy;
1041     unsigned int statemask;
1042     int n;
1043     char buf[8];
1044    
1045     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
1046     if (n != 1)
1047     return;
1048     switch (buf[0]) {
1049     case 'h': /* turn on height motion lock */
1050 gwlarson 3.7 viewflags |= VWHEADLOCK;
1051 gwlarson 3.1 return;
1052     case 'H': /* turn off height motion lock */
1053 gwlarson 3.7 viewflags &= ~VWHEADLOCK;
1054 gwlarson 3.1 return;
1055     case 'l': /* retrieve last view */
1056     inpresflags |= DFL(DC_LASTVIEW);
1057     return;
1058     case 'p': /* pause computation */
1059     inpresflags |= DFL(DC_PAUSE);
1060     return;
1061     case 'v': /* spit out view */
1062     inpresflags |= DFL(DC_GETVIEW);
1063     return;
1064     case 'f': /* frame view position */
1065     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
1066     &rootx, &rooty, &wx, &wy, &statemask))
1067     return; /* on another screen */
1068     sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres,
1069     1.-(wy+.5)/odev.vres);
1070     inpresflags |= DFL(DC_FOCUS);
1071     return;
1072     case 'F': /* unfocus */
1073     odev_args[0] = '\0';
1074     inpresflags |= DFL(DC_FOCUS);
1075     return;
1076     case '\n':
1077     case '\r': /* resume computation */
1078     inpresflags |= DFL(DC_RESUME);
1079     return;
1080     case CTRL('R'): /* redraw screen */
1081 gwlarson 3.10 odRemap(0); /* new tone mapping */
1082 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT);
1083 gwlarson 3.1 #ifdef STEREO
1084     setstereobuf(STEREO_BUFFER_RIGHT);
1085 gwlarson 3.6 glClear(GL_DEPTH_BUFFER_BIT);
1086 gwlarson 3.1 setstereobuf(STEREO_BUFFER_LEFT);
1087     #endif
1088     return;
1089     case CTRL('L'): /* refresh from server */
1090     if (inpresflags & DFL(DC_REDRAW))
1091 gwlarson 3.12 return; /* already called */
1092 gwlarson 3.1 XRaiseWindow(ourdisplay, gwind);
1093 gwlarson 3.11 XFlush(ourdisplay); /* raise up window */
1094 gwlarson 3.10 sleep(1); /* wait for restacking */
1095 gwlarson 3.11 dev_clear(); /* clear buffer and samples */
1096 gwlarson 3.10 odRemap(1); /* start fresh histogram */
1097 gwlarson 3.1 inpresflags |= DFL(DC_REDRAW); /* resend values from server */
1098     return;
1099     case 'K': /* kill rtrace process(es) */
1100     inpresflags |= DFL(DC_KILL);
1101     break;
1102     case 'R': /* restart rtrace */
1103     inpresflags |= DFL(DC_RESTART);
1104     break;
1105     case 'C': /* clobber holodeck */
1106     inpresflags |= DFL(DC_CLOBBER);
1107     break;
1108     case 'q': /* quit the program */
1109     inpresflags |= DFL(DC_QUIT);
1110     return;
1111     default:
1112     XBell(ourdisplay, 0);
1113     return;
1114     }
1115     }
1116    
1117    
1118 schorsch 3.24 static void
1119     fixwindow( /* repair damage to window */
1120     register XExposeEvent *eexp
1121     )
1122 gwlarson 3.1 {
1123     int xmin, ymin, xmax, ymax;
1124    
1125 schorsch 3.24 if ((odev.hres == 0) | (odev.vres == 0)) { /* first exposure */
1126 gwlarson 3.1 resizewindow((XConfigureEvent *)eexp);
1127     return;
1128     }
1129     xmin = eexp->x; xmax = eexp->x + eexp->width;
1130     ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
1131 gwlarson 3.10
1132     if (xmin <= 0 && xmax >= odev.hres-1 &&
1133     ymin <= 0 && ymax >= odev.vres) {
1134     DCHECK(eexp->count, WARNING, "multiple clear in fixwindow");
1135 gwlarson 3.11 wipeclean(); /* make sure we're go */
1136 gwlarson 3.10 return;
1137     }
1138 gwlarson 3.1 /* clear portion of depth */
1139 gwlarson 3.2 glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1140 gwlarson 3.1 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1141     glDepthFunc(GL_ALWAYS);
1142     glBegin(GL_POLYGON);
1143     glVertex3i(xmin, ymin, OMAXDEPTH);
1144     glVertex3i(xmax, ymin, OMAXDEPTH);
1145     glVertex3i(xmax, ymax, OMAXDEPTH);
1146     glVertex3i(xmin, ymax, OMAXDEPTH);
1147     glEnd();
1148     #ifdef STEREO
1149     setstereobuf(STEREO_BUFFER_RIGHT);
1150     glBegin(GL_POLYGON);
1151     glVertex3i(xmin, ymin, OMAXDEPTH);
1152     glVertex3i(xmax, ymin, OMAXDEPTH);
1153     glVertex3i(xmax, ymax, OMAXDEPTH);
1154     glVertex3i(xmin, ymax, OMAXDEPTH);
1155     glEnd();
1156     odRedraw(1, xmin, ymin, xmax, ymax);
1157     setstereobuf(STEREO_BUFFER_LEFT);
1158     #endif
1159 gwlarson 3.2 glPopAttrib();
1160 gwlarson 3.1 odRedraw(0, xmin, ymin, xmax, ymax);
1161     }
1162    
1163    
1164 schorsch 3.24 static void
1165     resizewindow( /* resize window */
1166     register XConfigureEvent *ersz
1167     )
1168 gwlarson 3.1 {
1169     glViewport(0, 0, ersz->width, ersz->height);
1170    
1171     if (ersz->width == odev.hres && ersz->height == odev.vres)
1172     return;
1173    
1174     odev.hres = ersz->width;
1175     odev.vres = ersz->height;
1176    
1177     odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
1178     odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
1179    
1180 gwlarson 3.2 inpresflags |= DFL(DC_SETVIEW);
1181 gwlarson 3.7 viewflags |= VWCHANGE;
1182 gwlarson 3.1 }