ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_glx.c
(Generate patch)

Comparing ray/src/hd/rhd_glx.c (file contents):
Revision 3.8 by gregl, Fri Dec 26 21:56:43 1997 UTC vs.
Revision 3.30 by schorsch, Mon Jul 21 22:30:18 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * OpenGL GLX driver for holodeck display.
6 < * Based on x11 driver.
6 > * Based on old GLX driver using cones.
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   #include "standard.h"
13 #include "rhd_qtree.h"
21  
22 < #include  <GL/glx.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  "x11icon.h"
28 > #include "rhd_sample.h"
29 > #ifdef DOBJ
30 > #include "rhdobj.h"
31 > #endif
32  
33 + #include "x11icon.h"
34 +
35 + #ifndef RAYQLEN
36 + #define RAYQLEN         1024            /* max. rays to queue before flush */
37 + #endif
38 +
39   #ifndef FEQ
40   #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
41   #endif
42  
23 #ifndef MAXCONE
24 #define MAXCONE         16              /* number of different cone sizes */
25 #endif
26 #ifndef MAXVERT
27 #define MAXVERT         32              /* maximum number of cone vertices */
28 #endif
29 #ifndef MINVERT
30 #define MINVERT         4               /* minimum number of cone vertices */
31 #endif
32 #ifndef DEPTHFACT
33 #define DEPTHFACT       16.             /* multiplier for depth tests */
34 #endif
35
43   #define GAMMA           1.4             /* default gamma correction */
44  
45 + #define FRAMESTATE(s)   (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask))
46 +
47   #define MOVPCT          7               /* percent distance to move /frame */
48   #define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
49   #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */
50   #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
51  
52 + #ifndef TARGETFPS
53 + #define TARGETFPS       2.0             /* target frames/sec during motion */
54 + #endif
55 +
56   #define MINWIDTH        480             /* minimum graphics window width */
57   #define MINHEIGHT       400             /* minimum graphics window height */
58  
# Line 47 | Line 60 | static char SCCSid[] = "$SunId$ SGI";
60  
61   #define BORWIDTH        5               /* border width */
62  
63 + #define setstereobuf(bid)       (glXWaitGL(), \
64 +                                XSGISetStereoBuffer(ourdisplay, gwind, bid), \
65 +                                glXWaitX())
66 +
67   #define  ourscreen      DefaultScreen(ourdisplay)
68   #define  ourroot        RootWindow(ourdisplay,ourscreen)
69   #define  ourmask        (StructureNotifyMask|ExposureMask|KeyPressMask|\
# Line 56 | Line 73 | static char SCCSid[] = "$SunId$ SGI";
73  
74   struct driver   odev;                   /* global device driver structure */
75  
76 + char odev_args[64];                     /* command arguments */
77 +
78 + #ifdef STEREO
79 + static VIEW     vwright;                /* right eye view */
80 + #endif
81 +
82 + static int      rayqleft = 0;           /* rays left to queue before flush */
83 +
84   static XEvent  currentevent;            /* current event */
85  
86   static int  mapped = 0;                 /* window is mapped? */
# Line 68 | Line 93 | static GLXContext      gctx;           /* our GLX context */
93  
94   static double   pwidth, pheight;        /* pixel dimensions (mm) */
95  
96 < static double   curzmax = 1e4;          /* current depth upper limit */
72 < static double   nxtzmax = 0.;           /* maximum (finite) depth so far */
96 > static double   mindpth, maxdpth;       /* min. and max. depth */
97  
98 < static struct {
75 <        double  rad;            /* cone radius */
76 <        int     nverts;         /* number of vertices */
77 <        FVECT   *va;            /* allocated vertex array */
78 < } cone[MAXCONE];        /* precomputed cones for drawing */
98 > double  dev_zmin, dev_zmax;             /* fore and aft clipping plane dist. */
99  
100   static int      inpresflags;            /* input result flags */
101  
102   static int      headlocked = 0;         /* lock vertical motion */
103  
104 < static int  resizewindow(), getevent(), getkey(), moveview(),
105 <                initcones(), freecones(),
86 <                getmove(), fixwindow(), mytmflags();
104 > static int  resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
105 >                setglpersp(), getframe(), getmove(), fixwindow(), mytmflags();
106  
107 + #ifdef STEREO
108 + static int  pushright(), popright();
109 + #endif
110 + static double   getdistance();
111 + #define mapdepth(d) ((d)> 0.9995 ? FHUGE: dev_zmin/ \
112 +                     (1.-(d)*(1.-dev_zmin/dev_zmax)))
113  
114 < dev_open(id)                    /* initialize X11 driver */
114 > extern time_t   time();
115 >
116 >
117 > dev_open(id)                    /* initialize GLX driver */
118   char  *id;
119   {
120          extern char     *getenv();
121 +        static RGBPRIMS myprims = STDPRIMS;
122          static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
123                                  GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
124                                  GLX_DEPTH_SIZE,15, None};
125 <        char    *gv;
125 >        char    *ev;
126          double  gamval = GAMMA;
127 +        RGBPRIMP        dpri = stdprims;
128          XSetWindowAttributes    ourwinattr;
129          XWMHints        ourxwmhints;
130          XSizeHints      oursizhints;
131 <                                        /* set quadtree globals */
132 <        qtMinNodesiz = 3;
131 >                                        /* check for unsupported stereo */
132 > #ifdef NOSTEREO
133 >        error(INTERNAL, "stereo display driver unavailable");
134 > #endif
135                                          /* open display server */
136          ourdisplay = XOpenDisplay(NULL);
137          if (ourdisplay == NULL)
138                  error(USER, "cannot open X-windows; DISPLAY variable set?\n");
139 + #ifdef STEREO
140 +        switch (XSGIQueryStereoMode(ourdisplay, ourroot)) {
141 +        case STEREO_TOP:
142 +        case STEREO_BOTTOM:
143 +                break;
144 +        case STEREO_OFF:
145 +                error(USER,
146 +        "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
147 +        case X_STEREO_UNSUPPORTED:
148 +                error(USER, "stereo mode not supported on this screen");
149 +        default:
150 +                error(INTERNAL, "unknown stereo mode");
151 +        }
152 + #endif
153                                          /* find a usable visual */
154          ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
155          if (ourvinf == NULL)
# Line 111 | Line 157 | char  *id;
157                                          /* get a context */
158          gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
159                                          /* set gamma and tone mapping */
160 <        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
161 <                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
162 <                gamval = atof(gv);
163 <        if (tmInit(mytmflags(), stdprims, gamval) == NULL)
160 >        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
161 >                        || (ev = getenv("DISPLAY_GAMMA")) != NULL)
162 >                gamval = atof(ev);
163 >        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
164 >                        sscanf(ev, "%f %f %f %f %f %f %f %f",
165 >                                &myprims[RED][CIEX],&myprims[RED][CIEY],
166 >                                &myprims[GRN][CIEX],&myprims[GRN][CIEY],
167 >                                &myprims[BLU][CIEX],&myprims[BLU][CIEY],
168 >                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
169 >                dpri = myprims;
170 >        if (tmInit(mytmflags(), dpri, gamval) == NULL)
171                  error(SYSTEM, "not enough memory in dev_open");
172                                          /* open window */
173          ourwinattr.background_pixel = ourblack;
# Line 125 | Line 178 | char  *id;
178                                  ourvinf->visual, AllocNone);
179          gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
180                  DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
181 + #ifdef STEREO
182 +                (DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2,
183 + #else
184                  DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
185 + #endif
186                  BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
187                  CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
188          if (gwind == 0)
# Line 138 | Line 195 | char  *id;
195                          gwind, x11icon_bits, x11icon_width, x11icon_height);
196          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
197          oursizhints.min_width = MINWIDTH;
198 + #ifdef STEREO
199 +        oursizhints.min_height = MINHEIGHT/2;
200 +        oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH;
201 +        oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) -
202 +                                        2*BORWIDTH)/2;
203 +        oursizhints.flags = PMinSize|PMaxSize;
204 + #else
205          oursizhints.min_height = MINHEIGHT;
206          oursizhints.flags = PMinSize;
207 + #endif
208          XSetNormalHints(ourdisplay, gwind, &oursizhints);
209                                          /* set GLX context */
210          glXMakeCurrent(ourdisplay, gwind, gctx);
211          glEnable(GL_DEPTH_TEST);
212          glDepthFunc(GL_LEQUAL);
213 <        glShadeModel(GL_FLAT);
213 >        glShadeModel(GL_SMOOTH);
214          glDisable(GL_DITHER);
215          glDisable(GL_CULL_FACE);
151        glMatrixMode(GL_PROJECTION);
152        glOrtho(0., 1., 0., 1., -.01, 1.01);
153        glTranslated(0., 0., -1.01);
216                                          /* figure out sensible view */
217          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
218                          DisplayWidth(ourdisplay, ourscreen);
219          pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
220                          DisplayHeight(ourdisplay, ourscreen);
221 <        copystruct(&odev.v, &stdview);
221 > #ifdef STEREO
222 >        pheight *= 2.;
223 >        setstereobuf(STEREO_BUFFER_LEFT);
224 > #endif
225 >        checkglerr("setting rendering parameters");
226 >        odev.v = stdview;
227          odev.v.type = VT_PER;
228                                          /* map the window */
229          XMapWindow(ourdisplay, gwind);
230          dev_input();                    /* sets size and view angles */
231 <                                        /* allocate our leaf pile */
232 <        if (!qtAllocLeaves(2 * DisplayWidth(ourdisplay,ourscreen) *
233 <                        DisplayHeight(ourdisplay,ourscreen) /
167 <                        (qtMinNodesiz*qtMinNodesiz)))
231 >                                        /* allocate our samples */
232 >        if (!smInit(DisplayWidth(ourdisplay,ourscreen) *
233 >                        DisplayHeight(ourdisplay,ourscreen) / 10))
234                  error(SYSTEM, "insufficient memory for value storage");
235 +        mindpth = FHUGE; maxdpth = FTINY;
236          odev.name = id;
237          odev.ifd = ConnectionNumber(ourdisplay);
171                                        /* initialize cone array */
172        initcones();
238   }
239  
240  
241   dev_close()                     /* close our display and free resources */
242   {
243 +        smInit(0);
244 + #ifdef DOBJ
245 +        dobj_cleanup();
246 + #endif
247          glXMakeCurrent(ourdisplay, None, NULL);
248          glXDestroyContext(ourdisplay, gctx);
249          XDestroyWindow(ourdisplay, gwind);
250          gwind = 0;
251          XCloseDisplay(ourdisplay);
252          ourdisplay = NULL;
184        qtFreeLeaves();
253          tmDone(NULL);
186        freecones();
254          odev.v.type = 0;
255          odev.hres = odev.vres = 0;
256          odev.ifd = -1;
257   }
258  
259  
260 + dev_clear()                     /* clear our representation */
261 + {
262 +        smInit(rsL.max_samp);
263 +        wipeclean(1);
264 +        rayqleft = 0;                   /* hold off update */
265 + }
266 +
267 +
268   int
269   dev_view(nv)                    /* assign new driver view */
270   register VIEW   *nv;
271   {
272 <        if (nv->type == VT_PAR ||               /* check view legality */
272 >        double  d;
273 >
274 >        if (nv->type != VT_PER ||               /* check view legality */
275                          nv->horiz > 160. || nv->vert > 160.) {
276                  error(COMMAND, "illegal view type/angle");
277                  nv->type = odev.v.type;
# Line 202 | Line 279 | register VIEW  *nv;
279                  nv->vert = odev.v.vert;
280                  return(0);
281          }
205        if (nv->vfore > FTINY) {
206                error(COMMAND, "cannot handle fore clipping");
207                nv->vfore = 0.;
208                return(0);
209        }
282          if (nv != &odev.v) {
283 <                if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
283 >                                                /* resize window? */
284 >                if (!FEQ(nv->horiz,odev.v.horiz) ||
285                                  !FEQ(nv->vert,odev.v.vert)) {
286                          int     dw = DisplayWidth(ourdisplay,ourscreen);
287                          int     dh = DisplayHeight(ourdisplay,ourscreen);
288  
289                          dw -= 25;       /* for window frame */
290                          dh -= 50;
291 + #ifdef STEREO
292 +                        dh /= 2;
293 + #endif
294                          odev.hres = 2.*VIEWDIST/pwidth *
295                                          tan(PI/180./2.*nv->horiz);
296                          odev.vres = 2.*VIEWDIST/pheight *
# Line 230 | Line 306 | register VIEW  *nv;
306                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
307                          dev_input();    /* get resize event */
308                  }
309 <                copystruct(&odev.v, nv);
309 >                odev.v = *nv;   /* setview() already called */
310          }
311 <        if (nxtzmax > FTINY) {
312 <                curzmax = nxtzmax;
313 <                nxtzmax = 0.;
314 <        }
315 <        glClear(GL_DEPTH_BUFFER_BIT);
316 <        qtReplant();
311 > #ifdef STEREO
312 >        vwright = *nv;
313 >        d = eyesepdist / sqrt(nv->hn2);
314 >        VSUM(vwright.vp, nv->vp, nv->hvec, d);
315 >        /* setview(&vwright);   -- Unnecessary */
316 > #endif
317 >        wipeclean(0);
318          return(1);
319   }
320  
321  
322 + dev_section(gfn, pfn)           /* add octree for geometry rendering */
323 + char    *gfn, *pfn;
324 + {
325 +        /* unimplemented */
326 + }
327 +
328 +
329 + dev_auxcom(cmd, args)           /* process an auxiliary command */
330 + char    *cmd, *args;
331 + {
332 + #ifdef DOBJ
333 +        if (dobj_command(cmd, args) >= 0)
334 +                return;
335 + #endif
336 +        sprintf(errmsg, "%s: unknown command", cmd);
337 +        error(COMMAND, errmsg);
338 + }
339 +
340 +
341 + VIEW *
342 + dev_auxview(n, hvres)           /* return nth auxiliary view */
343 + int     n;
344 + int     hvres[2];
345 + {
346 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
347 +        if (n == 0)
348 +                return(&odev.v);
349 + #ifdef STEREO
350 +        if (n == 1)
351 +                return(&vwright);
352 + #endif
353 +        return(NULL);
354 + }
355 +
356 +
357   int
358   dev_input()                     /* get X11 input */
359   {
# Line 250 | Line 362 | dev_input()                    /* get X11 input */
362          do
363                  getevent();
364  
365 <        while (XQLength(ourdisplay) > 0);
365 >        while (XPending(ourdisplay) > 0);
366  
367 +        odev.inpready = 0;
368 +
369          return(inpresflags);
370   }
371  
372  
373 + dev_value(c, d, p)              /* add a pixel value to our mesh */
374 + COLR    c;
375 + FVECT   d, p;
376 + {
377 +        double  depth;
378 + #ifdef DOBJ
379 +        if (dobj_lightsamp != NULL) {   /* in light source sampling */
380 +                (*dobj_lightsamp)(c, d, p);
381 +                return;
382 +        }
383 + #endif
384 +        if (p != NULL) {                /* add depth to our range */
385 +                depth = (p[0] - odev.v.vp[0])*d[0] +
386 +                        (p[1] - odev.v.vp[1])*d[1] +
387 +                        (p[2] - odev.v.vp[2])*d[2];
388 +                if (depth > FTINY) {
389 +                        if (depth < mindpth)
390 +                                mindpth = depth;
391 +                        if (depth > maxdpth)
392 +                                maxdpth = depth;
393 +                }
394 +        }
395 +        smNewSamp(c, d, p);             /* add to display representation */
396 +        if (!--rayqleft)
397 +                dev_flush();            /* flush output */
398 + }
399 +
400 +
401   int
402   dev_flush()                     /* flush output */
403   {
404 <        qtUpdate();
405 <        glFlush();
406 <        return(XPending(ourdisplay));
404 >        if (mapped) {
405 > #ifdef STEREO
406 >                pushright();                    /* update right eye */
407 >                smUpdate(&vwright, MAXQUALITY);
408 > #ifdef DOBJ
409 >                dobj_render();                  /* usually in foreground */
410 > #endif
411 >                popright();                     /* update left eye */
412 > #endif
413 >                smUpdate(&odev.v, MAXQUALITY);
414 >                checkglerr("rendering mesh");
415 > #ifdef DOBJ
416 >                dobj_render();
417 > #endif
418 >                glFlush();                      /* flush OGL */
419 >        }
420 >        rayqleft = RAYQLEN;
421 >                                        /* flush X11 and return # pending */
422 >        return(odev.inpready = XPending(ourdisplay));
423   }
424  
425  
426 < dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */
427 < BYTE    rgb[3];
270 < FVECT   ip;
271 < double  rad;
426 > checkglerr(where)               /* check for GL or GLU error */
427 > char    *where;
428   {
429 <        register int    ci, j;
430 <        double  apexh, basez;
431 <                                        /* compute apex height (0. to 1.) */
432 <        if (ip[2] > 1e6)
433 <                apexh = 1. - 1./DEPTHFACT;
434 <        else {
279 <                if (ip[2] > nxtzmax)
280 <                        nxtzmax = ip[2];
281 <                if (ip[2] >= curzmax)
282 <                        apexh = 1. - 1./DEPTHFACT;
283 <                else
284 <                        apexh = 1. - ip[2]/(curzmax*DEPTHFACT);
429 >        register GLenum errcode;
430 >
431 >        while ((errcode = glGetError()) != GL_NO_ERROR) {
432 >                sprintf(errmsg, "OpenGL error %s: %s",
433 >                                where, gluErrorString(errcode));
434 >                error(WARNING, errmsg);
435          }
286        rad *= 1.25;                    /* find conservative cone match */
287        for (ci = 0; ci < MAXCONE-1; ci++)
288                if (cone[ci].rad >= rad)
289                        break;
290                                        /* draw it */
291        glColor3ub(rgb[0], rgb[1], rgb[2]);
292        glBegin(GL_TRIANGLE_FAN);
293        glVertex3d(ip[0], ip[1], apexh);        /* start with apex */
294        basez = apexh*cone[ci].va[0][2];        /* base z's all the same */
295        for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */
296                glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1],
297                                basez);
298                                                /* connect last to first */
299        glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez);
300        glEnd();                                /* all done */
436   }
437  
438  
439 + #ifdef STEREO
440 + static
441 + pushright()                     /* push on right view */
442 + {
443 +        double  d;
444 +
445 +        setstereobuf(STEREO_BUFFER_RIGHT);
446 +        glMatrixMode(GL_MODELVIEW);
447 +        glPushMatrix();
448 +        d = -eyesepdist / sqrt(odev.v.hn2);
449 +        glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]);
450 +        checkglerr("setting right view");
451 + }
452 +
453 +
454 + static
455 + popright()                      /* pop off right view */
456 + {
457 +        glMatrixMode(GL_MODELVIEW);
458 +        glPopMatrix();
459 +        setstereobuf(STEREO_BUFFER_LEFT);
460 + }
461 + #endif
462 +
463 +
464   static int
465   mytmflags()                     /* figure out tone mapping flags */
466   {
# Line 312 | Line 472 | mytmflags()                    /* figure out tone mapping flags */
472                          tail = cp+1;
473          for (cp = tail; *cp && *cp != '.'; cp++)
474                  ;
475 <        if (cp-tail == 3 && !strncmp(tail, "glx", 3))
476 <                return(TM_F_CAMERA);
477 <        if (cp-tail == 4 && !strncmp(tail, "glxh", 4))
478 <                return(TM_F_HUMAN);
319 <        error(USER, "illegal driver name");
475 >        if (cp > tail && cp[-1] == 'h')
476 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
477 >        else
478 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
479   }
480  
481  
482   static
324 initcones()                     /* initialize cone vertices */
325 {
326        register int    i, j;
327        double  minrad, d;
328
329        if (cone[0].nverts)
330                freecones();
331        minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) +
332                                        DisplayHeight(ourdisplay,ourscreen));
333        for (i = 0; i < MAXCONE; i++) {
334                d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */
335                cone[i].rad = minrad + (1.-minrad)*d;
336                cone[i].nverts = MINVERT + (MAXVERT-MINVERT)*d;
337                cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT));
338                if (cone[i].va == NULL)
339                        error(SYSTEM, "out of memory in initcones");
340                for (j = cone[i].nverts; j--; ) {
341                        d = 2.*PI * (j+.5) / (cone[i].nverts);
342                        cone[i].va[j][0] = cos(d) * cone[i].rad;
343                        cone[i].va[j][1] = sin(d) * cone[i].rad;
344                        cone[i].va[j][2] = 1. - cone[i].rad;
345                }
346        }
347 }
348
349
350 static
351 freecones()                     /* free cone vertices */
352 {
353        register int    i;
354
355        for (i = MAXCONE; i--; )
356                if (cone[i].nverts) {
357                        free((char *)cone[i].va);
358                        cone[i].va = NULL;
359                        cone[i].nverts = 0;
360                }
361 }
362
363
364 static
483   getevent()                      /* get next event */
484   {
485          XNextEvent(ourdisplay, levptr(XEvent));
# Line 382 | Line 500 | getevent()                     /* get next event */
500                  getkey(levptr(XKeyPressedEvent));
501                  break;
502          case ButtonPress:
503 <                getmove(levptr(XButtonPressedEvent));
503 >                if (FRAMESTATE(levptr(XButtonPressedEvent)->state))
504 >                        getframe(levptr(XButtonPressedEvent));
505 >                else
506 >                        getmove(levptr(XButtonPressedEvent));
507                  break;
508          }
509   }
# Line 398 | Line 519 | register FVECT wp[2];
519  
520  
521   static
522 < draw_grids()                    /* draw holodeck section grids */
522 > draw_grids(fore)                /* draw holodeck section grids */
523 > int     fore;
524   {
525 <        static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
526 <        double  xmin, xmax, ymin, ymax, zmin, zmax;
527 <        double  d, cx, sx, crad;
528 <        FVECT   vx, vy;
407 <        register int    i, j;
408 <                                        /* can we even do it? */
409 <        if (!mapped || odev.v.type != VT_PER)
410 <                return;
411 <                                        /* compute view frustum */
412 <        if (normalize(odev.v.vdir) == 0.0)
413 <                return;
414 <        zmin = 0.01;
415 <        zmax = 10000.;
416 <        if (odev.v.vfore > FTINY)
417 <                zmin = odev.v.vfore;
418 <        if (odev.v.vaft > FTINY)
419 <                zmax = odev.v.vaft;
420 <        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
421 <        xmin = -xmax;
422 <        d = odev.v.hoff * (xmax - xmin);
423 <        xmin += d; xmax += d;
424 <        ymax = zmin * tan(PI/180./2. * odev.v.vert);
425 <        ymin = -ymax;
426 <        d = odev.v.voff * (ymax - ymin);
427 <        ymin += d; ymax += d;
428 <                                        /* set view matrix */
429 <        glMatrixMode(GL_PROJECTION);
430 <        glPushMatrix();
431 <        glLoadIdentity();
432 <        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
433 <        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
434 <                odev.v.vp[0] + odev.v.vdir[0],
435 <                odev.v.vp[1] + odev.v.vdir[1],
436 <                odev.v.vp[2] + odev.v.vdir[2],
437 <                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
438 <        glDisable(GL_DEPTH_TEST);       /* write no depth values */
439 <        glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
525 >        if (fore)
526 >                glColor4ub(0, 255, 255, 0);
527 >        else
528 >                glColor4ub(0, 0, 0, 0);
529          glBegin(GL_LINES);              /* draw each grid line */
530          gridlines(draw3dline);
531          glEnd();
532 <        glEnable(GL_DEPTH_TEST);        /* restore rendering params */
444 <        glPopMatrix();
532 >        checkglerr("drawing grid lines");
533   }
534  
535 + static double
536 + getdistance(dx, dy, direc)      /* distance from fore plane along view ray */
537 + int     dx, dy;
538 + FVECT   direc;
539 + {
540 +        GLfloat gldepth;
541 +        double  dist;
542  
543 +        if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres)
544 +                return(FHUGE);
545 +        glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT,GL_FLOAT, &gldepth);
546 +        dist = mapdepth(gldepth);
547 +        if (dist >= .99*FHUGE)
548 +                return(FHUGE);
549 +        return((dist-odev.v.vfore)/DOT(direc,odev.v.vdir));
550 + }
551 +
552 +
553   static
554   moveview(dx, dy, mov, orb)      /* move our view */
555   int     dx, dy, mov, orb;
556   {
557          VIEW    nv;
558 <        FVECT   odir, v1;
559 <        double  d;
558 >        FVECT   odir, v1, wip;
559 >        double  d,d1;
560          register int    li;
561                                  /* start with old view */
562 <        copystruct(&nv, &odev.v);
563 <                                /* change view direction */
564 <        if (mov | orb) {
565 <                if ((li = qtFindLeaf(dx, dy)) < 0)
566 <                        return(0);      /* not on window */
567 <                VSUM(odir, qtL.wp[li], nv.vp, -1.);
568 <        } else {
569 <                if (viewray(nv.vp, nv.vdir, &odev.v,
570 <                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
571 <                        return(0);      /* outside view */
572 <        }
562 >        nv = odev.v;
563 >                                /* orient our motion */
564 >        if (viewray(v1, odir, &odev.v,
565 >                        (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
566 >                return(0);              /* outside view */
567 >        if (mov | orb) {        /* moving relative to geometry */
568 >                d = getdistance(dx, dy, odir);/*distance from front plane */
569 > #ifdef DOBJ
570 >                d1 = dobj_trace(NULL, v1, odir);        /* check objects */
571 >                                                /* check holodeck */
572 >                if (d1 < d)
573 >                        d = d1;
574 > #endif
575 >                if (d >= .99*FHUGE)
576 >                        d = 0.5*(dev_zmax+dev_zmin);    /* just guess */
577 >                VSUM(wip, v1, odir, d);
578 >                VSUB(odir, wip, odev.v.vp);
579 > #if 0
580 >                fprintf(stderr, "moveview: hit %s at (%f,%f,%f) (t=%f)\n",
581 >                                li < 0 ? "object" : "mesh",
582 >                                wip[0], wip[1], wip[2],
583 >                                (wip[0]-odev.v.vp[0])*odir[0] +
584 >                                (wip[1]-odev.v.vp[1])*odir[1] +
585 >                                (wip[2]-odev.v.vp[2])*odir[2]);
586 > #endif
587 >        } else                  /* panning with constant viewpoint */
588 >                VCOPY(nv.vdir, odir);
589          if (orb && mov) {               /* orbit left/right */
590                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
591 <                VSUM(nv.vp, qtL.wp[li], odir, -1.);
591 >                VSUM(nv.vp, wip, odir, -1.);
592                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
593          } else if (orb) {               /* orbit up/down */
594                  fcross(v1, odir, nv.vup);
595                  if (normalize(v1) == 0.)
596                          return(0);
597                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
598 <                VSUM(nv.vp, qtL.wp[li], odir, -1.);
598 >                VSUM(nv.vp, wip, odir, -1.);
599                  spinvector(nv.vdir, nv.vdir, v1, d);
600          } else if (mov) {               /* move forward/backward */
601                  d = MOVPCT/100. * mov;
# Line 482 | Line 603 | int    dx, dy, mov, orb;
603          }
604          if (!mov ^ !orb && headlocked) {        /* restore head height */
605                  VSUM(v1, odev.v.vp, nv.vp, -1.);
606 <                d = DOT(v1, odev.v.vup);
606 >                d = DOT(v1, nv.vup);
607                  VSUM(nv.vp, nv.vp, odev.v.vup, d);
608          }
609          if (setview(&nv) != NULL)
# Line 494 | Line 615 | int    dx, dy, mov, orb;
615  
616  
617   static
618 + getframe(ebut)                          /* get focus frame */
619 + XButtonPressedEvent     *ebut;
620 + {
621 +        int     startx = ebut->x, starty = ebut->y;
622 +        int     endx, endy;
623 +
624 +        XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent));
625 +        endx = levptr(XButtonReleasedEvent)->x;
626 +        endy = levptr(XButtonReleasedEvent)->y;
627 +        if (endx == startx | endy == starty) {
628 +                XBell(ourdisplay, 0);
629 +                return;
630 +        }
631 +        if (endx < startx) {register int c = endx; endx = startx; startx = c;}
632 +        if (endy < starty) {register int c = endy; endy = starty; starty = c;}
633 +        sprintf(odev_args, "%.3f %.3f %.3f %.3f",
634 +                        (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres,
635 +                        (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres);
636 +        inpresflags |= DFL(DC_FOCUS);
637 + }
638 +
639 +
640 + static
641   getmove(ebut)                           /* get view change */
642   XButtonPressedEvent     *ebut;
643   {
644          int     movdir = MOVDIR(ebut->button);
645          int     movorb = MOVORB(ebut->state);
646 <        int     oldnodesiz = qtMinNodesiz;
646 >        int     qlevel = MAXQUALITY-1;
647 >        time_t  lasttime, thistime;
648 >        int     nframes;
649          Window  rootw, childw;
650          int     rootx, rooty, wx, wy;
651          unsigned int    statemask;
652  
653 <        qtMinNodesiz = 24;              /* accelerate update rate */
508 <        XNoOp(ourdisplay);
653 >        XNoOp(ourdisplay);              /* makes sure we're not idle */
654  
655 +        nframes = 0;
656          while (!XCheckMaskEvent(ourdisplay,
657                          ButtonReleaseMask, levptr(XEvent))) {
658 <
658 >                                        /* get cursor position */
659                  if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
660                                  &rootx, &rooty, &wx, &wy, &statemask))
661                          break;          /* on another screen */
662  
663 +                draw_grids(0);          /* clear old grid lines */
664 + #ifdef STEREO
665 +                pushright(); draw_grids(0); popright();
666 + #endif
667 +                                        /* compute view motion */
668                  if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
669                          sleep(1);
670 <                        continue;
670 >                        lasttime++;
671 >                        continue;       /* cursor in bad place */
672                  }
673 <                glClear(GL_COLOR_BUFFER_BIT);
674 <                qtUpdate();
675 <                draw_grids();
673 >                draw_grids(1);          /* redraw grid */
674 > #ifdef STEREO
675 >                pushright();
676 >                draw_grids(1);
677 >                smUpdate(&vwright, qlevel);
678 > #ifdef DOBJ
679 >                dobj_render();
680 > #endif
681 >                popright();
682 > #endif
683 >                                        /* redraw mesh */
684 >                smUpdate(&odev.v, qlevel);
685 > #ifdef DOBJ
686 >                dobj_render();          /* redraw object */
687 > #endif
688                  glFlush();
689 +                                        /* figure out good quality level */
690 +                if (!nframes++) {               /* ignore first frame */
691 +                        lasttime = time(0);
692 +                        continue;
693 +                }
694 +                thistime = time(0);
695 +                if (thistime - lasttime >= 6 ||
696 +                                nframes > (int)(3*3*TARGETFPS)) {
697 +                        qlevel = thistime<=lasttime ? 3*MAXQUALITY :
698 +                                (int)((double)nframes/(thistime-lasttime)
699 +                                        / TARGETFPS * qlevel + 0.5);
700 +                        nframes = 0;
701 +                        if (qlevel >= MAXQUALITY) {
702 +                                if (qlevel >= 3*MAXQUALITY) {   /* brakes!! */
703 +                                        sleep(1);
704 +                                        lasttime++;
705 +                                }
706 +                                qlevel = MAXQUALITY-1;
707 +                        } else if (qlevel < 1)
708 +                                qlevel = 1;
709 +                }
710          }
711          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
712                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 529 | Line 714 | XButtonPressedEvent    *ebut;
714                  wy = levptr(XButtonReleasedEvent)->y;
715                  moveview(wx, odev.vres-1-wy, movdir, movorb);
716          }
717 <        dev_flush();
717 > }
718  
719 <        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
719 >
720 > static
721 > setglpersp(vp)                  /* set perspective view in GL */
722 > register VIEW   *vp;
723 > {
724 >        double  d, xmin, xmax, ymin, ymax;
725 >
726 >        if (mindpth >= maxdpth) {
727 >                dev_zmin = 1.;
728 >                dev_zmax = 100.;
729 >        } else {
730 >                dev_zmin = 0.1*mindpth;
731 >                dev_zmax = 5.0*maxdpth;
732 >        }
733 >        if (odev.v.vfore > FTINY)
734 >                dev_zmin = odev.v.vfore;
735 >        if (odev.v.vaft > FTINY)
736 >                dev_zmax = odev.v.vaft;
737 >        if (dev_zmin*500. < dev_zmax)
738 >                dev_zmax = dev_zmin*500.;
739 >        xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
740 >        xmin = -xmax;
741 >        d = odev.v.hoff * (xmax - xmin);
742 >        xmin += d; xmax += d;
743 >        ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
744 >        ymin = -ymax;
745 >        d = odev.v.voff * (ymax - ymin);
746 >        ymin += d; ymax += d;
747 >                                        /* set view matrix */
748 >        glMatrixMode(GL_PROJECTION);
749 >        glLoadIdentity();
750 >        glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
751 >        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
752 >                odev.v.vp[0] + odev.v.vdir[0],
753 >                odev.v.vp[1] + odev.v.vdir[1],
754 >                odev.v.vp[2] + odev.v.vdir[2],
755 >                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
756 >        checkglerr("setting perspective view");
757   }
758  
759  
760   static
761 + wipeclean(tmflag)                       /* prepare for redraw */
762 + int     tmflag;
763 + {
764 +                                        /* clear depth buffer */
765 + #ifdef STEREO
766 +        setstereobuf(STEREO_BUFFER_RIGHT);
767 +        glClear(GL_DEPTH_BUFFER_BIT);
768 +        setstereobuf(STEREO_BUFFER_LEFT);
769 + #endif
770 +        glClear(GL_DEPTH_BUFFER_BIT);
771 +        smClean(tmflag);                /* reset drawing routines */
772 +        setglpersp(&odev.v);            /* reset view & clipping planes */
773 + }
774 +
775 +
776 + static
777   getkey(ekey)                            /* get input key */
778   register XKeyPressedEvent  *ekey;
779   {
780 +        Window  rootw, childw;
781 +        int     rootx, rooty, wx, wy;
782 +        unsigned int    statemask;
783          int  n;
784          char    buf[8];
785  
# Line 561 | Line 802 | register XKeyPressedEvent  *ekey;
802          case 'v':                       /* spit out view */
803                  inpresflags |= DFL(DC_GETVIEW);
804                  return;
805 +        case 'f':                       /* frame view position */
806 +                if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
807 +                                &rootx, &rooty, &wx, &wy, &statemask))
808 +                        return;         /* on another screen */
809 +                sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres,
810 +                                1.-(wy+.5)/odev.vres);
811 +                inpresflags |= DFL(DC_FOCUS);
812 +                return;
813 +        case 'F':                       /* unfocus */
814 +                odev_args[0] = '\0';
815 +                inpresflags |= DFL(DC_FOCUS);
816 +                return;
817          case '\n':
818          case '\r':                      /* resume computation */
819                  inpresflags |= DFL(DC_RESUME);
820                  return;
821          case CTRL('R'):                 /* redraw screen */
822 <                if (nxtzmax > FTINY) {
570 <                        curzmax = nxtzmax;
571 <                        nxtzmax = 0.;
572 <                }
573 <                glClear(GL_DEPTH_BUFFER_BIT);
574 <                qtRedraw(0, 0, odev.hres, odev.vres);
822 >                wipeclean(1);
823                  return;
824          case CTRL('L'):                 /* refresh from server */
825                  if (inpresflags & DFL(DC_REDRAW))
826                          return;
827 <                if (nxtzmax > FTINY) {
580 <                        curzmax = nxtzmax;
581 <                        nxtzmax = 0.;
582 <                }
827 >                setglpersp(&odev.v);            /* reset clipping planes */
828                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
829 <                draw_grids();
829 >                glDisable(GL_DEPTH_TEST);       /* so grids will clear */
830 >                draw_grids(1);
831 > #ifdef STEREO
832 >                pushright();
833 >                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
834 >                draw_grids(1);
835 >                popright();
836 > #endif
837 >                glEnable(GL_DEPTH_TEST);
838                  glFlush();
839 <                qtCompost(100);                 /* get rid of old values */
839 >                smInit(rsL.max_samp);           /* get rid of old values */
840                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
841 +                rayqleft = 0;                   /* hold off update */
842                  return;
843          case 'K':                       /* kill rtrace process(es) */
844                  inpresflags |= DFL(DC_KILL);
# Line 609 | Line 863 | static
863   fixwindow(eexp)                         /* repair damage to window */
864   register XExposeEvent  *eexp;
865   {
866 <        int     xmin, xmax, ymin, ymax;
613 <
614 <        if (odev.hres == 0 || odev.vres == 0)   /* first exposure */
866 >        if (odev.hres == 0 | odev.vres == 0) {  /* first exposure */
867                  resizewindow((XConfigureEvent *)eexp);
868 <        xmin = eexp->x; xmax = eexp->x + eexp->width;
869 <        ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
870 <                                                /* clear portion of depth */
871 <        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
872 <        glDepthFunc(GL_ALWAYS);
621 <        glBegin(GL_POLYGON);
622 <        glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.);
623 <        glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.);
624 <        glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.);
625 <        glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.);
626 <        glEnd();
627 <        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
628 <        glDepthFunc(GL_LEQUAL);
629 <        qtRedraw(xmin, ymin, xmax, ymax);
868 >                return;
869 >        }
870 >        if (eexp->count)                /* wait for final exposure */
871 >                return;
872 >        wipeclean(0);                   /* clear depth */
873   }
874  
875  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines