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

Comparing ray/src/hd/rhd_ogl.c (file contents):
Revision 3.2 by gwlarson, Sun Dec 20 20:36:48 1998 UTC vs.
Revision 3.34 by greg, Fri Feb 12 00:53:56 2021 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * OpenGL driver for holodeck display.
6   * Based on GLX driver using T-mesh.
# Line 20 | Line 17 | static char SCCSid[] = "$SunId$ SGI";
17   #endif
18   #endif
19  
23 #include "standard.h"
20  
21 < #include <sys/types.h>
21 > #include <time.h>
22   #include <GL/glx.h>
23   #include <GL/glu.h>
24   #ifdef STEREO
25   #include <X11/extensions/SGIStereo.h>
26   #endif
27  
28 + #include "standard.h"
29   #include "rhd_odraw.h"
30 + #include "rhdisp.h"
31 + #include "paths.h"
32   #ifdef DOBJ
33   #include "rhdobj.h"
34   #endif
# Line 37 | Line 36 | static char SCCSid[] = "$SunId$ SGI";
36   #include "x11icon.h"
37  
38   #ifndef RAYQLEN
39 < #define RAYQLEN         10240           /* max. rays to queue before flush */
39 > #define RAYQLEN         0               /* max. rays to queue before flush */
40   #endif
41 <
42 < #ifndef FEQ
43 < #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
41 > #ifndef MINWIDTH
42 > #define MINWIDTH        480             /* minimum graphics window width */
43 > #define MINHEIGHT       400             /* minimum graphics window height */
44   #endif
45 + #ifndef VIEWDIST
46 + #define VIEWDIST        356             /* assumed viewing distance (mm) */
47 + #endif
48 + #ifndef BORWIDTH
49 + #define BORWIDTH        5               /* border width */
50 + #endif
51  
52 + #define VWHEADLOCK      01              /* head position is locked flag */
53 + #define VWPERSP         02              /* perspective view is set */
54 + #define VWORTHO         04              /* orthographic view is set */
55 + #define VWCHANGE        010             /* view has changed */
56 + #define VWSTEADY        020             /* view is now steady */
57 + #define VWMAPPED        040             /* window is mapped */
58 +
59   #define GAMMA           1.4             /* default gamma correction */
60  
61   #define FRAMESTATE(s)   (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask))
# Line 53 | Line 65 | static char SCCSid[] = "$SunId$ SGI";
65   #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */
66   #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
67  
56 #define MINWIDTH        480             /* minimum graphics window width */
57 #define MINHEIGHT       400             /* minimum graphics window height */
58
59 #define VIEWDIST        356             /* assumed viewing distance (mm) */
60
61 #define BORWIDTH        5               /* border width */
62
68   #define setstereobuf(bid)       (glXWaitGL(), \
69                                  XSGISetStereoBuffer(ourdisplay, gwind, bid), \
70                                  glXWaitX())
# Line 73 | Line 78 | static char SCCSid[] = "$SunId$ SGI";
78  
79   struct driver   odev;                   /* global device driver structure */
80  
81 + TMstruct        *tmGlobal;              /* global tone-mapping structure */
82 +
83   char odev_args[64];                     /* command arguments */
84  
85   static GLfloat  *depthbuffer = NULL;    /* depth buffer */
# Line 86 | Line 93 | static int     rayqleft = 0;           /* rays left to queue before
93  
94   static XEvent  currentevent;            /* current event */
95  
89 static int  mapped = 0;                 /* window is mapped? */
96   static unsigned long  ourblack=0, ourwhite=~0;
97  
98   static Display  *ourdisplay = NULL;     /* our display */
# Line 104 | Line 110 | static double  dev_zrat;               /* (1. - dev_zmin/dev_zmax) *
110  
111   static int      inpresflags;            /* input result flags */
112  
113 < static int      headlocked;             /* lock vertical motion */
113 > static int      viewflags;              /* what's happening with view */
114  
115 < static int      isperspective;          /* perspective/ortho view */
110 <
111 < static int      viewsteady;             /* is view steady? */
112 <
115 > /*
116   static int  resizewindow(), getevent(), getkey(), moveview(), wipeclean(),
117                  xferdepth(), freedepth(), setglortho(),
118                  setglpersp(), getframe(), getmove(), fixwindow(), mytmflags();
119  
120   static double   getdistance();
121 + */
122 + static void checkglerr(char *where);
123 + static void xferdepth(void);
124 + static void freedepth(void);
125 + static double getdistance(int dx, int dy, FVECT direc);
126 + static int mytmflags(void);
127 + static void getevent(void);
128 + static void draw3dline(FVECT wp[2]);
129 + static void draw_grids(int fore);
130 + static int moveview(int dx, int dy, int mov, int orb);
131 + static void getframe(XButtonPressedEvent *ebut);
132 + static void getmove(XButtonPressedEvent *ebut);
133 + static void getkey(XKeyPressedEvent *ekey);
134 + static void fixwindow(XExposeEvent *eexp);
135 + static void resizewindow(XConfigureEvent *ersz);
136 + static void waitabit(void);
137 + static void setglpersp(void);
138 + static void setglortho(void);
139 + static void wipeclean(void);
140  
141 +
142   #ifdef STEREO
143 < static int  pushright(), popright();
143 > static void pushright(void);
144 > static void popright(void);
145   #endif
146  
147   extern int      gmPortals;      /* GL portal list id */
# Line 125 | Line 149 | extern int     gmPortals;      /* GL portal list id */
149   extern time_t   time();
150  
151  
152 + void
153   dev_open(id)                    /* initialize GLX driver */
154   char  *id;
155   {
156          extern char     *getenv();
157          static RGBPRIMS myprims = STDPRIMS;
158 <        static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8,
159 <                                GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8,
160 <                                GLX_DEPTH_SIZE,15, None};
158 >        static int      atlBest[] = {GLX_RGBA, GLX_DOUBLEBUFFER,
159 >                                GLX_RED_SIZE,8, GLX_GREEN_SIZE,8,
160 >                                GLX_BLUE_SIZE,8, GLX_DEPTH_SIZE,15, None};
161 >        static int      atlOK[] = {GLX_RGBA, GLX_DOUBLEBUFFER,
162 >                                GLX_RED_SIZE,4, GLX_GREEN_SIZE,4,
163 >                                GLX_BLUE_SIZE,4, GLX_DEPTH_SIZE,15, None};
164          char    *ev;
165          double  gamval = GAMMA;
166          RGBPRIMP        dpri = stdprims;
# Line 145 | Line 173 | char  *id;
173   #endif
174                                          /* open display server */
175          ourdisplay = XOpenDisplay(NULL);
176 <        if (ourdisplay == NULL)
177 <                error(USER, "cannot open X-windows; DISPLAY variable set?\n");
176 >        CHECK(ourdisplay==NULL, USER,
177 >                        "cannot open X-windows; DISPLAY variable set?");
178   #ifdef STEREO
179          switch (XSGIQueryStereoMode(ourdisplay, ourroot)) {
180          case STEREO_TOP:
# Line 154 | Line 182 | char  *id;
182                  break;
183          case STEREO_OFF:
184                  error(USER,
185 <        "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
185 >                "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
186          case X_STEREO_UNSUPPORTED:
187                  error(USER, "stereo mode not supported on this screen");
188          default:
# Line 164 | Line 192 | char  *id;
192                                          /* find a usable visual */
193          ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
194          if (ourvinf == NULL)
195 <                error(USER, "no suitable visuals available");
195 >                ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK);
196 >        CHECK(ourvinf==NULL, USER, "no suitable visuals available");
197                                          /* get a context */
198          gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
199                                          /* set gamma and tone mapping */
# Line 178 | Line 207 | char  *id;
207                                  &myprims[BLU][CIEX],&myprims[BLU][CIEY],
208                                  &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
209                  dpri = myprims;
210 <        if (tmInit(mytmflags(), dpri, gamval) == NULL)
210 >        tmGlobal = tmInit(mytmflags(), dpri, gamval);
211 >        if (tmGlobal == NULL)
212                  error(SYSTEM, "not enough memory in dev_open");
213                                          /* open window */
214          ourwinattr.background_pixel = ourblack;
# Line 196 | Line 226 | char  *id;
226   #endif
227                  BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
228                  CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
229 <        if (gwind == 0)
200 <                error(SYSTEM, "cannot create window\n");
229 >        CHECK(gwind==0, SYSTEM, "cannot create window");
230          XStoreName(ourdisplay, gwind, id);
231                                          /* set window manager hints */
232          ourxwmhints.flags = InputHint|IconPixmapHint;
233          ourxwmhints.input = True;
234 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
235 <                        gwind, x11icon_bits, x11icon_width, x11icon_height);
234 >        ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, gwind,
235 >                        (char *)x11icon_bits, x11icon_width, x11icon_height);
236          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
237          oursizhints.min_width = MINWIDTH;
238   #ifdef STEREO
# Line 221 | Line 250 | char  *id;
250          glXMakeCurrent(ourdisplay, gwind, gctx);
251          glEnable(GL_DEPTH_TEST);
252          glDepthFunc(GL_LEQUAL);
253 <        glDisable(GL_DITHER);
253 >        glClearColor(0, 0, 0, 0);
254          glFrontFace(GL_CCW);
255          glDisable(GL_CULL_FACE);
256          glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
257          glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
258 +        glPixelStorei(GL_PACK_ALIGNMENT, 1);
259 +        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
260                                          /* figure out sensible view */
261          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
262                          DisplayWidth(ourdisplay, ourscreen);
# Line 236 | Line 267 | char  *id;
267          setstereobuf(STEREO_BUFFER_LEFT);
268   #endif
269          checkglerr("setting rendering parameters");
270 <        copystruct(&odev.v, &stdview);
270 >        odev.v = stdview;
271          odev.v.type = VT_PER;
272 <        headlocked = 0;                 /* free up head movement */
242 <        viewsteady = 1;                 /* view starts static */
243 <        isperspective = -1;             /* but no view set, yet */
272 >        viewflags = VWSTEADY;           /* view starts static */
273                                          /* map the window */
274          XMapWindow(ourdisplay, gwind);
275          dev_input();                    /* sets size and view angles */
276          if (!odInit(DisplayWidth(ourdisplay,ourscreen) *
277 <                        DisplayHeight(ourdisplay,ourscreen) / 10))
277 >                        DisplayHeight(ourdisplay,ourscreen) / 3))
278                  error(SYSTEM, "insufficient memory for value storage");
279          odev.name = id;
280          odev.firstuse = 1;              /* can't recycle samples */
# Line 253 | Line 282 | char  *id;
282   }
283  
284  
285 < dev_close()                     /* close our display and free resources */
285 > void
286 > dev_close(void)                 /* close our display and free resources */
287   {
288   #ifdef DOBJ
289          dobj_cleanup();
# Line 268 | Line 298 | dev_close()                    /* close our display and free resources
298          gwind = 0;
299          XCloseDisplay(ourdisplay);
300          ourdisplay = NULL;
301 <        tmDone(NULL);
301 >        tmDone(tmGlobal);
302          odev.v.type = 0;
303          odev.hres = odev.vres = 0;
304          odev.ifd = -1;
305   }
306  
307  
308 < dev_clear()                     /* clear our representation */
308 > void
309 > dev_clear(void)                 /* clear our representation */
310   {
311 <        wipeclean();
311 >        viewflags |= VWCHANGE;          /* pretend our view has changed */
312 >        wipeclean();                    /* clean off display and samples */
313 >        dev_flush();                    /* redraw geometry & get depth */
314          rayqleft = 0;                   /* hold off update */
315   }
316  
317  
318   int
319 < dev_view(nv)                    /* assign new driver view */
320 < register VIEW   *nv;
319 > dev_view(                       /* assign new driver view */
320 >        VIEW    *nv
321 > )
322   {
323 + #ifdef STEREO
324          double  d;
325 <
325 > #endif
326          if (nv->type != VT_PER ||               /* check view legality */
327                          nv->horiz > 160. || nv->vert > 160.) {
328                  error(COMMAND, "illegal view type/angle");
# Line 298 | Line 333 | register VIEW  *nv;
333          }
334          if (nv != &odev.v) {
335                                                  /* resize window? */
336 <                if (!FEQ(nv->horiz,odev.v.horiz) ||
337 <                                !FEQ(nv->vert,odev.v.vert)) {
336 >                if (!FABSEQ(nv->horiz,odev.v.horiz) ||
337 >                                !FABSEQ(nv->vert,odev.v.vert)) {
338                          int     dw = DisplayWidth(ourdisplay,ourscreen);
339                          int     dh = DisplayHeight(ourdisplay,ourscreen);
340  
# Line 323 | Line 358 | register VIEW  *nv;
358                          XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
359                          dev_input();    /* get resize event */
360                  }
361 <                copystruct(&odev.v, nv);        /* setview() already called */
361 >                odev.v = *nv;   /* setview() already called */
362 >                viewflags |= VWCHANGE;
363 >        }
364   #ifdef STEREO
365 <                copystruct(&vwright, nv);
366 <                d = eyesepdist / sqrt(nv->hn2);
367 <                VSUM(vwright.vp, nv->vp, nv->hvec, d);
368 <                /* setview(&vwright);   -- Unnecessary */
365 >        vwright = *nv;
366 >        d = eyesepdist / sqrt(nv->hn2);
367 >        VSUM(vwright.vp, nv->vp, nv->hvec, d);
368 >        /* setview(&vwright);   -- Unnecessary */
369   #endif
333        } else
334                viewsteady = 1;
370          wipeclean();
371          return(1);
372   }
373  
374  
375 < dev_section(gfn, pfn)           /* add octree for geometry rendering */
376 < char    *gfn, *pfn;
375 > void
376 > dev_section(            /* add octree for geometry rendering */
377 >        char    *gfn,
378 >        char    *pfn
379 > )
380   {
343        extern char     *index();
344        char    *cp;
345
381          if (gfn == NULL) {
382                  gmEndGeom();
383                  gmEndPortal();
384 +                wipeclean();            /* new geometry, so redraw it */
385                  return;
386          }
387          if (access(gfn, R_OK) == 0)
# Line 361 | Line 397 | char   *gfn, *pfn;
397   }
398  
399  
400 < dev_auxcom(cmd, args)           /* process an auxiliary command */
401 < char    *cmd, *args;
400 > void
401 > dev_auxcom(             /* process an auxiliary command */
402 >        char    *cmd,
403 >        char    *args
404 > )
405   {
406   #ifdef DOBJ
407 <        if (dobj_command(cmd, args) >= 0)
407 >        int     vischange;
408 >
409 >        if ((vischange = dobj_command(cmd, args)) >= 0) {
410 >                if (vischange) {
411 >                        imm_mode = beam_sync(1) > 0;
412 >                        dev_clear();
413 >                }
414                  return;
415 +        }
416   #endif
417          sprintf(errmsg, "%s: unknown command", cmd);
418          error(COMMAND, errmsg);
# Line 374 | Line 420 | char   *cmd, *args;
420  
421  
422   VIEW *
423 < dev_auxview(n, hvres)           /* return nth auxiliary view */
424 < int     n;
425 < int     hvres[2];
423 > dev_auxview(            /* return nth auxiliary view */
424 >        int     n,
425 >        int     hvres[2]
426 > )
427   {
428          hvres[0] = odev.hres; hvres[1] = odev.vres;
429          if (n == 0)
# Line 390 | Line 437 | int    hvres[2];
437  
438  
439   int
440 < dev_input()                     /* get X11 input */
440 > dev_input(void)                 /* get X11 input */
441   {
442          inpresflags = 0;
443  
# Line 405 | Line 452 | dev_input()                    /* get X11 input */
452   }
453  
454  
455 < dev_value(c, d, p)              /* add a pixel value to our texture */
456 < COLR    c;
457 < FVECT   d, p;
455 > void
456 > dev_value(              /* add a pixel value to our texture */
457 >        COLR    c,
458 >        FVECT   d,
459 >        FVECT   p
460 > )
461   {
462   #ifdef DOBJ
463          if (dobj_lightsamp != NULL) {   /* in light source sampling */
# Line 422 | Line 472 | FVECT  d, p;
472  
473  
474   int
475 < dev_flush()                     /* flush output as appropriate */
475 > dev_flush(void)                 /* flush output as appropriate */
476   {
477          int     ndrawn;
478  
479 <        if (mapped && isperspective > 0) {
479 >        if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) {
480   #ifdef STEREO
481                  pushright();                    /* draw right eye */
482 <                gmDrawGeom(0);
482 >                ndrawn = gmDrawGeom();
483   #ifdef DOBJ
484 <                dobj_render();
484 >                ndrawn += dobj_render();
485   #endif
486                  checkglerr("rendering right eye");
487                  popright();                     /* draw left eye */
488   #endif
489 <                ndrawn = gmDrawGeom(1);
489 >                ndrawn = gmDrawGeom();
490   #ifdef DOBJ
491                  ndrawn += dobj_render();
492   #endif
493 +                glXSwapBuffers(ourdisplay, gwind);
494                  checkglerr("rendering base view");
495          }
496 <        if (mapped && viewsteady) {
497 <                if (isperspective) {            /* first time after steady */
496 >        if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) ==
497 >                        (VWMAPPED|VWSTEADY|VWPERSP)) {
498 >                                        /* first time after steady */
499 >                if (ndrawn)
500 >                        xferdepth();    /* transfer and clear depth */
501 >                setglortho();           /* set orthographic view */
502 >
503 >        }
504 >        if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) ==
505 >                        (VWMAPPED|VWSTEADY|VWORTHO)) {
506 >                                        /* else update cones */
507   #ifdef STEREO
508 <                        pushright();
509 <                        popright();
508 >                pushright();
509 >                odUpdate(1);            /* draw right eye */
510 >                popright();
511   #endif
512 <                        if (ndrawn)
513 <                                xferdepth();    /* transfer and clear depth */
453 <                        setglortho();           /* set orthographic view */
454 <                } else {
455 < #ifdef STEREO
456 <                        pushright();
457 <                        odUpdate(1);            /* draw right eye */
458 <                        popright();
459 < #endif
460 <                        odUpdate(0);            /* draw left eye */
461 <                }
512 >                odUpdate(0);            /* draw left eye */
513 >                glFlush();              /* flush OpenGL */
514          }
463        glFlush();                              /* flush OpenGL */
515          rayqleft = RAYQLEN;
516                                          /* flush X11 and return # pending */
517          return(odev.inpready = XPending(ourdisplay));
518   }
519  
520  
521 < checkglerr(where)               /* check for GL or GLU error */
522 < char    *where;
521 > static void
522 > checkglerr(             /* check for GL or GLU error */
523 >        char    *where
524 > )
525   {
526 <        register GLenum errcode;
526 >        GLenum  errcode;
527  
528          while ((errcode = glGetError()) != GL_NO_ERROR) {
529                  sprintf(errmsg, "OpenGL error %s: %s",
# Line 480 | Line 533 | char   *where;
533   }
534  
535  
536 < static
537 < xferdepth()                     /* load and clear depth buffer */
536 > static void
537 > xferdepth(void)                 /* load and clear depth buffer */
538   {
539 <        register GLfloat        *dbp;
540 <        register GLubyte        *abuf;
539 >        GLfloat *dbp;
540 >        GLubyte *pbuf;
541  
542 <        if (depthbuffer == NULL) {
542 >        if (depthbuffer == NULL) {      /* allocate private depth buffer */
543   #ifdef STEREO
544                  depthright = (GLfloat *)malloc(
545                                  odev.hres*odev.vres*sizeof(GLfloat));
546   #endif
547                  depthbuffer = (GLfloat *)malloc(
548                                  odev.hres*odev.vres*sizeof(GLfloat));
549 <                if (depthbuffer == NULL)
497 <                        error(SYSTEM, "out of memory in xferdepth");
549 >                CHECK(depthbuffer==NULL, SYSTEM, "out of memory in xferdepth");
550          }
551 +                                /* allocate alpha buffer for portals */
552 +        if (gmPortals)
553 +                pbuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte));
554 +        else
555 +                pbuf = NULL;
556   #ifdef STEREO
557 <        setstereobuf(STEREO_BUFFER_RIGHT);
557 >        pushright();
558          glReadPixels(0, 0, odev.hres, odev.vres,
559                          GL_DEPTH_COMPONENT, GL_FLOAT, depthright);
560 <        setstereobuf(STEREO_BUFFER_LEFT);
560 >        if (pbuf != NULL) {
561 >                glClear(GL_COLOR_BUFFER_BIT);
562 >                gmDrawPortals(0xff, -1, -1, -1);
563 >                glReadPixels(0, 0, odev.hres, odev.vres,
564 >                                GL_RED, GL_UNSIGNED_BYTE, pbuf);
565 >        }
566          for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; )
567 <                *dbp = mapdepth(*dbp);
568 <        odDepthMap(1, depthright);
567 >                if (pbuf != NULL && pbuf[dbp-depthright]&0x40)
568 >                        *dbp = FHUGE;
569 >                else
570 >                        *dbp = mapdepth(*dbp);
571          glClear(GL_DEPTH_BUFFER_BIT);
572 +        odDepthMap(1, depthright);
573 +        popright();
574   #endif
575                                  /* read back depth buffer */
576          glReadPixels(0, 0, odev.hres, odev.vres,
577                          GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer);
578 <                                /* read alpha buffer for portals */
579 <        if (gmPortals)
580 <                abuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte));
515 <        else
516 <                abuf = NULL;
517 <        if (abuf != NULL)
578 >        if (pbuf != NULL) {
579 >                glClear(GL_COLOR_BUFFER_BIT);           /* find portals */
580 >                gmDrawPortals(0xff, -1, -1, -1);
581                  glReadPixels(0, 0, odev.hres, odev.vres,
582 <                                GL_ALPHA, GL_UNSIGNED_BYTE, abuf);
582 >                                GL_RED, GL_UNSIGNED_BYTE, pbuf);
583 > #ifdef DEBUG
584 >                glXSwapBuffers(ourdisplay, gwind);
585 > #endif
586 >        }
587          for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; )
588 <                if (abuf != NULL && abuf[dbp-depthbuffer])
588 >                if (pbuf != NULL && pbuf[dbp-depthbuffer]&0x40)
589                          *dbp = FHUGE;
590                  else
591                          *dbp = mapdepth(*dbp);
592 +        glClear(GL_DEPTH_BUFFER_BIT);           /* clear system depth buffer */
593          odDepthMap(0, depthbuffer);             /* transfer depth data */
594 <        glClear(GL_DEPTH_BUFFER_BIT);           /* clear system buffer */
595 <        if (abuf != NULL)
528 <                free((char *)abuf);             /* free alpha buffer */
594 >        if (pbuf != NULL)
595 >                free((void *)pbuf);             /* free our portal buffer */
596   }
597  
598  
599 < static
600 < freedepth()                             /* free recorded depth buffer */
599 > static void
600 > freedepth(void)                         /* free recorded depth buffer */
601   {
602          if (depthbuffer == NULL)
603                  return;
604   #ifdef STEREO
605          odDepthMap(1, NULL);
606 <        free((char *)depthright);
606 >        free((void *)depthright);
607          depthright = NULL;
608   #endif
609          odDepthMap(0, NULL);
610 <        free((char *)depthbuffer);
610 >        free((void *)depthbuffer);
611          depthbuffer = NULL;
612   }
613  
614  
615   static double
616 < getdistance(dx, dy, direc)      /* distance from fore plane along view ray */
617 < int     dx, dy;
618 < FVECT   direc;
616 > getdistance(    /* distance from fore plane along view ray */
617 >        int     dx,
618 >        int     dy,
619 >        FVECT   direc
620 > )
621   {
622          GLfloat gldepth;
554        GLubyte glalpha;
623          double  dist;
624  
625 <        if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres)
625 >        if ((dx<0) | (dx>=odev.hres) | (dy<0) | (dy>=odev.vres))
626                  return(FHUGE);
627          if (depthbuffer != NULL)
628                  dist = depthbuffer[dy*odev.hres + dx];
629          else {
630                  glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT,
631                                  GL_FLOAT, &gldepth);
632 <                if (gmPortals)
633 <                        glReadPixels(dx,dy, 1,1, GL_ALPHA,
634 <                                GL_UNSIGNED_BYTE, &glalpha);
567 <                else
568 <                        glalpha = 0;
569 <                dist = glalpha ? FHUGE : mapdepth(gldepth);
632 >                if (gldepth <= FTINY)
633 >                        return (FHUGE); /* call failed */
634 >                dist = mapdepth(gldepth);
635          }
636          if (dist >= .99*FHUGE)
637                  return(FHUGE);
# Line 575 | Line 640 | FVECT  direc;
640  
641  
642   #ifdef STEREO
643 < static
644 < pushright()                     /* push on right view & buffer */
643 > static void
644 > pushright(void)                 /* push on right view & buffer */
645   {
646          double  d;
647  
648          setstereobuf(STEREO_BUFFER_RIGHT);
649 <        if (isperspective > 0) {
649 >        if (viewflags & VWPERSP) {
650                  glMatrixMode(GL_MODELVIEW);
651                  glPushMatrix();
652                  d = -eyesepdist / sqrt(odev.v.hn2);
# Line 592 | Line 657 | pushright()                    /* push on right view & buffer */
657   }
658  
659  
660 < static
661 < popright()                      /* pop off right view & buffer */
660 > static void
661 > popright(void)                  /* pop off right view & buffer */
662   {
663 <        if (isperspective > 0) {
663 >        if (viewflags & VWPERSP) {
664                  glMatrixMode(GL_MODELVIEW);
665                  glPopMatrix();
666          }
# Line 605 | Line 670 | popright()                     /* pop off right view & buffer */
670  
671  
672   static int
673 < mytmflags()                     /* figure out tone mapping flags */
673 > mytmflags(void)                 /* figure out tone mapping flags */
674   {
675          extern char     *progname;
676 <        register char   *cp, *tail;
676 >        char    *cp, *tail;
677                                          /* find basic name */
678          for (cp = tail = progname; *cp; cp++)
679                  if (*cp == '/')
# Line 629 | Line 694 | mytmflags()                    /* figure out tone mapping flags */
694   }
695  
696  
697 < static
698 < getevent()                      /* get next event */
697 > static void
698 > getevent(void)                  /* get next event */
699   {
700          XNextEvent(ourdisplay, levptr(XEvent));
701          switch (levptr(XEvent)->type) {
# Line 638 | Line 703 | getevent()                     /* get next event */
703                  resizewindow(levptr(XConfigureEvent));
704                  break;
705          case UnmapNotify:
706 <                mapped = 0;
706 >                viewflags &= ~VWMAPPED;
707                  break;
708          case MapNotify:
709                  odRemap(0);
710 <                mapped = 1;
710 >                viewflags |= VWMAPPED;
711                  break;
712          case Expose:
713                  fixwindow(levptr(XExposeEvent));
# Line 651 | Line 716 | getevent()                     /* get next event */
716                  getkey(levptr(XKeyPressedEvent));
717                  break;
718          case ButtonPress:
719 <                if (FRAMESTATE(levptr(XButtonPressedEvent)->state))
720 <                        getframe(levptr(XButtonPressedEvent));
719 >                if (FRAMESTATE(levptr(XButtonPressedEvent)->state))
720 >                        getframe(levptr(XButtonPressedEvent));
721                  else
722 <                        getmove(levptr(XButtonPressedEvent));
722 >                        switch (levptr(XButtonPressedEvent)->button) {
723 >                        case Button4:           /* wheel up */
724 >                        case Button5:           /* wheel down */
725 >                                break;
726 >                        default:
727 >                                getmove(levptr(XButtonPressedEvent));
728 >                                break;
729 >                        }
730                  break;
731          }
732   }
733  
734  
735 < static
736 < draw3dline(wp)                  /* draw 3d line in world coordinates */
737 < register FVECT  wp[2];
735 > static void
736 > draw3dline(                     /* draw 3d line in world coordinates */
737 >        FVECT   wp[2]
738 > )
739   {
740          glVertex3d(wp[0][0], wp[0][1], wp[0][2]);
741          glVertex3d(wp[1][0], wp[1][1], wp[1][2]);
742   }
743  
744  
745 < static
746 < draw_grids(fore)                /* draw holodeck section grids */
747 < int     fore;
745 > static void
746 > draw_grids(             /* draw holodeck section grids */
747 >        int     fore
748 > )
749   {
750          glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT);
751          glDisable(GL_LIGHTING);
752          if (fore)
753 <                glColor3ub(0, 255, 255);
753 >                glColor3ub(4, 250, 250);
754          else
755                  glColor3ub(0, 0, 0);
756          glBegin(GL_LINES);              /* draw each grid line */
# Line 687 | Line 761 | int    fore;
761   }
762  
763  
764 < static
765 < moveview(dx, dy, mov, orb)      /* move our view */
766 < int     dx, dy, mov, orb;
764 > static int
765 > moveview(       /* move our view */
766 >        int     dx,
767 >        int     dy,
768 >        int     mov,
769 >        int     orb
770 > )
771   {
772          VIEW    nv;
773          FVECT   odir, v1, wip;
774 <        double  d, d1;
775 <        register int    li;
774 >        double  d;
775 > #ifdef DOBJ
776 >        double d1;
777 > #endif
778                                  /* start with old view */
779 <        copystruct(&nv, &odev.v);
779 >        nv = odev.v;
780                                  /* orient our motion */
781          if (viewray(v1, odir, &odev.v,
782                          (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
# Line 719 | Line 799 | int    dx, dy, mov, orb;
799                  VSUM(nv.vp, wip, odir, -1.);
800                  spinvector(nv.vdir, nv.vdir, nv.vup, d);
801          } else if (orb) {               /* orbit up/down */
802 <                fcross(v1, odir, nv.vup);
803 <                if (normalize(v1) == 0.)
802 >                if (geodesic(odir, odir, nv.vup,
803 >                                d=MOVDEG*PI/180.*orb, GEOD_RAD) == 0.0)
804                          return(0);
725                spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
805                  VSUM(nv.vp, wip, odir, -1.);
806 <                spinvector(nv.vdir, nv.vdir, v1, d);
806 >                geodesic(nv.vdir, nv.vdir, nv.vup, d, GEOD_RAD);
807          } else if (mov) {               /* move forward/backward */
808                  d = MOVPCT/100. * mov;
809                  VSUM(nv.vp, nv.vp, odir, d);
810          }
811 <        if (!mov ^ !orb && headlocked) {        /* restore head height */
811 >        if (!mov ^ !orb && viewflags&VWHEADLOCK) {      /* restore height */
812                  VSUM(v1, odev.v.vp, nv.vp, -1.);
813                  d = DOT(v1, nv.vup);
814                  VSUM(nv.vp, nv.vp, odev.v.vup, d);
# Line 742 | Line 821 | int    dx, dy, mov, orb;
821   }
822  
823  
824 < static
825 < getframe(ebut)                          /* get focus frame */
826 < XButtonPressedEvent     *ebut;
824 > static void
825 > getframe(                               /* get focus frame */
826 >        XButtonPressedEvent     *ebut
827 > )
828   {
829          int     startx = ebut->x, starty = ebut->y;
830 <        int     endx, endy;
831 <
830 >        int     endx, endy, midx, midy;
831 >        FVECT   odir, v1;
832 >        double  d;
833 > #ifdef DOBJ
834 >        double d1;
835 > #endif
836 >                                                /* get mouse drag */
837          XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent));
838          endx = levptr(XButtonReleasedEvent)->x;
839          endy = levptr(XButtonReleasedEvent)->y;
840 <        if (endx == startx | endy == starty) {
841 <                XBell(ourdisplay, 0);
840 >        midx = (startx + endx) >> 1;
841 >        midy = (starty + endy) >> 1;
842 >                                                /* set focus distance */
843 >        if (viewray(v1, odir, &odev.v,
844 >                        (midx+.5)/odev.hres, (midy+.5)/odev.vres) < -FTINY)
845                  return;
846 <        }
847 <        if (endx < startx) {register int c = endx; endx = startx; startx = c;}
848 <        if (endy < starty) {register int c = endy; endy = starty; starty = c;}
846 >        d = getdistance(midx, midy, odir);      /* distance from front plane */
847 > #ifdef DOBJ
848 >        d1 = dobj_trace(NULL, v1, odir);
849 >        if (d1 < d)
850 >                d = d1;
851 > #endif
852 >        if (d < .99*FHUGE)
853 >                odev.v.vdist = d + sqrt(dist2(v1, odev.v.vp));
854 >                                                /* set frame for rendering */
855 >        if ((endx == startx) | (endy == starty))
856 >                return;
857 >        if (endx < startx) {int c = endx; endx = startx; startx = c;}
858 >        if (endy < starty) {int c = endy; endy = starty; starty = c;}
859          sprintf(odev_args, "%.3f %.3f %.3f %.3f",
860                          (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres,
861                          (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres);
# Line 765 | Line 863 | XButtonPressedEvent    *ebut;
863   }
864  
865  
866 < static
867 < getmove(ebut)                           /* get view change */
770 < XButtonPressedEvent     *ebut;
866 > static void
867 > waitabit(void)                          /* pause a moment */
868   {
869 +        struct timespec ts;
870 +        ts.tv_sec = 0;
871 +        ts.tv_nsec = 100000000L;
872 +        nanosleep(&ts, NULL);
873 + }
874 +
875 +
876 + static void
877 + getmove(                                /* get view change */
878 +        XButtonPressedEvent     *ebut
879 + )
880 + {
881          int     movdir = MOVDIR(ebut->button);
882          int     movorb = MOVORB(ebut->state);
883          int     ndrawn;
# Line 778 | Line 887 | XButtonPressedEvent    *ebut;
887  
888          XNoOp(ourdisplay);              /* makes sure we're not idle */
889  
890 <        viewsteady = 0;                 /* flag moving view */
891 <        setglpersp(&odev.v);            /* start us off in perspective */
890 >        viewflags &= ~VWSTEADY;         /* flag moving view */
891 >        setglpersp();                   /* start us off in perspective */
892          while (!XCheckMaskEvent(ourdisplay,
893                          ButtonReleaseMask, levptr(XEvent))) {
894 +                                        /* pause so as not to move too fast */
895 +                waitabit();
896                                          /* get cursor position */
897                  if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
898                                  &rootx, &rooty, &wx, &wy, &statemask))
# Line 795 | Line 906 | XButtonPressedEvent    *ebut;
906   #ifdef STEREO
907                  pushright();
908                  draw_grids(1);
909 <                gmDrawGeom(0);
909 >                ndrawn = gmDrawGeom();
910   #ifdef DOBJ
911 <                dobj_render();
911 >                ndrawn += dobj_render();
912   #endif
913                  popright();
914   #endif
915                                          /* redraw octrees */
916 <                ndrawn = gmDrawGeom(1);
916 >                ndrawn = gmDrawGeom();
917   #ifdef DOBJ
918                  ndrawn += dobj_render();        /* redraw objects */
919   #endif
920 <                glFlush();
921 <                if (!ndrawn) {
920 >                glXSwapBuffers(ourdisplay, gwind);
921 >                if (!ndrawn)
922                          sleep(1);       /* for reasonable interaction */
812 #ifdef STEREO
813                        pushright();
814                        draw_grids(0);
815                        popright();
816 #endif
817                        draw_grids(0);
818                }
923          }
924          if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
925                  movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
# Line 823 | Line 927 | XButtonPressedEvent    *ebut;
927                  wy = levptr(XButtonReleasedEvent)->y;
928                  moveview(wx, odev.vres-1-wy, movdir, movorb);
929          }
930 +        viewflags |= VWSTEADY;          /* done goofing around */
931   }
932  
933  
934 < static
935 < setglpersp(vp)                  /* set perspective view in GL */
831 < register VIEW   *vp;
934 > static void
935 > setglpersp(void)                        /* set perspective view in GL */
936   {
937          double  d, xmin, xmax, ymin, ymax;
938          GLfloat vec[4];
# Line 840 | Line 944 | register VIEW  *vp;
944                  dev_zmax = 100.;
945          } else {
946                  dev_zmin = 0.5*depthlim[0];
947 <                dev_zmax = 1.75*depthlim[1];
947 >                dev_zmax = 1.25*depthlim[1];
948                  if (dev_zmin > dev_zmax/5.)
949                          dev_zmin = dev_zmax/5.;
950          }
# Line 848 | Line 952 | register VIEW  *vp;
952                  dev_zmin = odev.v.vfore;
953          if (odev.v.vaft > FTINY)
954                  dev_zmax = odev.v.vaft;
955 <        if (dev_zmin < dev_zmax/100.)
956 <                dev_zmin = dev_zmax/100.;
955 >        if (dev_zmin*500. < dev_zmax)
956 >                dev_zmin = dev_zmax/500.;
957          setzrat();
958          xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz);
959          xmin = -xmax;
# Line 869 | Line 973 | register VIEW  *vp;
973                  odev.v.vp[2] + odev.v.vdir[2],
974                  odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]);
975          checkglerr("setting perspective view");
872        isperspective = 1;
976          vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.;
977          glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec);
978          vec[0] = -odev.v.vdir[0];
# Line 885 | Line 988 | register VIEW  *vp;
988          glEnable(GL_LIGHT0);
989          glEnable(GL_LIGHTING);          /* light our GL objects */
990          glShadeModel(GL_SMOOTH);
991 +        viewflags &= ~VWORTHO;
992 +        viewflags |= VWPERSP;
993   }
994  
995  
996 < static
997 < setglortho()                    /* set up orthographic view for cone drawing */
996 > static void
997 > setglortho(void)                        /* set up orthographic view for cone drawing */
998   {
999 +        glDrawBuffer(GL_FRONT);         /* use single-buffer mode */
1000                                          /* set view matrix */
1001          glMatrixMode(GL_PROJECTION);
1002          glLoadIdentity();
1003          glOrtho(0., (double)odev.hres, 0., (double)odev.vres,
1004                          0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH));
1005          checkglerr("setting orthographic view");
900        isperspective = 0;
1006          glDisable(GL_LIGHTING);         /* cones are constant color */
1007          glShadeModel(GL_FLAT);
1008 +        viewflags &= ~VWPERSP;
1009 +        viewflags |= VWORTHO;
1010   }
1011  
1012  
1013 < static
1014 < wipeclean()                     /* prepare for redraw */
1013 > static void
1014 > wipeclean(void)                 /* prepare for redraw */
1015   {
1016 <                                        /* clear depth buffer */
1016 >        glDrawBuffer(GL_BACK);          /* use double-buffer mode */
1017 >        glReadBuffer(GL_BACK);
1018 >                                        /* clear buffers */
1019   #ifdef STEREO
1020          setstereobuf(STEREO_BUFFER_RIGHT);
1021 <        glClear(GL_DEPTH_BUFFER_BIT);
1021 >        glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1022          setstereobuf(STEREO_BUFFER_LEFT);
1023   #endif
1024 <        glClear(GL_DEPTH_BUFFER_BIT);
916 <        if (viewsteady)                 /* clear samples if steady */
917 <                odClean();
1024 >        glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1025          freedepth();
1026 <        setglpersp(&odev.v);            /* reset view & clipping planes */
1026 >        if ((viewflags&(VWCHANGE|VWSTEADY)) ==
1027 >                        (VWCHANGE|VWSTEADY)) {  /* clear samples if new */
1028 >                odClean();
1029 >                viewflags &= ~VWCHANGE;         /* change noted */
1030 >        } else if (viewflags & VWSTEADY)
1031 >                odRedrawAll();
1032 >        setglpersp();                   /* reset view & clipping planes */
1033   }
1034  
1035  
1036 < static
1037 < getkey(ekey)                            /* get input key */
1038 < register XKeyPressedEvent  *ekey;
1036 > static void
1037 > getkey(                         /* get input key */
1038 >        XKeyPressedEvent  *ekey
1039 > )
1040   {
1041          Window  rootw, childw;
1042          int     rootx, rooty, wx, wy;
# Line 935 | Line 1049 | register XKeyPressedEvent  *ekey;
1049                  return;
1050          switch (buf[0]) {
1051          case 'h':                       /* turn on height motion lock */
1052 <                headlocked = 1;
1052 >                viewflags |= VWHEADLOCK;
1053                  return;
1054          case 'H':                       /* turn off height motion lock */
1055 <                headlocked = 0;
1055 >                viewflags &= ~VWHEADLOCK;
1056                  return;
1057          case 'l':                       /* retrieve last view */
1058                  inpresflags |= DFL(DC_LASTVIEW);
# Line 966 | Line 1080 | register XKeyPressedEvent  *ekey;
1080                  inpresflags |= DFL(DC_RESUME);
1081                  return;
1082          case CTRL('R'):                 /* redraw screen */
1083 <                odRemap(0);
1084 <                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1083 >                odRemap(0);                     /* new tone mapping */
1084 >                glClear(GL_DEPTH_BUFFER_BIT);
1085   #ifdef STEREO
1086                  setstereobuf(STEREO_BUFFER_RIGHT);
1087 <                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1087 >                glClear(GL_DEPTH_BUFFER_BIT);
1088                  setstereobuf(STEREO_BUFFER_LEFT);
1089   #endif
1090                  return;
1091          case CTRL('L'):                 /* refresh from server */
1092                  if (inpresflags & DFL(DC_REDRAW))
1093 <                        return;
1093 >                        return;                 /* already called */
1094                  XRaiseWindow(ourdisplay, gwind);
1095 <                XFlush(ourdisplay);
1096 <                sleep(1);
1097 <                wipeclean();                    /* fresh display */
1098 <                odRemap(1);                     /* fresh tone mapping */
985 <                dev_flush();                    /* draw octrees */
1095 >                XFlush(ourdisplay);             /* raise up window */
1096 >                sleep(1);                       /* wait for restacking */
1097 >                dev_clear();                    /* clear buffer and samples */
1098 >                odRemap(1);                     /* start fresh histogram */
1099                  inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
987                rayqleft = 0;                   /* hold off update */
1100                  return;
1101          case 'K':                       /* kill rtrace process(es) */
1102                  inpresflags |= DFL(DC_KILL);
# Line 1005 | Line 1117 | register XKeyPressedEvent  *ekey;
1117   }
1118  
1119  
1120 < static
1121 < fixwindow(eexp)                         /* repair damage to window */
1122 < register XExposeEvent  *eexp;
1120 > static void
1121 > fixwindow(                              /* repair damage to window */
1122 >        XExposeEvent  *eexp
1123 > )
1124   {
1125          int     xmin, ymin, xmax, ymax;
1126  
1127 <        if (odev.hres == 0 | odev.vres == 0) {  /* first exposure */
1127 >        if ((odev.hres == 0) | (odev.vres == 0)) {      /* first exposure */
1128                  resizewindow((XConfigureEvent *)eexp);
1129                  return;
1130          }
1131          xmin = eexp->x; xmax = eexp->x + eexp->width;
1132          ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y;
1133 +
1134 +        if (xmin <= 0 && xmax >= odev.hres-1 &&
1135 +                        ymin <= 0 && ymax >= odev.vres) {
1136 +                DCHECK(eexp->count, WARNING, "multiple clear in fixwindow");
1137 +                wipeclean();                    /* make sure we're go */
1138 +                return;
1139 +        }
1140                                                  /* clear portion of depth */
1141          glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1142          glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
# Line 1043 | Line 1163 | register XExposeEvent  *eexp;
1163   }
1164  
1165  
1166 < static
1167 < resizewindow(ersz)                      /* resize window */
1168 < register XConfigureEvent  *ersz;
1166 > static void
1167 > resizewindow(                   /* resize window */
1168 >        XConfigureEvent  *ersz
1169 > )
1170   {
1171          glViewport(0, 0, ersz->width, ersz->height);
1172  
# Line 1059 | Line 1180 | register XConfigureEvent  *ersz;
1180          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
1181  
1182          inpresflags |= DFL(DC_SETVIEW);
1183 +        viewflags |= VWCHANGE;
1184   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines