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.3 by gregl, Wed Dec 24 15:55:43 1997 UTC vs.
Revision 3.27 by gwlarson, Sun Jan 3 13:40:25 1999 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           3.              /* cone height (fraction of depth) */
37 #endif
38 #ifndef CONEW
39 #define CONEW           0.05            /* cone width (fraction of screen) */
40 #endif
41 #ifndef DIRPEN
42 #define DIRPEN          0.001           /* direction penalty factor */
43 #endif
44 #ifndef VALUA
45 #define VALUA           16              /* target value area (pixels) */
46 #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 59 | 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 68 | 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 78 | Line 95 | static XVisualInfo  *ourvinf;          /* our visual informati
95   static Window  gwind = 0;               /* our graphics window */
96   static GLXContext       gctx;           /* our GLX context */
97  
81 static double   mindepth = FHUGE;       /* minimum depth value so far */
82 static double   maxdepth = 0.;          /* maximum depth value so far */
83
98   static double   pwidth, pheight;        /* pixel dimensions (mm) */
99  
100 < static FVECT    conev[NCONEV];          /* drawing cone */
87 < 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 int      quicken = 0;            /* quicker, sloppier update rate? */
108 > static int  resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
109 >                setglpersp(), getframe(), getmove(), fixwindow(), mytmflags();
110  
111 < static struct {
112 <        float           (*wp)[3];       /* world intersection point array */
113 <        int4            *wd;            /* world direction array */
98 <        TMbright        *brt;           /* encoded brightness array */
99 <        BYTE            (*chr)[3];      /* encoded chrominance array */
100 <        BYTE            (*rgb)[3];      /* tone-mapped color array */
101 <        BYTE            *alpha;         /* alpha values */
102 <        int             nl;             /* count of values */
103 <        int             bl, tl;         /* bottom and top (next) value index */
104 <        int             tml;            /* next value needing tone-mapping */
105 <        int             drl;            /* next value in need of drawing */
106 <        char            *base;          /* base of allocated memory */
107 < }       rV;                     /* our collection of values */
111 > #ifdef STEREO
112 > static int  pushright(), popright();
113 > #endif
114  
115 < static int      *valmap = NULL;         /* sorted map of screen values */
110 < static int      vmaplen = 0;            /* value map length */
115 > extern time_t   time();
116  
112 #define redraw()        (rV.drl = rV.bl)
117  
118 < static int  resizewindow(), getevent(), getkey(), moveview(),
115 <                setGLview(), getmove(), fixwindow(), mytmflags(),
116 <                drawvalue(), valcmp(), clralphas(), setalphas(), mergalphas(),
117 <                IndexValue(), Compost(), FindValue(), TMapValues(),
118 <                AllocValues(), FreeValues();
119 <
120 < extern int4     encodedir();
121 < extern double   fdir2diff(), dir2diff();
122 <
123 <
124 < 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};
132 <        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 145 | 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 159 | 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 172 | 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) *
197 <                        DisplayHeight(ourdisplay,ourscreen) / VALUA))
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 203 | 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 210 | Line 252 | dev_close()                    /* close our display and free resources
252          XCloseDisplay(ourdisplay);
253          ourdisplay = NULL;
254          tmDone(NULL);
213        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 252 | 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);
256 <                setGLview();
257 <                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
258 <                redraw();
310 >                copystruct(&odev.v, nv);        /* setview() already called */
311          }
312 + #ifdef STEREO
313 +        copystruct(&vwright, nv);
314 +        d = eyesepdist / sqrt(nv->hn2);
315 +        VSUM(vwright.vp, nv->vp, nv->hvec, d);
316 +        /* setview(&vwright);   -- Unnecessary */
317 + #endif
318 +        wipeclean();
319          return(1);
320   }
321  
322  
323 < int
324 < dev_input()                     /* get X11 input */
323 > dev_section(gfn, pfn)           /* add octree for geometry rendering */
324 > char    *gfn, *pfn;
325   {
326 <        inpresflags = 0;
268 <
269 <        do
270 <                getevent();
271 <
272 <        while (XQLength(ourdisplay) > 0);
273 <
274 <        return(inpresflags);
326 >        /* unimplemented */
327   }
328  
329  
330 < dev_value(c, p, v)              /* add a pixel value to our list */
331 < COLR    c;
280 < FVECT   p, v;
330 > dev_auxcom(cmd, args)           /* process an auxiliary command */
331 > char    *cmd, *args;
332   {
333 <        register int    li;
334 <
284 <        li = rV.tl++;
285 <        if (rV.tl >= rV.nl)     /* get next leaf in ring */
286 <                rV.tl = 0;
287 <        if (rV.tl == rV.bl)     /* need to shake some free */
288 <                Compost(FREEPCT);
289 <        VCOPY(rV.wp[li], p);
290 <        rV.wd[li] = encodedir(v);
291 <        tmCvColrs(&rV.brt[li], rV.chr[li], c, 1);
292 < }
293 <
294 <
295 < int
296 < dev_flush()                     /* flush output */
297 < {
298 <        if (mapped) {
299 <                TMapValues(0);
300 <                while (rV.drl != rV.tl) {
301 <                        drawvalue(rV.drl);
302 <                        if (++rV.drl >= rV.nl)
303 <                                rV.drl = 0;
304 <                }
305 <                glFlush();
306 <        }
307 <        return(XPending(ourdisplay));
308 < }
309 <
310 <
311 < static int
312 < mytmflags()                     /* figure out tone mapping flags */
313 < {
314 <        extern char     *progname;
315 <        register char   *cp, *tail;
316 <                                        /* find basic name */
317 <        for (cp = tail = progname; *cp; cp++)
318 <                if (*cp == '/')
319 <                        tail = cp+1;
320 <        for (cp = tail; *cp && *cp != '.'; cp++)
321 <                ;
322 <        if (cp-tail == 3 && !strncmp(tail, "glx", 3))
323 <                return(TM_F_CAMERA);
324 <        if (cp-tail == 4 && !strncmp(tail, "glxh", 4))
325 <                return(TM_F_HUMAN);
326 <        error(USER, "illegal driver name");
327 < }
328 <
329 <
330 < static
331 < setGLview()                     /* set our GL view */
332 < {
333 <        double  xmin, xmax, ymin, ymax, zmin, zmax;
334 <        double  d, cx, sx, crad;
335 <        FVECT   vx, vy;
336 <        register int    i, j;
337 <                                        /* compute view frustum */
338 <        if (normalize(odev.v.vdir) == 0.0)
333 > #ifdef DOBJ
334 >        if (dobj_command(cmd, args) >= 0)
335                  return;
336 <        if (mindepth < maxdepth) {
337 <                zmin = 0.25*mindepth;
338 <                zmax = 4.0*(1.+CONEH)*maxdepth;
343 <        } else {
344 <                zmin = 0.01;
345 <                zmax = 1000.;
346 <        }
347 <        if (odev.v.vfore > FTINY)
348 <                zmin = odev.v.vfore;
349 <        if (odev.v.vaft > FTINY)
350 <                zmax = odev.v.vaft;
351 <        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
352 <        xmin = -xmax;
353 <        d = odev.v.hoff * (xmax - xmin);
354 <        xmin += d; xmax += d;
355 <        ymax = zmin * tan(PI/180./2. * odev.v.vert);
356 <        ymin = -ymax;
357 <        d = odev.v.voff * (ymax - ymin);
358 <        ymin += d; ymax += d;
359 <                                        /* set view matrix */
360 <        glMatrixMode(GL_PROJECTION);
361 <        glLoadIdentity();
362 <        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
363 <        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
364 <                odev.v.vp[0] + odev.v.vdir[0],
365 <                odev.v.vp[1] + odev.v.vdir[1],
366 <                odev.v.vp[2] + odev.v.vdir[2],
367 <                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
368 <                                        /* set viewport */
369 <        glViewport(0, 0, odev.hres, odev.vres);
370 <                                        /* initialize cone for Vornoi polys */
371 <        coneh = CONEH*(zmax - zmin);
372 <        crad = 0.5 * CONEW * 0.5*(xmax-xmin + ymax-ymin) * (zmin+coneh)/zmin;
373 <        vy[0] = vy[1] = vy[2] = 0.;
374 <        for (i = 0; i < 3; i++)
375 <                if (odev.v.vdir[i] < 0.6 && odev.v.vdir[i] > -0.6)
376 <                        break;
377 <        vy[i] = 1.;
378 <        fcross(vx, vy, odev.v.vdir);
379 <        normalize(vx);
380 <        fcross(vy, odev.v.vdir, vx);
381 <        for (j = 0, d = 0.; j < NCONEV; j++, d += 2.*PI/NCONEV) {
382 <                cx = crad*cos(d); sx = crad*sin(d);
383 <                for (i = 0; i < 3; i++)
384 <                        conev[j][i] = coneh*odev.v.vdir[i] +
385 <                                        cx*vx[i] + sx*vy[i];
386 <        }
336 > #endif
337 >        sprintf(errmsg, "%s: unknown command", cmd);
338 >        error(COMMAND, errmsg);
339   }
340  
341  
342 < #define SUCCSTEP        8       /* skip step when successful */
343 < #define MAXSTEP         64
344 <
345 < static
394 < drawvalue(li)                   /* draw a pixel value as a cone */
395 < register int    li;
342 > VIEW *
343 > dev_auxview(n, hvres)           /* return nth auxiliary view */
344 > int     n;
345 > int     hvres[2];
346   {
347 <        static int      skipstep = 1;
348 <        static FVECT    disp;
349 <        FVECT   apex;
350 <        double  d, dorg, dnew, h, v;
351 <        register int    i;
352 <                                /* check for quicker update */
353 <        if (quicken) {
354 <                if (li % skipstep)
405 <                        return;
406 <                if (skipstep < MAXSTEP)
407 <                        skipstep++;
408 <        }
409 <                                /* compute cone coordinates */
410 <        disp[0] = rV.wp[li][0] - odev.v.vp[0];
411 <        disp[1] = rV.wp[li][1] - odev.v.vp[1];
412 <        disp[2] = rV.wp[li][2] - odev.v.vp[2];
413 <        dorg = DOT(disp,odev.v.vdir);
414 <        if (dorg <= odev.v.vfore)
415 <                return;         /* clipped too near */
416 <        if (odev.v.vaft > FTINY && dorg > odev.v.vaft)
417 <                return;         /* clipped too far */
418 <        if (dorg > 1e5) {       /* background pixel */
419 <                dnew = maxdepth;
420 <                d = dnew/dorg;
421 <                dorg = maxdepth;
422 <        } else {                /* foreground pixel, compute penalty */
423 <                normalize(disp);
424 <                d = dnew = dorg + coneh*fdir2diff(rV.wd[li],disp)*DIRPEN;
425 <        }
426 <                                /* compute adjusted apex position */
427 <        disp[0] *= d; disp[1] *= d; disp[2] *= d;
428 <        apex[0] = odev.v.vp[0] + disp[0];
429 <        apex[1] = odev.v.vp[1] + disp[1];
430 <        apex[2] = odev.v.vp[2] + disp[2];
431 <                                /* compute view position and base offset */
432 <        h = DOT(disp,odev.v.hvec)/(dnew*odev.v.hn2);
433 <        v = DOT(disp,odev.v.vvec)/(dnew*odev.v.vn2);
434 <        if (fabs(h - odev.v.hoff) > 0.5 || fabs(v - odev.v.voff) > 0.5)
435 <                return;         /* clipped off screen */
436 <        if (dorg < mindepth)
437 <                mindepth = dorg;
438 <        if (dorg > maxdepth)
439 <                maxdepth = dorg;
440 <        for (i = 0; i < 3; i++)
441 <                disp[i] = apex[i] + coneh*(h*odev.v.hvec[i] + v*odev.v.vvec[i]);
442 <                                /* draw cone (pyramid approx.) */
443 <        glColor4ub(rV.rgb[li][0], rV.rgb[li][1], rV.rgb[li][2], rV.alpha[li]);
444 <        glBegin(GL_TRIANGLE_FAN);
445 <        glVertex3f(apex[0], apex[1], apex[2]);
446 <        for (i = 0; i < NCONEV; i++)
447 <                glVertex3d(conev[i][0] + disp[0], conev[i][1] + disp[1],
448 <                                conev[i][2] + disp[2]);
449 <                                /* connect last face to first */
450 <        glVertex3d(conev[0][0] + disp[0], conev[0][1] + disp[1],
451 <                        conev[0][2] + disp[2]);
452 <        glEnd();                /* done */
453 <        skipstep = SUCCSTEP;
347 >        hvres[0] = odev.hres; hvres[1] = odev.vres;
348 >        if (n == 0)
349 >                return(&odev.v);
350 > #ifdef STEREO
351 >        if (n == 1)
352 >                return(&vwright);
353 > #endif
354 >        return(NULL);
355   }
356  
456 #undef SUCCSTEP
457 #undef MAXSTEP
357  
358 <
359 < #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
461 <                        sizeof(TMbright)+7*sizeof(BYTE))
462 <
463 < static
464 < AllocValues(n)                  /* allocate space for n values */
465 < register int    n;
358 > int
359 > dev_input()                     /* get X11 input */
360   {
361 <        unsigned        nbytes;
468 <        register unsigned       i;
361 >        inpresflags = 0;
362  
363 <        if (n <= 0)
364 <                return(0);
472 <        if (rV.nl >= n)
473 <                return(rV.nl);
474 <        else if (rV.nl > 0)
475 <                free(rV.base);
476 <                                /* round space up to nearest power of 2 */
477 <        nbytes = n*LEAFSIZ + 8;
478 <        for (i = 1024; nbytes > i; i <<= 1)
479 <                ;
480 <        n = (i - 8) / LEAFSIZ;  /* should we make sure n is even? */
481 <        rV.base = (char *)malloc(n*LEAFSIZ);
482 <        if (rV.base == NULL)
483 <                return(0);
484 <                                /* assign larger alignment types earlier */
485 <        rV.wp = (float (*)[3])rV.base;
486 <        rV.wd = (int4 *)(rV.wp + n);
487 <        rV.brt = (TMbright *)(rV.wd + n);
488 <        rV.chr = (BYTE (*)[3])(rV.brt + n);
489 <        rV.rgb = (BYTE (*)[3])(rV.chr + n);
490 <        rV.alpha = (BYTE *)(rV.rgb + n);
491 <        rV.nl = n;
492 <        rV.drl = rV.tml = rV.bl = rV.tl = 0;
493 <        return(n);
494 < }
363 >        do
364 >                getevent();
365  
366 < #undef  LEAFSIZ
366 >        while (XPending(ourdisplay) > 0);
367  
368 +        odev.inpready = 0;
369  
370 < static
500 < FreeValues()                    /* free our allocated values */
501 < {
502 <        if (rV.nl <= 0)
503 <                return;
504 <        free(rV.base);
505 <        rV.base = NULL;
506 <        rV.nl = 0;
370 >        return(inpresflags);
371   }
372  
373  
374 < static
375 < clralphas()                     /* prepare for new alpha values */
374 > dev_value(c, d, p)              /* add a pixel value to our mesh */
375 > COLR    c;
376 > FVECT   d, p;
377   {
378 <        if (!vmaplen)
378 >        double  depth;
379 > #ifdef DOBJ
380 >        if (dobj_lightsamp != NULL) {   /* in light source sampling */
381 >                (*dobj_lightsamp)(c, d, p);
382                  return;
383 <        free((char *)valmap);
384 <        valmap = NULL;
385 <        vmaplen = 0;
383 >        }
384 > #endif
385 >        if (p != NULL) {                /* add depth to our range */
386 >                depth = (p[0] - odev.v.vp[0])*d[0] +
387 >                        (p[1] - odev.v.vp[1])*d[1] +
388 >                        (p[2] - odev.v.vp[2])*d[2];
389 >                if (depth > FTINY) {
390 >                        if (depth < mindpth)
391 >                                mindpth = depth;
392 >                        if (depth > maxdpth)
393 >                                maxdpth = depth;
394 >                }
395 >        }
396 >        smNewSamp(c, d, p);             /* add to display representation */
397 >        if (!--rayqleft)
398 >                dev_flush();            /* flush output */
399   }
400  
401  
402 < static int
403 < valcmp(v1p, v2p)                /* compare two pixel values */
523 < int     *v1p, *v2p;
402 > int
403 > dev_flush()                     /* flush output */
404   {
405 <        register int    v1 = *v1p, v2 = *v2p;
406 <        register int    c;
407 <
408 <        if ((c = rV.rgb[v1][0] - rV.rgb[v2][0])) return(c);
409 <        if ((c = rV.rgb[v1][1] - rV.rgb[v2][1])) return(c);
410 <        if ((c = rV.rgb[v1][2] - rV.rgb[v2][2])) return(c);
411 <        return(rV.alpha[v1] - rV.alpha[v2]);
405 >        if (mapped) {
406 > #ifdef STEREO
407 >                pushright();                    /* update right eye */
408 >                smUpdate(&vwright, 100);
409 > #ifdef DOBJ
410 >                dobj_render();                  /* usually in foreground */
411 > #endif
412 >                popright();                     /* update left eye */
413 > #endif
414 >                smUpdate(&odev.v, 100);
415 >                checkglerr("rendering mesh");
416 > #ifdef DOBJ
417 >                dobj_render();
418 > #endif
419 >                glFlush();                      /* flush OGL */
420 >        }
421 >        rayqleft = RAYQLEN;
422 >                                        /* flush X11 and return # pending */
423 >        return(odev.inpready = XPending(ourdisplay));
424   }
425  
426  
427 < static
428 < mergalphas(adest, al1, n1, al2, n2)     /* merge two sorted alpha lists */
537 < register int    *adest, *al1, *al2;
538 < int     n1, n2;
427 > checkglerr(where)               /* check for GL or GLU error */
428 > char    *where;
429   {
430 <        register int    cmp;
430 >        register GLenum errcode;
431  
432 <        while (n1 | n2) {
433 <                if (!n1) cmp = 1;
434 <                else if (!n2) cmp = -1;
435 <                else cmp = valcmp(al1, al2);
546 <                if (cmp > 0) {
547 <                        *adest++ = *al2++;
548 <                        n2--;
549 <                } else {
550 <                        *adest++ = *al1++;
551 <                        n1--;
552 <                }
432 >        while ((errcode = glGetError()) != GL_NO_ERROR) {
433 >                sprintf(errmsg, "OpenGL error %s: %s",
434 >                                where, gluErrorString(errcode));
435 >                error(WARNING, errmsg);
436          }
437   }
438  
439  
440 + #ifdef STEREO
441   static
442 < setalphas(vbeg, nvals)          /* add values to our map and set alphas */
559 < int     vbeg, nvals;
442 > pushright()                     /* push on right view */
443   {
444 <        register int    *newmap;
562 <        short   ccmp[3], lastalpha;
563 <        int     newmaplen;
444 >        double  d;
445  
446 <        if (nvals <= 0)
447 <                return;
448 <        newmaplen = vmaplen + nvals;    /* allocate new map */
449 <        newmap = (int *)malloc(newmaplen*sizeof(int));
450 <        if (newmap == NULL)
451 <                error(SYSTEM, "out of memory in setalphas");
571 <        while (nvals--) {               /* add new values to end */
572 <                rV.alpha[vbeg] = 255;
573 <                newmap[vmaplen+nvals] = vbeg++;
574 <        }
575 <        if (nvals >= 3*vmaplen) {       /* resort the combined array */
576 <                while (vmaplen--)
577 <                        newmap[vmaplen] = valmap[vmaplen];
578 <                qsort((char *)newmap, newmaplen, sizeof(int), valcmp);
579 <        } else {                        /* perform merge sort */
580 <                qsort((char *)(newmap+vmaplen), newmaplen-vmaplen,
581 <                                sizeof(int), valcmp);
582 <                mergalphas(newmap, valmap, vmaplen,
583 <                                newmap+vmaplen, newmaplen-vmaplen);
584 <        }
585 <        if (valmap != NULL)             /* free old map and assign new one */
586 <                free((char *)valmap);
587 <        valmap = newmap;
588 <        vmaplen = newmaplen;
589 <        lastalpha = 0;                  /* set new alpha values */
590 <        ccmp[0] = ccmp[1] = ccmp[2] = 256;
591 <        while (newmaplen--)
592 <                if (rV.rgb[*newmap][0] == ccmp[0] &&
593 <                                rV.rgb[*newmap][1] == ccmp[1] &&
594 <                                rV.rgb[*newmap][2] == ccmp[2]) {
595 <                        if (lastalpha >= 255)
596 <                                newmap++;
597 <                        else if (rV.alpha[*newmap] < 255)
598 <                                lastalpha = rV.alpha[*newmap++];
599 <                        else
600 <                                rV.alpha[*newmap++] = ++lastalpha;
601 <                } else {
602 <                        ccmp[0] = rV.rgb[*newmap][0];
603 <                        ccmp[1] = rV.rgb[*newmap][1];
604 <                        ccmp[2] = rV.rgb[*newmap][2];
605 <                        if (rV.alpha[*newmap] < 255)
606 <                                lastalpha = rV.alpha[*newmap++];
607 <                        else
608 <                                rV.alpha[*newmap++] = lastalpha = 1;
609 <                }
446 >        setstereobuf(STEREO_BUFFER_RIGHT);
447 >        glMatrixMode(GL_MODELVIEW);
448 >        glPushMatrix();
449 >        d = -eyesepdist / sqrt(odev.v.hn2);
450 >        glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]);
451 >        checkglerr("setting right view");
452   }
453  
454  
455   static
456 < TMapValues(redo)                /* map our values to RGB */
615 < int     redo;
456 > popright()                      /* pop off right view */
457   {
458 <        int     aorg, alen, borg, blen;
459 <                                        /* recompute mapping? */
460 <        if (redo)
620 <                rV.tml = rV.bl;
621 <                                        /* already done? */
622 <        if (rV.tml == rV.tl)
623 <                return(1);
624 <                                        /* compute segments */
625 <        aorg = rV.tml;
626 <        if (rV.tl >= aorg) {
627 <                alen = rV.tl - aorg;
628 <                blen = 0;
629 <        } else {
630 <                alen = rV.nl - aorg;
631 <                borg = 0;
632 <                blen = rV.tl;
633 <        }
634 <                                        /* (re)compute tone mapping? */
635 <        if (rV.tml == rV.bl) {
636 <                tmClearHisto();
637 <                tmAddHisto(rV.brt+aorg, alen, 1);
638 <                if (blen > 0)
639 <                        tmAddHisto(rV.brt+borg, blen, 1);
640 <                if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
641 <                        return(0);
642 <                clralphas();            /* restart value list */
643 <                rV.drl = rV.bl;         /* need to redraw */
644 <        }
645 <        if (tmMapPixels(rV.rgb+aorg, rV.brt+aorg,
646 <                        rV.chr+aorg, alen) != TM_E_OK)
647 <                return(0);
648 <        if (blen > 0)
649 <                tmMapPixels(rV.rgb+borg, rV.brt+borg,
650 <                                rV.chr+borg, blen);
651 <        setalphas(aorg, alen);          /* compute add'l alpha values */
652 <        if (blen > 0)
653 <                setalphas(borg, blen);
654 <        rV.tml = rV.tl;                 /* we're all up to date */
655 <        return(1);
458 >        glMatrixMode(GL_MODELVIEW);
459 >        glPopMatrix();
460 >        setstereobuf(STEREO_BUFFER_LEFT);
461   }
462 + #endif
463  
464  
465   static int
466 < Compost(pct)                    /* free up some values */
661 < int     pct;
466 > mytmflags()                     /* figure out tone mapping flags */
467   {
468 <        int     nused, nclear, nmapped, ndrawn;
469 <                                /* figure out how many values to clear */
470 <        nclear = rV.nl * pct / 100;
471 <        nused = rV.tl - rV.bl;
472 <        if (nused <= 0) nused += rV.nl;
473 <        nclear -= rV.nl - nused;
474 <        if (nclear <= 0)
475 <                return(0);
476 <        if (nclear >= nused) {  /* clear them all? */
477 <                rV.drl = rV.tml = rV.bl = rV.tl = 0;
478 <                return(nused);
479 <        }
675 <                                /* else clear values from bottom */
676 <        ndrawn = rV.drl - rV.bl;
677 <        if (ndrawn < 0) ndrawn += rV.nl;
678 <        nmapped = rV.tml - rV.bl;
679 <        if (nmapped < 0) nmapped += rV.nl;
680 <        rV.bl += nclear;
681 <        if (rV.bl >= rV.nl) rV.bl -= rV.nl;
682 <        if (ndrawn < nclear) rV.drl = rV.bl;
683 <        if (nmapped < nclear) rV.tml = rV.bl;
684 <        return(nclear);
468 >        extern char     *progname;
469 >        register char   *cp, *tail;
470 >                                        /* find basic name */
471 >        for (cp = tail = progname; *cp; cp++)
472 >                if (*cp == '/')
473 >                        tail = cp+1;
474 >        for (cp = tail; *cp && *cp != '.'; cp++)
475 >                ;
476 >        if (cp > tail && cp[-1] == 'h')
477 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
478 >        else
479 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
480   }
481  
482  
# Line 706 | Line 501 | getevent()                     /* get next event */
501                  getkey(levptr(XKeyPressedEvent));
502                  break;
503          case ButtonPress:
504 <                getmove(levptr(XButtonPressedEvent));
504 >                if (FRAMESTATE(levptr(XButtonPressedEvent)->state))
505 >                        getframe(levptr(XButtonPressedEvent));
506 >                else
507 >                        getmove(levptr(XButtonPressedEvent));
508                  break;
509          }
510   }
# Line 722 | Line 520 | register FVECT wp[2];
520  
521  
522   static
523 < draw_grids()                    /* draw holodeck section grids */
523 > draw_grids(fore)                /* draw holodeck section grids */
524 > int     fore;
525   {
526 <        static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
527 <
528 <        if (!mapped)
529 <                return;
530 <        glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
732 <                                        /* draw each grid line */
733 <        glBegin(GL_LINES);
526 >        if (fore)
527 >                glColor4ub(0, 255, 255, 0);
528 >        else
529 >                glColor4ub(0, 0, 0, 0);
530 >        glBegin(GL_LINES);              /* draw each grid line */
531          gridlines(draw3dline);
532          glEnd();
533 +        checkglerr("drawing grid lines");
534   }
535  
536  
739 static int
740 IndexValue(rgba)                /* locate a pixel by it's framebuffer value */
741 register BYTE   rgba[4];
742 {
743        register int    *vp;
744                                        /* check legality */
745        if (rgba[3] == 0 || rgba[3] == 255)
746                return(-1);
747                                        /* borrow a value slot */
748        rV.rgb[rV.tl][0] = rgba[0];
749        rV.rgb[rV.tl][1] = rgba[1];
750        rV.rgb[rV.tl][2] = rgba[2];
751        rV.alpha[rV.tl] = rgba[3];
752                                        /* find it */
753        vp = (int *)bsearch((char *)&rV.tl, (char *)valmap, vmaplen,
754                        sizeof(int), valcmp);
755        if (vp == NULL)
756                return(-1);
757        return(*vp);
758 }
759
760
761 static int
762 FindValue(dx, dy)               /* find a value on the display */
763 int     dx, dy;
764 {
765        BYTE    rgba[4];
766
767        if (dx < 0 || dy < 0 || dx >= odev.hres || dy >= odev.vres)
768                return(-1);
769        glReadPixels(dx, dy, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
770        return(IndexValue(rgba));
771 }
772
773
537   static
538   moveview(dx, dy, mov, orb)      /* move our view */
539   int     dx, dy, mov, orb;
540   {
541          VIEW    nv;
542 <        FVECT   odir, v1;
542 >        FVECT   odir, v1, wip;
543          double  d;
544          register int    li;
545                                  /* start with old view */
546          copystruct(&nv, &odev.v);
547 <                                /* change view direction */
548 <        if (mov | orb) {
549 <                if ((li = FindValue(dx, dy)) < 0)
547 >                                /* orient our motion */
548 >        if (viewray(v1, odir, &odev.v,
549 >                        (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
550 >                return(0);              /* outside view */
551 >        if (mov | orb) {        /* moving relative to geometry */
552 > #ifdef DOBJ
553 >                d = dobj_trace(NULL, v1, odir); /* check objects */
554 >                                                /* check holodeck */
555 >                if ((li = smFindSamp(v1, odir)) >= 0) {
556 >                        VCOPY(wip, rsL.wp[li]);
557 >                        if (d < .99*FHUGE && d*d <= dist2(v1, wip))
558 >                                li = -1;        /* object is closer */
559 >                } else if (d >= .99*FHUGE)
560 >                        return(0);              /* nothing visible */
561 >                if (li < 0)
562 >                        VSUM(wip, v1, odir, d); /* else get object point */
563 > #else
564 >                if ((li = smFindSamp(v1, odir)) < 0)
565                          return(0);      /* not on window */
566 <                VSUM(odir, rV.wp[li], nv.vp, -1.);
567 <        } else {
568 <                if (viewray(nv.vp, nv.vdir, &odev.v,
569 <                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
570 <                        return(0);      /* outside view */
571 <        }
566 >                VCOPY(wip, rsL.wp[li]);
567 > #endif
568 > #ifdef DEBUG
569 >                fprintf(stderr, "moveview: hit %s at (%f,%f,%f) (t=%f)\n",
570 >                                li < 0 ? "object" : "mesh",
571 >                                wip[0], wip[1], wip[2],
572 >                                (wip[0]-odev.v.vp[0])*odir[0] +
573 >                                (wip[1]-odev.v.vp[1])*odir[1] +
574 >                                (wip[2]-odev.v.vp[2])*odir[2]);
575 > #endif
576 >                VSUM(odir, wip, odev.v.vp, -1.);
577 >        } else                  /* panning with constant viewpoint */
578 >                VCOPY(nv.vdir, odir);
579          if (orb && mov) {               /* orbit left/right */
580                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
581 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
581 >                VSUM(nv.vp, wip, odir, -1.);
582                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
583          } else if (orb) {               /* orbit up/down */
584                  fcross(v1, odir, nv.vup);
585                  if (normalize(v1) == 0.)
586                          return(0);
587                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
588 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
588 >                VSUM(nv.vp, wip, odir, -1.);
589                  spinvector(nv.vdir, nv.vdir, v1, d);
590          } else if (mov) {               /* move forward/backward */
591                  d = MOVPCT/100. * mov;
# Line 808 | Line 593 | int    dx, dy, mov, orb;
593          }
594          if (!mov ^ !orb && headlocked) {        /* restore head height */
595                  VSUM(v1, odev.v.vp, nv.vp, -1.);
596 <                d = DOT(v1, odev.v.vup);
596 >                d = DOT(v1, nv.vup);
597                  VSUM(nv.vp, nv.vp, odev.v.vup, d);
598          }
599          if (setview(&nv) != NULL)
# Line 820 | Line 605 | int    dx, dy, mov, orb;
605  
606  
607   static
608 + getframe(ebut)                          /* get focus frame */
609 + XButtonPressedEvent     *ebut;
610 + {
611 +        int     startx = ebut->x, starty = ebut->y;
612 +        int     endx, endy;
613 +
614 +        XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent));
615 +        endx = levptr(XButtonReleasedEvent)->x;
616 +        endy = levptr(XButtonReleasedEvent)->y;
617 +        if (endx == startx | endy == starty) {
618 +                XBell(ourdisplay, 0);
619 +                return;
620 +        }
621 +        if (endx < startx) {register int c = endx; endx = startx; startx = c;}
622 +        if (endy < starty) {register int c = endy; endy = starty; starty = c;}
623 +        sprintf(odev_args, "%.3f %.3f %.3f %.3f",
624 +                        (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres,
625 +                        (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres);
626 +        inpresflags |= DFL(DC_FOCUS);
627 + }
628 +
629 +
630 + static
631   getmove(ebut)                           /* get view change */
632   XButtonPressedEvent     *ebut;
633   {
634          int     movdir = MOVDIR(ebut->button);
635          int     movorb = MOVORB(ebut->state);
636 +        int     qlevel = 99;
637 +        time_t  lasttime, thistime;
638 +        int     nframes;
639          Window  rootw, childw;
640          int     rootx, rooty, wx, wy;
641          unsigned int    statemask;
642  
643 <        quicken = 1;                    /* accelerate update rate */
833 <        XNoOp(ourdisplay);
643 >        XNoOp(ourdisplay);              /* makes sure we're not idle */
644  
645 +        lasttime = time(0); nframes = 0;
646          while (!XCheckMaskEvent(ourdisplay,
647                          ButtonReleaseMask, levptr(XEvent))) {
648 <
648 >                                        /* get cursor position */
649                  if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
650                                  &rootx, &rooty, &wx, &wy, &statemask))
651                          break;          /* on another screen */
652  
653 +                draw_grids(0);          /* clear old grid lines */
654 + #ifdef STEREO
655 +                pushright(); draw_grids(0); popright();
656 + #endif
657 +                                        /* compute view motion */
658                  if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
659                          sleep(1);
660 <                        continue;
660 >                        lasttime++;
661 >                        continue;       /* cursor in bad place */
662                  }
663 <                draw_grids();
664 <                dev_flush();
663 >                draw_grids(1);          /* redraw grid */
664 > #ifdef STEREO
665 >                pushright();
666 >                draw_grids(1);
667 >                smUpdate(&vwright, qlevel);
668 > #ifdef DOBJ
669 >                dobj_render();
670 > #endif
671 >                popright();
672 > #endif
673 >                                        /* redraw mesh */
674 >                smUpdate(&odev.v, qlevel);
675 > #ifdef DOBJ
676 >                dobj_render();          /* redraw object */
677 > #endif
678 >                glFlush();
679 >                nframes++;              /* figure out good quality level */
680 >                thistime = time(0);
681 >                if (thistime - lasttime >= 3 ||
682 >                                nframes > (int)(3*3*TARGETFPS)) {
683 >                        qlevel = thistime<=lasttime ? 1000 :
684 >                                (int)((double)nframes/(thistime-lasttime)
685 >                                        / TARGETFPS * qlevel + 0.5);
686 >                        lasttime = thistime; nframes = 0;
687 >                        if (qlevel > 99) {
688 >                                if (qlevel > 300) {     /* put on the brakes */
689 >                                        sleep(1);
690 >                                        lasttime++;
691 >                                }
692 >                                qlevel = 99;
693 >                        } else if (qlevel < 1)
694 >                                qlevel = 1;
695 >                }
696          }
697          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
698                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
699                  wx = levptr(XButtonReleasedEvent)->x;
700                  wy = levptr(XButtonReleasedEvent)->y;
701                  moveview(wx, odev.vres-1-wy, movdir, movorb);
702 +        }
703 + }
704 +
705 +
706 + static
707 + setglpersp(vp)                  /* set perspective view in GL */
708 + register VIEW   *vp;
709 + {
710 +        double  d, xmin, xmax, ymin, ymax;
711 +
712 +        if (mindpth >= maxdpth) {
713 +                dev_zmin = 1.;
714 +                dev_zmax = 100.;
715          } else {
716 <                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
717 <                redraw();
716 >                dev_zmin = 0.5*mindpth;
717 >                dev_zmax = 5.0*maxdpth;
718          }
719 <        quicken = 0;
720 <        dev_flush();
719 >        if (odev.v.vfore > FTINY)
720 >                dev_zmin = odev.v.vfore;
721 >        if (odev.v.vaft > FTINY)
722 >                dev_zmax = odev.v.vaft;
723 >        if (dev_zmin < dev_zmax/100.)
724 >                dev_zmin = dev_zmax/100.;
725 >        xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
726 >        xmin = -xmax;
727 >        d = odev.v.hoff * (xmax - xmin);
728 >        xmin += d; xmax += d;
729 >        ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
730 >        ymin = -ymax;
731 >        d = odev.v.voff * (ymax - ymin);
732 >        ymin += d; ymax += d;
733 >                                        /* set view matrix */
734 >        glMatrixMode(GL_PROJECTION);
735 >        glLoadIdentity();
736 >        glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
737 >        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
738 >                odev.v.vp[0] + odev.v.vdir[0],
739 >                odev.v.vp[1] + odev.v.vdir[1],
740 >                odev.v.vp[2] + odev.v.vdir[2],
741 >                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
742 >        checkglerr("setting perspective view");
743   }
744  
745  
746   static
747 + wipeclean()                     /* prepare for redraw */
748 + {
749 +                                        /* clear depth buffer */
750 + #ifdef STEREO
751 +        setstereobuf(STEREO_BUFFER_RIGHT);
752 +        glClear(GL_DEPTH_BUFFER_BIT);
753 +        setstereobuf(STEREO_BUFFER_LEFT);
754 + #endif
755 +        glClear(GL_DEPTH_BUFFER_BIT);
756 +        smClean();                      /* reset drawing routines */
757 +        setglpersp(&odev.v);            /* reset view & clipping planes */
758 + }
759 +
760 +
761 + static
762   getkey(ekey)                            /* get input key */
763   register XKeyPressedEvent  *ekey;
764   {
765 +        Window  rootw, childw;
766 +        int     rootx, rooty, wx, wy;
767 +        unsigned int    statemask;
768          int  n;
769          char    buf[8];
770  
# Line 886 | Line 787 | register XKeyPressedEvent  *ekey;
787          case 'v':                       /* spit out view */
788                  inpresflags |= DFL(DC_GETVIEW);
789                  return;
790 +        case 'f':                       /* frame view position */
791 +                if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
792 +                                &rootx, &rooty, &wx, &wy, &statemask))
793 +                        return;         /* on another screen */
794 +                sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres,
795 +                                1.-(wy+.5)/odev.vres);
796 +                inpresflags |= DFL(DC_FOCUS);
797 +                return;
798 +        case 'F':                       /* unfocus */
799 +                odev_args[0] = '\0';
800 +                inpresflags |= DFL(DC_FOCUS);
801 +                return;
802          case '\n':
803          case '\r':                      /* resume computation */
804                  inpresflags |= DFL(DC_RESUME);
805                  return;
806          case CTRL('R'):                 /* redraw screen */
807 <                TMapValues(1);
895 <                glClear(GL_DEPTH_BUFFER_BIT);
896 <                redraw();
807 >                wipeclean();
808                  return;
809          case CTRL('L'):                 /* refresh from server */
810                  if (inpresflags & DFL(DC_REDRAW))
811                          return;
812 +                setglpersp(&odev.v);            /* reset clipping planes */
813                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
814 <                glDisable(GL_DEPTH_TEST);
815 <                draw_grids();
814 >                glDisable(GL_DEPTH_TEST);       /* so grids will clear */
815 >                draw_grids(1);
816 > #ifdef STEREO
817 >                pushright();
818 >                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
819 >                draw_grids(1);
820 >                popright();
821 > #endif
822                  glEnable(GL_DEPTH_TEST);
823                  glFlush();
824 <                Compost(100);                   /* get rid of old values */
824 >                smInit(rsL.max_samp);           /* get rid of old values */
825                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
826 +                rayqleft = 0;                   /* hold off update */
827                  return;
828          case 'K':                       /* kill rtrace process(es) */
829                  inpresflags |= DFL(DC_KILL);
# Line 929 | Line 848 | static
848   fixwindow(eexp)                         /* repair damage to window */
849   register XExposeEvent  *eexp;
850   {
851 <        if (odev.hres == 0 || odev.vres == 0)   /* first exposure */
851 >        if (odev.hres == 0 | odev.vres == 0) {  /* first exposure */
852                  resizewindow((XConfigureEvent *)eexp);
853 <        if (eexp->width == odev.hres && eexp->height == odev.vres)
935 <                TMapValues(1);
936 <        if (!eexp->count) {
937 <                glClear(GL_DEPTH_BUFFER_BIT);
938 <                redraw();
853 >                return;
854          }
855 +        if (eexp->count)                /* wait for final exposure */
856 +                return;
857 +        wipeclean();                    /* clear depth */
858   }
859  
860  
# Line 944 | Line 862 | static
862   resizewindow(ersz)                      /* resize window */
863   register XConfigureEvent  *ersz;
864   {
865 +        glViewport(0, 0, ersz->width, ersz->height);
866 +
867          if (ersz->width == odev.hres && ersz->height == odev.vres)
868                  return;
869  
# Line 952 | Line 872 | register XConfigureEvent  *ersz;
872  
873          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
874          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
955
956        setGLview();
875  
876          inpresflags |= DFL(DC_SETVIEW);
877   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines