ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_glx.c
(Generate patch)

Comparing ray/src/hd/rhd_glx.c (file contents):
Revision 3.2 by gregl, Wed Dec 24 14:06:40 1997 UTC vs.
Revision 3.7 by gregl, Fri Dec 26 16:50:03 1997 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
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  
# Line 21 | Line 20 | static char SCCSid[] = "$SunId$ SGI";
20   #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
21   #endif
22  
23 < #ifndef int4
24 < #define int4    int
23 > #ifndef MAXCONE
24 > #define MAXCONE         16              /* number of different cone sizes */
25   #endif
26 <
27 < #ifndef FREEPCT
29 < #define FREEPCT         10              /* percentage of values to free */
26 > #ifndef MAXVERT
27 > #define MAXVERT         32              /* maximum number of cone vertices */
28   #endif
29 <
30 < #ifndef NCONEV
33 < #define NCONEV          7               /* number of cone base vertices */
29 > #ifndef MINVERT
30 > #define MINVERT         4               /* minimum number of cone vertices */
31   #endif
32 < #ifndef CONEH
33 < #define CONEH           5.              /* cone height (fraction of depth) */
32 > #ifndef DEPTHFACT
33 > #define DEPTHFACT       16.             /* multiplier for depth tests */
34   #endif
38 #ifndef CONEW
39 #define CONEW           0.07            /* cone width (fraction of screen) */
40 #endif
41 #ifndef DIRPEN
42 #define DIRPEN          0.001           /* direction penalty factor */
43 #endif
35  
36   #define GAMMA           1.4             /* default gamma correction */
37  
# Line 75 | Line 66 | static XVisualInfo  *ourvinf;          /* our visual informati
66   static Window  gwind = 0;               /* our graphics window */
67   static GLXContext       gctx;           /* our GLX context */
68  
78 static double   mindepth = FHUGE;       /* minimum depth value so far */
79 static double   maxdepth = 0.;          /* maximum depth value so far */
80
69   static double   pwidth, pheight;        /* pixel dimensions (mm) */
70  
71 < static FVECT    conev[NCONEV];          /* drawing cone */
72 < static double   coneh;                  /* cone height */
71 > static double   curzmax = 1e4;          /* current depth upper limit */
72 > static double   nxtzmax = 0.;           /* maximum (finite) depth so far */
73  
74 + static struct {
75 +        double  rad;            /* cone radius */
76 +        int     nverts;         /* number of vertices */
77 +        FVECT   *va;            /* allocated vertex array */
78 + } cone[MAXCONE];        /* precomputed cones for drawing */
79 +
80   static int      inpresflags;            /* input result flags */
81  
82   static int      headlocked = 0;         /* lock vertical motion */
83  
90 static struct {
91        float           (*wp)[3];       /* world intersection point array */
92        int4            *wd;            /* world direction array */
93        TMbright        *brt;           /* encoded brightness array */
94        BYTE            (*chr)[3];      /* encoded chrominance array */
95        BYTE            (*rgb)[3];      /* tone-mapped color array */
96        BYTE            *alpha;         /* alpha values */
97        int             nl;             /* count of values */
98        int             bl, tl;         /* bottom and top (next) value index */
99        int             tml;            /* next value needing tone-mapping */
100        int             drl;            /* next value in need of drawing */
101        char            *base;          /* base of allocated memory */
102 }       rV;                     /* our collection of values */
103
104 static int      *valmap = NULL;         /* sorted map of screen values */
105 static int      vmaplen = 0;            /* value map length */
106
107 #define redraw()        (rV.drl = rV.bl)
108
84   static int  resizewindow(), getevent(), getkey(), moveview(),
85 <                setGLview(), getmove(), fixwindow(), mytmflags(),
86 <                drawvalue(), valcmp(), clralphas(), setalphas(), mergalphas(),
112 <                IndexValue(), Compost(), FindValue(), TMapValues(),
113 <                AllocValues(), FreeValues();
85 >                initcones(), freecones(),
86 >                getmove(), fixwindow(), mytmflags();
87  
115 extern int4     encodedir();
116 extern double   fdir2diff(), dir2diff();
88  
118
89   dev_open(id)                    /* initialize X11 driver */
90   char  *id;
91   {
92          extern char     *getenv();
93          static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
94                                  GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
95 <                                GLX_ALPHA_SIZE,8, GLX_DEPTH_SIZE,15,
126 <                                None};
95 >                                GLX_DEPTH_SIZE,15, None};
96          char    *gv;
97          double  gamval = GAMMA;
98          XSetWindowAttributes    ourwinattr;
99          XWMHints        ourxwmhints;
100          XSizeHints      oursizhints;
101 +                                        /* set quadtree globals */
102 +        qtMinNodesiz = 3;
103                                          /* open display server */
104          ourdisplay = XOpenDisplay(NULL);
105          if (ourdisplay == NULL)
# Line 176 | Line 147 | char  *id;
147          glDepthFunc(GL_LEQUAL);
148          glShadeModel(GL_FLAT);
149          glDisable(GL_DITHER);
150 +        glDisable(GL_CULL_FACE);
151 +        glMatrixMode(GL_PROJECTION);
152 +        glOrtho(0., 1., 0., 1., -.01, 1.01);
153 +        glTranslated(0., 0., -1.01);
154                                          /* figure out sensible view */
155          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
156                          DisplayWidth(ourdisplay, ourscreen);
# Line 186 | Line 161 | char  *id;
161                                          /* map the window */
162          XMapWindow(ourdisplay, gwind);
163          dev_input();                    /* sets size and view angles */
164 <        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
165 <                                        /* allocate our value list */
166 <        if (!AllocValues(DisplayWidth(ourdisplay,ourscreen) *
167 <                        DisplayHeight(ourdisplay,ourscreen) / 4))
164 >                                        /* allocate our leaf pile */
165 >        if (!qtAllocLeaves(2 * DisplayWidth(ourdisplay,ourscreen) *
166 >                        DisplayHeight(ourdisplay,ourscreen) /
167 >                        (qtMinNodesiz*qtMinNodesiz)))
168                  error(SYSTEM, "insufficient memory for value storage");
169          odev.name = id;
170          odev.ifd = ConnectionNumber(ourdisplay);
171 +                                        /* initialize cone array */
172 +        initcones();
173   }
174  
175  
# Line 204 | Line 181 | dev_close()                    /* close our display and free resources
181          gwind = 0;
182          XCloseDisplay(ourdisplay);
183          ourdisplay = NULL;
184 +        qtFreeLeaves();
185          tmDone(NULL);
186 <        FreeValues();
186 >        freecones();
187          odev.v.type = 0;
188          odev.hres = odev.vres = 0;
189          odev.ifd = -1;
# Line 216 | Line 194 | int
194   dev_view(nv)                    /* assign new driver view */
195   register VIEW   *nv;
196   {
197 <        if (nv->type != VT_PER ||               /* check view legality */
198 <                        nv->horiz > 120. || nv->vert > 120.) {
197 >        if (nv->type == VT_PAR ||               /* check view legality */
198 >                        nv->horiz > 160. || nv->vert > 160.) {
199                  error(COMMAND, "illegal view type/angle");
200 <                nv->type = VT_PER;
200 >                nv->type = odev.v.type;
201                  nv->horiz = odev.v.horiz;
202                  nv->vert = odev.v.vert;
203                  return(0);
204          }
205 +        if (nv->vfore > FTINY) {
206 +                error(COMMAND, "cannot handle fore clipping");
207 +                nv->vfore = 0.;
208 +                return(0);
209 +        }
210          if (nv != &odev.v) {
211                  if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
212                                  !FEQ(nv->vert,odev.v.vert)) {
# Line 248 | Line 231 | register VIEW  *nv;
231                          dev_input();    /* get resize event */
232                  }
233                  copystruct(&odev.v, nv);
251                setGLview();
252                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
253                redraw();
234          }
235 +        if (nxtzmax > FTINY) {
236 +                curzmax = nxtzmax;
237 +                nxtzmax = 0.;
238 +        }
239 +        glClear(GL_DEPTH_BUFFER_BIT);
240 +        qtReplant();
241          return(1);
242   }
243  
# Line 270 | Line 256 | dev_input()                    /* get X11 input */
256   }
257  
258  
259 < dev_value(c, p, v)              /* add a pixel value to our list */
260 < COLR    c;
275 < FVECT   p, v;
259 > int
260 > dev_flush()                     /* flush output */
261   {
262 <        register int    li;
263 <
264 <        li = rV.tl++;
280 <        if (rV.tl >= rV.nl)     /* get next leaf in ring */
281 <                rV.tl = 0;
282 <        if (rV.tl == rV.bl)     /* need to shake some free */
283 <                Compost(FREEPCT);
284 <        VCOPY(rV.wp[li], p);
285 <        rV.wd[li] = encodedir(v);
286 <        tmCvColrs(&rV.brt[li], rV.chr[li], c, 1);
262 >        qtUpdate();
263 >        glFlush();
264 >        return(XPending(ourdisplay));
265   }
266  
267  
268 < int
269 < dev_flush()                     /* flush output */
268 > dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */
269 > BYTE    rgb[3];
270 > FVECT   ip;
271 > double  rad;
272   {
273 <        if (mapped) {
274 <                TMapValues(0);
275 <                while (rV.drl != rV.tl) {
276 <                        drawvalue(rV.drl);
277 <                        if (++rV.drl >= rV.nl)
278 <                                rV.drl = 0;
279 <                }
280 <                glFlush();
273 >        register int    ci, j;
274 >        double  apexh, basez;
275 >                                        /* compute apex height (0. to 1.) */
276 >        if (ip[2] > 1e6)
277 >                apexh = 1. - 1./DEPTHFACT;
278 >        else {
279 >                if (ip[2] > nxtzmax)
280 >                        nxtzmax = ip[2];
281 >                if (ip[2] >= curzmax)
282 >                        apexh = 1. - 1./DEPTHFACT;
283 >                else
284 >                        apexh = 1. - ip[2]/(curzmax*DEPTHFACT);
285          }
286 <        return(XPending(ourdisplay));
286 >        rad *= 1.25;                    /* find conservative cone match */
287 >        for (ci = 0; ci < MAXCONE-1; ci++)
288 >                if (cone[ci].rad >= rad)
289 >                        break;
290 >                                        /* draw it */
291 >        glColor3ub(rgb[0], rgb[1], rgb[2]);
292 >        glBegin(GL_TRIANGLE_FAN);
293 >        glVertex3d(ip[0], ip[1], apexh);        /* start with apex */
294 >        basez = apexh*cone[ci].va[0][2];        /* base z's all the same */
295 >        for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */
296 >                glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1],
297 >                                basez);
298 >                                                /* connect last to first */
299 >        glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez);
300 >        glEnd();                                /* all done */
301   }
302  
303  
# Line 323 | Line 321 | mytmflags()                    /* figure out tone mapping flags */
321  
322  
323   static
324 < setGLview()                     /* set our GL view */
324 > initcones()                     /* initialize cone vertices */
325   {
328        double  xmin, xmax, ymin, ymax, zmin, zmax;
329        double  d, cx, sx, crad;
330        FVECT   vx, vy;
326          register int    i, j;
327 <                                        /* compute view frustum */
333 <        if (normalize(odev.v.vdir) == 0.0)
334 <                return;
335 <        if (mindepth < maxdepth) {
336 <                zmin = 0.25*mindepth;
337 <                zmax = 4.0*(1.+CONEH)*maxdepth;
338 <        } else {
339 <                zmin = 0.01;
340 <                zmax = 1000.;
341 <        }
342 <        if (odev.v.vfore > FTINY)
343 <                zmin = odev.v.vfore;
344 <        if (odev.v.vaft > FTINY)
345 <                zmax = odev.v.vaft;
346 <        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
347 <        xmin = -xmax;
348 <        d = odev.v.hoff * (xmax - xmin);
349 <        xmin += d; xmax += d;
350 <        ymax = zmin * tan(PI/180./2. * odev.v.vert);
351 <        ymin = -ymax;
352 <        d = odev.v.voff * (ymax - ymin);
353 <        ymin += d; ymax += d;
354 <                                        /* set view matrix */
355 <        glMatrixMode(GL_PROJECTION);
356 <        glLoadIdentity();
357 <        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
358 <        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
359 <                odev.v.vp[0] + odev.v.vdir[0],
360 <                odev.v.vp[1] + odev.v.vdir[1],
361 <                odev.v.vp[2] + odev.v.vdir[2],
362 <                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
363 <                                        /* set viewport */
364 <        glViewport(0, 0, odev.hres, odev.vres);
365 <                                        /* initialize cone for Vornoi polys */
366 <        coneh = CONEH*(zmax - zmin);
367 <        crad = 0.5 * CONEW * 0.5*(xmax-xmin + ymax-ymin) * (zmin+coneh)/zmin;
368 <        vy[0] = vy[1] = vy[2] = 0.;
369 <        for (i = 0; i < 3; i++)
370 <                if (odev.v.vdir[i] < 0.6 && odev.v.vdir[i] > -0.6)
371 <                        break;
372 <        vy[i] = 1.;
373 <        fcross(vx, vy, odev.v.vdir);
374 <        normalize(vx);
375 <        fcross(vy, odev.v.vdir, vx);
376 <        for (j = 0, d = 0.; j < NCONEV; j++, d += 2.*PI/NCONEV) {
377 <                cx = crad*cos(d); sx = crad*sin(d);
378 <                for (i = 0; i < 3; i++)
379 <                        conev[j][i] = coneh*odev.v.vdir[i] +
380 <                                        cx*vx[i] + sx*vy[i];
381 <        }
382 < }
327 >        double  minrad, d;
328  
329 <
330 < static
331 < drawvalue(li)                   /* draw a pixel value as a cone */
332 < register int    li;
333 < {
334 <        static FVECT    disp;
335 <        FVECT   apex;
336 <        double  d, dorg, dnew, h, v;
337 <        register int    i;
338 <                                /* compute cone coordinates */
339 <        disp[0] = rV.wp[li][0] - odev.v.vp[0];
340 <        disp[1] = rV.wp[li][1] - odev.v.vp[1];
341 <        disp[2] = rV.wp[li][2] - odev.v.vp[2];
342 <        dorg = DOT(disp,odev.v.vdir);
343 <        if (dorg <= odev.v.vfore)
344 <                return;         /* clipped too near */
400 <        if (odev.v.vaft > FTINY && dorg > odev.v.vaft)
401 <                return;         /* clipped too far */
402 <        if (dorg > 1e5) {       /* background pixel */
403 <                dnew = maxdepth;
404 <                d = dnew/dorg;
405 <        } else {                /* foreground pixel, compute penalty */
406 <                normalize(disp);
407 <                d = dnew = dorg + coneh*fdir2diff(rV.wd[li],disp)*DIRPEN;
408 <        }
409 <                                /* compute adjusted apex position */
410 <        disp[0] *= d; disp[1] *= d; disp[2] *= d;
411 <        apex[0] = odev.v.vp[0] + disp[0];
412 <        apex[1] = odev.v.vp[1] + disp[1];
413 <        apex[2] = odev.v.vp[2] + disp[2];
414 <                                /* compute view position and base offset */
415 <        h = DOT(disp,odev.v.hvec)/(dnew*odev.v.hn2);
416 <        v = DOT(disp,odev.v.vvec)/(dnew*odev.v.vn2);
417 <        if (fabs(h - odev.v.hoff) > 0.5 || fabs(v - odev.v.voff) > 0.5)
418 <                return;         /* clipped off screen */
419 <        if (dorg < mindepth)
420 <                mindepth = dorg;
421 <        if (dorg > maxdepth)
422 <                maxdepth = dorg;
423 <        for (i = 0; i < 3; i++)
424 <                disp[i] = apex[i] + coneh*(h*odev.v.hvec[i] + v*odev.v.vvec[i]);
425 <                                /* draw cone (pyramid approx.) */
426 <        glColor4ub(rV.rgb[li][0], rV.rgb[li][1], rV.rgb[li][2], rV.alpha[li]);
427 <        glBegin(GL_TRIANGLE_FAN);
428 <        glVertex3f(apex[0], apex[1], apex[2]);
429 <        for (i = 0; i < NCONEV; i++)
430 <                glVertex3d(conev[i][0] + disp[0], conev[i][1] + disp[1],
431 <                                conev[i][2] + disp[2]);
432 <                                /* connect last face to first */
433 <        glVertex3d(conev[0][0] + disp[0], conev[0][1] + disp[1],
434 <                        conev[0][2] + disp[2]);
435 <        glEnd();                /* done */
436 < }
437 <
438 <
439 < #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
440 <                        sizeof(TMbright)+7*sizeof(BYTE))
441 <
442 < static
443 < AllocValues(n)                  /* allocate space for n values */
444 < register int    n;
445 < {
446 <        unsigned        nbytes;
447 <        register unsigned       i;
448 <
449 <        if (n <= 0)
450 <                return(0);
451 <        if (rV.nl >= n)
452 <                return(rV.nl);
453 <        else if (rV.nl > 0)
454 <                free(rV.base);
455 <                                /* round space up to nearest power of 2 */
456 <        nbytes = n*LEAFSIZ + 8;
457 <        for (i = 1024; nbytes > i; i <<= 1)
458 <                ;
459 <        n = (i - 8) / LEAFSIZ;  /* should we make sure n is even? */
460 <        rV.base = (char *)malloc(n*LEAFSIZ);
461 <        if (rV.base == NULL)
462 <                return(0);
463 <                                /* assign larger alignment types earlier */
464 <        rV.wp = (float (*)[3])rV.base;
465 <        rV.wd = (int4 *)(rV.wp + n);
466 <        rV.brt = (TMbright *)(rV.wd + n);
467 <        rV.chr = (BYTE (*)[3])(rV.brt + n);
468 <        rV.rgb = (BYTE (*)[3])(rV.chr + n);
469 <        rV.alpha = (BYTE *)(rV.rgb + n);
470 <        rV.nl = n;
471 <        rV.drl = rV.tml = rV.bl = rV.tl = 0;
472 <        return(n);
473 < }
474 <
475 < #undef  LEAFSIZ
476 <
477 <
478 < static
479 < FreeValues()                    /* free our allocated values */
480 < {
481 <        if (rV.nl <= 0)
482 <                return;
483 <        free(rV.base);
484 <        rV.base = NULL;
485 <        rV.nl = 0;
486 < }
487 <
488 <
489 < static
490 < clralphas()                     /* prepare for new alpha values */
491 < {
492 <        if (!vmaplen)
493 <                return;
494 <        free((char *)valmap);
495 <        valmap = NULL;
496 <        vmaplen = 0;
497 < }
498 <
499 <
500 < static int
501 < valcmp(v1p, v2p)                /* compare two pixel values */
502 < int     *v1p, *v2p;
503 < {
504 <        register int    v1 = *v1p, v2 = *v2p;
505 <        register int    c;
506 <
507 <        if ((c = rV.rgb[v1][0] - rV.rgb[v2][0])) return(c);
508 <        if ((c = rV.rgb[v1][1] - rV.rgb[v2][1])) return(c);
509 <        if ((c = rV.rgb[v1][2] - rV.rgb[v2][2])) return(c);
510 <        return(rV.alpha[v1] - rV.alpha[v2]);
511 < }
512 <
513 <
514 < static
515 < mergalphas(adest, al1, n1, al2, n2)     /* merge two sorted alpha lists */
516 < register int    *adest, *al1, *al2;
517 < int     n1, n2;
518 < {
519 <        register int    cmp;
520 <
521 <        while (n1 | n2) {
522 <                if (!n1) cmp = 1;
523 <                else if (!n2) cmp = -1;
524 <                else cmp = valcmp(al1, al2);
525 <                if (cmp > 0) {
526 <                        *adest++ = *al2++;
527 <                        n2--;
528 <                } else {
529 <                        *adest++ = *al1++;
530 <                        n1--;
329 >        if (cone[0].nverts)
330 >                freecones();
331 >        minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) +
332 >                                        DisplayHeight(ourdisplay,ourscreen));
333 >        for (i = 0; i < MAXCONE; i++) {
334 >                d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */
335 >                cone[i].rad = minrad + (1.-minrad)*d;
336 >                cone[i].nverts = MINVERT + (MAXVERT-MINVERT)*d;
337 >                cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT));
338 >                if (cone[i].va == NULL)
339 >                        error(SYSTEM, "out of memory in initcones");
340 >                for (j = cone[i].nverts; j--; ) {
341 >                        d = 2.*PI * (j+.5) / (cone[i].nverts);
342 >                        cone[i].va[j][0] = cos(d) * cone[i].rad;
343 >                        cone[i].va[j][1] = sin(d) * cone[i].rad;
344 >                        cone[i].va[j][2] = 1. - cone[i].rad;
345                  }
346          }
347   }
348  
349  
350   static
351 < setalphas(vbeg, nvals)          /* add values to our map and set alphas */
538 < int     vbeg, nvals;
351 > freecones()                     /* free cone vertices */
352   {
353 <        register int    *newmap;
541 <        short   ccmp[3], lastalpha;
542 <        int     newmaplen;
353 >        register int    i;
354  
355 <        if (nvals <= 0)
356 <                return;
357 <        newmaplen = vmaplen + nvals;    /* allocate new map */
358 <        newmap = (int *)malloc(newmaplen*sizeof(int));
359 <        if (newmap == NULL)
549 <                error(SYSTEM, "out of memory in setalphas");
550 <        while (nvals--) {               /* add new values to end */
551 <                rV.alpha[vbeg] = 255;
552 <                newmap[vmaplen+nvals] = vbeg++;
553 <        }
554 <        if (nvals >= 3*vmaplen) {       /* resort the combined array */
555 <                while (vmaplen--)
556 <                        newmap[vmaplen] = valmap[vmaplen];
557 <                qsort((char *)newmap, newmaplen, sizeof(int), valcmp);
558 <        } else {                        /* perform merge sort */
559 <                qsort((char *)(newmap+vmaplen), newmaplen-vmaplen,
560 <                                sizeof(int), valcmp);
561 <                mergalphas(newmap, valmap, vmaplen,
562 <                                newmap+vmaplen, newmaplen-vmaplen);
563 <        }
564 <        if (valmap != NULL)             /* free old map and assign new one */
565 <                free((char *)valmap);
566 <        valmap = newmap;
567 <        vmaplen = newmaplen;
568 <        lastalpha = 0;                  /* set new alpha values */
569 <        ccmp[0] = ccmp[1] = ccmp[2] = 256;
570 <        while (newmaplen--)
571 <                if (rV.rgb[*newmap][0] == ccmp[0] &&
572 <                                rV.rgb[*newmap][1] == ccmp[1] &&
573 <                                rV.rgb[*newmap][2] == ccmp[2]) {
574 <                        if (lastalpha >= 255)
575 <                                newmap++;
576 <                        else if (rV.alpha[*newmap] < 255)
577 <                                lastalpha = rV.alpha[*newmap++];
578 <                        else
579 <                                rV.alpha[*newmap++] = ++lastalpha;
580 <                } else {
581 <                        ccmp[0] = rV.rgb[*newmap][0];
582 <                        ccmp[1] = rV.rgb[*newmap][1];
583 <                        ccmp[2] = rV.rgb[*newmap][2];
584 <                        if (rV.alpha[*newmap] < 255)
585 <                                lastalpha = rV.alpha[*newmap++];
586 <                        else
587 <                                rV.alpha[*newmap++] = lastalpha = 1;
355 >        for (i = MAXCONE; i--; )
356 >                if (cone[i].nverts) {
357 >                        free((char *)cone[i].va);
358 >                        cone[i].va = NULL;
359 >                        cone[i].nverts = 0;
360                  }
361   }
362  
363  
364   static
593 TMapValues(redo)                /* map our values to RGB */
594 int     redo;
595 {
596        int     aorg, alen, borg, blen;
597                                        /* recompute mapping? */
598        if (redo)
599                rV.tml = rV.bl;
600                                        /* already done? */
601        if (rV.tml == rV.tl)
602                return(1);
603                                        /* compute segments */
604        aorg = rV.tml;
605        if (rV.tl >= aorg) {
606                alen = rV.tl - aorg;
607                blen = 0;
608        } else {
609                alen = rV.nl - aorg;
610                borg = 0;
611                blen = rV.tl;
612        }
613                                        /* (re)compute tone mapping? */
614        if (rV.tml == rV.bl) {
615                tmClearHisto();
616                tmAddHisto(rV.brt+aorg, alen, 1);
617                if (blen > 0)
618                        tmAddHisto(rV.brt+borg, blen, 1);
619                if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
620                        return(0);
621                clralphas();            /* restart value list */
622                rV.drl = rV.bl;         /* need to redraw */
623        }
624        if (tmMapPixels(rV.rgb+aorg, rV.brt+aorg,
625                        rV.chr+aorg, alen) != TM_E_OK)
626                return(0);
627        if (blen > 0)
628                tmMapPixels(rV.rgb+borg, rV.brt+borg,
629                                rV.chr+borg, blen);
630        setalphas(aorg, alen);          /* compute add'l alpha values */
631        if (blen > 0)
632                setalphas(borg, blen);
633        rV.tml = rV.tl;                 /* we're all up to date */
634        return(1);
635 }
636
637
638 static int
639 Compost(pct)                    /* free up some values */
640 int     pct;
641 {
642        int     nused, nclear, nmapped, ndrawn;
643                                /* figure out how many values to clear */
644        nclear = rV.nl * pct / 100;
645        nused = rV.tl - rV.bl;
646        if (nused <= 0) nused += rV.nl;
647        nclear -= rV.nl - nused;
648        if (nclear <= 0)
649                return(0);
650        if (nclear >= nused) {  /* clear them all? */
651                rV.drl = rV.tml = rV.bl = rV.tl = 0;
652                return(nused);
653        }
654                                /* else clear values from bottom */
655        ndrawn = rV.drl - rV.bl;
656        if (ndrawn < 0) ndrawn += rV.nl;
657        nmapped = rV.tml - rV.bl;
658        if (nmapped < 0) nmapped += rV.nl;
659        rV.bl += nclear;
660        if (rV.bl >= rV.nl) rV.bl -= rV.nl;
661        if (ndrawn < nclear) rV.drl = rV.bl;
662        if (nmapped < nclear) rV.tml = rV.bl;
663        return(nclear);
664 }
665
666
667 static
365   getevent()                      /* get next event */
366   {
367          XNextEvent(ourdisplay, levptr(XEvent));
# Line 704 | Line 401 | static
401   draw_grids()                    /* draw holodeck section grids */
402   {
403          static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
404 <
405 <        if (!mapped)
404 >        double  xmin, xmax, ymin, ymax, zmin, zmax;
405 >        double  d, cx, sx, crad;
406 >        FVECT   vx, vy;
407 >        register int    i, j;
408 >                                        /* can we even do it? */
409 >        if (!mapped || odev.v.type != VT_PER)
410                  return;
411 +                                        /* compute view frustum */
412 +        if (normalize(odev.v.vdir) == 0.0)
413 +                return;
414 +        zmin = 0.01;
415 +        zmax = 10000.;
416 +        if (odev.v.vfore > FTINY)
417 +                zmin = odev.v.vfore;
418 +        if (odev.v.vaft > FTINY)
419 +                zmax = odev.v.vaft;
420 +        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
421 +        xmin = -xmax;
422 +        d = odev.v.hoff * (xmax - xmin);
423 +        xmin += d; xmax += d;
424 +        ymax = zmin * tan(PI/180./2. * odev.v.vert);
425 +        ymin = -ymax;
426 +        d = odev.v.voff * (ymax - ymin);
427 +        ymin += d; ymax += d;
428 +                                        /* set view matrix */
429 +        glMatrixMode(GL_PROJECTION);
430 +        glPushMatrix();
431 +        glLoadIdentity();
432 +        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
433 +        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
434 +                odev.v.vp[0] + odev.v.vdir[0],
435 +                odev.v.vp[1] + odev.v.vdir[1],
436 +                odev.v.vp[2] + odev.v.vdir[2],
437 +                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
438 +        glDisable(GL_DEPTH_TEST);       /* write no depth values */
439          glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
440 <                                        /* draw each grid line */
712 <        glBegin(GL_LINES);
440 >        glBegin(GL_LINES);              /* draw each grid line */
441          gridlines(draw3dline);
442          glEnd();
443 +        glEnable(GL_DEPTH_TEST);        /* restore rendering params */
444 +        glPopMatrix();
445   }
446  
447  
718 static int
719 IndexValue(rgba)                /* locate a pixel by it's framebuffer value */
720 register BYTE   rgba[4];
721 {
722        register int    *vp;
723                                        /* check legality */
724        if (rgba[3] == 0 || rgba[3] == 255)
725                return(-1);
726                                        /* borrow a value slot */
727        rV.rgb[rV.tl][0] = rgba[0];
728        rV.rgb[rV.tl][1] = rgba[1];
729        rV.rgb[rV.tl][2] = rgba[2];
730        rV.alpha[rV.tl] = rgba[3];
731                                        /* find it */
732        vp = (int *)bsearch((char *)&rV.tl, (char *)valmap, vmaplen,
733                        sizeof(int), valcmp);
734        if (vp == NULL)
735                return(-1);
736        return(*vp);
737 }
738
739
740 static int
741 FindValue(dx, dy)               /* find a value on the display */
742 int     dx, dy;
743 {
744        BYTE    rgba[4];
745
746        if (dx < 0 || dy < 0 || dx >= odev.hres || dy >= odev.vres)
747                return(-1);
748        glReadPixels(dx, dy, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
749        return(IndexValue(rgba));
750 }
751
752
448   static
449   moveview(dx, dy, mov, orb)      /* move our view */
450   int     dx, dy, mov, orb;
# Line 762 | Line 457 | int    dx, dy, mov, orb;
457          copystruct(&nv, &odev.v);
458                                  /* change view direction */
459          if (mov | orb) {
460 <                if ((li = FindValue(dx, dy)) < 0)
460 >                if ((li = qtFindLeaf(dx, dy)) < 0)
461                          return(0);      /* not on window */
462 <                VSUM(odir, rV.wp[li], nv.vp, -1.);
462 >                VSUM(odir, qtL.wp[li], nv.vp, -1.);
463          } else {
464                  if (viewray(nv.vp, nv.vdir, &odev.v,
465                                  (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
# Line 772 | Line 467 | int    dx, dy, mov, orb;
467          }
468          if (orb && mov) {               /* orbit left/right */
469                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
470 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
470 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
471                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
472          } else if (orb) {               /* orbit up/down */
473                  fcross(v1, odir, nv.vup);
474                  if (normalize(v1) == 0.)
475                          return(0);
476                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
477 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
477 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
478                  spinvector(nv.vdir, nv.vdir, v1, d);
479          } else if (mov) {               /* move forward/backward */
480                  d = MOVPCT/100. * mov;
# Line 804 | Line 499 | XButtonPressedEvent    *ebut;
499   {
500          int     movdir = MOVDIR(ebut->button);
501          int     movorb = MOVORB(ebut->state);
502 +        int     oldnodesiz = qtMinNodesiz;
503          Window  rootw, childw;
504          int     rootx, rooty, wx, wy;
505          unsigned int    statemask;
506  
507 +        qtMinNodesiz = 24;              /* accelerate update rate */
508          XNoOp(ourdisplay);
509  
510          while (!XCheckMaskEvent(ourdisplay,
# Line 821 | Line 518 | XButtonPressedEvent    *ebut;
518                          sleep(1);
519                          continue;
520                  }
521 +                glClear(GL_COLOR_BUFFER_BIT);
522 +                qtUpdate();
523                  draw_grids();
524 <                dev_flush();
524 >                glFlush();
525          }
526          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
527                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
528                  wx = levptr(XButtonReleasedEvent)->x;
529                  wy = levptr(XButtonReleasedEvent)->y;
530                  moveview(wx, odev.vres-1-wy, movdir, movorb);
832        } else {
833                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
834                redraw();
531          }
532          dev_flush();
533 +
534 +        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
535   }
536  
537  
# Line 868 | Line 566 | register XKeyPressedEvent  *ekey;
566                  inpresflags |= DFL(DC_RESUME);
567                  return;
568          case CTRL('R'):                 /* redraw screen */
871                TMapValues(1);
569                  glClear(GL_DEPTH_BUFFER_BIT);
570 <                redraw();
570 >                qtRedraw(0, 0, odev.hres, odev.vres);
571                  return;
572          case CTRL('L'):                 /* refresh from server */
573                  if (inpresflags & DFL(DC_REDRAW))
574                          return;
575                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
879                glDisable(GL_DEPTH_TEST);
576                  draw_grids();
881                glEnable(GL_DEPTH_TEST);
577                  glFlush();
578 <                Compost(100);                   /* get rid of old values */
578 >                qtCompost(100);                 /* get rid of old values */
579                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
580                  return;
581          case 'K':                       /* kill rtrace process(es) */
# Line 906 | Line 601 | static
601   fixwindow(eexp)                         /* repair damage to window */
602   register XExposeEvent  *eexp;
603   {
604 +        int     xmin, xmax, ymin, ymax;
605 +
606          if (odev.hres == 0 || odev.vres == 0)   /* first exposure */
607                  resizewindow((XConfigureEvent *)eexp);
608 <        if (eexp->width == odev.hres && eexp->height == odev.vres)
609 <                TMapValues(1);
610 <        if (!eexp->count) {
611 <                glClear(GL_DEPTH_BUFFER_BIT);
612 <                redraw();
613 <        }
608 >        xmin = eexp->x; xmax = eexp->x + eexp->width;
609 >        ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
610 >                                                /* clear portion of depth */
611 >        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
612 >        glDepthFunc(GL_ALWAYS);
613 >        glBegin(GL_POLYGON);
614 >        glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.);
615 >        glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.);
616 >        glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.);
617 >        glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.);
618 >        glEnd();
619 >        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
620 >        glDepthFunc(GL_LEQUAL);
621 >        qtRedraw(xmin, ymin, xmax, ymax);
622   }
623  
624  
# Line 921 | Line 626 | static
626   resizewindow(ersz)                      /* resize window */
627   register XConfigureEvent  *ersz;
628   {
629 +        glViewport(0, 0, ersz->width, ersz->height);
630 +
631          if (ersz->width == odev.hres && ersz->height == odev.vres)
632                  return;
633  
# Line 929 | Line 636 | register XConfigureEvent  *ersz;
636  
637          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
638          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
932
933        setGLview();
639  
640          inpresflags |= DFL(DC_SETVIEW);
641   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines