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.1 by gregl, Tue Dec 23 18:31:00 1997 UTC vs.
Revision 3.19 by gwlarson, Wed Aug 19 17:45:58 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 rview driver.
9 > * Based on old GLX driver using cones.
10 > *
11 > * Define symbol STEREO for stereo viewing.
12 > * Define symbol DOBJ for display object viewing.
13   */
14  
15 + #ifdef NOSTEREO
16 + #ifdef STEREO
17 + #undef STEREO
18 + #else
19 + #undef NOSTEREO
20 + #endif
21 + #endif
22 +
23   #include "standard.h"
24 < #include "tonemap.h"
14 < #include "rhdriver.h"
24 > #include "rhd_sample.h"
25  
26 < #include  <GL/glx.h>
27 <
28 < #include  "x11icon.h"
29 <
30 < #ifndef FEQ
21 < #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
26 > #include <sys/types.h>
27 > #include <GL/glx.h>
28 > #include <GL/glu.h>
29 > #ifdef STEREO
30 > #include <X11/extensions/SGIStereo.h>
31   #endif
32 <
33 < #ifndef int4
25 < #define int4    int
32 > #ifdef DOBJ
33 > #include "rhdobj.h"
34   #endif
35  
36 < #ifndef FREEPCT
37 < #define FREEPCT         25              /* percentage of values to free */
36 > #include "x11icon.h"
37 >
38 > #ifndef RAYQLEN
39 > #define RAYQLEN         250             /* max. rays to queue before flush */
40   #endif
41  
42 < #ifndef NCONEV
43 < #define NCONEV          7               /* number of cone base vertices */
42 > #ifndef FEQ
43 > #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
44   #endif
35 #ifndef CONEH
36 #define CONEH           0.25            /* cone height (fraction of depth) */
37 #endif
38 #ifndef CONEW
39 #define CONEW           0.035           /* cone width (fraction of screen) */
40 #endif
45  
46 < #define GAMMA           2.2             /* default gamma correction */
46 > #define GAMMA           1.4             /* default gamma correction */
47  
48   #define MOVPCT          7               /* percent distance to move /frame */
49   #define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
50   #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */
51   #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
52  
53 + #ifndef TARGETFPS
54 + #define TARGETFPS       4.0             /* target frames/sec during motion */
55 + #endif
56 +
57   #define MINWIDTH        480             /* minimum graphics window width */
58   #define MINHEIGHT       400             /* minimum graphics window height */
59  
# Line 53 | Line 61 | static char SCCSid[] = "$SunId$ SGI";
61  
62   #define BORWIDTH        5               /* border width */
63  
64 + #define setstereobuf(bid)       (glXWaitGL(), \
65 +                                XSGISetStereoBuffer(ourdisplay, gwind, bid), \
66 +                                glXWaitX())
67 +
68   #define  ourscreen      DefaultScreen(ourdisplay)
69   #define  ourroot        RootWindow(ourdisplay,ourscreen)
70   #define  ourmask        (StructureNotifyMask|ExposureMask|KeyPressMask|\
# Line 62 | Line 74 | static char SCCSid[] = "$SunId$ SGI";
74  
75   struct driver   odev;                   /* global device driver structure */
76  
77 + static VIEW     vwright;                /* right eye view */
78 +
79 + static int      rayqleft = 0;           /* rays left to queue before flush */
80 +
81   static XEvent  currentevent;            /* current event */
82  
83   static int  mapped = 0;                 /* window is mapped? */
68 static unsigned long  *pixval = NULL;   /* allocated pixels */
84   static unsigned long  ourblack=0, ourwhite=~0;
85  
86   static Display  *ourdisplay = NULL;     /* our display */
# Line 73 | Line 88 | static XVisualInfo  *ourvinf;          /* our visual informati
88   static Window  gwind = 0;               /* our graphics window */
89   static GLXContext       gctx;           /* our GLX context */
90  
76 static double   mindepth = FHUGE;       /* minimum depth value so far */
77 static double   maxdepth = 0.;          /* maximum depth value */
78
91   static double   pwidth, pheight;        /* pixel dimensions (mm) */
92  
93 < static FVECT    conev[NCONEV];          /* drawing cone */
82 < static double   coneh;                  /* cone height */
93 > static double   mindpth, maxdpth;       /* min. and max. depth */
94  
95 + double  dev_zmin, dev_zmax;             /* fore and aft clipping plane dist. */
96 +
97   static int      inpresflags;            /* input result flags */
98  
99   static int      headlocked = 0;         /* lock vertical motion */
100  
101 < static struct {
102 <        float           (*wp)[3];       /* world intersection point array */
90 <        int4            *wd;            /* world direction array */
91 <        TMbright        *brt;           /* encoded brightness array */
92 <        BYTE            (*chr)[3];      /* encoded chrominance array */
93 <        BYTE            (*rgb)[3];      /* tone-mapped color array */
94 <        BYTE            *alpha;         /* alpha values */
95 <        int             nl;             /* count of values */
96 <        int             bl, tl;         /* bottom and top (next) value index */
97 <        int             tml;            /* next value needing tone-mapping */
98 <        int             drl;            /* next value in need of drawing */
99 <        char            *base;          /* base of allocated memory */
100 < }       rV;                     /* our collection of values */
101 > static int  resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
102 >                setglpersp(), getmove(), fixwindow(), mytmflags();
103  
104 < #define redraw()        (rV.drl = rV.bl)
104 > #ifdef STEREO
105 > static int  pushright(), popright();
106 > #endif
107  
108 < static int  resizewindow(), getevent(), getkey(), moveview(),
105 <                setGLview(), getmove(), fixwindow(), mytmflags(),
106 <                newvalue(), drawvalue(), Compost(),
107 <                FindValue(), AllocValues(), TMapValues(), FreeValues();
108 > extern time_t   time();
109  
110  
111 < dev_open(id)                    /* initialize X11 driver */
111 > dev_open(id)                    /* initialize GLX driver */
112   char  *id;
113   {
114          extern char     *getenv();
115 +        static RGBPRIMS myprims = STDPRIMS;
116          static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
117                                  GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
118 <                                GLX_ALPHA_SIZE,8, GLX_DEPTH_SIZE,23,
119 <                                /*GLX_DOUBLEBUFFER,*/ None};
118 <        static int      atlOK[] = {GLX_RGBA, GLX_RED_SIZE,4,
119 <                                GLX_GREEN_SIZE,4, GLX_BLUE_SIZE,4,
120 <                                GLX_ALPHA_SIZE,4, GLX_DEPTH_SIZE,15,
121 <                                /*GLX_DOUBLEBUFFER,*/ None};
122 <        char    *gv;
118 >                                GLX_DEPTH_SIZE,15, None};
119 >        char    *ev;
120          double  gamval = GAMMA;
121 +        RGBPRIMP        dpri = stdprims;
122          XSetWindowAttributes    ourwinattr;
123          XWMHints        ourxwmhints;
124          XSizeHints      oursizhints;
125 +                                        /* check for unsupported stereo */
126 + #ifdef NOSTEREO
127 +        error(USER, "stereo display driver unavailable");
128 + #endif
129                                          /* open display server */
130          ourdisplay = XOpenDisplay(NULL);
131          if (ourdisplay == NULL)
132                  error(USER, "cannot open X-windows; DISPLAY variable set?\n");
133 + #ifdef STEREO
134 +        switch (XSGIQueryStereoMode(ourdisplay, ourroot)) {
135 +        case STEREO_TOP:
136 +        case STEREO_BOTTOM:
137 +                break;
138 +        case STEREO_OFF:
139 +                error(USER,
140 +        "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
141 +        case X_STEREO_UNSUPPORTED:
142 +                error(USER, "stereo mode not supported on this screen");
143 +        default:
144 +                error(INTERNAL, "unknown stereo mode");
145 +        }
146 + #endif
147                                          /* find a usable visual */
148          ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
149          if (ourvinf == NULL)
134                ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK);
135        if (ourvinf == NULL)
150                  error(USER, "no suitable visuals available");
151                                          /* get a context */
152          gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
153                                          /* set gamma and tone mapping */
154 <        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
155 <                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
156 <                gamval = atof(gv);
157 <        if (tmInit(mytmflags(), stdprims, gamval) == NULL)
154 >        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
155 >                        || (ev = getenv("DISPLAY_GAMMA")) != NULL)
156 >                gamval = atof(ev);
157 >        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
158 >                        sscanf(ev, "%f %f %f %f %f %f %f %f",
159 >                                &myprims[RED][CIEX],&myprims[RED][CIEY],
160 >                                &myprims[GRN][CIEX],&myprims[GRN][CIEY],
161 >                                &myprims[BLU][CIEX],&myprims[BLU][CIEY],
162 >                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
163 >                dpri = myprims;
164 >        if (tmInit(mytmflags(), dpri, gamval) == NULL)
165                  error(SYSTEM, "not enough memory in dev_open");
166                                          /* open window */
167          ourwinattr.background_pixel = ourblack;
# Line 151 | Line 172 | char  *id;
172                                  ourvinf->visual, AllocNone);
173          gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
174                  DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
175 + #ifdef STEREO
176 +                (DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2,
177 + #else
178                  DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
179 + #endif
180                  BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
181                  CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
182          if (gwind == 0)
# Line 164 | Line 189 | char  *id;
189                          gwind, x11icon_bits, x11icon_width, x11icon_height);
190          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
191          oursizhints.min_width = MINWIDTH;
192 + #ifdef STEREO
193 +        oursizhints.min_height = MINHEIGHT/2;
194 +        oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH;
195 +        oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) -
196 +                                        2*BORWIDTH)/2;
197 +        oursizhints.flags = PMinSize|PMaxSize;
198 + #else
199          oursizhints.min_height = MINHEIGHT;
200          oursizhints.flags = PMinSize;
201 + #endif
202          XSetNormalHints(ourdisplay, gwind, &oursizhints);
203 <                                        /* map the window and get its size */
204 <        XMapWindow(ourdisplay, gwind);
205 <        dev_input();
206 <                                        /* allocate our value list */
207 <        if (!AllocValues(DisplayWidth(ourdisplay,ourscreen) *
208 <                        DisplayHeight(ourdisplay,ourscreen) / 4))
209 <                error(SYSTEM, "insufficient memory for value storage");
203 >                                        /* set GLX context */
204 >        glXMakeCurrent(ourdisplay, gwind, gctx);
205 >        glEnable(GL_DEPTH_TEST);
206 >        glDepthFunc(GL_LEQUAL);
207 >        glShadeModel(GL_SMOOTH);
208 >        glDisable(GL_DITHER);
209 >        glDisable(GL_CULL_FACE);
210                                          /* figure out sensible view */
211          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
212                          DisplayWidth(ourdisplay, ourscreen);
213          pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
214                          DisplayHeight(ourdisplay, ourscreen);
215 + #ifdef STEREO
216 +        pheight *= 2.;
217 +        setstereobuf(STEREO_BUFFER_LEFT);
218 + #endif
219          copystruct(&odev.v, &stdview);
220          odev.v.type = VT_PER;
221 <        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
222 <        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
221 >                                        /* map the window */
222 >        XMapWindow(ourdisplay, gwind);
223 >        dev_input();                    /* sets size and view angles */
224 >                                        /* allocate our samples */
225 >        if (!smInit(DisplayWidth(ourdisplay,ourscreen) *
226 >                        DisplayHeight(ourdisplay,ourscreen) / 10))
227 >                error(SYSTEM, "insufficient memory for value storage");
228 >        mindpth = FHUGE; maxdpth = FTINY;
229          odev.name = id;
230          odev.ifd = ConnectionNumber(ourdisplay);
188                                        /* set GLX context and clear buffers */
189        glXMakeCurrent(ourdisplay, gwind, gctx);
190        glEnable(GL_DEPTH_TEST);
191        glDepthFunc(GL_LEQUAL);
192        glShadeModel(GL_FLAT);
193        glDisable(GL_DITHER);
194        glDrawBuffer(GL_FRONT_AND_BACK);
195        glReadBuffer(GL_BACK);
196        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
197        setGLview();                    /* initialize view */
231   }
232  
233  
234   dev_close()                     /* close our display and free resources */
235   {
236 +        smInit(0);
237 + #ifdef DOBJ
238 +        dobj_cleanup();
239 + #endif
240          glXMakeCurrent(ourdisplay, None, NULL);
241          glXDestroyContext(ourdisplay, gctx);
242          XDestroyWindow(ourdisplay, gwind);
# Line 207 | Line 244 | dev_close()                    /* close our display and free resources
244          XCloseDisplay(ourdisplay);
245          ourdisplay = NULL;
246          tmDone(NULL);
210        FreeValues();
247          odev.v.type = 0;
248          odev.hres = odev.vres = 0;
249          odev.ifd = -1;
250   }
251  
252  
253 + dev_clear()                     /* clear our representation */
254 + {
255 +        smInit(rsL.max_samp);
256 +        wipeclean();
257 +        rayqleft = 0;                   /* hold off update */
258 + }
259 +
260 +
261   int
262   dev_view(nv)                    /* assign new driver view */
263   register VIEW   *nv;
264   {
265 +        double  d;
266 +
267          if (nv->type != VT_PER ||               /* check view legality */
268 <                        nv->horiz > 120. || nv->vert > 120.) {
268 >                        nv->horiz > 160. || nv->vert > 160.) {
269                  error(COMMAND, "illegal view type/angle");
270 <                nv->type = VT_PER;
270 >                nv->type = odev.v.type;
271                  nv->horiz = odev.v.horiz;
272                  nv->vert = odev.v.vert;
273                  return(0);
# Line 234 | Line 280 | register VIEW  *nv;
280  
281                          dw -= 25;       /* for window frame */
282                          dh -= 50;
283 + #ifdef STEREO
284 +                        dh /= 2;
285 + #endif
286                          odev.hres = 2.*VIEWDIST/pwidth *
287                                          tan(PI/180./2.*nv->horiz);
288                          odev.vres = 2.*VIEWDIST/pheight *
# Line 247 | Line 296 | register VIEW  *nv;
296                                  odev.vres = dh;
297                          }
298                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
299 +                        dev_input();    /* get resize event */
300                  }
301 <                copystruct(&odev.v, nv);
302 <                setGLview();
301 >                copystruct(&odev.v, nv);        /* setview() already called */
302 >                setglpersp(&odev.v);
303 > #ifdef STEREO
304 >                copystruct(&vwright, nv);
305 >                d = eyesepdist / sqrt(nv->hn2);
306 >                VSUM(vwright.vp, nv->vp, nv->hvec, d);
307 >                /* setview(&vwright);   -- Unnecessary */
308 > #endif
309 >                checkglerr("setting view");
310          }
311 <        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
255 <        redraw();
311 >        wipeclean();
312          return(1);
313   }
314  
315  
316 + dev_auxcom(cmd, args)           /* process an auxiliary command */
317 + char    *cmd, *args;
318 + {
319 + #ifdef DOBJ
320 +        if (dobj_command(cmd, args) >= 0)
321 +                return;
322 + #endif
323 +        sprintf(errmsg, "%s: unknown command", cmd);
324 +        error(COMMAND, errmsg);
325 + }
326 +
327 +
328 + VIEW *
329 + dev_auxview(n, hvres)           /* return nth auxiliary view */
330 + int     n;
331 + int     hvres[2];
332 + {
333 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
334 +        if (n == 0)
335 +                return(&odev.v);
336 + #ifdef STEREO
337 +        if (n == 1)
338 +                return(&vwright);
339 + #endif
340 +        return(NULL);
341 + }
342 +
343 +
344   int
345   dev_input()                     /* get X11 input */
346   {
# Line 265 | Line 349 | dev_input()                    /* get X11 input */
349          do
350                  getevent();
351  
352 <        while (XQLength(ourdisplay) > 0);
352 >        while (XPending(ourdisplay) > 0);
353  
354 +        odev.inpready = 0;
355 +
356          return(inpresflags);
357   }
358  
359  
360 < dev_value(c, p, v)              /* add a pixel value to our list */
360 > dev_value(c, d, p)              /* add a pixel value to our mesh */
361   COLR    c;
362 < FVECT   p, v;
362 > FVECT   d, p;
363   {
364 <        register int    li;
365 <
366 <        li = newvalue();
367 <        VCOPY(rV.wp[li], p);
368 <        rV.wd[li] = encodedir(v);
369 <        tmCvColrs(&rV.brt[li], rV.chr[li], c, 1);
364 >        double  depth;
365 > #ifdef DOBJ
366 >        if (dobj_lightsamp != NULL) {   /* in light source sampling */
367 >                (*dobj_lightsamp)(c, d, p);
368 >                return;
369 >        }
370 > #endif
371 >        smNewSamp(c, d, p);             /* add to display representation */
372 >        if (p != NULL) {
373 >                depth = (p[0] - odev.v.vp[0])*d[0] +
374 >                        (p[1] - odev.v.vp[1])*d[1] +
375 >                        (p[2] - odev.v.vp[2])*d[2];
376 >                if (depth > FTINY) {
377 >                        if (depth < mindpth)
378 >                                mindpth = depth;
379 >                        if (depth > maxdpth)
380 >                                maxdpth = depth;
381 >                }
382 >        }
383 >        if (!--rayqleft)
384 >                dev_flush();            /* flush output */
385   }
386  
387  
388   int
389   dev_flush()                     /* flush output */
390   {
391 <        if (mapped) {
392 <                TMapValues(0);
393 <                while (rV.drl != rV.tl) {
394 <                        drawvalue(rV.drl);
395 <                        if (++rV.drl >= rV.nl)
396 <                                rV.drl = 0;
397 <                }
398 <                glFlush();
399 <        }
400 <        return(XPending(ourdisplay));
391 > #ifdef STEREO
392 >        pushright();                    /* update right eye */
393 >        glClear(GL_DEPTH_BUFFER_BIT);
394 >        smUpdate(&vwright, 100);
395 > #ifdef DOBJ
396 >        dobj_render();                  /* usually in foreground */
397 > #endif
398 >        popright();                     /* update left eye */
399 >        glClear(GL_DEPTH_BUFFER_BIT);
400 > #endif
401 >        smUpdate(&odev.v, 100);
402 > #ifdef DOBJ
403 >        dobj_render();
404 > #endif
405 >        glFlush();                      /* flush OGL */
406 >        checkglerr("flushing display");
407 >        rayqleft = RAYQLEN;
408 >                                        /* flush X11 and return # pending */
409 >        return(odev.inpready = XPending(ourdisplay));
410   }
411  
412  
413 < static int
414 < mytmflags()                     /* figure out tone mapping flags */
413 > checkglerr(where)               /* check for GL or GLU error */
414 > char    *where;
415   {
416 <        extern char     *progname;
307 <        register char   *cp, *tail;
308 <                                        /* find basic name */
309 <        for (cp = tail = progname; *cp; cp++)
310 <                if (*cp == '/')
311 <                        tail = cp+1;
312 <        for (cp = tail; *cp && *cp != '.'; cp++)
313 <                ;
314 <        if (cp-tail == 3 && !strncmp(tail, "glx", 3))
315 <                return(TM_F_CAMERA);
316 <        if (cp-tail == 4 && !strncmp(tail, "glxh", 4))
317 <                return(TM_F_HUMAN);
318 <        error(USER, "illegal driver name");
319 < }
416 >        register GLenum errcode;
417  
418 <
419 < static
420 < setGLview()                     /* set our GL view */
421 < {
325 <        double  xmin, xmax, ymin, ymax, zmin, zmax;
326 <        double  d, cx, sx, crad;
327 <        FVECT   vx, vy;
328 <        register int    i, j;
329 <                                        /* compute view frustum */
330 <        if (normalize(odev.v.vdir) == 0.0)
331 <                return;
332 <        if (mindepth < maxdepth) {
333 <                zmin = 0.25*mindepth;
334 <                zmax = 8.0*maxdepth;
335 <        } else {
336 <                zmin = 0.01;
337 <                zmax = 1000.;
418 >        while ((errcode = glGetError()) != GL_NO_ERROR) {
419 >                sprintf(errmsg, "OpenGL error %s: %s",
420 >                                where, gluErrorString(errcode));
421 >                error(WARNING, errmsg);
422          }
339        if (odev.v.vfore > FTINY)
340                zmin = odev.v.vfore;
341        if (odev.v.vaft > FTINY)
342                zmax = odev.v.vaft;
343        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
344        xmin = -xmax;
345        d = odev.v.hoff * (xmax - xmin);
346        xmin += d; xmax += d;
347        ymax = zmin * tan(PI/180./2. * odev.v.vert);
348        ymin = -ymax;
349        d = odev.v.voff * (ymax - ymin);
350        ymin += d; ymax += d;
351                                        /* set view matrix */
352        glMatrixMode(GL_PROJECTION);
353        glLoadIdentity();
354        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
355        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
356                odev.v.vp[0] + odev.v.vdir[0],
357                odev.v.vp[1] + odev.v.vdir[1],
358                odev.v.vp[2] + odev.v.vdir[2],
359                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
360                                        /* set viewport */
361        glViewport(0, 0, odev.hres, odev.vres);
362                                        /* initialize cone for Vornoi polys */
363        coneh = CONEH*(zmax - zmin);
364        crad = 0.5 * CONEW * 0.5*(xmax-xmin + ymax-ymin) * (zmin+coneh)/zmin;
365        vy[0] = vy[1] = vy[2] = 0.;
366        for (i = 0; i < 3; i++)
367                if (odev.v.vdir[i] < 0.6 && odev.v.vdir[i] > -0.6)
368                        break;
369        vy[i] = 1.;
370        fcross(vx, vy, odev.v.vdir);
371        normalize(vx);
372        fcross(vy, odev.v.vdir, vx);
373        for (j = 0, d = 0.; j < NCONEV; j++, d += 2.*PI/NCONEV) {
374                cx = crad*cos(d); sx = crad*sin(d);
375                for (i = 0; i < 3; i++)
376                        conev[j][i] = coneh*odev.v.vdir[i] +
377                                        cx*vx[i] + sx*vy[i];
378        }
423   }
424  
425  
426 < static int
383 < newvalue()                      /* allocate a leaf from our pile */
384 < {
385 <        int     li;
386 <        
387 <        li = rV.tl++;
388 <        if (rV.tl >= rV.nl)     /* get next leaf in ring */
389 <                rV.tl = 0;
390 <        if (rV.tl == rV.bl)     /* need to shake some free */
391 <                Compost(FREEPCT);
392 <        return(li);
393 < }
394 <
395 <
426 > #ifdef STEREO
427   static
428 < drawvalue(li)                   /* draw a pixel value as a cone */
398 < register int    li;
428 > pushright()                     /* push on right view */
429   {
430 <        static FVECT    apex, disp;
401 <        double  d, h, v;
402 <        register int    i;
403 <                                        /* compute cone's base displacement */
404 <        VCOPY(apex, rV.wp[li]);
405 <        disp[0] = apex[0] - odev.v.vp[0];
406 <        disp[1] = apex[1] - odev.v.vp[1];
407 <        disp[2] = apex[2] - odev.v.vp[2];
408 <        if ((d = DOT(disp,odev.v.vdir)) <= odev.v.vfore || d <= FTINY)
409 <                return;
410 <        if (d > 1e5) {          /* background region */
411 <                if (odev.v.vaft > FTINY)
412 <                        return;
413 <                h = maxdepth/d;
414 <                disp[0] *= h; disp[1] *= h; disp[2] *= h;
415 <                apex[0] = odev.v.vp[0] + disp[0];
416 <                apex[1] = odev.v.vp[1] + disp[1];
417 <                apex[2] = odev.v.vp[2] + disp[2];
418 <                d = maxdepth;
419 <        } else if (d > maxdepth)
420 <                maxdepth = d;
421 <        h = DOT(disp,odev.v.hvec)/(d*odev.v.hn2);
422 <        v = DOT(disp,odev.v.vvec)/(d*odev.v.vn2);
423 <        if (fabs(h - odev.v.hoff) < 0.5 && fabs(v - odev.v.voff) < 0.5 &&
424 <                        d < mindepth)
425 < {
426 < fprintf(stderr, "min depth now %lf (%lf,%lf,%lf)\n", d, apex[0], apex[1], apex[2]);
427 <                mindepth = d;
428 < }
429 <        for (i = 0; i < 3; i++)
430 <                disp[i] = apex[i] + coneh*(h*odev.v.hvec[i] + v*odev.v.vvec[i]);
431 <                                        /* draw pyramid (cone approx.) */
432 <        glColor4ub(rV.rgb[li][0], rV.rgb[li][1], rV.rgb[li][2], rV.alpha[li]);
433 <        glBegin(GL_TRIANGLE_FAN);
434 <        glVertex3f(apex[0], apex[1], apex[2]);
435 <        for (i = 0; i < NCONEV; i++)
436 <                glVertex3d(conev[i][0] + disp[0], conev[i][1] + disp[1],
437 <                                conev[i][2] + disp[2]);
438 <                                        /* connect last side to first */
439 <        glVertex3d(conev[0][0] + disp[0], conev[0][1] + disp[1],
440 <                        conev[0][2] + disp[2]);
441 <        glEnd();                        /* done */
442 < }
430 >        double  d;
431  
432 <
433 < #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
434 <                        sizeof(TMbright)+7*sizeof(BYTE))
435 <
436 < static
449 < AllocValues(n)                  /* allocate space for n values */
450 < register int    n;
451 < {
452 <        unsigned        nbytes;
453 <        register unsigned       i;
454 <
455 <        if (n <= 0)
456 <                return(0);
457 <        if (rV.nl >= n)
458 <                return(rV.nl);
459 <        else if (rV.nl > 0)
460 <                free(rV.base);
461 <                                /* round space up to nearest power of 2 */
462 <        nbytes = n*LEAFSIZ + 8;
463 <        for (i = 1024; nbytes > i; i <<= 1)
464 <                ;
465 <        n = (i - 8) / LEAFSIZ;  /* should we make sure n is even? */
466 <        rV.base = (char *)malloc(n*LEAFSIZ);
467 <        if (rV.base == NULL)
468 <                return(0);
469 <                                /* assign larger alignment types earlier */
470 <        rV.wp = (float (*)[3])rV.base;
471 <        rV.wd = (int4 *)(rV.wp + n);
472 <        rV.brt = (TMbright *)(rV.wd + n);
473 <        rV.chr = (BYTE (*)[3])(rV.brt + n);
474 <        rV.rgb = (BYTE (*)[3])(rV.chr + n);
475 <        rV.alpha = (BYTE *)(rV.rgb + n);
476 <        rV.nl = n;
477 <        rV.drl = rV.tml = rV.bl = rV.tl = 0;
478 <        return(n);
432 >        setstereobuf(STEREO_BUFFER_RIGHT);
433 >        glMatrixMode(GL_MODELVIEW);
434 >        glPushMatrix();
435 >        d = -eyesepdist / sqrt(odev.v.hn2);
436 >        glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]);
437   }
438  
481 #undef  LEAFSIZ
439  
483
440   static
441 < FreeValues()                    /* free our allocated values */
441 > popright()                      /* pop off right view */
442   {
443 <        if (rV.nl <= 0)
444 <                return;
445 <        free(rV.base);
490 <        rV.base = NULL;
491 <        rV.nl = 0;
443 >        glMatrixMode(GL_MODELVIEW);
444 >        glPopMatrix();
445 >        setstereobuf(STEREO_BUFFER_LEFT);
446   }
447 + #endif
448  
449  
495 static
496 TMapValues(redo)                /* map our values to RGB */
497 int     redo;
498 {
499        int     aorg, alen, borg, blen;
500                                        /* recompute mapping? */
501        if (redo)
502                rV.tml = rV.bl;
503                                        /* already done? */
504        if (rV.tml == rV.tl)
505                return(1);
506                                        /* compute segments */
507        aorg = rV.tml;
508        if (rV.tl >= aorg) {
509                alen = rV.tl - aorg;
510                blen = 0;
511        } else {
512                alen = rV.nl - aorg;
513                borg = 0;
514                blen = rV.tl;
515        }
516                                        /* (re)compute tone mapping? */
517        if (rV.tml == rV.bl) {
518                tmClearHisto();
519                tmAddHisto(rV.brt+aorg, alen, 1);
520                if (blen > 0)
521                        tmAddHisto(rV.brt+borg, blen, 1);
522                if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
523                        return(0);
524                rV.drl = rV.bl;
525        }
526        if (tmMapPixels(rV.rgb+aorg, rV.brt+aorg,
527                        rV.chr+aorg, alen) != TM_E_OK)
528                return(0);
529        if (blen > 0)
530                tmMapPixels(rV.rgb+borg, rV.brt+borg,
531                                rV.chr+borg, blen);
532                                        /* compute unique alpha values */
533        rV.tml = rV.tl;
534        return(1);
535 }
536
537
450   static int
451 < Compost(pct)                    /* free up some values */
540 < int     pct;
451 > mytmflags()                     /* figure out tone mapping flags */
452   {
453 <        int     nused, nclear, nmapped, ndrawn;
454 <                                /* figure out how many values to clear */
455 <        nclear = rV.nl * pct / 100;
456 <        nused = rV.tl - rV.bl;
457 <        if (nused <= 0) nused += rV.nl;
458 <        nclear -= rV.nl - nused;
459 <        if (nclear <= 0)
460 <                return(0);
461 <        if (nclear >= nused) {  /* clear them all */
462 <                rV.drl = rV.tml = rV.bl = rV.tl = 0;
463 <                return(nused);
464 <        }
554 <                                /* else clear values from bottom */
555 <        ndrawn = rV.drl - rV.bl;
556 <        if (ndrawn < 0) ndrawn += rV.nl;
557 <        nmapped = rV.tml - rV.bl;
558 <        if (nmapped < 0) nmapped += rV.nl;
559 <        rV.bl += nclear;
560 <        if (rV.bl >= rV.nl) rV.bl -= rV.nl;
561 <        if (ndrawn < nclear) rV.drl = rV.bl;
562 <        if (nmapped < nclear) rV.tml = rV.bl;
563 <        return(nclear);
453 >        extern char     *progname;
454 >        register char   *cp, *tail;
455 >                                        /* find basic name */
456 >        for (cp = tail = progname; *cp; cp++)
457 >                if (*cp == '/')
458 >                        tail = cp+1;
459 >        for (cp = tail; *cp && *cp != '.'; cp++)
460 >                ;
461 >        if (cp > tail && cp[-1] == 'h')
462 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
463 >        else
464 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
465   }
466  
467  
# Line 603 | Line 504 | register FVECT wp[2];
504   static
505   draw_grids()                    /* draw holodeck section grids */
506   {
507 <        static GLubyte  gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
507 >        static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
508  
509          if (!mapped)
510                  return;
511          glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
512 <                                        /* draw each grid line */
612 <        glBegin(GL_LINES);
512 >        glBegin(GL_LINES);              /* draw each grid line */
513          gridlines(draw3dline);
514          glEnd();
515   }
516  
517  
618 static int
619 FindValue(dx, dy)               /* find a value on the display */
620 int     dx, dy;
621 {
622        return(-1);             /* not found */
623 }
624
625
518   static
519   moveview(dx, dy, mov, orb)      /* move our view */
520   int     dx, dy, mov, orb;
521   {
522          VIEW    nv;
523 <        FVECT   odir, v1;
523 >        FVECT   odir, v1, wip;
524          double  d;
525          register int    li;
526                                  /* start with old view */
527          copystruct(&nv, &odev.v);
528                                  /* change view direction */
529 +        if (viewray(v1, odir, &odev.v,
530 +                        (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
531 +                return(0);              /* outside view */
532          if (mov | orb) {
533 <                if ((li = FindValue(dx, dy)) < 0)
533 > #ifdef DOBJ
534 >                d = dobj_trace(v1, odir);       /* check objects */
535 >                                                /* is holodeck in front? */
536 >                if ((li = smFindSamp(v1, odir)) >= 0 &&
537 >                                (rsL.wp[li][0] - nv.vp[0])*odir[0] +
538 >                                (rsL.wp[li][1] - nv.vp[1])*odir[1] +
539 >                                (rsL.wp[li][2] - nv.vp[2])*odir[2] < d)
540 >                        VCOPY(wip, rsL.wp[li]);
541 >                else if (d < .99*FHUGE)         /* object is closer */
542 >                        VSUM(wip, nv.vp, odir, d);
543 >                else                            /* nothing visible */
544 >                        return(0);
545 > #else
546 >                if ((li = smFindSamp(v1, odir)) < 0)
547                          return(0);      /* not on window */
548 <                VSUM(odir, rV.wp[li], nv.vp, -1.);
549 <        } else {
550 <                if (viewray(nv.vp, nv.vdir, &odev.v,
551 <                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
552 <                        return(0);      /* outside view */
645 <        }
548 >                VCOPY(wip, rsL.wp[li]);
549 > #endif
550 >                VSUM(odir, wip, nv.vp, -1.);
551 >        } else
552 >                VCOPY(nv.vdir, odir);
553          if (orb && mov) {               /* orbit left/right */
554                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
555 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
555 >                VSUM(nv.vp, wip, odir, -1.);
556                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
557          } else if (orb) {               /* orbit up/down */
558                  fcross(v1, odir, nv.vup);
559                  if (normalize(v1) == 0.)
560                          return(0);
561                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
562 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
562 >                VSUM(nv.vp, wip, odir, -1.);
563                  spinvector(nv.vdir, nv.vdir, v1, d);
564          } else if (mov) {               /* move forward/backward */
565                  d = MOVPCT/100. * mov;
# Line 677 | Line 584 | XButtonPressedEvent    *ebut;
584   {
585          int     movdir = MOVDIR(ebut->button);
586          int     movorb = MOVORB(ebut->state);
587 +        int     qlevel = 99;
588 +        time_t  lasttime, thistime;
589 +        int     nframes;
590          Window  rootw, childw;
591          int     rootx, rooty, wx, wy;
592          unsigned int    statemask;
593  
594          XNoOp(ourdisplay);
595  
596 +        lasttime = time(0); nframes = 0;
597          while (!XCheckMaskEvent(ourdisplay,
598                          ButtonReleaseMask, levptr(XEvent))) {
599  
# Line 692 | Line 603 | XButtonPressedEvent    *ebut;
603  
604                  if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
605                          sleep(1);
606 +                        lasttime++;
607                          continue;
608                  }
609 + #ifdef STEREO
610 +                pushright();
611 +                glClear(GL_COLOR_BUFFER_BIT);
612                  draw_grids();
613 <                dev_flush();
613 >                smUpdate(&vwright, qlevel);
614 > #ifdef DOBJ
615 >                dobj_render();
616 > #endif
617 >                popright();
618 > #endif
619 >                glClear(GL_COLOR_BUFFER_BIT);
620 >                draw_grids();
621 >                smUpdate(&odev.v, qlevel);
622 > #ifdef DOBJ
623 >                dobj_render();
624 > #endif
625 >                glFlush();
626 >                checkglerr("moving view");
627 >                nframes++;
628 >                thistime = time(0);
629 >                if (thistime - lasttime >= 3 ||
630 >                                nframes > (int)(3*3*TARGETFPS)) {
631 >                        qlevel = thistime<=lasttime ? 1000 :
632 >                                (int)((double)nframes/(thistime-lasttime)
633 >                                        / TARGETFPS * qlevel + 0.5);
634 >                        lasttime = thistime; nframes = 0;
635 >                        if (qlevel > 99) {
636 >                                if (qlevel > 300) {     /* put on the brakes */
637 >                                        sleep(1);
638 >                                        lasttime++;
639 >                                }
640 >                                qlevel = 99;
641 >                        } else if (qlevel < 1)
642 >                                qlevel = 1;
643 >                }
644          }
645          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
646                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 703 | Line 648 | XButtonPressedEvent    *ebut;
648                  wy = levptr(XButtonReleasedEvent)->y;
649                  moveview(wx, odev.vres-1-wy, movdir, movorb);
650          }
706        dev_flush();
651   }
652  
653  
654   static
655 + setglpersp(vp)                  /* set perspective view in GL */
656 + register VIEW   *vp;
657 + {
658 +        double  d, xmin, xmax, ymin, ymax;
659 +
660 +        if (mindpth >= maxdpth) {
661 +                dev_zmin = 0.1;
662 +                dev_zmax = 100.;
663 +        } else {
664 +                dev_zmin = 0.5*mindpth;
665 +                dev_zmax = 1.5*maxdpth;
666 +                if (dev_zmin > dev_zmax/100.)
667 +                        dev_zmin = dev_zmax/100.;
668 +        }
669 +        if (odev.v.vfore > FTINY)
670 +                dev_zmin = odev.v.vfore;
671 +        if (odev.v.vaft > FTINY)
672 +                dev_zmax = odev.v.vaft;
673 +        if (dev_zmin < dev_zmax/5000.)
674 +                dev_zmin = dev_zmax/5000.;
675 +        xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
676 +        xmin = -xmax;
677 +        d = odev.v.hoff * (xmax - xmin);
678 +        xmin += d; xmax += d;
679 +        ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
680 +        ymin = -ymax;
681 +        d = odev.v.voff * (ymax - ymin);
682 +        ymin += d; ymax += d;
683 +                                        /* set view matrix */
684 +        glMatrixMode(GL_PROJECTION);
685 +        glLoadIdentity();
686 +        glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
687 +        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
688 +                odev.v.vp[0] + odev.v.vdir[0],
689 +                odev.v.vp[1] + odev.v.vdir[1],
690 +                odev.v.vp[2] + odev.v.vdir[2],
691 +                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
692 + }
693 +
694 +
695 + static
696 + wipeclean()                     /* prepare for redraw */
697 + {
698 + #ifdef STEREO
699 +        setstereobuf(STEREO_BUFFER_RIGHT);
700 +        glClear(GL_DEPTH_BUFFER_BIT);
701 +        setstereobuf(STEREO_BUFFER_LEFT);
702 + #endif
703 +        glClear(GL_DEPTH_BUFFER_BIT);
704 +        smClean();
705 + }
706 +
707 +
708 + static
709   getkey(ekey)                            /* get input key */
710   register XKeyPressedEvent  *ekey;
711   {
# Line 738 | Line 736 | register XKeyPressedEvent  *ekey;
736                  inpresflags |= DFL(DC_RESUME);
737                  return;
738          case CTRL('R'):                 /* redraw screen */
739 <                TMapValues(1);
742 <                redraw();
739 >                wipeclean();
740                  return;
741          case CTRL('L'):                 /* refresh from server */
742                  if (inpresflags & DFL(DC_REDRAW))
743                          return;
744                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
748                glDrawBuffer(GL_FRONT);
745                  glDisable(GL_DEPTH_TEST);
746                  draw_grids();
747 + #ifdef STEREO
748 +                pushright();
749 +                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
750 +                draw_grids();
751 +                popright();
752 + #endif
753                  glEnable(GL_DEPTH_TEST);
752                glDrawBuffer(GL_FRONT_AND_BACK);
754                  glFlush();
755 <                Compost(100);                   /* get rid of old values */
755 >                smInit(rsL.max_samp);           /* get rid of old values */
756                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
757 +                rayqleft = 0;                   /* hold off update */
758                  return;
759          case 'K':                       /* kill rtrace process(es) */
760                  inpresflags |= DFL(DC_KILL);
# Line 777 | Line 779 | static
779   fixwindow(eexp)                         /* repair damage to window */
780   register XExposeEvent  *eexp;
781   {
782 <        if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
783 <                odev.hres = eexp->width;
784 <                odev.vres = eexp->height;
782 >        if (odev.hres == 0 | odev.vres == 0) {  /* first exposure */
783 >                resizewindow((XConfigureEvent *)eexp);
784 >                return;
785          }
786 <        if (eexp->width == odev.hres && eexp->height == odev.vres)
787 <                TMapValues(1);
788 <        if (!eexp->count)
787 <                redraw();
786 >        if (eexp->count)                /* wait for final exposure */
787 >                return;
788 >        wipeclean();                    /* clear depth */
789   }
790  
791  
# Line 792 | Line 793 | static
793   resizewindow(ersz)                      /* resize window */
794   register XConfigureEvent  *ersz;
795   {
796 +        glViewport(0, 0, ersz->width, ersz->height);
797 +
798          if (ersz->width == odev.hres && ersz->height == odev.vres)
799                  return;
800  
# Line 800 | Line 803 | register XConfigureEvent  *ersz;
803  
804          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
805          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
803
804        setGLview();
806  
807          inpresflags |= DFL(DC_SETVIEW);
808   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines