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

Comparing ray/src/util/glrad.c (file contents):
Revision 3.1 by gwlarson, Tue Jun 9 11:43:18 1998 UTC vs.
Revision 3.15 by schorsch, Thu Jun 26 00:58:11 2003 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   * Program to display Radiance scene using OpenGL.
6   */
7  
11 #include "radogl.h"
12 #include "view.h"
13 #include "paths.h"
8   #include <sys/types.h>
9   #include <GL/glx.h>
10 + #ifndef NOSTEREO
11 + #include <X11/extensions/SGIStereo.h>
12 + #endif
13   #include <ctype.h>
14 + #include <time.h>
15 +
16 + #include "radogl.h"
17 + #include "view.h"
18 + #include "paths.h"
19   #include "glradicon.h"
20 + #include "rtprocess.h"
21  
22   #ifndef MAXVIEW
23   #define MAXVIEW         63              /* maximum number of standard views */
# Line 30 | Line 33 | static char SCCSid[] = "$SunId$ SGI";
33   #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */
34   #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
35  
33 #define MINWIDTH        480             /* minimum graphics window width */
34 #define MINHEIGHT       400             /* minimum graphics window height */
35
36   #define BORWIDTH        5               /* border width */
37  
38   #define  ourscreen      DefaultScreen(ourdisplay)
# Line 65 | Line 65 | struct {
65  
66   int     currentview = 0;                /* current view number */
67   VIEW    thisview = STDVIEW;             /* displayed view */
68 + double  eyedist = 1;                    /* interocular distance */
69   VIEW    lastview;                       /* last recorded view */
69 char    *lastvc = NULL;                 /* cause of last view change */
70  
71   char    *progname;                      /* global argv[0] */
72   char    *radfile;                       /* rad input file */
# Line 74 | Line 74 | char   *scene[MAXSCENE+1];             /* material and scene file l
74   int     nscenef = 0;                    /* number of scene files */
75   char    *octree;                        /* octree name (NULL if unnec.) */
76  
77 < int     rtpd[3];                        /* rtrace process descriptors */
77 > SUBPROC rtpd;                   /* rtrace process descriptors */
78  
79 + int     silent = 0;                     /* run rad silently? */
80   int     backvis = 1;                    /* back faces visible? */
81 + int     stereo = 0;                     /* do stereo? */
82  
83 + #ifdef NOSTEREO
84 + #define setstereobuf(bid)       0
85 + #else
86 + #define setstereobuf(bid)       (glXWaitGL(), \
87 +                                XSGISetStereoBuffer(ourdisplay, gwind, bid), \
88 +                                glXWaitX())
89 + #endif
90 +
91   int     displist;                       /* our scene display list */
92  
93 < extern char     *fgets(), *fgetline(), *atos(), *scan4var();
93 > int     no_render = 0;                  /* don't rerender */
94 >
95 > extern char     *fgetline(), *atos(), *scan4var();
96   extern int      nowarn;                 /* turn warnings off? */
85 extern time_t   time();
97  
98  
99   main(argc, argv)
# Line 102 | Line 113 | char   *argv[];
113                  case 'w':
114                          nowarn = !nowarn;
115                          break;
116 +                case 's':
117 +                        silent = !silent;
118 +                        break;
119 +                case 'S':
120 +                        stereo = !stereo;
121 +                        break;
122                  case 'c':
123                          vwintvl = atoi(argv[++i]);
124                          break;
# Line 113 | Line 130 | char   *argv[];
130                  }
131          if (i >= argc)
132                  goto userr;
133 + #ifdef NOSTEREO
134 +        if (stereo)
135 +                error(INTERNAL, "stereo not supported in this version");
136 + #endif
137                                          /* run rad and get views */
138          runrad(argc-i, argv+i);
139                                          /* check view */
140 <        if (viewsel != NULL && (currentview = findvw(viewsel)) < 0) {
141 <                fprintf(stderr, "%s: no such view\n", viewsel);
142 <                quit(1);
143 <        }
140 >        if (viewsel != NULL)
141 >                if (viewsel[0] == '-') {
142 >                        char    *ve = viewsel;
143 >                        if (!sscanview(&thisview, viewsel) ||
144 >                                        (ve = setview(&thisview)) != NULL) {
145 >                                fprintf(stderr, "%s: bad view: %s\n",
146 >                                                progname, ve);
147 >                                quit(1);
148 >                        }
149 >                        currentview = -1;
150 >                } else if ((currentview = findvw(viewsel)) < 0) {
151 >                        fprintf(stderr, "%s: no such view: %s\n",
152 >                                                progname, viewsel);
153 >                        quit(1);
154 >                }
155                                          /* open GL */
156          dev_open(radfile = argv[i]);
157                                          /* load octree or scene files */
158          if (octree != NULL) {
159 <                displist = rgl_octlist(octree, NULL, NULL);
159 >                displist = rgl_octlist(octree, NULL, NULL, NULL);
160                  startrtrace(octree);
161          } else
162 <                displist = rgl_filelist(nscenef, scene);
162 >                displist = rgl_filelist(nscenef, scene, NULL);
163                                          /* set initial view */
164 <        dev_view(vwl[currentview].v);
164 >        dev_view(currentview < 0 ? &thisview : vwl[currentview].v);
165                                          /* input/render loop */
166          while (dev_input(vwintvl))
167                  ;
168                                          /* all done */
169          quit(0);
170   userr:
171 <        fprintf(stderr, "Usage: %s [-w][-c #secs][-v view] rfile [VAR=value]..\n",
171 >        fprintf(stderr,
172 >                "Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n",
173                          argv[0]);
174          quit(1);
175   }
176  
177  
178 + void
179   quit(code)                              /* exit gracefully */
180   int     code;
181   {
182          if (ourdisplay != NULL)
183                  dev_close();
184 <        if (rtpd[2] > 0) {
185 <                if (close_process(rtpd) > 0)
184 >        /* if (rtpd.pid > 0) { */
185 >        if (rtpd.running) {
186 >                if (close_process(&rtpd) > 0)
187                          wputs("bad exit status from rtrace\n");
188 <                rtpd[2] = 0;
188 >                /* rtpd.pid = 0; */
189          }
190          exit(code);
191   }
# Line 166 | Line 201 | char   *octname;
201          if (nowarn) av[ac++] = "-w-";
202          av[ac++] = octname;
203          av[ac] = NULL;
204 <        if (open_process(rtpd, av) <= 0)
204 >        if (open_process(&rtpd, av) <= 0)
205                  error(SYSTEM, "cannot start rtrace process");
206   }
207  
# Line 184 | Line 219 | char   **av;
219                                          /* set rad commmand */
220          strcpy(radcomm, "rad -w -v 0        "); /* look out below! */
221          cp = radcomm + 19;
222 +        if (silent) {
223 +                strcpy(cp, "-s ");
224 +                cp += 3;
225 +        }
226          while (ac--) {
227                  strcpy(cp, *av++);
228                  while (*cp) cp++;
# Line 204 | Line 243 | char   **av;
243                  expval = atof(cp);
244                  if (*cp == '-' | *cp == '+')
245                          expval = pow(2., expval);
246 +                expval *= 0.5;          /* compensate for local shading */
247          }
248 +                                                /* look for eye separation */
249 +        if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL)
250 +                eyedist = atof(cp);
251                                                  /* look for materials */
252          while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) {
253                  nscenef += wordstring(scene+nscenef, cp);
# Line 245 | Line 288 | char   **av;
288                                                  /* open options file */
289          if ((fp = fopen(optfile, "r")) == NULL)
290                  error(SYSTEM, "cannot open options file");
291 <                                                /* get ambient value */
291 >                                                /* get relevant options */
292          while (fgets(buf, sizeof(buf), fp) != NULL)
293 <                if (!strncmp(buf, "-av ", 4)) {
293 >                if (!strncmp(buf, "-av ", 4))
294                          setcolor(ambval, atof(buf+4),
295                                          atof(sskip2(buf+4,1)),
296                                          atof(sskip2(buf+4,2)));
297 <                        break;
298 <                }
297 >                else if (backvis && !strncmp(buf, "-bv", 3) &&
298 >                                (!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL))
299 >                        backvis = 0;
300          fclose(fp);
301          unlink(optfile);                        /* delete options file */
302   }
# Line 325 | Line 369 | char  *id;
369                                  GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None};
370          XSetWindowAttributes    ourwinattr;
371          XWMHints        ourxwmhints;
328        XSizeHints      oursizhints;
372                                          /* open display server */
373          ourdisplay = XOpenDisplay(NULL);
374          if (ourdisplay == NULL)
# Line 351 | Line 394 | char  *id;
394          if (gwind == 0)
395                  error(SYSTEM, "cannot create window\n");
396          XStoreName(ourdisplay, gwind, id);
397 + #ifndef NOSTEREO
398 +        if (stereo)                     /* check if stereo working */
399 +                switch (XSGIQueryStereoMode(ourdisplay, gwind)) {
400 +                case STEREO_TOP:
401 +                case STEREO_BOTTOM:
402 +                        break;
403 +                case STEREO_OFF:
404 +                        error(USER,
405 +                "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
406 +                case X_STEREO_UNSUPPORTED:
407 +                        error(USER, "stereo not supported on this screen");
408 +                default:
409 +                        error(INTERNAL, "unknown stereo mode");
410 +                }
411 + #endif
412                                          /* set window manager hints */
413          ourxwmhints.flags = InputHint|IconPixmapHint;
414          ourxwmhints.input = True;
415          ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
416                  gwind, glradicon_bits, glradicon_width, glradicon_height);
417          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
360        oursizhints.min_width = MINWIDTH;
361        oursizhints.min_height = MINHEIGHT;
362        oursizhints.flags = PMinSize;
363        XSetNormalHints(ourdisplay, gwind, &oursizhints);
418                                          /* set GLX context */
419          glXMakeCurrent(ourdisplay, gwind, gctx);
420          glEnable(GL_DEPTH_TEST);
421          glDepthFunc(GL_LESS);
422          glShadeModel(GL_SMOOTH);
423          glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
424 <        glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
424 >        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
425          glEnable(GL_LIGHTING);
426 +        glFrontFace(GL_CCW);
427 +        glCullFace(GL_BACK);
428          if (backvis)
429                  glDisable(GL_CULL_FACE);
430 <        else {
375 <                glFrontFace(GL_CCW);
376 <                glCullFace(GL_BACK);
430 >        else
431                  glEnable(GL_CULL_FACE);
378        }
432          glDrawBuffer(GL_BACK);
433                                          /* figure out sensible view */
434          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
435                          DisplayWidth(ourdisplay, ourscreen);
436          pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
437                          DisplayHeight(ourdisplay, ourscreen);
438 +        if (stereo) {                   /* set stereo mode */
439 +                setstereobuf(STEREO_BUFFER_LEFT);
440 +                pheight *= 2.;
441 +        }
442                                          /* map the window */
443          XMapWindow(ourdisplay, gwind);
444 +        no_render++;
445 +        do
446 +                dev_input(0);           /* get resize event */
447 +        while (hres == 0 & vres == 0);
448 +        no_render--;
449          rgl_checkerr("initializing GLX");
450   }
451  
# Line 419 | Line 481 | register VIEW  *nv;
481          }
482          if (hres != 0 & vres != 0) {
483                  wa = (vres*pheight)/(hres*pwidth);
484 <                va = viewaspect(&thisview);
484 >                va = viewaspect(nv);
485                  if (va > wa+.05) {
486                          newvres = (pwidth/pheight)*va*newhres + .5;
487                          if (newvres > maxvres) {
# Line 434 | Line 496 | register VIEW  *nv;
496                          }
497                  }
498                  if (newhres != hres | newvres != vres) {
499 +                        no_render++;
500                          XResizeWindow(ourdisplay, gwind, newhres, newvres);
501                          do
502                                  dev_input(0);           /* get resize event */
503                          while (newhres != hres | newvres != vres);
504 +                        no_render--;
505                  }
506          }
507          copystruct(&thisview, nv);
# Line 489 | Line 553 | int    nsecs;
553  
554   render()                        /* render our display list and swap buffers */
555   {
556 <        if (!mapped)
556 >        double  d;
557 >
558 >        if (!mapped | no_render)
559                  return;
560          glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
561          glCallList(displist);
562 +        if (stereo) {                           /* do right eye for stereo */
563 +                setstereobuf(STEREO_BUFFER_RIGHT);
564 +                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
565 +                glMatrixMode(GL_MODELVIEW);
566 +                glPushMatrix();
567 +                d = -eyedist / sqrt(thisview.hn2);
568 +                glTranslated(d*thisview.hvec[0], d*thisview.hvec[1],
569 +                                d*thisview.hvec[2]);
570 +                glCallList(displist);
571 +                glMatrixMode(GL_MODELVIEW);
572 +                glPopMatrix();
573 +                setstereobuf(STEREO_BUFFER_LEFT);
574 +        }
575          glXSwapBuffers(ourdisplay, gwind);      /* calls glFlush() */
576          rgl_checkerr("rendering display list");
577   }
# Line 544 | Line 623 | int    dx, dy, mov, orb;
623   }
624  
625  
626 + static
627 + waitabit()                              /* pause a moment */
628 + {
629 +        struct timespec ts;
630 +        ts.tv_sec = 0;
631 +        ts.tv_nsec = 5000000;
632 +        nanosleep(&ts, NULL);
633 + }
634 +
635 +
636   getmove(ebut)                           /* get view change */
637   XButtonPressedEvent     *ebut;
638   {
# Line 554 | Line 643 | XButtonPressedEvent    *ebut;
643          int     rootx, rooty, wx, wy;
644          unsigned int    statemask;
645  
646 <        copylastv("moving");
646 >        copylastv( movorb ? (movdir ? "left/right" : "up/down") :
647 >                        (movdir ? "fore/back" : "rotate") );
648          XNoOp(ourdisplay);
649  
650          while (!XCheckMaskEvent(ourdisplay,
651                          ButtonReleaseMask, levptr(XEvent))) {
652 +                                        /* pause so as not to move too fast */
653 +                waitabit();
654  
655                  if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
656                                  &rootx, &rooty, &wx, &wy, &statemask))
# Line 586 | Line 678 | double md;
678   {
679          float   fbuf[6];
680                                  /* check to see if rtrace is running */
681 <        if (rtpd[2] <= 0)
681 >        /* if (rtpd.pid <= 0) */
682 >        if (!rtpd.running)
683                  return(0);
684                                  /* assign origin */
685          fbuf[0] = org[0]; fbuf[1] = org[1]; fbuf[2] = org[2];
# Line 594 | Line 687 | double md;
687          if (md <= FTINY) md = FHUGE;
688          fbuf[3] = dir[0]*md; fbuf[4] = dir[1]*md; fbuf[5] = dir[2]*md;
689                                  /* trace that ray */
690 <        if (process(rtpd, fbuf, fbuf, 4*sizeof(float), 6*sizeof(float)) !=
691 <                        4*sizeof(float))
690 >        if (process(&rtpd, (char *)fbuf, (char *)fbuf,
691 >                        4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float))
692                  error(INTERNAL, "error getting data back from rtrace process");
693          if (fbuf[3] >= .99*FHUGE)
694                  return(0);      /* missed local objects */
# Line 609 | Line 702 | register VIEW  *vp;
702   {
703          double  d, xmin, xmax, ymin, ymax, zmin, zmax;
704  
705 <        zmin = 0.05;
706 <        zmax = 5000.;
705 >        zmin = 0.1;
706 >        zmax = 1000.;
707          if (thisview.vfore > FTINY)
708                  zmin = thisview.vfore;
709          if (thisview.vaft > FTINY)
# Line 654 | Line 747 | register XKeyPressedEvent  *ekey;
747                  headlocked = 0;
748                  break;
749          case 'l':                       /* retrieve last (premouse) view */
750 <                if (lastvc != NULL) {
750 >                if (lastview.type) {
751                          VIEW    vtmp;
752                          copystruct(&vtmp, &thisview);
753                          dev_view(&lastview);
# Line 726 | Line 819 | int    vwnum;
819                          ;
820          else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL)
821                  vwnum = 0;
822 <        if (vwnum == currentview)
730 <                return;
822 >        copylastv("standard view");
823          dev_view(vwl[currentview=vwnum].v);
824   }
825  
# Line 768 | Line 860 | VIEW   *vp;
860   copylastv(cause)                        /* copy last view position */
861   char    *cause;
862   {
863 +        static char     *lastvc;
864 +
865          if (cause == lastvc)
866                  return;                 /* only record one view per cause */
867          lastvc = cause;
# Line 798 | Line 892 | register XConfigureEvent  *ersz;
892          glViewport(0, 0, hres=ersz->width, vres=ersz->height);
893          if (hres > maxhres) maxhres = hres;
894          if (vres > maxvres) maxvres = vres;
895 +        if (no_render)
896 +                return;
897          wa = (vres*pheight)/(hres*pwidth);
898          va = viewaspect(&thisview);
899          if (va > wa+.05) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines