1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rhd_ogl.c,v 3.33 2018/10/05 19:19:16 greg Exp $"; |
3 |
#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 |
#include <time.h> |
22 |
#include <GL/glx.h> |
23 |
#include <GL/glu.h> |
24 |
#ifdef STEREO |
25 |
#include <X11/extensions/SGIStereo.h> |
26 |
#endif |
27 |
|
28 |
#include "standard.h" |
29 |
#include "rhd_odraw.h" |
30 |
#include "rhdisp.h" |
31 |
#include "paths.h" |
32 |
#ifdef DOBJ |
33 |
#include "rhdobj.h" |
34 |
#endif |
35 |
|
36 |
#include "x11icon.h" |
37 |
|
38 |
#ifndef RAYQLEN |
39 |
#define RAYQLEN 0 /* max. rays to queue before flush */ |
40 |
#endif |
41 |
#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 |
|
52 |
#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 |
#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 *)¤tevent) |
78 |
|
79 |
struct driver odev; /* global device driver structure */ |
80 |
|
81 |
TMstruct *tmGlobal; /* global tone-mapping structure */ |
82 |
|
83 |
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 |
static int viewflags; /* what's happening with view */ |
114 |
|
115 |
/* |
116 |
static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), |
117 |
xferdepth(), freedepth(), setglortho(), |
118 |
setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); |
119 |
|
120 |
static double getdistance(); |
121 |
*/ |
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 |
static void draw3dline(FVECT wp[2]); |
129 |
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 |
static void getkey(XKeyPressedEvent *ekey); |
134 |
static void fixwindow(XExposeEvent *eexp); |
135 |
static void resizewindow(XConfigureEvent *ersz); |
136 |
static void waitabit(void); |
137 |
static void setglpersp(void); |
138 |
static void setglortho(void); |
139 |
static void wipeclean(void); |
140 |
|
141 |
|
142 |
#ifdef STEREO |
143 |
static void pushright(void); |
144 |
static void popright(void); |
145 |
#endif |
146 |
|
147 |
extern int gmPortals; /* GL portal list id */ |
148 |
|
149 |
extern time_t time(); |
150 |
|
151 |
|
152 |
void |
153 |
dev_open(id) /* initialize GLX driver */ |
154 |
char *id; |
155 |
{ |
156 |
extern char *getenv(); |
157 |
static RGBPRIMS myprims = STDPRIMS; |
158 |
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 |
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 |
CHECK(ourdisplay==NULL, USER, |
177 |
"cannot open X-windows; DISPLAY variable set?"); |
178 |
#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 |
"wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first"); |
186 |
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 |
if (ourvinf == NULL) |
195 |
ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK); |
196 |
CHECK(ourvinf==NULL, USER, "no suitable visuals available"); |
197 |
/* 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 |
tmGlobal = tmInit(mytmflags(), dpri, gamval); |
211 |
if (tmGlobal == NULL) |
212 |
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 |
CHECK(gwind==0, SYSTEM, "cannot create window"); |
230 |
XStoreName(ourdisplay, gwind, id); |
231 |
/* set window manager hints */ |
232 |
ourxwmhints.flags = InputHint|IconPixmapHint; |
233 |
ourxwmhints.input = True; |
234 |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, gwind, |
235 |
(char *)x11icon_bits, x11icon_width, x11icon_height); |
236 |
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 |
glClearColor(0, 0, 0, 0); |
254 |
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 |
glPixelStorei(GL_PACK_ALIGNMENT, 1); |
259 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
260 |
/* 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 |
odev.v = stdview; |
271 |
odev.v.type = VT_PER; |
272 |
viewflags = VWSTEADY; /* view starts static */ |
273 |
/* map the window */ |
274 |
XMapWindow(ourdisplay, gwind); |
275 |
dev_input(); /* sets size and view angles */ |
276 |
if (!odInit(DisplayWidth(ourdisplay,ourscreen) * |
277 |
DisplayHeight(ourdisplay,ourscreen) / 3)) |
278 |
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 |
void |
286 |
dev_close(void) /* close our display and free resources */ |
287 |
{ |
288 |
#ifdef DOBJ |
289 |
dobj_cleanup(); |
290 |
#endif |
291 |
freedepth(); |
292 |
gmEndGeom(); |
293 |
gmEndPortal(); |
294 |
odDone(); |
295 |
glXMakeCurrent(ourdisplay, None, NULL); |
296 |
glXDestroyContext(ourdisplay, gctx); |
297 |
XDestroyWindow(ourdisplay, gwind); |
298 |
gwind = 0; |
299 |
XCloseDisplay(ourdisplay); |
300 |
ourdisplay = NULL; |
301 |
tmDone(tmGlobal); |
302 |
odev.v.type = 0; |
303 |
odev.hres = odev.vres = 0; |
304 |
odev.ifd = -1; |
305 |
} |
306 |
|
307 |
|
308 |
void |
309 |
dev_clear(void) /* clear our representation */ |
310 |
{ |
311 |
viewflags |= VWCHANGE; /* pretend our view has changed */ |
312 |
wipeclean(); /* clean off display and samples */ |
313 |
dev_flush(); /* redraw geometry & get depth */ |
314 |
rayqleft = 0; /* hold off update */ |
315 |
} |
316 |
|
317 |
|
318 |
int |
319 |
dev_view( /* assign new driver view */ |
320 |
VIEW *nv |
321 |
) |
322 |
{ |
323 |
#ifdef STEREO |
324 |
double d; |
325 |
#endif |
326 |
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 |
if (!FABSEQ(nv->horiz,odev.v.horiz) || |
337 |
!FABSEQ(nv->vert,odev.v.vert)) { |
338 |
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 |
odev.v = *nv; /* setview() already called */ |
362 |
viewflags |= VWCHANGE; |
363 |
} |
364 |
#ifdef STEREO |
365 |
vwright = *nv; |
366 |
d = eyesepdist / sqrt(nv->hn2); |
367 |
VSUM(vwright.vp, nv->vp, nv->hvec, d); |
368 |
/* setview(&vwright); -- Unnecessary */ |
369 |
#endif |
370 |
wipeclean(); |
371 |
return(1); |
372 |
} |
373 |
|
374 |
|
375 |
void |
376 |
dev_section( /* add octree for geometry rendering */ |
377 |
char *gfn, |
378 |
char *pfn |
379 |
) |
380 |
{ |
381 |
if (gfn == NULL) { |
382 |
gmEndGeom(); |
383 |
gmEndPortal(); |
384 |
wipeclean(); /* new geometry, so redraw it */ |
385 |
return; |
386 |
} |
387 |
if (access(gfn, R_OK) == 0) |
388 |
gmNewGeom(gfn); |
389 |
#ifdef DEBUG |
390 |
else { |
391 |
sprintf(errmsg, "cannot load octree \"%s\"", gfn); |
392 |
error(WARNING, errmsg); |
393 |
} |
394 |
#endif |
395 |
if (pfn != NULL) |
396 |
gmNewPortal(pfn); |
397 |
} |
398 |
|
399 |
|
400 |
void |
401 |
dev_auxcom( /* process an auxiliary command */ |
402 |
char *cmd, |
403 |
char *args |
404 |
) |
405 |
{ |
406 |
#ifdef DOBJ |
407 |
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 |
return; |
415 |
} |
416 |
#endif |
417 |
sprintf(errmsg, "%s: unknown command", cmd); |
418 |
error(COMMAND, errmsg); |
419 |
} |
420 |
|
421 |
|
422 |
VIEW * |
423 |
dev_auxview( /* return nth auxiliary view */ |
424 |
int n, |
425 |
int hvres[2] |
426 |
) |
427 |
{ |
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 |
int |
440 |
dev_input(void) /* get X11 input */ |
441 |
{ |
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 |
void |
456 |
dev_value( /* add a pixel value to our texture */ |
457 |
COLR c, |
458 |
FVECT d, |
459 |
FVECT p |
460 |
) |
461 |
{ |
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 |
int |
475 |
dev_flush(void) /* flush output as appropriate */ |
476 |
{ |
477 |
int ndrawn; |
478 |
|
479 |
if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) { |
480 |
#ifdef STEREO |
481 |
pushright(); /* draw right eye */ |
482 |
ndrawn = gmDrawGeom(); |
483 |
#ifdef DOBJ |
484 |
ndrawn += dobj_render(); |
485 |
#endif |
486 |
checkglerr("rendering right eye"); |
487 |
popright(); /* draw left eye */ |
488 |
#endif |
489 |
ndrawn = gmDrawGeom(); |
490 |
#ifdef DOBJ |
491 |
ndrawn += dobj_render(); |
492 |
#endif |
493 |
glXSwapBuffers(ourdisplay, gwind); |
494 |
checkglerr("rendering base view"); |
495 |
} |
496 |
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 |
#ifdef STEREO |
508 |
pushright(); |
509 |
odUpdate(1); /* draw right eye */ |
510 |
popright(); |
511 |
#endif |
512 |
odUpdate(0); /* draw left eye */ |
513 |
glFlush(); /* flush OpenGL */ |
514 |
} |
515 |
rayqleft = RAYQLEN; |
516 |
/* flush X11 and return # pending */ |
517 |
return(odev.inpready = XPending(ourdisplay)); |
518 |
} |
519 |
|
520 |
|
521 |
static void |
522 |
checkglerr( /* check for GL or GLU error */ |
523 |
char *where |
524 |
) |
525 |
{ |
526 |
GLenum errcode; |
527 |
|
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 |
static void |
537 |
xferdepth(void) /* load and clear depth buffer */ |
538 |
{ |
539 |
GLfloat *dbp; |
540 |
GLubyte *pbuf; |
541 |
|
542 |
if (depthbuffer == NULL) { /* allocate private depth buffer */ |
543 |
#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 |
CHECK(depthbuffer==NULL, SYSTEM, "out of memory in xferdepth"); |
550 |
} |
551 |
/* allocate alpha buffer for portals */ |
552 |
if (gmPortals) |
553 |
pbuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte)); |
554 |
else |
555 |
pbuf = NULL; |
556 |
#ifdef STEREO |
557 |
pushright(); |
558 |
glReadPixels(0, 0, odev.hres, odev.vres, |
559 |
GL_DEPTH_COMPONENT, GL_FLOAT, depthright); |
560 |
if (pbuf != NULL) { |
561 |
glClear(GL_COLOR_BUFFER_BIT); |
562 |
gmDrawPortals(0xff, -1, -1, -1); |
563 |
glReadPixels(0, 0, odev.hres, odev.vres, |
564 |
GL_RED, GL_UNSIGNED_BYTE, pbuf); |
565 |
} |
566 |
for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; ) |
567 |
if (pbuf != NULL && pbuf[dbp-depthright]&0x40) |
568 |
*dbp = FHUGE; |
569 |
else |
570 |
*dbp = mapdepth(*dbp); |
571 |
glClear(GL_DEPTH_BUFFER_BIT); |
572 |
odDepthMap(1, depthright); |
573 |
popright(); |
574 |
#endif |
575 |
/* read back depth buffer */ |
576 |
glReadPixels(0, 0, odev.hres, odev.vres, |
577 |
GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); |
578 |
if (pbuf != NULL) { |
579 |
glClear(GL_COLOR_BUFFER_BIT); /* find portals */ |
580 |
gmDrawPortals(0xff, -1, -1, -1); |
581 |
glReadPixels(0, 0, odev.hres, odev.vres, |
582 |
GL_RED, GL_UNSIGNED_BYTE, pbuf); |
583 |
#ifdef DEBUG |
584 |
glXSwapBuffers(ourdisplay, gwind); |
585 |
#endif |
586 |
} |
587 |
for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; ) |
588 |
if (pbuf != NULL && pbuf[dbp-depthbuffer]&0x40) |
589 |
*dbp = FHUGE; |
590 |
else |
591 |
*dbp = mapdepth(*dbp); |
592 |
glClear(GL_DEPTH_BUFFER_BIT); /* clear system depth buffer */ |
593 |
odDepthMap(0, depthbuffer); /* transfer depth data */ |
594 |
if (pbuf != NULL) |
595 |
free((void *)pbuf); /* free our portal buffer */ |
596 |
} |
597 |
|
598 |
|
599 |
static void |
600 |
freedepth(void) /* free recorded depth buffer */ |
601 |
{ |
602 |
if (depthbuffer == NULL) |
603 |
return; |
604 |
#ifdef STEREO |
605 |
odDepthMap(1, NULL); |
606 |
free((void *)depthright); |
607 |
depthright = NULL; |
608 |
#endif |
609 |
odDepthMap(0, NULL); |
610 |
free((void *)depthbuffer); |
611 |
depthbuffer = NULL; |
612 |
} |
613 |
|
614 |
|
615 |
static double |
616 |
getdistance( /* distance from fore plane along view ray */ |
617 |
int dx, |
618 |
int dy, |
619 |
FVECT direc |
620 |
) |
621 |
{ |
622 |
GLfloat gldepth; |
623 |
double dist; |
624 |
|
625 |
if ((dx<0) | (dx>=odev.hres) | (dy<0) | (dy>=odev.vres)) |
626 |
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 |
if (gldepth <= FTINY) |
633 |
return (FHUGE); /* call failed */ |
634 |
dist = mapdepth(gldepth); |
635 |
} |
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 |
static void |
644 |
pushright(void) /* push on right view & buffer */ |
645 |
{ |
646 |
double d; |
647 |
|
648 |
setstereobuf(STEREO_BUFFER_RIGHT); |
649 |
if (viewflags & VWPERSP) { |
650 |
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 |
static void |
661 |
popright(void) /* pop off right view & buffer */ |
662 |
{ |
663 |
if (viewflags & VWPERSP) { |
664 |
glMatrixMode(GL_MODELVIEW); |
665 |
glPopMatrix(); |
666 |
} |
667 |
setstereobuf(STEREO_BUFFER_LEFT); |
668 |
} |
669 |
#endif |
670 |
|
671 |
|
672 |
static int |
673 |
mytmflags(void) /* figure out tone mapping flags */ |
674 |
{ |
675 |
extern char *progname; |
676 |
char *cp, *tail; |
677 |
/* 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 |
static void |
698 |
getevent(void) /* get next event */ |
699 |
{ |
700 |
XNextEvent(ourdisplay, levptr(XEvent)); |
701 |
switch (levptr(XEvent)->type) { |
702 |
case ConfigureNotify: |
703 |
resizewindow(levptr(XConfigureEvent)); |
704 |
break; |
705 |
case UnmapNotify: |
706 |
viewflags &= ~VWMAPPED; |
707 |
break; |
708 |
case MapNotify: |
709 |
odRemap(0); |
710 |
viewflags |= VWMAPPED; |
711 |
break; |
712 |
case Expose: |
713 |
fixwindow(levptr(XExposeEvent)); |
714 |
break; |
715 |
case KeyPress: |
716 |
getkey(levptr(XKeyPressedEvent)); |
717 |
break; |
718 |
case ButtonPress: |
719 |
if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) |
720 |
getframe(levptr(XButtonPressedEvent)); |
721 |
else |
722 |
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 |
break; |
731 |
} |
732 |
} |
733 |
|
734 |
|
735 |
static void |
736 |
draw3dline( /* draw 3d line in world coordinates */ |
737 |
FVECT wp[2] |
738 |
) |
739 |
{ |
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 |
static void |
746 |
draw_grids( /* draw holodeck section grids */ |
747 |
int fore |
748 |
) |
749 |
{ |
750 |
glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT); |
751 |
glDisable(GL_LIGHTING); |
752 |
if (fore) |
753 |
glColor3ub(4, 250, 250); |
754 |
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 |
static int |
765 |
moveview( /* move our view */ |
766 |
int dx, |
767 |
int dy, |
768 |
int mov, |
769 |
int orb |
770 |
) |
771 |
{ |
772 |
VIEW nv; |
773 |
FVECT odir, v1, wip; |
774 |
double d; |
775 |
#ifdef DOBJ |
776 |
double d1; |
777 |
#endif |
778 |
/* start with old view */ |
779 |
nv = odev.v; |
780 |
/* 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 |
if (geodesic(odir, odir, nv.vup, |
803 |
d=MOVDEG*PI/180.*orb, GEOD_RAD) == 0.0) |
804 |
return(0); |
805 |
VSUM(nv.vp, wip, odir, -1.); |
806 |
geodesic(nv.vdir, nv.vdir, nv.vup, d, GEOD_RAD); |
807 |
} else if (mov) { /* move forward/backward */ |
808 |
d = MOVPCT/100. * mov; |
809 |
VSUM(nv.vp, nv.vp, odir, d); |
810 |
} |
811 |
if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */ |
812 |
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 |
inpresflags |= DFL(DC_SETVIEW); |
820 |
return(1); |
821 |
} |
822 |
|
823 |
|
824 |
static void |
825 |
getframe( /* get focus frame */ |
826 |
XButtonPressedEvent *ebut |
827 |
) |
828 |
{ |
829 |
int startx = ebut->x, starty = ebut->y; |
830 |
int endx, endy, midx, midy; |
831 |
FVECT odir, v1; |
832 |
double d; |
833 |
#ifdef DOBJ |
834 |
double d1; |
835 |
#endif |
836 |
/* get mouse drag */ |
837 |
XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); |
838 |
endx = levptr(XButtonReleasedEvent)->x; |
839 |
endy = levptr(XButtonReleasedEvent)->y; |
840 |
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 |
return; |
857 |
if (endx < startx) {int c = endx; endx = startx; startx = c;} |
858 |
if (endy < starty) {int c = endy; endy = starty; starty = c;} |
859 |
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 |
static void |
867 |
waitabit(void) /* pause a moment */ |
868 |
{ |
869 |
struct timespec ts; |
870 |
ts.tv_sec = 0; |
871 |
ts.tv_nsec = 100000000L; |
872 |
nanosleep(&ts, NULL); |
873 |
} |
874 |
|
875 |
|
876 |
static void |
877 |
getmove( /* get view change */ |
878 |
XButtonPressedEvent *ebut |
879 |
) |
880 |
{ |
881 |
int movdir = MOVDIR(ebut->button); |
882 |
int movorb = MOVORB(ebut->state); |
883 |
int ndrawn; |
884 |
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 |
viewflags &= ~VWSTEADY; /* flag moving view */ |
891 |
setglpersp(); /* start us off in perspective */ |
892 |
while (!XCheckMaskEvent(ourdisplay, |
893 |
ButtonReleaseMask, levptr(XEvent))) { |
894 |
/* pause so as not to move too fast */ |
895 |
waitabit(); |
896 |
/* 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 |
ndrawn = gmDrawGeom(); |
910 |
#ifdef DOBJ |
911 |
ndrawn += dobj_render(); |
912 |
#endif |
913 |
popright(); |
914 |
#endif |
915 |
/* redraw octrees */ |
916 |
ndrawn = gmDrawGeom(); |
917 |
#ifdef DOBJ |
918 |
ndrawn += dobj_render(); /* redraw objects */ |
919 |
#endif |
920 |
glXSwapBuffers(ourdisplay, gwind); |
921 |
if (!ndrawn) |
922 |
sleep(1); /* for reasonable interaction */ |
923 |
} |
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 |
viewflags |= VWSTEADY; /* done goofing around */ |
931 |
} |
932 |
|
933 |
|
934 |
static void |
935 |
setglpersp(void) /* set perspective view in GL */ |
936 |
{ |
937 |
double d, xmin, xmax, ymin, ymax; |
938 |
GLfloat vec[4]; |
939 |
double depthlim[2]; |
940 |
/* set depth limits */ |
941 |
gmDepthLimit(depthlim, odev.v.vp, odev.v.vdir); |
942 |
if (depthlim[0] >= depthlim[1]) { |
943 |
dev_zmin = 1.; |
944 |
dev_zmax = 100.; |
945 |
} else { |
946 |
dev_zmin = 0.5*depthlim[0]; |
947 |
dev_zmax = 1.25*depthlim[1]; |
948 |
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 |
if (dev_zmin*500. < dev_zmax) |
956 |
dev_zmin = dev_zmax/500.; |
957 |
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 |
viewflags &= ~VWORTHO; |
992 |
viewflags |= VWPERSP; |
993 |
} |
994 |
|
995 |
|
996 |
static void |
997 |
setglortho(void) /* set up orthographic view for cone drawing */ |
998 |
{ |
999 |
glDrawBuffer(GL_FRONT); /* use single-buffer mode */ |
1000 |
/* 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 |
viewflags &= ~VWPERSP; |
1009 |
viewflags |= VWORTHO; |
1010 |
} |
1011 |
|
1012 |
|
1013 |
static void |
1014 |
wipeclean(void) /* prepare for redraw */ |
1015 |
{ |
1016 |
glDrawBuffer(GL_BACK); /* use double-buffer mode */ |
1017 |
glReadBuffer(GL_BACK); |
1018 |
/* clear buffers */ |
1019 |
#ifdef STEREO |
1020 |
setstereobuf(STEREO_BUFFER_RIGHT); |
1021 |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
1022 |
setstereobuf(STEREO_BUFFER_LEFT); |
1023 |
#endif |
1024 |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
1025 |
freedepth(); |
1026 |
if ((viewflags&(VWCHANGE|VWSTEADY)) == |
1027 |
(VWCHANGE|VWSTEADY)) { /* clear samples if new */ |
1028 |
odClean(); |
1029 |
viewflags &= ~VWCHANGE; /* change noted */ |
1030 |
} else if (viewflags & VWSTEADY) |
1031 |
odRedrawAll(); |
1032 |
setglpersp(); /* reset view & clipping planes */ |
1033 |
} |
1034 |
|
1035 |
|
1036 |
static void |
1037 |
getkey( /* get input key */ |
1038 |
XKeyPressedEvent *ekey |
1039 |
) |
1040 |
{ |
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 |
viewflags |= VWHEADLOCK; |
1053 |
return; |
1054 |
case 'H': /* turn off height motion lock */ |
1055 |
viewflags &= ~VWHEADLOCK; |
1056 |
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 |
odRemap(0); /* new tone mapping */ |
1084 |
glClear(GL_DEPTH_BUFFER_BIT); |
1085 |
#ifdef STEREO |
1086 |
setstereobuf(STEREO_BUFFER_RIGHT); |
1087 |
glClear(GL_DEPTH_BUFFER_BIT); |
1088 |
setstereobuf(STEREO_BUFFER_LEFT); |
1089 |
#endif |
1090 |
return; |
1091 |
case CTRL('L'): /* refresh from server */ |
1092 |
if (inpresflags & DFL(DC_REDRAW)) |
1093 |
return; /* already called */ |
1094 |
XRaiseWindow(ourdisplay, gwind); |
1095 |
XFlush(ourdisplay); /* raise up window */ |
1096 |
sleep(1); /* wait for restacking */ |
1097 |
dev_clear(); /* clear buffer and samples */ |
1098 |
odRemap(1); /* start fresh histogram */ |
1099 |
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 |
static void |
1121 |
fixwindow( /* repair damage to window */ |
1122 |
XExposeEvent *eexp |
1123 |
) |
1124 |
{ |
1125 |
int xmin, ymin, xmax, ymax; |
1126 |
|
1127 |
if ((odev.hres == 0) | (odev.vres == 0)) { /* first exposure */ |
1128 |
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 |
|
1134 |
if (xmin <= 0 && xmax >= odev.hres-1 && |
1135 |
ymin <= 0 && ymax >= odev.vres) { |
1136 |
DCHECK(eexp->count, WARNING, "multiple clear in fixwindow"); |
1137 |
wipeclean(); /* make sure we're go */ |
1138 |
return; |
1139 |
} |
1140 |
/* clear portion of depth */ |
1141 |
glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
1142 |
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 |
glPopAttrib(); |
1162 |
odRedraw(0, xmin, ymin, xmax, ymax); |
1163 |
} |
1164 |
|
1165 |
|
1166 |
static void |
1167 |
resizewindow( /* resize window */ |
1168 |
XConfigureEvent *ersz |
1169 |
) |
1170 |
{ |
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 |
inpresflags |= DFL(DC_SETVIEW); |
1183 |
viewflags |= VWCHANGE; |
1184 |
} |