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.18 by gwlarson, Wed Jun 17 15:44:07 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 x11 driver.
10   */
11  
12   #include "standard.h"
13 < #include "tonemap.h"
14 < #include "rhdriver.h"
13 > #include "rhd_qtree.h"
14  
15   #include  <GL/glx.h>
16  
17   #include  "x11icon.h"
18  
19 + #ifndef RAYQLEN
20 + #define RAYQLEN         50000           /* max. rays to queue before flush */
21 + #endif
22 +
23   #ifndef FEQ
24   #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
25   #endif
26  
27 < #ifndef int4
28 < #define int4    int
27 > #ifndef MAXCONE
28 > #define MAXCONE         16              /* number of different cone sizes */
29   #endif
30 <
31 < #ifndef FREEPCT
29 < #define FREEPCT         25              /* percentage of values to free */
30 > #ifndef MAXVERT
31 > #define MAXVERT         64              /* maximum number of cone vertices */
32   #endif
33 <
34 < #ifndef NCONEV
33 < #define NCONEV          7               /* number of cone base vertices */
33 > #ifndef MINVERT
34 > #define MINVERT         4               /* minimum number of cone vertices */
35   #endif
36 < #ifndef CONEH
37 < #define CONEH           0.25            /* cone height (fraction of depth) */
36 > #ifndef DEPTHFACT
37 > #define DEPTHFACT       16.             /* multiplier for depth tests */
38   #endif
38 #ifndef CONEW
39 #define CONEW           0.035           /* cone width (fraction of screen) */
40 #endif
39  
40 < #define GAMMA           2.2             /* default gamma correction */
40 > #define GAMMA           1.4             /* default gamma correction */
41  
42   #define MOVPCT          7               /* percent distance to move /frame */
43   #define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
# Line 65 | Line 63 | struct driver  odev;                   /* global device driver structur
63   static XEvent  currentevent;            /* current event */
64  
65   static int  mapped = 0;                 /* window is mapped? */
68 static unsigned long  *pixval = NULL;   /* allocated pixels */
66   static unsigned long  ourblack=0, ourwhite=~0;
67  
68   static Display  *ourdisplay = NULL;     /* our display */
# Line 73 | Line 70 | static XVisualInfo  *ourvinf;          /* our visual informati
70   static Window  gwind = 0;               /* our graphics window */
71   static GLXContext       gctx;           /* our GLX context */
72  
76 static double   mindepth = FHUGE;       /* minimum depth value so far */
77 static double   maxdepth = 0.;          /* maximum depth value */
78
73   static double   pwidth, pheight;        /* pixel dimensions (mm) */
74  
75 < static FVECT    conev[NCONEV];          /* drawing cone */
76 < static double   coneh;                  /* cone height */
75 > static double   curzmax = 1e4;          /* current depth upper limit */
76 > static double   nxtzmax = 0.;           /* maximum (finite) depth so far */
77  
78 + static struct {
79 +        double  rad;            /* cone radius */
80 +        int     nverts;         /* number of vertices */
81 +        FVECT   *va;            /* allocated vertex array */
82 + } cone[MAXCONE];        /* precomputed cones for drawing */
83 +
84   static int      inpresflags;            /* input result flags */
85  
86   static int      headlocked = 0;         /* lock vertical motion */
87  
88 static struct {
89        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
102 #define redraw()        (rV.drl = rV.bl)
103
88   static int  resizewindow(), getevent(), getkey(), moveview(),
89 <                setGLview(), getmove(), fixwindow(), mytmflags(),
90 <                newvalue(), drawvalue(), Compost(),
107 <                FindValue(), AllocValues(), TMapValues(), FreeValues();
89 >                initcones(), freecones(),
90 >                getmove(), fixwindow(), mytmflags();
91  
92  
93   dev_open(id)                    /* initialize X11 driver */
94   char  *id;
95   {
96          extern char     *getenv();
97 +        static RGBPRIMS myprims = STDPRIMS;
98          static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
99                                  GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
100 <                                GLX_ALPHA_SIZE,8, GLX_DEPTH_SIZE,23,
101 <                                /*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;
100 >                                GLX_DEPTH_SIZE,15, None};
101 >        char    *ev;
102          double  gamval = GAMMA;
103 +        RGBPRIMP        dpri = stdprims;
104          XSetWindowAttributes    ourwinattr;
105          XWMHints        ourxwmhints;
106          XSizeHints      oursizhints;
107 +                                        /* set quadtree globals */
108 +        qtMinNodesiz = 3;
109 +        qtDepthEps = 0.07;
110                                          /* open display server */
111          ourdisplay = XOpenDisplay(NULL);
112          if (ourdisplay == NULL)
# Line 131 | Line 114 | char  *id;
114                                          /* find a usable visual */
115          ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
116          if (ourvinf == NULL)
134                ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK);
135        if (ourvinf == NULL)
117                  error(USER, "no suitable visuals available");
118                                          /* get a context */
119          gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
120                                          /* set gamma and tone mapping */
121 <        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
122 <                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
123 <                gamval = atof(gv);
124 <        if (tmInit(mytmflags(), stdprims, gamval) == NULL)
121 >        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
122 >                        || (ev = getenv("DISPLAY_GAMMA")) != NULL)
123 >                gamval = atof(ev);
124 >        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
125 >                        sscanf(ev, "%f %f %f %f %f %f %f %f",
126 >                                &myprims[RED][CIEX],&myprims[RED][CIEY],
127 >                                &myprims[GRN][CIEX],&myprims[GRN][CIEY],
128 >                                &myprims[BLU][CIEX],&myprims[BLU][CIEY],
129 >                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
130 >                dpri = myprims;
131 >        if (tmInit(mytmflags(), dpri, gamval) == NULL)
132                  error(SYSTEM, "not enough memory in dev_open");
133                                          /* open window */
134          ourwinattr.background_pixel = ourblack;
# Line 167 | Line 155 | char  *id;
155          oursizhints.min_height = MINHEIGHT;
156          oursizhints.flags = PMinSize;
157          XSetNormalHints(ourdisplay, gwind, &oursizhints);
158 <                                        /* map the window and get its size */
159 <        XMapWindow(ourdisplay, gwind);
160 <        dev_input();
161 <                                        /* allocate our value list */
162 <        if (!AllocValues(DisplayWidth(ourdisplay,ourscreen) *
163 <                        DisplayHeight(ourdisplay,ourscreen) / 4))
164 <                error(SYSTEM, "insufficient memory for value storage");
158 >                                        /* set GLX context */
159 >        glXMakeCurrent(ourdisplay, gwind, gctx);
160 >        glEnable(GL_DEPTH_TEST);
161 >        glDepthFunc(GL_LEQUAL);
162 >        glShadeModel(GL_FLAT);
163 >        glDisable(GL_DITHER);
164 >        glDisable(GL_CULL_FACE);
165 >        glMatrixMode(GL_PROJECTION);
166 >        glOrtho(0., 1., 0., 1., -.01, 1.01);
167 >        glTranslated(0., 0., -1.01);
168                                          /* figure out sensible view */
169          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
170                          DisplayWidth(ourdisplay, ourscreen);
# Line 181 | Line 172 | char  *id;
172                          DisplayHeight(ourdisplay, ourscreen);
173          copystruct(&odev.v, &stdview);
174          odev.v.type = VT_PER;
175 <        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
176 <        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
175 >                                        /* map the window */
176 >        XMapWindow(ourdisplay, gwind);
177 >        dev_input();                    /* sets size and view angles */
178 >                                        /* allocate our leaf pile */
179 >        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
180 >                        DisplayHeight(ourdisplay,ourscreen) * 3 /
181 >                        (qtMinNodesiz*qtMinNodesiz*2)))
182 >                error(SYSTEM, "insufficient memory for value storage");
183          odev.name = id;
184          odev.ifd = ConnectionNumber(ourdisplay);
185 <                                        /* set GLX context and clear buffers */
186 <        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 */
185 >                                        /* initialize cone array */
186 >        initcones();
187   }
188  
189  
# Line 206 | Line 195 | dev_close()                    /* close our display and free resources
195          gwind = 0;
196          XCloseDisplay(ourdisplay);
197          ourdisplay = NULL;
198 +        qtFreeLeaves();
199          tmDone(NULL);
200 <        FreeValues();
200 >        freecones();
201          odev.v.type = 0;
202          odev.hres = odev.vres = 0;
203          odev.ifd = -1;
204   }
205  
206  
207 + dev_clear()                     /* clear our quadtree */
208 + {
209 +        qtCompost(100);
210 +        glClear(GL_DEPTH_BUFFER_BIT);
211 +        rayqleft = 0;                   /* hold off update */
212 + }
213 +
214 +
215   int
216   dev_view(nv)                    /* assign new driver view */
217   register VIEW   *nv;
218   {
219 <        if (nv->type != VT_PER ||               /* check view legality */
220 <                        nv->horiz > 120. || nv->vert > 120.) {
219 >        if (nv->type == VT_PAR ||               /* check view legality */
220 >                        nv->horiz > 160. || nv->vert > 160.) {
221                  error(COMMAND, "illegal view type/angle");
222 <                nv->type = VT_PER;
222 >                nv->type = odev.v.type;
223                  nv->horiz = odev.v.horiz;
224                  nv->vert = odev.v.vert;
225                  return(0);
226          }
227 +        if (nv->vfore > FTINY) {
228 +                error(COMMAND, "cannot handle fore clipping");
229 +                nv->vfore = 0.;
230 +                return(0);
231 +        }
232          if (nv != &odev.v) {
233                  if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
234                                  !FEQ(nv->vert,odev.v.vert)) {
# Line 247 | Line 250 | register VIEW  *nv;
250                                  odev.vres = dh;
251                          }
252                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
253 +                        dev_input();    /* get resize event */
254                  }
255                  copystruct(&odev.v, nv);
252                setGLview();
256          }
257 <        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
258 <        redraw();
257 >        if (nxtzmax > FTINY) {
258 >                curzmax = nxtzmax;
259 >                nxtzmax = 0.;
260 >        }
261 >        glClear(GL_DEPTH_BUFFER_BIT);
262 >        qtReplant();
263          return(1);
264   }
265  
266  
267 + dev_auxcom(cmd, args)           /* process an auxiliary command */
268 + char    *cmd, *args;
269 + {
270 +        sprintf(errmsg, "%s: unknown command", cmd);
271 +        error(COMMAND, errmsg);
272 + }
273 +
274 +
275 + VIEW *
276 + dev_auxview(n, hvres)           /* return nth auxiliary view */
277 + int     n;
278 + int     hvres[2];
279 + {
280 +        if (n)
281 +                return(NULL);
282 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
283 +        return(&odev.v);
284 + }
285 +
286 +
287   int
288   dev_input()                     /* get X11 input */
289   {
# Line 271 | Line 298 | dev_input()                    /* get X11 input */
298   }
299  
300  
301 < dev_value(c, p, v)              /* add a pixel value to our list */
302 < COLR    c;
276 < FVECT   p, v;
301 > int
302 > dev_flush()                     /* flush output */
303   {
304 <        register int    li;
305 <
306 <        li = newvalue();
307 <        VCOPY(rV.wp[li], p);
282 <        rV.wd[li] = encodedir(v);
283 <        tmCvColrs(&rV.brt[li], rV.chr[li], c, 1);
304 >        qtUpdate();
305 >        glFlush();
306 >        rayqleft = RAYQLEN;
307 >        return(XPending(ourdisplay));
308   }
309  
310  
311 < int
312 < dev_flush()                     /* flush output */
311 > dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */
312 > BYTE    rgb[3];
313 > FVECT   ip;
314 > double  rad;
315   {
316 <        if (mapped) {
317 <                TMapValues(0);
318 <                while (rV.drl != rV.tl) {
319 <                        drawvalue(rV.drl);
320 <                        if (++rV.drl >= rV.nl)
321 <                                rV.drl = 0;
322 <                }
323 <                glFlush();
316 >        register int    ci, j;
317 >        double  apexh, basez;
318 >                                        /* is window mapped? */
319 >        if (!mapped)
320 >                return;
321 >                                        /* compute apex height (0. to 1.) */
322 >        if (ip[2] > 1e6)
323 >                apexh = 1. - 1./DEPTHFACT;
324 >        else {
325 >                if (ip[2] > nxtzmax)
326 >                        nxtzmax = ip[2];
327 >                if (ip[2] >= curzmax)
328 >                        apexh = 1. - 1./DEPTHFACT;
329 >                else
330 >                        apexh = 1. - ip[2]/(curzmax*DEPTHFACT);
331          }
332 <        return(XPending(ourdisplay));
332 >        rad *= 1.25;                    /* find conservative cone match */
333 >        for (ci = 0; ci < MAXCONE-1; ci++)
334 >                if (cone[ci].rad >= rad)
335 >                        break;
336 >                                        /* draw it */
337 >        glColor3ub(rgb[0], rgb[1], rgb[2]);
338 >        glBegin(GL_TRIANGLE_FAN);
339 >        glVertex3d(ip[0], ip[1], apexh);        /* start with apex */
340 >        basez = apexh*cone[ci].va[0][2];        /* base z's all the same */
341 >        for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */
342 >                glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1],
343 >                                basez);
344 >                                                /* connect last to first */
345 >        glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez);
346 >        glEnd();                                /* all done */
347   }
348  
349  
# Line 312 | Line 359 | mytmflags()                    /* figure out tone mapping flags */
359          for (cp = tail; *cp && *cp != '.'; cp++)
360                  ;
361          if (cp-tail == 3 && !strncmp(tail, "glx", 3))
362 <                return(TM_F_CAMERA);
362 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
363          if (cp-tail == 4 && !strncmp(tail, "glxh", 4))
364 <                return(TM_F_HUMAN);
364 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
365          error(USER, "illegal driver name");
366   }
367  
368  
369   static
370 < setGLview()                     /* set our GL view */
370 > initcones()                     /* initialize cone vertices */
371   {
325        double  xmin, xmax, ymin, ymax, zmin, zmax;
326        double  d, cx, sx, crad;
327        FVECT   vx, vy;
372          register int    i, j;
373 <                                        /* compute view frustum */
374 <        if (normalize(odev.v.vdir) == 0.0)
375 <                return;
376 <        if (mindepth < maxdepth) {
377 <                zmin = 0.25*mindepth;
378 <                zmax = 8.0*maxdepth;
379 <        } else {
380 <                zmin = 0.01;
381 <                zmax = 1000.;
373 >        double  minrad, d;
374 >
375 >        if (cone[0].nverts)
376 >                freecones();
377 >        minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) +
378 >                                        DisplayHeight(ourdisplay,ourscreen));
379 >        for (i = 0; i < MAXCONE; i++) {
380 >                d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */
381 >                cone[i].rad = minrad + (1.-minrad)*d;
382 >                cone[i].nverts = MINVERT + 0.5 + (MAXVERT-MINVERT)*d;
383 >                cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT));
384 >                if (cone[i].va == NULL)
385 >                        error(SYSTEM, "out of memory in initcones");
386 >                for (j = cone[i].nverts; j--; ) {
387 >                        d = 2.*PI * (j+.5) / (cone[i].nverts);
388 >                        cone[i].va[j][0] = cos(d) * cone[i].rad;
389 >                        cone[i].va[j][1] = sin(d) * cone[i].rad;
390 >                        cone[i].va[j][2] = 1. - cone[i].rad;
391 >                }
392          }
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        }
393   }
394  
395  
382 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
396   static
397 < drawvalue(li)                   /* draw a pixel value as a cone */
398 < register int    li;
397 > freecones()                     /* free cone vertices */
398   {
400        static FVECT    apex, disp;
401        double  d, h, v;
399          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 }
400  
401 <
402 < #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
403 <                        sizeof(TMbright)+7*sizeof(BYTE))
404 <
405 < static
406 < 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);
401 >        for (i = MAXCONE; i--; )
402 >                if (cone[i].nverts) {
403 >                        free((char *)cone[i].va);
404 >                        cone[i].va = NULL;
405 >                        cone[i].nverts = 0;
406 >                }
407   }
408  
481 #undef  LEAFSIZ
409  
483
410   static
485 FreeValues()                    /* free our allocated values */
486 {
487        if (rV.nl <= 0)
488                return;
489        free(rV.base);
490        rV.base = NULL;
491        rV.nl = 0;
492 }
493
494
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
538 static int
539 Compost(pct)                    /* free up some values */
540 int     pct;
541 {
542        int     nused, nclear, nmapped, ndrawn;
543                                /* figure out how many values to clear */
544        nclear = rV.nl * pct / 100;
545        nused = rV.tl - rV.bl;
546        if (nused <= 0) nused += rV.nl;
547        nclear -= rV.nl - nused;
548        if (nclear <= 0)
549                return(0);
550        if (nclear >= nused) {  /* clear them all */
551                rV.drl = rV.tml = rV.bl = rV.tl = 0;
552                return(nused);
553        }
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);
564 }
565
566
567 static
411   getevent()                      /* get next event */
412   {
413          XNextEvent(ourdisplay, levptr(XEvent));
# Line 603 | Line 446 | register FVECT wp[2];
446   static
447   draw_grids()                    /* draw holodeck section grids */
448   {
449 <        static GLubyte  gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
450 <
451 <        if (!mapped)
449 >        static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
450 >        double  xmin, xmax, ymin, ymax, zmin, zmax;
451 >        double  d, cx, sx, crad;
452 >        FVECT   vx, vy;
453 >        register int    i, j;
454 >                                        /* can we even do it? */
455 >        if (!mapped || odev.v.type != VT_PER)
456                  return;
457 +                                        /* compute view frustum */
458 +        if (normalize(odev.v.vdir) == 0.0)
459 +                return;
460 +        zmin = 0.01;
461 +        zmax = 10000.;
462 +        if (odev.v.vfore > FTINY)
463 +                zmin = odev.v.vfore;
464 +        if (odev.v.vaft > FTINY)
465 +                zmax = odev.v.vaft;
466 +        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
467 +        xmin = -xmax;
468 +        d = odev.v.hoff * (xmax - xmin);
469 +        xmin += d; xmax += d;
470 +        ymax = zmin * tan(PI/180./2. * odev.v.vert);
471 +        ymin = -ymax;
472 +        d = odev.v.voff * (ymax - ymin);
473 +        ymin += d; ymax += d;
474 +                                        /* set view matrix */
475 +        glMatrixMode(GL_PROJECTION);
476 +        glPushMatrix();
477 +        glLoadIdentity();
478 +        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
479 +        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
480 +                odev.v.vp[0] + odev.v.vdir[0],
481 +                odev.v.vp[1] + odev.v.vdir[1],
482 +                odev.v.vp[2] + odev.v.vdir[2],
483 +                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
484 +        glDisable(GL_DEPTH_TEST);       /* write no depth values */
485          glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
486 <                                        /* draw each grid line */
612 <        glBegin(GL_LINES);
486 >        glBegin(GL_LINES);              /* draw each grid line */
487          gridlines(draw3dline);
488          glEnd();
489 +        glEnable(GL_DEPTH_TEST);        /* restore rendering params */
490 +        glPopMatrix();
491   }
492  
493  
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
494   static
495   moveview(dx, dy, mov, orb)      /* move our view */
496   int     dx, dy, mov, orb;
# Line 635 | Line 503 | int    dx, dy, mov, orb;
503          copystruct(&nv, &odev.v);
504                                  /* change view direction */
505          if (mov | orb) {
506 <                if ((li = FindValue(dx, dy)) < 0)
506 >                if ((li = qtFindLeaf(dx, dy)) < 0)
507                          return(0);      /* not on window */
508 <                VSUM(odir, rV.wp[li], nv.vp, -1.);
508 >                VSUM(odir, qtL.wp[li], nv.vp, -1.);
509          } else {
510                  if (viewray(nv.vp, nv.vdir, &odev.v,
511                                  (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
# Line 645 | Line 513 | int    dx, dy, mov, orb;
513          }
514          if (orb && mov) {               /* orbit left/right */
515                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
516 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
516 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
517                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
518          } else if (orb) {               /* orbit up/down */
519                  fcross(v1, odir, nv.vup);
520                  if (normalize(v1) == 0.)
521                          return(0);
522                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
523 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
523 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
524                  spinvector(nv.vdir, nv.vdir, v1, d);
525          } else if (mov) {               /* move forward/backward */
526                  d = MOVPCT/100. * mov;
# Line 677 | Line 545 | XButtonPressedEvent    *ebut;
545   {
546          int     movdir = MOVDIR(ebut->button);
547          int     movorb = MOVORB(ebut->state);
548 +        int     oldnodesiz = qtMinNodesiz;
549          Window  rootw, childw;
550          int     rootx, rooty, wx, wy;
551          unsigned int    statemask;
552  
553 +        qtMinNodesiz = 24;              /* accelerate update rate */
554          XNoOp(ourdisplay);
555  
556          while (!XCheckMaskEvent(ourdisplay,
# Line 694 | Line 564 | XButtonPressedEvent    *ebut;
564                          sleep(1);
565                          continue;
566                  }
567 +                glClear(GL_COLOR_BUFFER_BIT);
568 +                qtUpdate();
569                  draw_grids();
570 <                dev_flush();
570 >                glFlush();
571          }
572          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
573                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 704 | Line 576 | XButtonPressedEvent    *ebut;
576                  moveview(wx, odev.vres-1-wy, movdir, movorb);
577          }
578          dev_flush();
579 +
580 +        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
581   }
582  
583  
# Line 738 | Line 612 | register XKeyPressedEvent  *ekey;
612                  inpresflags |= DFL(DC_RESUME);
613                  return;
614          case CTRL('R'):                 /* redraw screen */
615 <                TMapValues(1);
616 <                redraw();
615 >                if (nxtzmax > FTINY) {
616 >                        curzmax = nxtzmax;
617 >                        nxtzmax = 0.;
618 >                }
619 >                glClear(GL_DEPTH_BUFFER_BIT);
620 >                qtRedraw(0, 0, odev.hres, odev.vres);
621                  return;
622          case CTRL('L'):                 /* refresh from server */
623                  if (inpresflags & DFL(DC_REDRAW))
624                          return;
625 +                if (nxtzmax > FTINY) {
626 +                        curzmax = nxtzmax;
627 +                        nxtzmax = 0.;
628 +                }
629                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
748                glDrawBuffer(GL_FRONT);
749                glDisable(GL_DEPTH_TEST);
630                  draw_grids();
751                glEnable(GL_DEPTH_TEST);
752                glDrawBuffer(GL_FRONT_AND_BACK);
631                  glFlush();
632 <                Compost(100);                   /* get rid of old values */
632 >                qtCompost(100);                 /* get rid of old values */
633                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
634 +                rayqleft = 0;                   /* hold off update */
635                  return;
636          case 'K':                       /* kill rtrace process(es) */
637                  inpresflags |= DFL(DC_KILL);
# Line 777 | Line 656 | static
656   fixwindow(eexp)                         /* repair damage to window */
657   register XExposeEvent  *eexp;
658   {
659 <        if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
660 <                odev.hres = eexp->width;
661 <                odev.vres = eexp->height;
662 <        }
663 <        if (eexp->width == odev.hres && eexp->height == odev.vres)
664 <                TMapValues(1);
665 <        if (!eexp->count)
666 <                redraw();
659 >        int     xmin, xmax, ymin, ymax;
660 >
661 >        if (odev.hres == 0 || odev.vres == 0)   /* first exposure */
662 >                resizewindow((XConfigureEvent *)eexp);
663 >        xmin = eexp->x; xmax = eexp->x + eexp->width;
664 >        ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
665 >                                                /* clear portion of depth */
666 >        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
667 >        glDepthFunc(GL_ALWAYS);
668 >        glBegin(GL_POLYGON);
669 >        glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.);
670 >        glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.);
671 >        glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.);
672 >        glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.);
673 >        glEnd();
674 >        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
675 >        glDepthFunc(GL_LEQUAL);
676 >        qtRedraw(xmin, ymin, xmax, ymax);
677   }
678  
679  
# Line 792 | Line 681 | static
681   resizewindow(ersz)                      /* resize window */
682   register XConfigureEvent  *ersz;
683   {
684 +        glViewport(0, 0, ersz->width, ersz->height);
685 +
686          if (ersz->width == odev.hres && ersz->height == odev.vres)
687                  return;
688  
# Line 800 | Line 691 | register XConfigureEvent  *ersz;
691  
692          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
693          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
803
804        setGLview();
694  
695          inpresflags |= DFL(DC_SETVIEW);
696   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines