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.13 by gregl, Sun Jan 4 08:32:00 1998 UTC

# 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         32              /* 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 */
# Line 113 | Line 96 | char  *id;
96          extern char     *getenv();
97          static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
98                                  GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
99 <                                GLX_ALPHA_SIZE,8, GLX_DEPTH_SIZE,23,
117 <                                /*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};
99 >                                GLX_DEPTH_SIZE,15, None};
100          char    *gv;
101          double  gamval = GAMMA;
102          XSetWindowAttributes    ourwinattr;
103          XWMHints        ourxwmhints;
104          XSizeHints      oursizhints;
105 +                                        /* set quadtree globals */
106 +        qtMinNodesiz = 3;
107 +        qtDepthEps = 0.07;
108                                          /* open display server */
109          ourdisplay = XOpenDisplay(NULL);
110          if (ourdisplay == NULL)
# Line 131 | Line 112 | char  *id;
112                                          /* find a usable visual */
113          ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
114          if (ourvinf == NULL)
134                ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK);
135        if (ourvinf == NULL)
115                  error(USER, "no suitable visuals available");
116                                          /* get a context */
117          gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
# Line 167 | Line 146 | char  *id;
146          oursizhints.min_height = MINHEIGHT;
147          oursizhints.flags = PMinSize;
148          XSetNormalHints(ourdisplay, gwind, &oursizhints);
149 <                                        /* map the window and get its size */
150 <        XMapWindow(ourdisplay, gwind);
151 <        dev_input();
152 <                                        /* allocate our value list */
153 <        if (!AllocValues(DisplayWidth(ourdisplay,ourscreen) *
154 <                        DisplayHeight(ourdisplay,ourscreen) / 4))
155 <                error(SYSTEM, "insufficient memory for value storage");
149 >                                        /* set GLX context */
150 >        glXMakeCurrent(ourdisplay, gwind, gctx);
151 >        glEnable(GL_DEPTH_TEST);
152 >        glDepthFunc(GL_LEQUAL);
153 >        glShadeModel(GL_FLAT);
154 >        glDisable(GL_DITHER);
155 >        glDisable(GL_CULL_FACE);
156 >        glMatrixMode(GL_PROJECTION);
157 >        glOrtho(0., 1., 0., 1., -.01, 1.01);
158 >        glTranslated(0., 0., -1.01);
159                                          /* figure out sensible view */
160          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
161                          DisplayWidth(ourdisplay, ourscreen);
# Line 181 | Line 163 | char  *id;
163                          DisplayHeight(ourdisplay, ourscreen);
164          copystruct(&odev.v, &stdview);
165          odev.v.type = VT_PER;
166 <        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
167 <        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
166 >                                        /* map the window */
167 >        XMapWindow(ourdisplay, gwind);
168 >        dev_input();                    /* sets size and view angles */
169 >                                        /* allocate our leaf pile */
170 >        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
171 >                        DisplayHeight(ourdisplay,ourscreen) * 3 /
172 >                        (qtMinNodesiz*qtMinNodesiz*2)))
173 >                error(SYSTEM, "insufficient memory for value storage");
174          odev.name = id;
175          odev.ifd = ConnectionNumber(ourdisplay);
176 <                                        /* set GLX context and clear buffers */
177 <        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 */
176 >                                        /* initialize cone array */
177 >        initcones();
178   }
179  
180  
# Line 206 | Line 186 | dev_close()                    /* close our display and free resources
186          gwind = 0;
187          XCloseDisplay(ourdisplay);
188          ourdisplay = NULL;
189 +        qtFreeLeaves();
190          tmDone(NULL);
191 <        FreeValues();
191 >        freecones();
192          odev.v.type = 0;
193          odev.hres = odev.vres = 0;
194          odev.ifd = -1;
# Line 218 | Line 199 | int
199   dev_view(nv)                    /* assign new driver view */
200   register VIEW   *nv;
201   {
202 <        if (nv->type != VT_PER ||               /* check view legality */
203 <                        nv->horiz > 120. || nv->vert > 120.) {
202 >        if (nv->type == VT_PAR ||               /* check view legality */
203 >                        nv->horiz > 160. || nv->vert > 160.) {
204                  error(COMMAND, "illegal view type/angle");
205 <                nv->type = VT_PER;
205 >                nv->type = odev.v.type;
206                  nv->horiz = odev.v.horiz;
207                  nv->vert = odev.v.vert;
208                  return(0);
209          }
210 +        if (nv->vfore > FTINY) {
211 +                error(COMMAND, "cannot handle fore clipping");
212 +                nv->vfore = 0.;
213 +                return(0);
214 +        }
215          if (nv != &odev.v) {
216                  if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
217                                  !FEQ(nv->vert,odev.v.vert)) {
# Line 247 | Line 233 | register VIEW  *nv;
233                                  odev.vres = dh;
234                          }
235                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
236 +                        dev_input();    /* get resize event */
237                  }
238                  copystruct(&odev.v, nv);
252                setGLview();
239          }
240 <        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
241 <        redraw();
240 >        if (nxtzmax > FTINY) {
241 >                curzmax = nxtzmax;
242 >                nxtzmax = 0.;
243 >        }
244 >        glClear(GL_DEPTH_BUFFER_BIT);
245 >        qtReplant();
246          return(1);
247   }
248  
# Line 271 | Line 261 | dev_input()                    /* get X11 input */
261   }
262  
263  
264 < dev_value(c, p, v)              /* add a pixel value to our list */
265 < COLR    c;
276 < FVECT   p, v;
264 > int
265 > dev_flush()                     /* flush output */
266   {
267 <        register int    li;
268 <
269 <        li = newvalue();
270 <        VCOPY(rV.wp[li], p);
282 <        rV.wd[li] = encodedir(v);
283 <        tmCvColrs(&rV.brt[li], rV.chr[li], c, 1);
267 >        qtUpdate();
268 >        glFlush();
269 >        rayqleft = RAYQLEN;
270 >        return(XPending(ourdisplay));
271   }
272  
273  
274 < int
275 < dev_flush()                     /* flush output */
274 > dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */
275 > BYTE    rgb[3];
276 > FVECT   ip;
277 > double  rad;
278   {
279 <        if (mapped) {
280 <                TMapValues(0);
281 <                while (rV.drl != rV.tl) {
282 <                        drawvalue(rV.drl);
283 <                        if (++rV.drl >= rV.nl)
284 <                                rV.drl = 0;
285 <                }
286 <                glFlush();
279 >        register int    ci, j;
280 >        double  apexh, basez;
281 >                                        /* is window mapped? */
282 >        if (!mapped)
283 >                return;
284 >                                        /* compute apex height (0. to 1.) */
285 >        if (ip[2] > 1e6)
286 >                apexh = 1. - 1./DEPTHFACT;
287 >        else {
288 >                if (ip[2] > nxtzmax)
289 >                        nxtzmax = ip[2];
290 >                if (ip[2] >= curzmax)
291 >                        apexh = 1. - 1./DEPTHFACT;
292 >                else
293 >                        apexh = 1. - ip[2]/(curzmax*DEPTHFACT);
294          }
295 <        return(XPending(ourdisplay));
295 >        rad *= 1.25;                    /* find conservative cone match */
296 >        for (ci = 0; ci < MAXCONE-1; ci++)
297 >                if (cone[ci].rad >= rad)
298 >                        break;
299 >                                        /* draw it */
300 >        glColor3ub(rgb[0], rgb[1], rgb[2]);
301 >        glBegin(GL_TRIANGLE_FAN);
302 >        glVertex3d(ip[0], ip[1], apexh);        /* start with apex */
303 >        basez = apexh*cone[ci].va[0][2];        /* base z's all the same */
304 >        for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */
305 >                glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1],
306 >                                basez);
307 >                                                /* connect last to first */
308 >        glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez);
309 >        glEnd();                                /* all done */
310   }
311  
312  
# Line 312 | Line 322 | mytmflags()                    /* figure out tone mapping flags */
322          for (cp = tail; *cp && *cp != '.'; cp++)
323                  ;
324          if (cp-tail == 3 && !strncmp(tail, "glx", 3))
325 <                return(TM_F_CAMERA);
325 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
326          if (cp-tail == 4 && !strncmp(tail, "glxh", 4))
327 <                return(TM_F_HUMAN);
327 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
328          error(USER, "illegal driver name");
329   }
330  
331  
332   static
333 < setGLview()                     /* set our GL view */
333 > initcones()                     /* initialize cone vertices */
334   {
325        double  xmin, xmax, ymin, ymax, zmin, zmax;
326        double  d, cx, sx, crad;
327        FVECT   vx, vy;
335          register int    i, j;
336 <                                        /* compute view frustum */
337 <        if (normalize(odev.v.vdir) == 0.0)
338 <                return;
339 <        if (mindepth < maxdepth) {
340 <                zmin = 0.25*mindepth;
341 <                zmax = 8.0*maxdepth;
342 <        } else {
343 <                zmin = 0.01;
344 <                zmax = 1000.;
336 >        double  minrad, d;
337 >
338 >        if (cone[0].nverts)
339 >                freecones();
340 >        minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) +
341 >                                        DisplayHeight(ourdisplay,ourscreen));
342 >        for (i = 0; i < MAXCONE; i++) {
343 >                d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */
344 >                cone[i].rad = minrad + (1.-minrad)*d;
345 >                cone[i].nverts = MINVERT + (MAXVERT-MINVERT)*d;
346 >                cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT));
347 >                if (cone[i].va == NULL)
348 >                        error(SYSTEM, "out of memory in initcones");
349 >                for (j = cone[i].nverts; j--; ) {
350 >                        d = 2.*PI * (j+.5) / (cone[i].nverts);
351 >                        cone[i].va[j][0] = cos(d) * cone[i].rad;
352 >                        cone[i].va[j][1] = sin(d) * cone[i].rad;
353 >                        cone[i].va[j][2] = 1. - cone[i].rad;
354 >                }
355          }
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        }
356   }
357  
358  
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
359   static
360 < drawvalue(li)                   /* draw a pixel value as a cone */
398 < register int    li;
360 > freecones()                     /* free cone vertices */
361   {
400        static FVECT    apex, disp;
401        double  d, h, v;
362          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 }
363  
364 <
365 < #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
366 <                        sizeof(TMbright)+7*sizeof(BYTE))
367 <
368 < static
369 < 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);
364 >        for (i = MAXCONE; i--; )
365 >                if (cone[i].nverts) {
366 >                        free((char *)cone[i].va);
367 >                        cone[i].va = NULL;
368 >                        cone[i].nverts = 0;
369 >                }
370   }
371  
481 #undef  LEAFSIZ
372  
483
373   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
374   getevent()                      /* get next event */
375   {
376          XNextEvent(ourdisplay, levptr(XEvent));
# Line 603 | Line 409 | register FVECT wp[2];
409   static
410   draw_grids()                    /* draw holodeck section grids */
411   {
412 <        static GLubyte  gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
413 <
414 <        if (!mapped)
412 >        static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00};
413 >        double  xmin, xmax, ymin, ymax, zmin, zmax;
414 >        double  d, cx, sx, crad;
415 >        FVECT   vx, vy;
416 >        register int    i, j;
417 >                                        /* can we even do it? */
418 >        if (!mapped || odev.v.type != VT_PER)
419                  return;
420 +                                        /* compute view frustum */
421 +        if (normalize(odev.v.vdir) == 0.0)
422 +                return;
423 +        zmin = 0.01;
424 +        zmax = 10000.;
425 +        if (odev.v.vfore > FTINY)
426 +                zmin = odev.v.vfore;
427 +        if (odev.v.vaft > FTINY)
428 +                zmax = odev.v.vaft;
429 +        xmax = zmin * tan(PI/180./2. * odev.v.horiz);
430 +        xmin = -xmax;
431 +        d = odev.v.hoff * (xmax - xmin);
432 +        xmin += d; xmax += d;
433 +        ymax = zmin * tan(PI/180./2. * odev.v.vert);
434 +        ymin = -ymax;
435 +        d = odev.v.voff * (ymax - ymin);
436 +        ymin += d; ymax += d;
437 +                                        /* set view matrix */
438 +        glMatrixMode(GL_PROJECTION);
439 +        glPushMatrix();
440 +        glLoadIdentity();
441 +        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
442 +        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2],
443 +                odev.v.vp[0] + odev.v.vdir[0],
444 +                odev.v.vp[1] + odev.v.vdir[1],
445 +                odev.v.vp[2] + odev.v.vdir[2],
446 +                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
447 +        glDisable(GL_DEPTH_TEST);       /* write no depth values */
448          glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]);
449 <                                        /* draw each grid line */
612 <        glBegin(GL_LINES);
449 >        glBegin(GL_LINES);              /* draw each grid line */
450          gridlines(draw3dline);
451          glEnd();
452 +        glEnable(GL_DEPTH_TEST);        /* restore rendering params */
453 +        glPopMatrix();
454   }
455  
456  
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
457   static
458   moveview(dx, dy, mov, orb)      /* move our view */
459   int     dx, dy, mov, orb;
# Line 635 | Line 466 | int    dx, dy, mov, orb;
466          copystruct(&nv, &odev.v);
467                                  /* change view direction */
468          if (mov | orb) {
469 <                if ((li = FindValue(dx, dy)) < 0)
469 >                if ((li = qtFindLeaf(dx, dy)) < 0)
470                          return(0);      /* not on window */
471 <                VSUM(odir, rV.wp[li], nv.vp, -1.);
471 >                VSUM(odir, qtL.wp[li], nv.vp, -1.);
472          } else {
473                  if (viewray(nv.vp, nv.vdir, &odev.v,
474                                  (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
# Line 645 | Line 476 | int    dx, dy, mov, orb;
476          }
477          if (orb && mov) {               /* orbit left/right */
478                  spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
479 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
479 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
480                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
481          } else if (orb) {               /* orbit up/down */
482                  fcross(v1, odir, nv.vup);
483                  if (normalize(v1) == 0.)
484                          return(0);
485                  spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
486 <                VSUM(nv.vp, rV.wp[li], odir, -1.);
486 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
487                  spinvector(nv.vdir, nv.vdir, v1, d);
488          } else if (mov) {               /* move forward/backward */
489                  d = MOVPCT/100. * mov;
# Line 677 | Line 508 | XButtonPressedEvent    *ebut;
508   {
509          int     movdir = MOVDIR(ebut->button);
510          int     movorb = MOVORB(ebut->state);
511 +        int     oldnodesiz = qtMinNodesiz;
512          Window  rootw, childw;
513          int     rootx, rooty, wx, wy;
514          unsigned int    statemask;
515  
516 +        qtMinNodesiz = 24;              /* accelerate update rate */
517          XNoOp(ourdisplay);
518  
519          while (!XCheckMaskEvent(ourdisplay,
# Line 694 | Line 527 | XButtonPressedEvent    *ebut;
527                          sleep(1);
528                          continue;
529                  }
530 +                glClear(GL_COLOR_BUFFER_BIT);
531 +                qtUpdate();
532                  draw_grids();
533 <                dev_flush();
533 >                glFlush();
534          }
535          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
536                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 704 | Line 539 | XButtonPressedEvent    *ebut;
539                  moveview(wx, odev.vres-1-wy, movdir, movorb);
540          }
541          dev_flush();
542 +
543 +        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
544   }
545  
546  
# Line 738 | Line 575 | register XKeyPressedEvent  *ekey;
575                  inpresflags |= DFL(DC_RESUME);
576                  return;
577          case CTRL('R'):                 /* redraw screen */
578 <                TMapValues(1);
579 <                redraw();
578 >                if (nxtzmax > FTINY) {
579 >                        curzmax = nxtzmax;
580 >                        nxtzmax = 0.;
581 >                }
582 >                glClear(GL_DEPTH_BUFFER_BIT);
583 >                qtRedraw(0, 0, odev.hres, odev.vres);
584                  return;
585          case CTRL('L'):                 /* refresh from server */
586                  if (inpresflags & DFL(DC_REDRAW))
587                          return;
588 +                if (nxtzmax > FTINY) {
589 +                        curzmax = nxtzmax;
590 +                        nxtzmax = 0.;
591 +                }
592                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
748                glDrawBuffer(GL_FRONT);
749                glDisable(GL_DEPTH_TEST);
593                  draw_grids();
751                glEnable(GL_DEPTH_TEST);
752                glDrawBuffer(GL_FRONT_AND_BACK);
594                  glFlush();
595 <                Compost(100);                   /* get rid of old values */
595 >                qtCompost(100);                 /* get rid of old values */
596                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
597 +                rayqleft = 0;                   /* hold off update */
598                  return;
599          case 'K':                       /* kill rtrace process(es) */
600                  inpresflags |= DFL(DC_KILL);
# Line 777 | Line 619 | static
619   fixwindow(eexp)                         /* repair damage to window */
620   register XExposeEvent  *eexp;
621   {
622 <        if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
623 <                odev.hres = eexp->width;
624 <                odev.vres = eexp->height;
625 <        }
626 <        if (eexp->width == odev.hres && eexp->height == odev.vres)
627 <                TMapValues(1);
628 <        if (!eexp->count)
629 <                redraw();
622 >        int     xmin, xmax, ymin, ymax;
623 >
624 >        if (odev.hres == 0 || odev.vres == 0)   /* first exposure */
625 >                resizewindow((XConfigureEvent *)eexp);
626 >        xmin = eexp->x; xmax = eexp->x + eexp->width;
627 >        ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
628 >                                                /* clear portion of depth */
629 >        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
630 >        glDepthFunc(GL_ALWAYS);
631 >        glBegin(GL_POLYGON);
632 >        glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.);
633 >        glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.);
634 >        glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.);
635 >        glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.);
636 >        glEnd();
637 >        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
638 >        glDepthFunc(GL_LEQUAL);
639 >        qtRedraw(xmin, ymin, xmax, ymax);
640   }
641  
642  
# Line 792 | Line 644 | static
644   resizewindow(ersz)                      /* resize window */
645   register XConfigureEvent  *ersz;
646   {
647 +        glViewport(0, 0, ersz->width, ersz->height);
648 +
649          if (ersz->width == odev.hres && ersz->height == odev.vres)
650                  return;
651  
# Line 800 | Line 654 | register XConfigureEvent  *ersz;
654  
655          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
656          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
803
804        setGLview();
657  
658          inpresflags |= DFL(DC_SETVIEW);
659   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines