ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_ogl.c
Revision: 3.10
Committed: Wed Dec 23 17:43:42 1998 UTC (25 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.9: +34 -14 lines
Log Message:
numerous bug fixes for stereo and object-drawing drivers

File Contents

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