ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.34
Committed: Fri Feb 12 00:53:56 2021 UTC (3 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 3.33: +3 -7 lines
Log Message:
refactor: Created comparison macros for RREAL and FVECT types

File Contents

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