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.2 by gregl, Wed Dec 24 14:06:40 1997 UTC vs.
Revision 3.28 by gwlarson, Thu Jun 10 15:22:21 1999 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines