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.5 by gregl, Fri Dec 26 14:46:21 1997 UTC vs.
Revision 3.22 by gwlarson, Tue Aug 25 18:11:17 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# 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         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  
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
46   #define GAMMA           1.4             /* default gamma correction */
47  
48   #define MOVPCT          7               /* percent distance to move /frame */
# Line 40 | 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 47 | 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 56 | 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 68 | 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 */
72 < static double   nxtzmax = 0.;           /* maximum (finite) depth so far */
93 > static double   mindpth, maxdpth;       /* min. and max. depth */
94  
95 < 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 */
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(),
86 <                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();
115 +        static RGBPRIMS myprims = STDPRIMS;
116          static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
117                                  GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
118                                  GLX_DEPTH_SIZE,15, None};
119 <        char    *gv;
119 >        char    *ev;
120          double  gamval = GAMMA;
121 +        RGBPRIMP        dpri = stdprims;
122          XSetWindowAttributes    ourwinattr;
123          XWMHints        ourxwmhints;
124          XSizeHints      oursizhints;
125 <                                        /* set quadtree globals */
126 <        qtMinNodesiz = 3;
125 >                                        /* check for unsupported stereo */
126 > #ifdef NOSTEREO
127 >        error(INTERNAL, "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 111 | Line 151 | char  *id;
151                                          /* get a context */
152          gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
153                                          /* set gamma and tone mapping */
154 <        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
155 <                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
156 <                gamval = atof(gv);
157 <        if (tmInit(mytmflags(), stdprims, gamval) == NULL)
154 >        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
155 >                        || (ev = getenv("DISPLAY_GAMMA")) != NULL)
156 >                gamval = atof(ev);
157 >        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
158 >                        sscanf(ev, "%f %f %f %f %f %f %f %f",
159 >                                &myprims[RED][CIEX],&myprims[RED][CIEY],
160 >                                &myprims[GRN][CIEX],&myprims[GRN][CIEY],
161 >                                &myprims[BLU][CIEX],&myprims[BLU][CIEY],
162 >                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
163 >                dpri = myprims;
164 >        if (tmInit(mytmflags(), dpri, gamval) == NULL)
165                  error(SYSTEM, "not enough memory in dev_open");
166                                          /* open window */
167          ourwinattr.background_pixel = ourblack;
# Line 125 | 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 138 | 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);
151        glMatrixMode(GL_PROJECTION);
152        glOrtho(0., 1., 0., 1., -.01, 1.01);
153        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 <        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
225 <                                        /* allocate our leaf pile */
226 <        if (!qtAllocLeaves(2 * DisplayWidth(ourdisplay,ourscreen) *
167 <                        DisplayHeight(ourdisplay,ourscreen) /
168 <                        (qtMinNodesiz*qtMinNodesiz)))
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);
172                                        /* initialize cone array */
173        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;
185        qtFreeLeaves();
246          tmDone(NULL);
187        freecones();
247          odev.v.type = 0;
248          odev.hres = odev.vres = 0;
249          odev.ifd = -1;
250   }
251  
252  
253 + dev_clear()                     /* clear our representation */
254 + {
255 +        smInit(rsL.max_samp);
256 +        wipeclean();
257 +        rayqleft = 0;                   /* hold off update */
258 + }
259 +
260 +
261   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 203 | Line 272 | register VIEW  *nv;
272                  nv->vert = odev.v.vert;
273                  return(0);
274          }
206        if (nv->vfore > FTINY) {
207                error(COMMAND, "cannot handle fore clipping");
208                nv->vfore = 0.;
209                return(0);
210        }
275          if (nv != &odev.v) {
276 <                if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
276 >                                                /* resize window? */
277 >                if (!FEQ(nv->horiz,odev.v.horiz) ||
278                                  !FEQ(nv->vert,odev.v.vert)) {
279                          int     dw = DisplayWidth(ourdisplay,ourscreen);
280                          int     dh = DisplayHeight(ourdisplay,ourscreen);
281  
282                          dw -= 25;       /* for window frame */
283                          dh -= 50;
284 + #ifdef STEREO
285 +                        dh /= 2;
286 + #endif
287                          odev.hres = 2.*VIEWDIST/pwidth *
288                                          tan(PI/180./2.*nv->horiz);
289                          odev.vres = 2.*VIEWDIST/pheight *
# Line 231 | Line 299 | register VIEW  *nv;
299                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
300                          dev_input();    /* get resize event */
301                  }
302 <                copystruct(&odev.v, nv);
302 >                copystruct(&odev.v, nv);        /* setview() already called */
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          }
310 <        if (nxtzmax > FTINY) {
237 <                curzmax = nxtzmax;
238 <                nxtzmax = 0.;
239 <        }
240 <        glClear(GL_DEPTH_BUFFER_BIT);
241 <        qtReplant();
310 >        wipeclean();
311          return(1);
312   }
313  
314  
315 + dev_auxcom(cmd, args)           /* process an auxiliary command */
316 + char    *cmd, *args;
317 + {
318 + #ifdef DOBJ
319 +        if (dobj_command(cmd, args) >= 0)
320 +                return;
321 + #endif
322 +        sprintf(errmsg, "%s: unknown command", cmd);
323 +        error(COMMAND, errmsg);
324 + }
325 +
326 +
327 + VIEW *
328 + dev_auxview(n, hvres)           /* return nth auxiliary view */
329 + int     n;
330 + int     hvres[2];
331 + {
332 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
333 +        if (n == 0)
334 +                return(&odev.v);
335 + #ifdef STEREO
336 +        if (n == 1)
337 +                return(&vwright);
338 + #endif
339 +        return(NULL);
340 + }
341 +
342 +
343   int
344   dev_input()                     /* get X11 input */
345   {
# Line 251 | Line 348 | dev_input()                    /* get X11 input */
348          do
349                  getevent();
350  
351 <        while (XQLength(ourdisplay) > 0);
351 >        while (XPending(ourdisplay) > 0);
352  
353 +        odev.inpready = 0;
354 +
355          return(inpresflags);
356   }
357  
358  
359 + dev_value(c, d, p)              /* add a pixel value to our mesh */
360 + COLR    c;
361 + FVECT   d, p;
362 + {
363 +        double  depth;
364 + #ifdef DOBJ
365 +        if (dobj_lightsamp != NULL) {   /* in light source sampling */
366 +                (*dobj_lightsamp)(c, d, p);
367 +                return;
368 +        }
369 + #endif
370 +        if (p != NULL) {                /* add depth to our range */
371 +                depth = (p[0] - odev.v.vp[0])*d[0] +
372 +                        (p[1] - odev.v.vp[1])*d[1] +
373 +                        (p[2] - odev.v.vp[2])*d[2];
374 +                if (depth > FTINY) {
375 +                        if (depth < mindpth)
376 +                                mindpth = depth;
377 +                        if (depth > maxdpth)
378 +                                maxdpth = depth;
379 +                }
380 +        }
381 +        smNewSamp(c, d, p);             /* add to display representation */
382 +        if (!--rayqleft)
383 +                dev_flush();            /* flush output */
384 + }
385 +
386 +
387   int
388   dev_flush()                     /* flush output */
389   {
390 <        qtUpdate();
391 <        glFlush();
392 <        return(XPending(ourdisplay));
390 >        if (mapped) {
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 >                checkglerr("rendering mesh");
403 > #ifdef DOBJ
404 >                dobj_render();
405 > #endif
406 >                glFlush();                      /* flush OGL */
407 >        }
408 >        rayqleft = RAYQLEN;
409 >                                        /* flush X11 and return # pending */
410 >        return(odev.inpready = XPending(ourdisplay));
411   }
412  
413  
414 < dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */
415 < BYTE    rgb[3];
271 < FVECT   ip;
272 < double  rad;
414 > checkglerr(where)               /* check for GL or GLU error */
415 > char    *where;
416   {
417 <        register int    ci, j;
418 <        double  apexh, basez;
419 <                                        /* compute apex height (0. to 1.) */
420 <        if (ip[2] > 1e6)
421 <                apexh = 1. - 1./DEPTHFACT;
422 <        else {
280 <                if (ip[2] > nxtzmax)
281 <                        nxtzmax = ip[2];
282 <                if (ip[2] >= curzmax)
283 <                        apexh = 1. - 1./DEPTHFACT;
284 <                else
285 <                        apexh = 1. - ip[2]/(curzmax*DEPTHFACT);
417 >        register GLenum errcode;
418 >
419 >        while ((errcode = glGetError()) != GL_NO_ERROR) {
420 >                sprintf(errmsg, "OpenGL error %s: %s",
421 >                                where, gluErrorString(errcode));
422 >                error(WARNING, errmsg);
423          }
287        rad *= 1.25;                    /* find conservative cone match */
288        for (ci = 0; ci < MAXCONE-1; ci++)
289                if (cone[ci].rad >= rad)
290                        break;
291                                        /* draw it */
292        glColor3ub(rgb[0], rgb[1], rgb[2]);
293        glBegin(GL_TRIANGLE_FAN);
294        glVertex3d(ip[0], ip[1], apexh);        /* start with apex */
295        basez = apexh*cone[ci].va[0][2];        /* base z's all the same */
296        for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */
297                glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1],
298                                basez);
299                                                /* connect last to first */
300        glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez);
301        glEnd();                                /* all done */
424   }
425  
426  
427 + #ifdef STEREO
428 + static
429 + pushright()                     /* push on right view */
430 + {
431 +        double  d;
432 +
433 +        setstereobuf(STEREO_BUFFER_RIGHT);
434 +        glMatrixMode(GL_MODELVIEW);
435 +        glPushMatrix();
436 +        d = -eyesepdist / sqrt(odev.v.hn2);
437 +        glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]);
438 + }
439 +
440 +
441 + static
442 + popright()                      /* pop off right view */
443 + {
444 +        glMatrixMode(GL_MODELVIEW);
445 +        glPopMatrix();
446 +        setstereobuf(STEREO_BUFFER_LEFT);
447 + }
448 + #endif
449 +
450 +
451   static int
452   mytmflags()                     /* figure out tone mapping flags */
453   {
# Line 313 | Line 459 | mytmflags()                    /* figure out tone mapping flags */
459                          tail = cp+1;
460          for (cp = tail; *cp && *cp != '.'; cp++)
461                  ;
462 <        if (cp-tail == 3 && !strncmp(tail, "glx", 3))
463 <                return(TM_F_CAMERA);
464 <        if (cp-tail == 4 && !strncmp(tail, "glxh", 4))
465 <                return(TM_F_HUMAN);
320 <        error(USER, "illegal driver name");
462 >        if (cp > tail && cp[-1] == 'h')
463 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
464 >        else
465 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
466   }
467  
468  
469   static
325 initcones()                     /* initialize cone vertices */
326 {
327        register int    i, j;
328        double  minrad, d;
329
330        if (cone[0].nverts)
331                freecones();
332        minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) +
333                                        DisplayHeight(ourdisplay,ourscreen));
334        for (i = 0; i < MAXCONE; i++) {
335                d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */
336                cone[i].rad = minrad + (1.-minrad)*d;
337                cone[i].nverts = MINVERT + (MAXVERT-MINVERT)*d;
338                cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT));
339                if (cone[i].va == NULL)
340                        error(SYSTEM, "out of memory in initcones");
341                for (j = cone[i].nverts; j--; ) {
342                        d = 2.*PI * (j+.5) / (cone[i].nverts);
343                        cone[i].va[j][0] = cos(d) * cone[i].rad;
344                        cone[i].va[j][1] = sin(d) * cone[i].rad;
345                        cone[i].va[j][2] = 1. - cone[i].rad;
346                }
347        }
348 }
349
350
351 static
352 freecones()                     /* free cone vertices */
353 {
354        register int    i;
355
356        for (i = MAXCONE; i--; )
357                if (cone[i].nverts) {
358                        free((char *)cone[i].va);
359                        cone[i].va = NULL;
360                        cone[i].nverts = 0;
361                }
362 }
363
364
365 static
470   getevent()                      /* get next event */
471   {
472          XNextEvent(ourdisplay, levptr(XEvent));
# Line 399 | Line 503 | register FVECT wp[2];
503  
504  
505   static
506 < draw_grids()                    /* draw holodeck section grids */
506 > draw_grids(fore)                /* draw holodeck section grids */
507 > int     fore;
508   {
509 <        static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
405 <        double  xmin, xmax, ymin, ymax, zmin, zmax;
406 <        double  d, cx, sx, crad;
407 <        FVECT   vx, vy;
408 <        register int    i, j;
409 <                                        /* can we even do it? */
410 <        if (!mapped || odev.v.type != VT_PER)
509 >        if (!mapped)
510                  return;
511 <                                        /* compute view frustum */
512 <        if (normalize(odev.v.vdir) == 0.0)
513 <                return;
514 <        zmin = 0.01;
416 <        zmax = 10000.;
417 <        if (odev.v.vfore > FTINY)
418 <                zmin = odev.v.vfore;
419 <        if (odev.v.vaft > FTINY)
420 <                zmax = odev.v.vaft;
421 <        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
422 <        xmin = -xmax;
423 <        d = odev.v.hoff * (xmax - xmin);
424 <        xmin += d; xmax += d;
425 <        ymax = zmin * tan(PI/180./2. * odev.v.vert);
426 <        ymin = -ymax;
427 <        d = odev.v.voff * (ymax - ymin);
428 <        ymin += d; ymax += d;
429 <                                        /* set view matrix */
430 <        glMatrixMode(GL_PROJECTION);
431 <        glPushMatrix();
432 <        glLoadIdentity();
433 <        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
434 <        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
435 <                odev.v.vp[0] + odev.v.vdir[0],
436 <                odev.v.vp[1] + odev.v.vdir[1],
437 <                odev.v.vp[2] + odev.v.vdir[2],
438 <                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
439 <        glDisable(GL_DEPTH_TEST);       /* write no depth values */
440 <        glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
511 >        if (fore)
512 >                glColor4ub(0, 255, 255, 0);
513 >        else
514 >                glColor4ub(0, 0, 0, 0);
515          glBegin(GL_LINES);              /* draw each grid line */
516          gridlines(draw3dline);
517          glEnd();
444        glEnable(GL_DEPTH_TEST);        /* restore rendering params */
445        glPopMatrix();
518   }
519  
520  
# Line 451 | Line 523 | moveview(dx, dy, mov, orb)     /* move our view */
523   int     dx, dy, mov, orb;
524   {
525          VIEW    nv;
526 <        FVECT   odir, v1;
526 >        FVECT   odir, v1, wip;
527          double  d;
528          register int    li;
529                                  /* start with old view */
530          copystruct(&nv, &odev.v);
531 <                                /* change view direction */
532 <        if (mov | orb) {
533 <                if ((li = qtFindLeaf(dx, dy)) < 0)
531 >                                /* orient our motion */
532 >        if (viewray(v1, odir, &odev.v,
533 >                        (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
534 >                return(0);              /* outside view */
535 >        if (mov | orb) {        /* moving relative to geometry */
536 > #ifdef DOBJ
537 >                d = dobj_trace(NULL, v1, odir); /* check objects */
538 >                                                /* check holodeck */
539 >                if ((li = smFindSamp(v1, odir)) >= 0) {
540 >                        VCOPY(wip, rsL.wp[li]);
541 >                        if (d < .99*FHUGE && d*d <= dist2(v1, wip))
542 >                                li = -1;        /* object is closer */
543 >                } else if (d >= .99*FHUGE)
544 >                        return(0);              /* nothing visible */
545 >                if (li < 0)
546 >                        VSUM(wip, v1, odir, d); /* else get object point */
547 > #else
548 >                if ((li = smFindSamp(v1, odir)) < 0)
549                          return(0);      /* not on window */
550 <                VSUM(odir, qtL.wp[li], nv.vp, -1.);
551 <        } else {
552 <                if (viewray(nv.vp, nv.vdir, &odev.v,
553 <                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
554 <                        return(0);      /* outside view */
468 <        }
550 >                VCOPY(wip, rsL.wp[li]);
551 > #endif
552 >                VSUM(odir, wip, odev.v.vp, -1.);
553 >        } else                  /* panning with constant viewpoint */
554 >                VCOPY(nv.vdir, odir);
555          if (orb && mov) {               /* orbit left/right */
556                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
557 <                VSUM(nv.vp, qtL.wp[li], odir, -1.);
557 >                VSUM(nv.vp, wip, odir, -1.);
558                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
559          } else if (orb) {               /* orbit up/down */
560                  fcross(v1, odir, nv.vup);
561                  if (normalize(v1) == 0.)
562                          return(0);
563                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
564 <                VSUM(nv.vp, qtL.wp[li], odir, -1.);
564 >                VSUM(nv.vp, wip, odir, -1.);
565                  spinvector(nv.vdir, nv.vdir, v1, d);
566          } else if (mov) {               /* move forward/backward */
567                  d = MOVPCT/100. * mov;
# Line 483 | Line 569 | int    dx, dy, mov, orb;
569          }
570          if (!mov ^ !orb && headlocked) {        /* restore head height */
571                  VSUM(v1, odev.v.vp, nv.vp, -1.);
572 <                d = DOT(v1, odev.v.vup);
572 >                d = DOT(v1, nv.vup);
573                  VSUM(nv.vp, nv.vp, odev.v.vup, d);
574          }
575          if (setview(&nv) != NULL)
# Line 500 | Line 586 | XButtonPressedEvent    *ebut;
586   {
587          int     movdir = MOVDIR(ebut->button);
588          int     movorb = MOVORB(ebut->state);
589 <        int     oldnodesiz = qtMinNodesiz;
589 >        int     qlevel = 99;
590 >        time_t  lasttime, thistime;
591 >        int     nframes;
592          Window  rootw, childw;
593          int     rootx, rooty, wx, wy;
594          unsigned int    statemask;
595  
508        qtMinNodesiz = 24;              /* accelerate update rate */
596          XNoOp(ourdisplay);
597  
598 +        lasttime = time(0); nframes = 0;
599          while (!XCheckMaskEvent(ourdisplay,
600                          ButtonReleaseMask, levptr(XEvent))) {
601  
# Line 515 | Line 603 | XButtonPressedEvent    *ebut;
603                                  &rootx, &rooty, &wx, &wy, &statemask))
604                          break;          /* on another screen */
605  
606 +                draw_grids(0);          /* clear old grid lines */
607                  if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
608                          sleep(1);
609 +                        lasttime++;
610                          continue;
611                  }
612 <                glClear(GL_COLOR_BUFFER_BIT);
613 <                qtUpdate();
614 <                draw_grids();
612 > #ifdef STEREO
613 >                pushright();
614 >                draw_grids(1);
615 >                smUpdate(&vwright, qlevel);
616 > #ifdef DOBJ
617 >                dobj_render();
618 > #endif
619 >                popright();
620 > #endif
621 >                draw_grids(1);
622 >                smUpdate(&odev.v, qlevel);
623 > #ifdef DOBJ
624 >                dobj_render();
625 > #endif
626                  glFlush();
627 +                checkglerr("moving view");
628 +                nframes++;
629 +                thistime = time(0);
630 +                if (thistime - lasttime >= 3 ||
631 +                                nframes > (int)(3*3*TARGETFPS)) {
632 +                        qlevel = thistime<=lasttime ? 1000 :
633 +                                (int)((double)nframes/(thistime-lasttime)
634 +                                        / TARGETFPS * qlevel + 0.5);
635 +                        lasttime = thistime; nframes = 0;
636 +                        if (qlevel > 99) {
637 +                                if (qlevel > 300) {     /* put on the brakes */
638 +                                        sleep(1);
639 +                                        lasttime++;
640 +                                }
641 +                                qlevel = 99;
642 +                        } else if (qlevel < 1)
643 +                                qlevel = 1;
644 +                }
645          }
646          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
647                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 530 | Line 649 | XButtonPressedEvent    *ebut;
649                  wy = levptr(XButtonReleasedEvent)->y;
650                  moveview(wx, odev.vres-1-wy, movdir, movorb);
651          }
652 <        dev_flush();
652 > }
653  
654 <        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
654 >
655 > static
656 > setglpersp(vp)                  /* set perspective view in GL */
657 > register VIEW   *vp;
658 > {
659 >        double  d, xmin, xmax, ymin, ymax;
660 >
661 >        if (mindpth >= maxdpth) {
662 >                dev_zmin = 0.1;
663 >                dev_zmax = 100.;
664 >        } else {
665 >                dev_zmin = 0.5*mindpth;
666 >                dev_zmax = 1.5*maxdpth;
667 >                if (dev_zmin > dev_zmax/100.)
668 >                        dev_zmin = dev_zmax/100.;
669 >        }
670 >        if (odev.v.vfore > FTINY)
671 >                dev_zmin = odev.v.vfore;
672 >        if (odev.v.vaft > FTINY)
673 >                dev_zmax = odev.v.vaft;
674 >        if (dev_zmin < dev_zmax/5000.)
675 >                dev_zmin = dev_zmax/5000.;
676 >        xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
677 >        xmin = -xmax;
678 >        d = odev.v.hoff * (xmax - xmin);
679 >        xmin += d; xmax += d;
680 >        ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
681 >        ymin = -ymax;
682 >        d = odev.v.voff * (ymax - ymin);
683 >        ymin += d; ymax += d;
684 >                                        /* set view matrix */
685 >        glMatrixMode(GL_PROJECTION);
686 >        glLoadIdentity();
687 >        glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
688 >        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
689 >                odev.v.vp[0] + odev.v.vdir[0],
690 >                odev.v.vp[1] + odev.v.vdir[1],
691 >                odev.v.vp[2] + odev.v.vdir[2],
692 >                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
693   }
694  
695  
696   static
697 + wipeclean()                     /* prepare for redraw */
698 + {
699 +                                        /* clear depth buffer */
700 + #ifdef STEREO
701 +        setstereobuf(STEREO_BUFFER_RIGHT);
702 +        glClear(GL_DEPTH_BUFFER_BIT);
703 +        setstereobuf(STEREO_BUFFER_LEFT);
704 + #endif
705 +        glClear(GL_DEPTH_BUFFER_BIT);
706 +        smClean();                      /* reset drawing routines */
707 +        setglpersp(&odev.v);            /* reset view & clipping planes */
708 + }
709 +
710 +
711 + static
712   getkey(ekey)                            /* get input key */
713   register XKeyPressedEvent  *ekey;
714   {
# Line 567 | Line 739 | register XKeyPressedEvent  *ekey;
739                  inpresflags |= DFL(DC_RESUME);
740                  return;
741          case CTRL('R'):                 /* redraw screen */
742 <                glClear(GL_DEPTH_BUFFER_BIT);
571 <                qtRedraw(0, 0, odev.hres, odev.vres);
742 >                wipeclean();
743                  return;
744          case CTRL('L'):                 /* refresh from server */
745                  if (inpresflags & DFL(DC_REDRAW))
746                          return;
747                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
748 <                draw_grids();
748 >                glDisable(GL_DEPTH_TEST);
749 >                draw_grids(1);
750 > #ifdef STEREO
751 >                pushright();
752 >                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
753 >                draw_grids(1);
754 >                popright();
755 > #endif
756 >                glEnable(GL_DEPTH_TEST);
757                  glFlush();
758 <                qtCompost(100);                 /* get rid of old values */
758 >                smInit(rsL.max_samp);           /* get rid of old values */
759                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
760 +                setglpersp(&odev.v);            /* reset clipping planes */
761 +                rayqleft = 0;                   /* hold off update */
762                  return;
763          case 'K':                       /* kill rtrace process(es) */
764                  inpresflags |= DFL(DC_KILL);
# Line 602 | Line 783 | static
783   fixwindow(eexp)                         /* repair damage to window */
784   register XExposeEvent  *eexp;
785   {
786 <        int     xmin, xmax, ymin, ymax;
606 <
607 <        if (odev.hres == 0 || odev.vres == 0)   /* first exposure */
786 >        if (odev.hres == 0 | odev.vres == 0) {  /* first exposure */
787                  resizewindow((XConfigureEvent *)eexp);
788 <        xmin = eexp->x; xmax = eexp->x + eexp->width;
789 <        ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
790 <                                                /* clear portion of depth */
791 <        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
792 <        glDepthFunc(GL_ALWAYS);
614 <        glBegin(GL_POLYGON);
615 <        glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.);
616 <        glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.);
617 <        glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.);
618 <        glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.);
619 <        glEnd();
620 <        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
621 <        glDepthFunc(GL_LEQUAL);
622 <        qtRedraw(xmin, ymin, xmax, ymax);
788 >                return;
789 >        }
790 >        if (eexp->count)                /* wait for final exposure */
791 >                return;
792 >        wipeclean();                    /* clear depth */
793   }
794  
795  
# Line 627 | Line 797 | static
797   resizewindow(ersz)                      /* resize window */
798   register XConfigureEvent  *ersz;
799   {
800 +        glViewport(0, 0, ersz->width, ersz->height);
801 +
802          if (ersz->width == odev.hres && ersz->height == odev.vres)
803                  return;
804  
# Line 635 | Line 807 | register XConfigureEvent  *ersz;
807  
808          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
809          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
638
639        glViewport(0, 0, odev.hres, odev.vres);
810  
811          inpresflags |= DFL(DC_SETVIEW);
812   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines