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.18 by gwlarson, Wed Jun 17 15:44:07 1998 UTC vs.
Revision 3.19 by gwlarson, Wed Aug 19 17:45:58 1998 UTC

# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ SGI";
6  
7   /*
8   * OpenGL GLX driver for holodeck display.
9 < * Based on x11 driver.
9 > * Based on old GLX driver using cones.
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 < #include "rhd_qtree.h"
24 > #include "rhd_sample.h"
25  
26 < #include  <GL/glx.h>
26 > #include <sys/types.h>
27 > #include <GL/glx.h>
28 > #include <GL/glu.h>
29 > #ifdef STEREO
30 > #include <X11/extensions/SGIStereo.h>
31 > #endif
32 > #ifdef DOBJ
33 > #include "rhdobj.h"
34 > #endif
35  
36 < #include  "x11icon.h"
36 > #include "x11icon.h"
37  
38   #ifndef RAYQLEN
39 < #define RAYQLEN         50000           /* max. rays to queue before flush */
39 > #define RAYQLEN         250             /* max. rays to queue before flush */
40   #endif
41  
42   #ifndef FEQ
43   #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
44   #endif
45  
27 #ifndef MAXCONE
28 #define MAXCONE         16              /* number of different cone sizes */
29 #endif
30 #ifndef MAXVERT
31 #define MAXVERT         64              /* maximum number of cone vertices */
32 #endif
33 #ifndef MINVERT
34 #define MINVERT         4               /* minimum number of cone vertices */
35 #endif
36 #ifndef DEPTHFACT
37 #define DEPTHFACT       16.             /* multiplier for depth tests */
38 #endif
39
46   #define GAMMA           1.4             /* default gamma correction */
47  
48   #define MOVPCT          7               /* percent distance to move /frame */
# Line 44 | Line 50 | static char SCCSid[] = "$SunId$ SGI";
50   #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */
51   #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
52  
53 + #ifndef TARGETFPS
54 + #define TARGETFPS       4.0             /* target frames/sec during motion */
55 + #endif
56 +
57   #define MINWIDTH        480             /* minimum graphics window width */
58   #define MINHEIGHT       400             /* minimum graphics window height */
59  
# Line 51 | Line 61 | static char SCCSid[] = "$SunId$ SGI";
61  
62   #define BORWIDTH        5               /* border width */
63  
64 + #define setstereobuf(bid)       (glXWaitGL(), \
65 +                                XSGISetStereoBuffer(ourdisplay, gwind, bid), \
66 +                                glXWaitX())
67 +
68   #define  ourscreen      DefaultScreen(ourdisplay)
69   #define  ourroot        RootWindow(ourdisplay,ourscreen)
70   #define  ourmask        (StructureNotifyMask|ExposureMask|KeyPressMask|\
# Line 60 | Line 74 | static char SCCSid[] = "$SunId$ SGI";
74  
75   struct driver   odev;                   /* global device driver structure */
76  
77 + static VIEW     vwright;                /* right eye view */
78 +
79 + static int      rayqleft = 0;           /* rays left to queue before flush */
80 +
81   static XEvent  currentevent;            /* current event */
82  
83   static int  mapped = 0;                 /* window is mapped? */
# Line 72 | Line 90 | static GLXContext      gctx;           /* our GLX context */
90  
91   static double   pwidth, pheight;        /* pixel dimensions (mm) */
92  
93 < static double   curzmax = 1e4;          /* current depth upper limit */
76 < static double   nxtzmax = 0.;           /* maximum (finite) depth so far */
93 > static double   mindpth, maxdpth;       /* min. and max. depth */
94  
95 < static struct {
79 <        double  rad;            /* cone radius */
80 <        int     nverts;         /* number of vertices */
81 <        FVECT   *va;            /* allocated vertex array */
82 < } cone[MAXCONE];        /* precomputed cones for drawing */
95 > double  dev_zmin, dev_zmax;             /* fore and aft clipping plane dist. */
96  
97   static int      inpresflags;            /* input result flags */
98  
99   static int      headlocked = 0;         /* lock vertical motion */
100  
101 < static int  resizewindow(), getevent(), getkey(), moveview(),
102 <                initcones(), freecones(),
90 <                getmove(), fixwindow(), mytmflags();
101 > static int  resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
102 >                setglpersp(), getmove(), fixwindow(), mytmflags();
103  
104 + #ifdef STEREO
105 + static int  pushright(), popright();
106 + #endif
107  
108 < dev_open(id)                    /* initialize X11 driver */
108 > extern time_t   time();
109 >
110 >
111 > dev_open(id)                    /* initialize GLX driver */
112   char  *id;
113   {
114          extern char     *getenv();
# Line 104 | Line 122 | char  *id;
122          XSetWindowAttributes    ourwinattr;
123          XWMHints        ourxwmhints;
124          XSizeHints      oursizhints;
125 <                                        /* set quadtree globals */
126 <        qtMinNodesiz = 3;
127 <        qtDepthEps = 0.07;
125 >                                        /* check for unsupported stereo */
126 > #ifdef NOSTEREO
127 >        error(USER, "stereo display driver unavailable");
128 > #endif
129                                          /* open display server */
130          ourdisplay = XOpenDisplay(NULL);
131          if (ourdisplay == NULL)
132                  error(USER, "cannot open X-windows; DISPLAY variable set?\n");
133 + #ifdef STEREO
134 +        switch (XSGIQueryStereoMode(ourdisplay, ourroot)) {
135 +        case STEREO_TOP:
136 +        case STEREO_BOTTOM:
137 +                break;
138 +        case STEREO_OFF:
139 +                error(USER,
140 +        "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
141 +        case X_STEREO_UNSUPPORTED:
142 +                error(USER, "stereo mode not supported on this screen");
143 +        default:
144 +                error(INTERNAL, "unknown stereo mode");
145 +        }
146 + #endif
147                                          /* find a usable visual */
148          ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
149          if (ourvinf == NULL)
# Line 139 | Line 172 | char  *id;
172                                  ourvinf->visual, AllocNone);
173          gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
174                  DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
175 + #ifdef STEREO
176 +                (DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2,
177 + #else
178                  DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
179 + #endif
180                  BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
181                  CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
182          if (gwind == 0)
# Line 152 | Line 189 | char  *id;
189                          gwind, x11icon_bits, x11icon_width, x11icon_height);
190          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
191          oursizhints.min_width = MINWIDTH;
192 + #ifdef STEREO
193 +        oursizhints.min_height = MINHEIGHT/2;
194 +        oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH;
195 +        oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) -
196 +                                        2*BORWIDTH)/2;
197 +        oursizhints.flags = PMinSize|PMaxSize;
198 + #else
199          oursizhints.min_height = MINHEIGHT;
200          oursizhints.flags = PMinSize;
201 + #endif
202          XSetNormalHints(ourdisplay, gwind, &oursizhints);
203                                          /* set GLX context */
204          glXMakeCurrent(ourdisplay, gwind, gctx);
205          glEnable(GL_DEPTH_TEST);
206          glDepthFunc(GL_LEQUAL);
207 <        glShadeModel(GL_FLAT);
207 >        glShadeModel(GL_SMOOTH);
208          glDisable(GL_DITHER);
209          glDisable(GL_CULL_FACE);
165        glMatrixMode(GL_PROJECTION);
166        glOrtho(0., 1., 0., 1., -.01, 1.01);
167        glTranslated(0., 0., -1.01);
210                                          /* figure out sensible view */
211          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
212                          DisplayWidth(ourdisplay, ourscreen);
213          pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
214                          DisplayHeight(ourdisplay, ourscreen);
215 + #ifdef STEREO
216 +        pheight *= 2.;
217 +        setstereobuf(STEREO_BUFFER_LEFT);
218 + #endif
219          copystruct(&odev.v, &stdview);
220          odev.v.type = VT_PER;
221                                          /* map the window */
222          XMapWindow(ourdisplay, gwind);
223          dev_input();                    /* sets size and view angles */
224 <                                        /* allocate our leaf pile */
225 <        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
226 <                        DisplayHeight(ourdisplay,ourscreen) * 3 /
181 <                        (qtMinNodesiz*qtMinNodesiz*2)))
224 >                                        /* allocate our samples */
225 >        if (!smInit(DisplayWidth(ourdisplay,ourscreen) *
226 >                        DisplayHeight(ourdisplay,ourscreen) / 10))
227                  error(SYSTEM, "insufficient memory for value storage");
228 +        mindpth = FHUGE; maxdpth = FTINY;
229          odev.name = id;
230          odev.ifd = ConnectionNumber(ourdisplay);
185                                        /* initialize cone array */
186        initcones();
231   }
232  
233  
234   dev_close()                     /* close our display and free resources */
235   {
236 +        smInit(0);
237 + #ifdef DOBJ
238 +        dobj_cleanup();
239 + #endif
240          glXMakeCurrent(ourdisplay, None, NULL);
241          glXDestroyContext(ourdisplay, gctx);
242          XDestroyWindow(ourdisplay, gwind);
243          gwind = 0;
244          XCloseDisplay(ourdisplay);
245          ourdisplay = NULL;
198        qtFreeLeaves();
246          tmDone(NULL);
200        freecones();
247          odev.v.type = 0;
248          odev.hres = odev.vres = 0;
249          odev.ifd = -1;
250   }
251  
252  
253 < dev_clear()                     /* clear our quadtree */
253 > dev_clear()                     /* clear our representation */
254   {
255 <        qtCompost(100);
256 <        glClear(GL_DEPTH_BUFFER_BIT);
255 >        smInit(rsL.max_samp);
256 >        wipeclean();
257          rayqleft = 0;                   /* hold off update */
258   }
259  
# Line 216 | Line 262 | int
262   dev_view(nv)                    /* assign new driver view */
263   register VIEW   *nv;
264   {
265 <        if (nv->type == VT_PAR ||               /* check view legality */
265 >        double  d;
266 >
267 >        if (nv->type != VT_PER ||               /* check view legality */
268                          nv->horiz > 160. || nv->vert > 160.) {
269                  error(COMMAND, "illegal view type/angle");
270                  nv->type = odev.v.type;
# Line 224 | Line 272 | register VIEW  *nv;
272                  nv->vert = odev.v.vert;
273                  return(0);
274          }
227        if (nv->vfore > FTINY) {
228                error(COMMAND, "cannot handle fore clipping");
229                nv->vfore = 0.;
230                return(0);
231        }
275          if (nv != &odev.v) {
276                  if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
277                                  !FEQ(nv->vert,odev.v.vert)) {
# Line 237 | Line 280 | register VIEW  *nv;
280  
281                          dw -= 25;       /* for window frame */
282                          dh -= 50;
283 + #ifdef STEREO
284 +                        dh /= 2;
285 + #endif
286                          odev.hres = 2.*VIEWDIST/pwidth *
287                                          tan(PI/180./2.*nv->horiz);
288                          odev.vres = 2.*VIEWDIST/pheight *
# Line 252 | Line 298 | register VIEW  *nv;
298                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
299                          dev_input();    /* get resize event */
300                  }
301 <                copystruct(&odev.v, nv);
301 >                copystruct(&odev.v, nv);        /* setview() already called */
302 >                setglpersp(&odev.v);
303 > #ifdef STEREO
304 >                copystruct(&vwright, nv);
305 >                d = eyesepdist / sqrt(nv->hn2);
306 >                VSUM(vwright.vp, nv->vp, nv->hvec, d);
307 >                /* setview(&vwright);   -- Unnecessary */
308 > #endif
309 >                checkglerr("setting view");
310          }
311 <        if (nxtzmax > FTINY) {
258 <                curzmax = nxtzmax;
259 <                nxtzmax = 0.;
260 <        }
261 <        glClear(GL_DEPTH_BUFFER_BIT);
262 <        qtReplant();
311 >        wipeclean();
312          return(1);
313   }
314  
# Line 267 | Line 316 | register VIEW  *nv;
316   dev_auxcom(cmd, args)           /* process an auxiliary command */
317   char    *cmd, *args;
318   {
319 + #ifdef DOBJ
320 +        if (dobj_command(cmd, args) >= 0)
321 +                return;
322 + #endif
323          sprintf(errmsg, "%s: unknown command", cmd);
324          error(COMMAND, errmsg);
325   }
# Line 277 | Line 330 | dev_auxview(n, hvres)          /* return nth auxiliary view */
330   int     n;
331   int     hvres[2];
332   {
280        if (n)
281                return(NULL);
333          hvres[0] = odev.hres; hvres[1] = odev.vres;
334 <        return(&odev.v);
334 >        if (n == 0)
335 >                return(&odev.v);
336 > #ifdef STEREO
337 >        if (n == 1)
338 >                return(&vwright);
339 > #endif
340 >        return(NULL);
341   }
342  
343  
# Line 292 | Line 349 | dev_input()                    /* get X11 input */
349          do
350                  getevent();
351  
352 <        while (XQLength(ourdisplay) > 0);
352 >        while (XPending(ourdisplay) > 0);
353  
354 +        odev.inpready = 0;
355 +
356          return(inpresflags);
357   }
358  
359  
360 + dev_value(c, d, p)              /* add a pixel value to our mesh */
361 + COLR    c;
362 + FVECT   d, p;
363 + {
364 +        double  depth;
365 + #ifdef DOBJ
366 +        if (dobj_lightsamp != NULL) {   /* in light source sampling */
367 +                (*dobj_lightsamp)(c, d, p);
368 +                return;
369 +        }
370 + #endif
371 +        smNewSamp(c, d, p);             /* add to display representation */
372 +        if (p != NULL) {
373 +                depth = (p[0] - odev.v.vp[0])*d[0] +
374 +                        (p[1] - odev.v.vp[1])*d[1] +
375 +                        (p[2] - odev.v.vp[2])*d[2];
376 +                if (depth > FTINY) {
377 +                        if (depth < mindpth)
378 +                                mindpth = depth;
379 +                        if (depth > maxdpth)
380 +                                maxdpth = depth;
381 +                }
382 +        }
383 +        if (!--rayqleft)
384 +                dev_flush();            /* flush output */
385 + }
386 +
387 +
388   int
389   dev_flush()                     /* flush output */
390   {
391 <        qtUpdate();
392 <        glFlush();
391 > #ifdef STEREO
392 >        pushright();                    /* update right eye */
393 >        glClear(GL_DEPTH_BUFFER_BIT);
394 >        smUpdate(&vwright, 100);
395 > #ifdef DOBJ
396 >        dobj_render();                  /* usually in foreground */
397 > #endif
398 >        popright();                     /* update left eye */
399 >        glClear(GL_DEPTH_BUFFER_BIT);
400 > #endif
401 >        smUpdate(&odev.v, 100);
402 > #ifdef DOBJ
403 >        dobj_render();
404 > #endif
405 >        glFlush();                      /* flush OGL */
406 >        checkglerr("flushing display");
407          rayqleft = RAYQLEN;
408 <        return(XPending(ourdisplay));
408 >                                        /* flush X11 and return # pending */
409 >        return(odev.inpready = XPending(ourdisplay));
410   }
411  
412  
413 < dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */
414 < BYTE    rgb[3];
313 < FVECT   ip;
314 < double  rad;
413 > checkglerr(where)               /* check for GL or GLU error */
414 > char    *where;
415   {
416 <        register int    ci, j;
417 <        double  apexh, basez;
418 <                                        /* is window mapped? */
419 <        if (!mapped)
420 <                return;
421 <                                        /* compute apex height (0. to 1.) */
322 <        if (ip[2] > 1e6)
323 <                apexh = 1. - 1./DEPTHFACT;
324 <        else {
325 <                if (ip[2] > nxtzmax)
326 <                        nxtzmax = ip[2];
327 <                if (ip[2] >= curzmax)
328 <                        apexh = 1. - 1./DEPTHFACT;
329 <                else
330 <                        apexh = 1. - ip[2]/(curzmax*DEPTHFACT);
416 >        register GLenum errcode;
417 >
418 >        while ((errcode = glGetError()) != GL_NO_ERROR) {
419 >                sprintf(errmsg, "OpenGL error %s: %s",
420 >                                where, gluErrorString(errcode));
421 >                error(WARNING, errmsg);
422          }
332        rad *= 1.25;                    /* find conservative cone match */
333        for (ci = 0; ci < MAXCONE-1; ci++)
334                if (cone[ci].rad >= rad)
335                        break;
336                                        /* draw it */
337        glColor3ub(rgb[0], rgb[1], rgb[2]);
338        glBegin(GL_TRIANGLE_FAN);
339        glVertex3d(ip[0], ip[1], apexh);        /* start with apex */
340        basez = apexh*cone[ci].va[0][2];        /* base z's all the same */
341        for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */
342                glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1],
343                                basez);
344                                                /* connect last to first */
345        glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez);
346        glEnd();                                /* all done */
423   }
424  
425  
426 + #ifdef STEREO
427 + static
428 + pushright()                     /* push on right view */
429 + {
430 +        double  d;
431 +
432 +        setstereobuf(STEREO_BUFFER_RIGHT);
433 +        glMatrixMode(GL_MODELVIEW);
434 +        glPushMatrix();
435 +        d = -eyesepdist / sqrt(odev.v.hn2);
436 +        glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]);
437 + }
438 +
439 +
440 + static
441 + popright()                      /* pop off right view */
442 + {
443 +        glMatrixMode(GL_MODELVIEW);
444 +        glPopMatrix();
445 +        setstereobuf(STEREO_BUFFER_LEFT);
446 + }
447 + #endif
448 +
449 +
450   static int
451   mytmflags()                     /* figure out tone mapping flags */
452   {
# Line 358 | Line 458 | mytmflags()                    /* figure out tone mapping flags */
458                          tail = cp+1;
459          for (cp = tail; *cp && *cp != '.'; cp++)
460                  ;
461 <        if (cp-tail == 3 && !strncmp(tail, "glx", 3))
362 <                return(TM_F_CAMERA|TM_F_NOSTDERR);
363 <        if (cp-tail == 4 && !strncmp(tail, "glxh", 4))
461 >        if (cp > tail && cp[-1] == 'h')
462                  return(TM_F_HUMAN|TM_F_NOSTDERR);
463 <        error(USER, "illegal driver name");
463 >        else
464 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
465   }
466  
467  
468   static
370 initcones()                     /* initialize cone vertices */
371 {
372        register int    i, j;
373        double  minrad, d;
374
375        if (cone[0].nverts)
376                freecones();
377        minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) +
378                                        DisplayHeight(ourdisplay,ourscreen));
379        for (i = 0; i < MAXCONE; i++) {
380                d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */
381                cone[i].rad = minrad + (1.-minrad)*d;
382                cone[i].nverts = MINVERT + 0.5 + (MAXVERT-MINVERT)*d;
383                cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT));
384                if (cone[i].va == NULL)
385                        error(SYSTEM, "out of memory in initcones");
386                for (j = cone[i].nverts; j--; ) {
387                        d = 2.*PI * (j+.5) / (cone[i].nverts);
388                        cone[i].va[j][0] = cos(d) * cone[i].rad;
389                        cone[i].va[j][1] = sin(d) * cone[i].rad;
390                        cone[i].va[j][2] = 1. - cone[i].rad;
391                }
392        }
393 }
394
395
396 static
397 freecones()                     /* free cone vertices */
398 {
399        register int    i;
400
401        for (i = MAXCONE; i--; )
402                if (cone[i].nverts) {
403                        free((char *)cone[i].va);
404                        cone[i].va = NULL;
405                        cone[i].nverts = 0;
406                }
407 }
408
409
410 static
469   getevent()                      /* get next event */
470   {
471          XNextEvent(ourdisplay, levptr(XEvent));
# Line 447 | Line 505 | static
505   draw_grids()                    /* draw holodeck section grids */
506   {
507          static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
508 <        double  xmin, xmax, ymin, ymax, zmin, zmax;
509 <        double  d, cx, sx, crad;
452 <        FVECT   vx, vy;
453 <        register int    i, j;
454 <                                        /* can we even do it? */
455 <        if (!mapped || odev.v.type != VT_PER)
508 >
509 >        if (!mapped)
510                  return;
457                                        /* compute view frustum */
458        if (normalize(odev.v.vdir) == 0.0)
459                return;
460        zmin = 0.01;
461        zmax = 10000.;
462        if (odev.v.vfore > FTINY)
463                zmin = odev.v.vfore;
464        if (odev.v.vaft > FTINY)
465                zmax = odev.v.vaft;
466        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
467        xmin = -xmax;
468        d = odev.v.hoff * (xmax - xmin);
469        xmin += d; xmax += d;
470        ymax = zmin * tan(PI/180./2. * odev.v.vert);
471        ymin = -ymax;
472        d = odev.v.voff * (ymax - ymin);
473        ymin += d; ymax += d;
474                                        /* set view matrix */
475        glMatrixMode(GL_PROJECTION);
476        glPushMatrix();
477        glLoadIdentity();
478        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
479        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
480                odev.v.vp[0] + odev.v.vdir[0],
481                odev.v.vp[1] + odev.v.vdir[1],
482                odev.v.vp[2] + odev.v.vdir[2],
483                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
484        glDisable(GL_DEPTH_TEST);       /* write no depth values */
511          glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
512          glBegin(GL_LINES);              /* draw each grid line */
513          gridlines(draw3dline);
514          glEnd();
489        glEnable(GL_DEPTH_TEST);        /* restore rendering params */
490        glPopMatrix();
515   }
516  
517  
# Line 496 | Line 520 | moveview(dx, dy, mov, orb)     /* move our view */
520   int     dx, dy, mov, orb;
521   {
522          VIEW    nv;
523 <        FVECT   odir, v1;
523 >        FVECT   odir, v1, wip;
524          double  d;
525          register int    li;
526                                  /* start with old view */
527          copystruct(&nv, &odev.v);
528                                  /* change view direction */
529 +        if (viewray(v1, odir, &odev.v,
530 +                        (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
531 +                return(0);              /* outside view */
532          if (mov | orb) {
533 <                if ((li = qtFindLeaf(dx, dy)) < 0)
533 > #ifdef DOBJ
534 >                d = dobj_trace(v1, odir);       /* check objects */
535 >                                                /* is holodeck in front? */
536 >                if ((li = smFindSamp(v1, odir)) >= 0 &&
537 >                                (rsL.wp[li][0] - nv.vp[0])*odir[0] +
538 >                                (rsL.wp[li][1] - nv.vp[1])*odir[1] +
539 >                                (rsL.wp[li][2] - nv.vp[2])*odir[2] < d)
540 >                        VCOPY(wip, rsL.wp[li]);
541 >                else if (d < .99*FHUGE)         /* object is closer */
542 >                        VSUM(wip, nv.vp, odir, d);
543 >                else                            /* nothing visible */
544 >                        return(0);
545 > #else
546 >                if ((li = smFindSamp(v1, odir)) < 0)
547                          return(0);      /* not on window */
548 <                VSUM(odir, qtL.wp[li], nv.vp, -1.);
549 <        } else {
550 <                if (viewray(nv.vp, nv.vdir, &odev.v,
551 <                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
552 <                        return(0);      /* outside view */
513 <        }
548 >                VCOPY(wip, rsL.wp[li]);
549 > #endif
550 >                VSUM(odir, wip, nv.vp, -1.);
551 >        } else
552 >                VCOPY(nv.vdir, odir);
553          if (orb && mov) {               /* orbit left/right */
554                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
555 <                VSUM(nv.vp, qtL.wp[li], odir, -1.);
555 >                VSUM(nv.vp, wip, odir, -1.);
556                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
557          } else if (orb) {               /* orbit up/down */
558                  fcross(v1, odir, nv.vup);
559                  if (normalize(v1) == 0.)
560                          return(0);
561                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
562 <                VSUM(nv.vp, qtL.wp[li], odir, -1.);
562 >                VSUM(nv.vp, wip, odir, -1.);
563                  spinvector(nv.vdir, nv.vdir, v1, d);
564          } else if (mov) {               /* move forward/backward */
565                  d = MOVPCT/100. * mov;
# Line 545 | Line 584 | XButtonPressedEvent    *ebut;
584   {
585          int     movdir = MOVDIR(ebut->button);
586          int     movorb = MOVORB(ebut->state);
587 <        int     oldnodesiz = qtMinNodesiz;
587 >        int     qlevel = 99;
588 >        time_t  lasttime, thistime;
589 >        int     nframes;
590          Window  rootw, childw;
591          int     rootx, rooty, wx, wy;
592          unsigned int    statemask;
593  
553        qtMinNodesiz = 24;              /* accelerate update rate */
594          XNoOp(ourdisplay);
595  
596 +        lasttime = time(0); nframes = 0;
597          while (!XCheckMaskEvent(ourdisplay,
598                          ButtonReleaseMask, levptr(XEvent))) {
599  
# Line 562 | Line 603 | XButtonPressedEvent    *ebut;
603  
604                  if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
605                          sleep(1);
606 +                        lasttime++;
607                          continue;
608                  }
609 + #ifdef STEREO
610 +                pushright();
611                  glClear(GL_COLOR_BUFFER_BIT);
568                qtUpdate();
612                  draw_grids();
613 +                smUpdate(&vwright, qlevel);
614 + #ifdef DOBJ
615 +                dobj_render();
616 + #endif
617 +                popright();
618 + #endif
619 +                glClear(GL_COLOR_BUFFER_BIT);
620 +                draw_grids();
621 +                smUpdate(&odev.v, qlevel);
622 + #ifdef DOBJ
623 +                dobj_render();
624 + #endif
625                  glFlush();
626 +                checkglerr("moving view");
627 +                nframes++;
628 +                thistime = time(0);
629 +                if (thistime - lasttime >= 3 ||
630 +                                nframes > (int)(3*3*TARGETFPS)) {
631 +                        qlevel = thistime<=lasttime ? 1000 :
632 +                                (int)((double)nframes/(thistime-lasttime)
633 +                                        / TARGETFPS * qlevel + 0.5);
634 +                        lasttime = thistime; nframes = 0;
635 +                        if (qlevel > 99) {
636 +                                if (qlevel > 300) {     /* put on the brakes */
637 +                                        sleep(1);
638 +                                        lasttime++;
639 +                                }
640 +                                qlevel = 99;
641 +                        } else if (qlevel < 1)
642 +                                qlevel = 1;
643 +                }
644          }
645          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
646                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 575 | Line 648 | XButtonPressedEvent    *ebut;
648                  wy = levptr(XButtonReleasedEvent)->y;
649                  moveview(wx, odev.vres-1-wy, movdir, movorb);
650          }
651 <        dev_flush();
651 > }
652  
653 <        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
653 >
654 > static
655 > setglpersp(vp)                  /* set perspective view in GL */
656 > register VIEW   *vp;
657 > {
658 >        double  d, xmin, xmax, ymin, ymax;
659 >
660 >        if (mindpth >= maxdpth) {
661 >                dev_zmin = 0.1;
662 >                dev_zmax = 100.;
663 >        } else {
664 >                dev_zmin = 0.5*mindpth;
665 >                dev_zmax = 1.5*maxdpth;
666 >                if (dev_zmin > dev_zmax/100.)
667 >                        dev_zmin = dev_zmax/100.;
668 >        }
669 >        if (odev.v.vfore > FTINY)
670 >                dev_zmin = odev.v.vfore;
671 >        if (odev.v.vaft > FTINY)
672 >                dev_zmax = odev.v.vaft;
673 >        if (dev_zmin < dev_zmax/5000.)
674 >                dev_zmin = dev_zmax/5000.;
675 >        xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
676 >        xmin = -xmax;
677 >        d = odev.v.hoff * (xmax - xmin);
678 >        xmin += d; xmax += d;
679 >        ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
680 >        ymin = -ymax;
681 >        d = odev.v.voff * (ymax - ymin);
682 >        ymin += d; ymax += d;
683 >                                        /* set view matrix */
684 >        glMatrixMode(GL_PROJECTION);
685 >        glLoadIdentity();
686 >        glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
687 >        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
688 >                odev.v.vp[0] + odev.v.vdir[0],
689 >                odev.v.vp[1] + odev.v.vdir[1],
690 >                odev.v.vp[2] + odev.v.vdir[2],
691 >                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
692   }
693  
694  
695   static
696 + wipeclean()                     /* prepare for redraw */
697 + {
698 + #ifdef STEREO
699 +        setstereobuf(STEREO_BUFFER_RIGHT);
700 +        glClear(GL_DEPTH_BUFFER_BIT);
701 +        setstereobuf(STEREO_BUFFER_LEFT);
702 + #endif
703 +        glClear(GL_DEPTH_BUFFER_BIT);
704 +        smClean();
705 + }
706 +
707 +
708 + static
709   getkey(ekey)                            /* get input key */
710   register XKeyPressedEvent  *ekey;
711   {
# Line 612 | Line 736 | register XKeyPressedEvent  *ekey;
736                  inpresflags |= DFL(DC_RESUME);
737                  return;
738          case CTRL('R'):                 /* redraw screen */
739 <                if (nxtzmax > FTINY) {
616 <                        curzmax = nxtzmax;
617 <                        nxtzmax = 0.;
618 <                }
619 <                glClear(GL_DEPTH_BUFFER_BIT);
620 <                qtRedraw(0, 0, odev.hres, odev.vres);
739 >                wipeclean();
740                  return;
741          case CTRL('L'):                 /* refresh from server */
742                  if (inpresflags & DFL(DC_REDRAW))
743                          return;
625                if (nxtzmax > FTINY) {
626                        curzmax = nxtzmax;
627                        nxtzmax = 0.;
628                }
744                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
745 +                glDisable(GL_DEPTH_TEST);
746                  draw_grids();
747 + #ifdef STEREO
748 +                pushright();
749 +                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
750 +                draw_grids();
751 +                popright();
752 + #endif
753 +                glEnable(GL_DEPTH_TEST);
754                  glFlush();
755 <                qtCompost(100);                 /* get rid of old values */
755 >                smInit(rsL.max_samp);           /* get rid of old values */
756                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
757                  rayqleft = 0;                   /* hold off update */
758                  return;
# Line 656 | Line 779 | static
779   fixwindow(eexp)                         /* repair damage to window */
780   register XExposeEvent  *eexp;
781   {
782 <        int     xmin, xmax, ymin, ymax;
660 <
661 <        if (odev.hres == 0 || odev.vres == 0)   /* first exposure */
782 >        if (odev.hres == 0 | odev.vres == 0) {  /* first exposure */
783                  resizewindow((XConfigureEvent *)eexp);
784 <        xmin = eexp->x; xmax = eexp->x + eexp->width;
785 <        ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
786 <                                                /* clear portion of depth */
787 <        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
788 <        glDepthFunc(GL_ALWAYS);
668 <        glBegin(GL_POLYGON);
669 <        glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.);
670 <        glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.);
671 <        glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.);
672 <        glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.);
673 <        glEnd();
674 <        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
675 <        glDepthFunc(GL_LEQUAL);
676 <        qtRedraw(xmin, ymin, xmax, ymax);
784 >                return;
785 >        }
786 >        if (eexp->count)                /* wait for final exposure */
787 >                return;
788 >        wipeclean();                    /* clear depth */
789   }
790  
791  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines