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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines