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.23 by gwlarson, Thu Aug 27 19:33:05 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 + #ifdef STEREO
78 + static VIEW     vwright;                /* right eye view */
79 + #endif
80 +
81 + static int      rayqleft = 0;           /* rays left to queue before flush */
82 +
83   static XEvent  currentevent;            /* current event */
84  
85   static int  mapped = 0;                 /* window is mapped? */
68 static unsigned long  *pixval = NULL;   /* allocated pixels */
86   static unsigned long  ourblack=0, ourwhite=~0;
87  
88   static Display  *ourdisplay = NULL;     /* our display */
# Line 73 | Line 90 | static XVisualInfo  *ourvinf;          /* our visual informati
90   static Window  gwind = 0;               /* our graphics window */
91   static GLXContext       gctx;           /* our GLX context */
92  
76 static double   mindepth = FHUGE;       /* minimum depth value so far */
77 static double   maxdepth = 0.;          /* maximum depth value */
78
93   static double   pwidth, pheight;        /* pixel dimensions (mm) */
94  
95 < static FVECT    conev[NCONEV];          /* drawing cone */
82 < static double   coneh;                  /* cone height */
95 > static double   mindpth, maxdpth;       /* min. and max. depth */
96  
97 + double  dev_zmin, dev_zmax;             /* fore and aft clipping plane dist. */
98 +
99   static int      inpresflags;            /* input result flags */
100  
101   static int      headlocked = 0;         /* lock vertical motion */
102  
103 < static struct {
104 <        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 */
103 > static int  resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
104 >                setglpersp(), getmove(), fixwindow(), mytmflags();
105  
106 < #define redraw()        (rV.drl = rV.bl)
106 > #ifdef STEREO
107 > static int  pushright(), popright();
108 > #endif
109  
110 < static int  resizewindow(), getevent(), getkey(), moveview(),
105 <                setGLview(), getmove(), fixwindow(), mytmflags(),
106 <                newvalue(), drawvalue(), Compost(),
107 <                FindValue(), AllocValues(), TMapValues(), FreeValues();
110 > extern time_t   time();
111  
112  
113 < dev_open(id)                    /* initialize X11 driver */
113 > dev_open(id)                    /* initialize GLX driver */
114   char  *id;
115   {
116          extern char     *getenv();
117 +        static RGBPRIMS myprims = STDPRIMS;
118          static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
119                                  GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
120 <                                GLX_ALPHA_SIZE,8, GLX_DEPTH_SIZE,23,
121 <                                /*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;
120 >                                GLX_DEPTH_SIZE,15, None};
121 >        char    *ev;
122          double  gamval = GAMMA;
123 +        RGBPRIMP        dpri = stdprims;
124          XSetWindowAttributes    ourwinattr;
125          XWMHints        ourxwmhints;
126          XSizeHints      oursizhints;
127 +                                        /* check for unsupported stereo */
128 + #ifdef NOSTEREO
129 +        error(INTERNAL, "stereo display driver unavailable");
130 + #endif
131                                          /* open display server */
132          ourdisplay = XOpenDisplay(NULL);
133          if (ourdisplay == NULL)
134                  error(USER, "cannot open X-windows; DISPLAY variable set?\n");
135 + #ifdef STEREO
136 +        switch (XSGIQueryStereoMode(ourdisplay, ourroot)) {
137 +        case STEREO_TOP:
138 +        case STEREO_BOTTOM:
139 +                break;
140 +        case STEREO_OFF:
141 +                error(USER,
142 +        "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
143 +        case X_STEREO_UNSUPPORTED:
144 +                error(USER, "stereo mode not supported on this screen");
145 +        default:
146 +                error(INTERNAL, "unknown stereo mode");
147 +        }
148 + #endif
149                                          /* find a usable visual */
150          ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
151          if (ourvinf == NULL)
134                ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK);
135        if (ourvinf == NULL)
152                  error(USER, "no suitable visuals available");
153                                          /* get a context */
154          gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
155                                          /* set gamma and tone mapping */
156 <        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
157 <                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
158 <                gamval = atof(gv);
159 <        if (tmInit(mytmflags(), stdprims, gamval) == NULL)
156 >        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
157 >                        || (ev = getenv("DISPLAY_GAMMA")) != NULL)
158 >                gamval = atof(ev);
159 >        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
160 >                        sscanf(ev, "%f %f %f %f %f %f %f %f",
161 >                                &myprims[RED][CIEX],&myprims[RED][CIEY],
162 >                                &myprims[GRN][CIEX],&myprims[GRN][CIEY],
163 >                                &myprims[BLU][CIEX],&myprims[BLU][CIEY],
164 >                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
165 >                dpri = myprims;
166 >        if (tmInit(mytmflags(), dpri, gamval) == NULL)
167                  error(SYSTEM, "not enough memory in dev_open");
168                                          /* open window */
169          ourwinattr.background_pixel = ourblack;
# Line 151 | Line 174 | char  *id;
174                                  ourvinf->visual, AllocNone);
175          gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
176                  DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
177 + #ifdef STEREO
178 +                (DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2,
179 + #else
180                  DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
181 + #endif
182                  BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
183                  CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
184          if (gwind == 0)
# Line 164 | Line 191 | char  *id;
191                          gwind, x11icon_bits, x11icon_width, x11icon_height);
192          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
193          oursizhints.min_width = MINWIDTH;
194 + #ifdef STEREO
195 +        oursizhints.min_height = MINHEIGHT/2;
196 +        oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH;
197 +        oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) -
198 +                                        2*BORWIDTH)/2;
199 +        oursizhints.flags = PMinSize|PMaxSize;
200 + #else
201          oursizhints.min_height = MINHEIGHT;
202          oursizhints.flags = PMinSize;
203 + #endif
204          XSetNormalHints(ourdisplay, gwind, &oursizhints);
205 <                                        /* map the window and get its size */
206 <        XMapWindow(ourdisplay, gwind);
207 <        dev_input();
208 <                                        /* allocate our value list */
209 <        if (!AllocValues(DisplayWidth(ourdisplay,ourscreen) *
210 <                        DisplayHeight(ourdisplay,ourscreen) / 4))
211 <                error(SYSTEM, "insufficient memory for value storage");
205 >                                        /* set GLX context */
206 >        glXMakeCurrent(ourdisplay, gwind, gctx);
207 >        glEnable(GL_DEPTH_TEST);
208 >        glDepthFunc(GL_LEQUAL);
209 >        glShadeModel(GL_SMOOTH);
210 >        glDisable(GL_DITHER);
211 >        glDisable(GL_CULL_FACE);
212                                          /* figure out sensible view */
213          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
214                          DisplayWidth(ourdisplay, ourscreen);
215          pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
216                          DisplayHeight(ourdisplay, ourscreen);
217 + #ifdef STEREO
218 +        pheight *= 2.;
219 +        setstereobuf(STEREO_BUFFER_LEFT);
220 + #endif
221 +        checkglerr("setting rendering parameters");
222          copystruct(&odev.v, &stdview);
223          odev.v.type = VT_PER;
224 <        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
225 <        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
224 >                                        /* map the window */
225 >        XMapWindow(ourdisplay, gwind);
226 >        dev_input();                    /* sets size and view angles */
227 >                                        /* allocate our samples */
228 >        if (!smInit(DisplayWidth(ourdisplay,ourscreen) *
229 >                        DisplayHeight(ourdisplay,ourscreen) / 10))
230 >                error(SYSTEM, "insufficient memory for value storage");
231 >        mindpth = FHUGE; maxdpth = FTINY;
232          odev.name = id;
233          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 */
234   }
235  
236  
237   dev_close()                     /* close our display and free resources */
238   {
239 +        smInit(0);
240 + #ifdef DOBJ
241 +        dobj_cleanup();
242 + #endif
243          glXMakeCurrent(ourdisplay, None, NULL);
244          glXDestroyContext(ourdisplay, gctx);
245          XDestroyWindow(ourdisplay, gwind);
# Line 207 | Line 247 | dev_close()                    /* close our display and free resources
247          XCloseDisplay(ourdisplay);
248          ourdisplay = NULL;
249          tmDone(NULL);
210        FreeValues();
250          odev.v.type = 0;
251          odev.hres = odev.vres = 0;
252          odev.ifd = -1;
253   }
254  
255  
256 + dev_clear()                     /* clear our representation */
257 + {
258 +        smInit(rsL.max_samp);
259 +        wipeclean();
260 +        rayqleft = 0;                   /* hold off update */
261 + }
262 +
263 +
264   int
265   dev_view(nv)                    /* assign new driver view */
266   register VIEW   *nv;
267   {
268 +        double  d;
269 +
270          if (nv->type != VT_PER ||               /* check view legality */
271 <                        nv->horiz > 120. || nv->vert > 120.) {
271 >                        nv->horiz > 160. || nv->vert > 160.) {
272                  error(COMMAND, "illegal view type/angle");
273 <                nv->type = VT_PER;
273 >                nv->type = odev.v.type;
274                  nv->horiz = odev.v.horiz;
275                  nv->vert = odev.v.vert;
276                  return(0);
277          }
278          if (nv != &odev.v) {
279 <                if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
279 >                                                /* resize window? */
280 >                if (!FEQ(nv->horiz,odev.v.horiz) ||
281                                  !FEQ(nv->vert,odev.v.vert)) {
282                          int     dw = DisplayWidth(ourdisplay,ourscreen);
283                          int     dh = DisplayHeight(ourdisplay,ourscreen);
284  
285                          dw -= 25;       /* for window frame */
286                          dh -= 50;
287 + #ifdef STEREO
288 +                        dh /= 2;
289 + #endif
290                          odev.hres = 2.*VIEWDIST/pwidth *
291                                          tan(PI/180./2.*nv->horiz);
292                          odev.vres = 2.*VIEWDIST/pheight *
# Line 247 | Line 300 | register VIEW  *nv;
300                                  odev.vres = dh;
301                          }
302                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
303 +                        dev_input();    /* get resize event */
304                  }
305 <                copystruct(&odev.v, nv);
306 <                setGLview();
305 >                copystruct(&odev.v, nv);        /* setview() already called */
306 > #ifdef STEREO
307 >                copystruct(&vwright, nv);
308 >                d = eyesepdist / sqrt(nv->hn2);
309 >                VSUM(vwright.vp, nv->vp, nv->hvec, d);
310 >                /* setview(&vwright);   -- Unnecessary */
311 > #endif
312          }
313 <        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
255 <        redraw();
313 >        wipeclean();
314          return(1);
315   }
316  
317  
318 + dev_auxcom(cmd, args)           /* process an auxiliary command */
319 + char    *cmd, *args;
320 + {
321 + #ifdef DOBJ
322 +        if (dobj_command(cmd, args) >= 0)
323 +                return;
324 + #endif
325 +        sprintf(errmsg, "%s: unknown command", cmd);
326 +        error(COMMAND, errmsg);
327 + }
328 +
329 +
330 + VIEW *
331 + dev_auxview(n, hvres)           /* return nth auxiliary view */
332 + int     n;
333 + int     hvres[2];
334 + {
335 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
336 +        if (n == 0)
337 +                return(&odev.v);
338 + #ifdef STEREO
339 +        if (n == 1)
340 +                return(&vwright);
341 + #endif
342 +        return(NULL);
343 + }
344 +
345 +
346   int
347   dev_input()                     /* get X11 input */
348   {
# Line 265 | Line 351 | dev_input()                    /* get X11 input */
351          do
352                  getevent();
353  
354 <        while (XQLength(ourdisplay) > 0);
354 >        while (XPending(ourdisplay) > 0);
355  
356 +        odev.inpready = 0;
357 +
358          return(inpresflags);
359   }
360  
361  
362 < dev_value(c, p, v)              /* add a pixel value to our list */
362 > dev_value(c, d, p)              /* add a pixel value to our mesh */
363   COLR    c;
364 < FVECT   p, v;
364 > FVECT   d, p;
365   {
366 <        register int    li;
367 <
368 <        li = newvalue();
369 <        VCOPY(rV.wp[li], p);
370 <        rV.wd[li] = encodedir(v);
371 <        tmCvColrs(&rV.brt[li], rV.chr[li], c, 1);
366 >        double  depth;
367 > #ifdef DOBJ
368 >        if (dobj_lightsamp != NULL) {   /* in light source sampling */
369 >                (*dobj_lightsamp)(c, d, p);
370 >                return;
371 >        }
372 > #endif
373 >        if (p != NULL) {                /* add depth to our range */
374 >                depth = (p[0] - odev.v.vp[0])*d[0] +
375 >                        (p[1] - odev.v.vp[1])*d[1] +
376 >                        (p[2] - odev.v.vp[2])*d[2];
377 >                if (depth > FTINY) {
378 >                        if (depth < mindpth)
379 >                                mindpth = depth;
380 >                        if (depth > maxdpth)
381 >                                maxdpth = depth;
382 >                }
383 >        }
384 >        smNewSamp(c, d, p);             /* add to display representation */
385 >        if (!--rayqleft)
386 >                dev_flush();            /* flush output */
387   }
388  
389  
# Line 288 | Line 391 | int
391   dev_flush()                     /* flush output */
392   {
393          if (mapped) {
394 <                TMapValues(0);
395 <                while (rV.drl != rV.tl) {
396 <                        drawvalue(rV.drl);
397 <                        if (++rV.drl >= rV.nl)
398 <                                rV.drl = 0;
399 <                }
400 <                glFlush();
394 > #ifdef STEREO
395 >                pushright();                    /* update right eye */
396 >                smUpdate(&vwright, 100);
397 > #ifdef DOBJ
398 >                dobj_render();                  /* usually in foreground */
399 > #endif
400 >                popright();                     /* update left eye */
401 > #endif
402 >                smUpdate(&odev.v, 100);
403 >                checkglerr("rendering mesh");
404 > #ifdef DOBJ
405 >                dobj_render();
406 > #endif
407 >                glFlush();                      /* flush OGL */
408          }
409 <        return(XPending(ourdisplay));
409 >        rayqleft = RAYQLEN;
410 >                                        /* flush X11 and return # pending */
411 >        return(odev.inpready = XPending(ourdisplay));
412   }
413  
414  
415 < static int
416 < mytmflags()                     /* figure out tone mapping flags */
415 > checkglerr(where)               /* check for GL or GLU error */
416 > char    *where;
417   {
418 <        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 < }
418 >        register GLenum errcode;
419  
420 <
421 < static
422 < setGLview()                     /* set our GL view */
423 < {
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.;
420 >        while ((errcode = glGetError()) != GL_NO_ERROR) {
421 >                sprintf(errmsg, "OpenGL error %s: %s",
422 >                                where, gluErrorString(errcode));
423 >                error(WARNING, errmsg);
424          }
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        }
425   }
426  
427  
428 < 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 <
428 > #ifdef STEREO
429   static
430 < drawvalue(li)                   /* draw a pixel value as a cone */
398 < register int    li;
430 > pushright()                     /* push on right view */
431   {
432 <        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 < }
432 >        double  d;
433  
434 <
435 < #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
436 <                        sizeof(TMbright)+7*sizeof(BYTE))
437 <
438 < static
439 < 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);
434 >        setstereobuf(STEREO_BUFFER_RIGHT);
435 >        glMatrixMode(GL_MODELVIEW);
436 >        glPushMatrix();
437 >        d = -eyesepdist / sqrt(odev.v.hn2);
438 >        glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]);
439 >        checkglerr("setting right view");
440   }
441  
481 #undef  LEAFSIZ
442  
483
443   static
444 < FreeValues()                    /* free our allocated values */
444 > popright()                      /* pop off right view */
445   {
446 <        if (rV.nl <= 0)
447 <                return;
448 <        free(rV.base);
490 <        rV.base = NULL;
491 <        rV.nl = 0;
446 >        glMatrixMode(GL_MODELVIEW);
447 >        glPopMatrix();
448 >        setstereobuf(STEREO_BUFFER_LEFT);
449   }
450 + #endif
451  
452  
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
453   static int
454 < Compost(pct)                    /* free up some values */
540 < int     pct;
454 > mytmflags()                     /* figure out tone mapping flags */
455   {
456 <        int     nused, nclear, nmapped, ndrawn;
457 <                                /* figure out how many values to clear */
458 <        nclear = rV.nl * pct / 100;
459 <        nused = rV.tl - rV.bl;
460 <        if (nused <= 0) nused += rV.nl;
461 <        nclear -= rV.nl - nused;
462 <        if (nclear <= 0)
463 <                return(0);
464 <        if (nclear >= nused) {  /* clear them all */
465 <                rV.drl = rV.tml = rV.bl = rV.tl = 0;
466 <                return(nused);
467 <        }
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);
456 >        extern char     *progname;
457 >        register char   *cp, *tail;
458 >                                        /* find basic name */
459 >        for (cp = tail = progname; *cp; cp++)
460 >                if (*cp == '/')
461 >                        tail = cp+1;
462 >        for (cp = tail; *cp && *cp != '.'; cp++)
463 >                ;
464 >        if (cp > tail && cp[-1] == 'h')
465 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
466 >        else
467 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
468   }
469  
470  
# Line 601 | Line 505 | register FVECT wp[2];
505  
506  
507   static
508 < draw_grids()                    /* draw holodeck section grids */
508 > draw_grids(fore)                /* draw holodeck section grids */
509 > int     fore;
510   {
511 <        static GLubyte  gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
512 <
513 <        if (!mapped)
514 <                return;
515 <        glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
611 <                                        /* draw each grid line */
612 <        glBegin(GL_LINES);
511 >        if (fore)
512 >                glColor4ub(0, 255, 255, 0);
513 >        else
514 >                glColor4ub(0, 0, 0, 0);
515 >        glBegin(GL_LINES);              /* draw each grid line */
516          gridlines(draw3dline);
517          glEnd();
518 +        checkglerr("drawing grid lines");
519   }
520  
521  
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
522   static
523   moveview(dx, dy, mov, orb)      /* move our view */
524   int     dx, dy, mov, orb;
525   {
526          VIEW    nv;
527 <        FVECT   odir, v1;
527 >        FVECT   odir, v1, wip;
528          double  d;
529          register int    li;
530                                  /* start with old view */
531          copystruct(&nv, &odev.v);
532 <                                /* change view direction */
533 <        if (mov | orb) {
534 <                if ((li = FindValue(dx, dy)) < 0)
532 >                                /* orient our motion */
533 >        if (viewray(v1, odir, &odev.v,
534 >                        (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
535 >                return(0);              /* outside view */
536 >        if (mov | orb) {        /* moving relative to geometry */
537 > #ifdef DOBJ
538 >                d = dobj_trace(NULL, v1, odir); /* check objects */
539 >                                                /* check holodeck */
540 >                if ((li = smFindSamp(v1, odir)) >= 0) {
541 >                        VCOPY(wip, rsL.wp[li]);
542 >                        if (d < .99*FHUGE && d*d <= dist2(v1, wip))
543 >                                li = -1;        /* object is closer */
544 >                } else if (d >= .99*FHUGE)
545 >                        return(0);              /* nothing visible */
546 >                if (li < 0)
547 >                        VSUM(wip, v1, odir, d); /* else get object point */
548 > #else
549 >                if ((li = smFindSamp(v1, odir)) < 0)
550                          return(0);      /* not on window */
551 <                VSUM(odir, rV.wp[li], nv.vp, -1.);
552 <        } else {
553 <                if (viewray(nv.vp, nv.vdir, &odev.v,
554 <                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
555 <                        return(0);      /* outside view */
645 <        }
551 >                VCOPY(wip, rsL.wp[li]);
552 > #endif
553 >                VSUM(odir, wip, odev.v.vp, -1.);
554 >        } else                  /* panning with constant viewpoint */
555 >                VCOPY(nv.vdir, odir);
556          if (orb && mov) {               /* orbit left/right */
557                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
558 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
558 >                VSUM(nv.vp, wip, odir, -1.);
559                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
560          } else if (orb) {               /* orbit up/down */
561                  fcross(v1, odir, nv.vup);
562                  if (normalize(v1) == 0.)
563                          return(0);
564                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
565 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
565 >                VSUM(nv.vp, wip, odir, -1.);
566                  spinvector(nv.vdir, nv.vdir, v1, d);
567          } else if (mov) {               /* move forward/backward */
568                  d = MOVPCT/100. * mov;
# Line 660 | Line 570 | int    dx, dy, mov, orb;
570          }
571          if (!mov ^ !orb && headlocked) {        /* restore head height */
572                  VSUM(v1, odev.v.vp, nv.vp, -1.);
573 <                d = DOT(v1, odev.v.vup);
573 >                d = DOT(v1, nv.vup);
574                  VSUM(nv.vp, nv.vp, odev.v.vup, d);
575          }
576          if (setview(&nv) != NULL)
# Line 677 | Line 587 | XButtonPressedEvent    *ebut;
587   {
588          int     movdir = MOVDIR(ebut->button);
589          int     movorb = MOVORB(ebut->state);
590 +        int     qlevel = 99;
591 +        time_t  lasttime, thistime;
592 +        int     nframes;
593          Window  rootw, childw;
594          int     rootx, rooty, wx, wy;
595          unsigned int    statemask;
596  
597 <        XNoOp(ourdisplay);
597 >        XNoOp(ourdisplay);              /* makes sure we're not idle */
598  
599 +        lasttime = time(0); nframes = 0;
600          while (!XCheckMaskEvent(ourdisplay,
601                          ButtonReleaseMask, levptr(XEvent))) {
602 <
602 >                                        /* get cursor position */
603                  if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
604                                  &rootx, &rooty, &wx, &wy, &statemask))
605                          break;          /* on another screen */
606  
607 +                draw_grids(0);          /* clear old grid lines */
608 + #ifdef STEREO
609 +                pushright(); draw_grids(0); popright();
610 + #endif
611 +                                        /* compute view motion */
612                  if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
613                          sleep(1);
614 <                        continue;
614 >                        lasttime++;
615 >                        continue;       /* cursor in bad place */
616                  }
617 <                draw_grids();
618 <                dev_flush();
617 >                draw_grids(1);          /* redraw grid */
618 > #ifdef STEREO
619 >                pushright();
620 >                draw_grids(1);
621 >                smUpdate(&vwright, qlevel);
622 > #ifdef DOBJ
623 >                dobj_render();
624 > #endif
625 >                popright();
626 > #endif
627 >                                        /* redraw mesh */
628 >                smUpdate(&odev.v, qlevel);
629 > #ifdef DOBJ
630 >                dobj_render();          /* redraw object */
631 > #endif
632 >                glFlush();
633 >                nframes++;              /* figure out good quality level */
634 >                thistime = time(0);
635 >                if (thistime - lasttime >= 3 ||
636 >                                nframes > (int)(3*3*TARGETFPS)) {
637 >                        qlevel = thistime<=lasttime ? 1000 :
638 >                                (int)((double)nframes/(thistime-lasttime)
639 >                                        / TARGETFPS * qlevel + 0.5);
640 >                        lasttime = thistime; nframes = 0;
641 >                        if (qlevel > 99) {
642 >                                if (qlevel > 300) {     /* put on the brakes */
643 >                                        sleep(1);
644 >                                        lasttime++;
645 >                                }
646 >                                qlevel = 99;
647 >                        } else if (qlevel < 1)
648 >                                qlevel = 1;
649 >                }
650          }
651          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
652                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 703 | Line 654 | XButtonPressedEvent    *ebut;
654                  wy = levptr(XButtonReleasedEvent)->y;
655                  moveview(wx, odev.vres-1-wy, movdir, movorb);
656          }
706        dev_flush();
657   }
658  
659  
660   static
661 + setglpersp(vp)                  /* set perspective view in GL */
662 + register VIEW   *vp;
663 + {
664 +        double  d, xmin, xmax, ymin, ymax;
665 +
666 +        if (mindpth >= maxdpth) {
667 +                dev_zmin = 0.1;
668 +                dev_zmax = 100.;
669 +        } else {
670 +                dev_zmin = 0.5*mindpth;
671 +                dev_zmax = 1.5*maxdpth;
672 +                if (dev_zmin > dev_zmax/100.)
673 +                        dev_zmin = dev_zmax/100.;
674 +        }
675 +        if (odev.v.vfore > FTINY)
676 +                dev_zmin = odev.v.vfore;
677 +        if (odev.v.vaft > FTINY)
678 +                dev_zmax = odev.v.vaft;
679 +        if (dev_zmin < dev_zmax/5000.)
680 +                dev_zmin = dev_zmax/5000.;
681 +        xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
682 +        xmin = -xmax;
683 +        d = odev.v.hoff * (xmax - xmin);
684 +        xmin += d; xmax += d;
685 +        ymax = dev_zmin * tan(PI/180./2. * odev.v.vert);
686 +        ymin = -ymax;
687 +        d = odev.v.voff * (ymax - ymin);
688 +        ymin += d; ymax += d;
689 +                                        /* set view matrix */
690 +        glMatrixMode(GL_PROJECTION);
691 +        glLoadIdentity();
692 +        glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax);
693 +        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
694 +                odev.v.vp[0] + odev.v.vdir[0],
695 +                odev.v.vp[1] + odev.v.vdir[1],
696 +                odev.v.vp[2] + odev.v.vdir[2],
697 +                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
698 +        checkglerr("setting perspective view");
699 + }
700 +
701 +
702 + static
703 + wipeclean()                     /* prepare for redraw */
704 + {
705 +                                        /* clear depth buffer */
706 + #ifdef STEREO
707 +        setstereobuf(STEREO_BUFFER_RIGHT);
708 +        glClear(GL_DEPTH_BUFFER_BIT);
709 +        setstereobuf(STEREO_BUFFER_LEFT);
710 + #endif
711 +        glClear(GL_DEPTH_BUFFER_BIT);
712 +        smClean();                      /* reset drawing routines */
713 +        setglpersp(&odev.v);            /* reset view & clipping planes */
714 + }
715 +
716 +
717 + static
718   getkey(ekey)                            /* get input key */
719   register XKeyPressedEvent  *ekey;
720   {
# Line 738 | Line 745 | register XKeyPressedEvent  *ekey;
745                  inpresflags |= DFL(DC_RESUME);
746                  return;
747          case CTRL('R'):                 /* redraw screen */
748 <                TMapValues(1);
742 <                redraw();
748 >                wipeclean();
749                  return;
750          case CTRL('L'):                 /* refresh from server */
751                  if (inpresflags & DFL(DC_REDRAW))
752                          return;
753 +                setglpersp(&odev.v);            /* reset clipping planes */
754                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
755 <                glDrawBuffer(GL_FRONT);
756 <                glDisable(GL_DEPTH_TEST);
757 <                draw_grids();
755 >                glDisable(GL_DEPTH_TEST);       /* so grids will clear */
756 >                draw_grids(1);
757 > #ifdef STEREO
758 >                pushright();
759 >                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
760 >                draw_grids(1);
761 >                popright();
762 > #endif
763                  glEnable(GL_DEPTH_TEST);
752                glDrawBuffer(GL_FRONT_AND_BACK);
764                  glFlush();
765 <                Compost(100);                   /* get rid of old values */
765 >                smInit(rsL.max_samp);           /* get rid of old values */
766                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
767 +                rayqleft = 0;                   /* hold off update */
768                  return;
769          case 'K':                       /* kill rtrace process(es) */
770                  inpresflags |= DFL(DC_KILL);
# Line 777 | Line 789 | static
789   fixwindow(eexp)                         /* repair damage to window */
790   register XExposeEvent  *eexp;
791   {
792 <        if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
793 <                odev.hres = eexp->width;
794 <                odev.vres = eexp->height;
792 >        if (odev.hres == 0 | odev.vres == 0) {  /* first exposure */
793 >                resizewindow((XConfigureEvent *)eexp);
794 >                return;
795          }
796 <        if (eexp->width == odev.hres && eexp->height == odev.vres)
797 <                TMapValues(1);
798 <        if (!eexp->count)
787 <                redraw();
796 >        if (eexp->count)                /* wait for final exposure */
797 >                return;
798 >        wipeclean();                    /* clear depth */
799   }
800  
801  
# Line 792 | Line 803 | static
803   resizewindow(ersz)                      /* resize window */
804   register XConfigureEvent  *ersz;
805   {
806 +        glViewport(0, 0, ersz->width, ersz->height);
807 +
808          if (ersz->width == odev.hres && ersz->height == odev.vres)
809                  return;
810  
# Line 800 | Line 813 | register XConfigureEvent  *ersz;
813  
814          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
815          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
803
804        setGLview();
816  
817          inpresflags |= DFL(DC_SETVIEW);
818   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines