ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.17
Committed: Mon Jul 21 22:30:19 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.16: +10 -10 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 schorsch 3.17 static const char RCSid[] = "$Id: glrad.c,v 3.16 2003/06/30 14:59:13 schorsch Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Program to display Radiance scene using OpenGL.
6     */
7    
8     #include <sys/types.h>
9     #include <GL/glx.h>
10 gwlarson 3.4 #ifndef NOSTEREO
11     #include <X11/extensions/SGIStereo.h>
12     #endif
13 gwlarson 3.1 #include <ctype.h>
14 schorsch 3.16 #include <string.h>
15 greg 3.13 #include <time.h>
16 schorsch 3.15
17 gwlarson 3.11 #include "radogl.h"
18     #include "view.h"
19     #include "paths.h"
20 gwlarson 3.1 #include "glradicon.h"
21 schorsch 3.15 #include "rtprocess.h"
22 gwlarson 3.1
23     #ifndef MAXVIEW
24     #define MAXVIEW 63 /* maximum number of standard views */
25     #endif
26     #ifndef MAXSCENE
27     #define MAXSCENE 127 /* maximum number of scene files */
28     #endif
29    
30     #define ZOOMPCT 9 /* percent to zoom at a time */
31    
32     #define MOVPCT 7 /* percent distance to move /frame */
33     #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
34     #define MOVDEG (-5) /* degrees to orbit CW/down /frame */
35     #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
36    
37     #define BORWIDTH 5 /* border width */
38    
39     #define ourscreen DefaultScreen(ourdisplay)
40     #define ourroot RootWindow(ourdisplay,ourscreen)
41     #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
42     ButtonPressMask|ButtonReleaseMask)
43    
44     #define levptr(etype) ((etype *)&currentevent)
45    
46     XEvent currentevent; /* current event */
47    
48     int mapped = 0; /* window is mapped? */
49     unsigned long ourblack=0, ourwhite=~0;
50    
51     Display *ourdisplay = NULL; /* our display */
52     XVisualInfo *ourvinf; /* our visual information */
53     Window gwind = 0; /* our graphics window */
54     int hres, vres; /* rendering window dimensions */
55     int maxhres, maxvres; /* maximum given dimensions */
56     GLXContext gctx; /* our GLX context */
57    
58     double pwidth, pheight; /* pixel dimensions (mm) */
59    
60     int headlocked = 0; /* lock vertical motion */
61    
62     struct {
63     char *nam; /* view name (NULL if none) */
64     VIEW *v; /* parameters (NULL term.) */
65     } vwl[MAXVIEW+1]; /* our list of views */
66    
67     int currentview = 0; /* current view number */
68     VIEW thisview = STDVIEW; /* displayed view */
69 gwlarson 3.4 double eyedist = 1; /* interocular distance */
70 gwlarson 3.1 VIEW lastview; /* last recorded view */
71    
72     char *progname; /* global argv[0] */
73     char *radfile; /* rad input file */
74     char *scene[MAXSCENE+1]; /* material and scene file list */
75     int nscenef = 0; /* number of scene files */
76     char *octree; /* octree name (NULL if unnec.) */
77    
78 schorsch 3.15 SUBPROC rtpd; /* rtrace process descriptors */
79 gwlarson 3.1
80 gwlarson 3.4 int silent = 0; /* run rad silently? */
81 gwlarson 3.1 int backvis = 1; /* back faces visible? */
82 gwlarson 3.4 int stereo = 0; /* do stereo? */
83 gwlarson 3.1
84 gwlarson 3.4 #ifdef NOSTEREO
85     #define setstereobuf(bid) 0
86     #else
87     #define setstereobuf(bid) (glXWaitGL(), \
88     XSGISetStereoBuffer(ourdisplay, gwind, bid), \
89     glXWaitX())
90     #endif
91    
92 gwlarson 3.1 int displist; /* our scene display list */
93    
94 gwlarson 3.5 int no_render = 0; /* don't rerender */
95 gwlarson 3.2
96 greg 3.14 extern char *fgetline(), *atos(), *scan4var();
97 gwlarson 3.1 extern int nowarn; /* turn warnings off? */
98    
99    
100     main(argc, argv)
101     int argc;
102     char *argv[];
103     {
104     char *viewsel = NULL;
105     long vwintvl = 0;
106     int i;
107    
108     progname = argv[0];
109     for (i = 1; i < argc && argv[i][0] == '-'; i++)
110     switch (argv[i][1]) {
111     case 'v':
112     viewsel = argv[++i];
113     break;
114     case 'w':
115     nowarn = !nowarn;
116     break;
117 gwlarson 3.4 case 's':
118     silent = !silent;
119     break;
120     case 'S':
121     stereo = !stereo;
122     break;
123 gwlarson 3.1 case 'c':
124     vwintvl = atoi(argv[++i]);
125     break;
126     case 'b':
127     backvis = !backvis;
128     break;
129     default:
130     goto userr;
131     }
132     if (i >= argc)
133     goto userr;
134 gwlarson 3.4 #ifdef NOSTEREO
135     if (stereo)
136     error(INTERNAL, "stereo not supported in this version");
137     #endif
138 gwlarson 3.1 /* run rad and get views */
139     runrad(argc-i, argv+i);
140     /* check view */
141 schorsch 3.17 if (viewsel != NULL) {
142 gwlarson 3.3 if (viewsel[0] == '-') {
143     char *ve = viewsel;
144     if (!sscanview(&thisview, viewsel) ||
145     (ve = setview(&thisview)) != NULL) {
146     fprintf(stderr, "%s: bad view: %s\n",
147     progname, ve);
148     quit(1);
149     }
150     currentview = -1;
151     } else if ((currentview = findvw(viewsel)) < 0) {
152     fprintf(stderr, "%s: no such view: %s\n",
153     progname, viewsel);
154     quit(1);
155     }
156 schorsch 3.17 }
157 gwlarson 3.1 /* open GL */
158     dev_open(radfile = argv[i]);
159     /* load octree or scene files */
160     if (octree != NULL) {
161 gwlarson 3.12 displist = rgl_octlist(octree, NULL, NULL, NULL);
162 gwlarson 3.1 startrtrace(octree);
163     } else
164 gwlarson 3.12 displist = rgl_filelist(nscenef, scene, NULL);
165 gwlarson 3.1 /* set initial view */
166 gwlarson 3.3 dev_view(currentview < 0 ? &thisview : vwl[currentview].v);
167 gwlarson 3.1 /* input/render loop */
168     while (dev_input(vwintvl))
169     ;
170     /* all done */
171     quit(0);
172     userr:
173 gwlarson 3.8 fprintf(stderr,
174     "Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n",
175 gwlarson 3.1 argv[0]);
176     quit(1);
177     }
178    
179    
180 greg 3.13 void
181 gwlarson 3.1 quit(code) /* exit gracefully */
182     int code;
183     {
184     if (ourdisplay != NULL)
185     dev_close();
186 schorsch 3.15 /* if (rtpd.pid > 0) { */
187     if (rtpd.running) {
188     if (close_process(&rtpd) > 0)
189 gwlarson 3.1 wputs("bad exit status from rtrace\n");
190 schorsch 3.15 /* rtpd.pid = 0; */
191 gwlarson 3.1 }
192     exit(code);
193     }
194    
195    
196     startrtrace(octname) /* start rtrace on octname */
197     char *octname;
198     {
199     static char *av[12] = {"rtrace", "-h", "-fff", "-ld+",
200     "-opL", "-x", "1"};
201     int ac = 7;
202    
203     if (nowarn) av[ac++] = "-w-";
204     av[ac++] = octname;
205     av[ac] = NULL;
206 schorsch 3.15 if (open_process(&rtpd, av) <= 0)
207 gwlarson 3.1 error(SYSTEM, "cannot start rtrace process");
208     }
209    
210    
211     runrad(ac, av) /* run rad and load variables */
212     int ac;
213     char **av;
214     {
215     static char optfile[] = TEMPLATE;
216     int nvn = 0, nvv = 0;
217     FILE *fp;
218     int cval;
219     register char *cp;
220     char radcomm[256], buf[128], nam[32];
221     /* set rad commmand */
222     strcpy(radcomm, "rad -w -v 0 "); /* look out below! */
223     cp = radcomm + 19;
224 gwlarson 3.4 if (silent) {
225     strcpy(cp, "-s ");
226     cp += 3;
227     }
228 gwlarson 3.1 while (ac--) {
229     strcpy(cp, *av++);
230     while (*cp) cp++;
231     *cp++ = ' ';
232     }
233     strcpy(cp, "OPTFILE="); /* create temporary options file */
234     strcpy(cp+8, mktemp(optfile));
235     if (system(radcomm)) /* update octree */
236     error(USER, "error executing rad command");
237     /* replace "-v 0" with "-n -e -s -V" */
238     strcpy(radcomm+7, "-n -e -s -V");
239     radcomm[18] = ' ';
240     if ((fp = popen(radcomm, "r")) == NULL)
241     error(SYSTEM, "cannot start rad command");
242     buf[0] = '\0'; /* read variables alphabetically */
243     /* get exposure */
244     if ((cp = scan4var(buf, sizeof(buf), "EXPOSURE", fp)) != NULL) {
245     expval = atof(cp);
246     if (*cp == '-' | *cp == '+')
247     expval = pow(2., expval);
248 gwlarson 3.3 expval *= 0.5; /* compensate for local shading */
249 gwlarson 3.1 }
250 gwlarson 3.4 /* look for eye separation */
251     if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL)
252     eyedist = atof(cp);
253 gwlarson 3.1 /* look for materials */
254     while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) {
255     nscenef += wordstring(scene+nscenef, cp);
256     buf[0] = '\0';
257     }
258     /* look for octree */
259     if ((cp = scan4var(buf, sizeof(buf), "OCTREE", fp)) != NULL)
260     octree = savqstr(cp);
261     /* look for scene files */
262     while ((cp = scan4var(buf, sizeof(buf), "scene", fp)) != NULL) {
263     nscenef += wordstring(scene+nscenef, cp);
264     buf[0] = '\0';
265     }
266     /* load view names */
267     while ((cp = scan4var(buf, sizeof(buf), "view", fp)) != NULL) {
268     if (nvn >= MAXVIEW)
269     error(INTERNAL, "too many views in rad file");
270     vwl[nvn++].nam = *cp == '-' ? (char *)NULL :
271     savqstr(atos(nam, sizeof(nam), cp));
272     buf[0] = '\0';
273     }
274     /* load actual views */
275     do
276     if (isview(buf)) {
277     vwl[nvv].v = (VIEW *)bmalloc(sizeof(VIEW));
278 schorsch 3.17 *(vwl[nvv].v) = stdview;
279 gwlarson 3.1 sscanview(vwl[nvv].v, buf);
280     if ((cp = setview(vwl[nvv++].v)) != NULL) {
281     fprintf(stderr, "%s: bad view %d - %s\n",
282     progname, nvv, cp);
283     quit(1);
284     }
285     }
286     while (fgets(buf, sizeof(buf), fp) != NULL);
287     if (nvv != nvn)
288     error(INTERNAL, "view miscount in runrad");
289     pclose(fp);
290     /* open options file */
291     if ((fp = fopen(optfile, "r")) == NULL)
292     error(SYSTEM, "cannot open options file");
293 gwlarson 3.4 /* get relevant options */
294 gwlarson 3.1 while (fgets(buf, sizeof(buf), fp) != NULL)
295 gwlarson 3.4 if (!strncmp(buf, "-av ", 4))
296 gwlarson 3.1 setcolor(ambval, atof(buf+4),
297     atof(sskip2(buf+4,1)),
298     atof(sskip2(buf+4,2)));
299 gwlarson 3.4 else if (backvis && !strncmp(buf, "-bv", 3) &&
300 gwlarson 3.10 (!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL))
301 gwlarson 3.4 backvis = 0;
302 gwlarson 3.1 fclose(fp);
303     unlink(optfile); /* delete options file */
304     }
305    
306    
307     int
308     findvw(nm) /* find named view */
309     register char *nm;
310     {
311     register int n;
312    
313     if (*nm >= '1' & *nm <= '9' &&
314     (n = atoi(nm)-1) <= MAXVIEW && vwl[n].v != NULL)
315     return(n);
316     for (n = 0; vwl[n].v != NULL; n++)
317     if (vwl[n].nam != NULL && !strcmp(nm, vwl[n].nam))
318     return(n);
319     return(-1);
320     }
321    
322    
323     int
324     varmatch(s, vn) /* match line to variable */
325     register char *s, *vn;
326     {
327     register int c;
328    
329     for ( ; *vn && *s == *vn; s++, vn++)
330     ;
331     while (isspace(*s))
332     s++;
333     if (*s == '=')
334     return(*vn);
335     while (!(c = toupper(*s++) - toupper(*vn)) && *vn++)
336     ;
337     return(c);
338     }
339    
340    
341     char *
342     scan4var(buf, buflen, vname, fp) /* scan for variable from fp */
343     char *buf;
344     int buflen;
345     char *vname;
346     FILE *fp;
347     {
348     int cval;
349     register char *cp;
350     /* search out matching line */
351     while ((cval = varmatch(buf, vname))) {
352     if (cval > 0) /* gone too far? */
353     return(NULL);
354     buf[0] = '\0'; /* else get next line */
355     if (fgetline(buf, buflen, fp) == NULL)
356     return(NULL);
357     }
358     /* skip variable name and '=' */
359     for (cp = buf; *cp++ != '='; )
360     ;
361     while (isspace(*cp)) cp++;
362     return(cp);
363     }
364    
365    
366     dev_open(id) /* initialize GLX driver */
367     char *id;
368     {
369     static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,4,
370     GLX_GREEN_SIZE,4, GLX_BLUE_SIZE,4,
371     GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None};
372     XSetWindowAttributes ourwinattr;
373     XWMHints ourxwmhints;
374     /* open display server */
375     ourdisplay = XOpenDisplay(NULL);
376     if (ourdisplay == NULL)
377     error(USER, "cannot open X-windows; DISPLAY variable set?\n");
378     /* find a usable visual */
379     ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
380     if (ourvinf == NULL)
381     error(USER, "no suitable visuals available");
382     /* get a context */
383     gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
384     /* open window */
385     ourwinattr.background_pixel = ourblack;
386     ourwinattr.border_pixel = ourblack;
387     ourwinattr.event_mask = ourmask;
388     /* this is stupid */
389     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
390     ourvinf->visual, AllocNone);
391     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
392     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
393     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
394     BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
395     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
396     if (gwind == 0)
397     error(SYSTEM, "cannot create window\n");
398     XStoreName(ourdisplay, gwind, id);
399 gwlarson 3.4 #ifndef NOSTEREO
400     if (stereo) /* check if stereo working */
401     switch (XSGIQueryStereoMode(ourdisplay, gwind)) {
402     case STEREO_TOP:
403     case STEREO_BOTTOM:
404     break;
405     case STEREO_OFF:
406     error(USER,
407     "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
408     case X_STEREO_UNSUPPORTED:
409     error(USER, "stereo not supported on this screen");
410     default:
411     error(INTERNAL, "unknown stereo mode");
412     }
413     #endif
414 gwlarson 3.1 /* set window manager hints */
415     ourxwmhints.flags = InputHint|IconPixmapHint;
416     ourxwmhints.input = True;
417     ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
418     gwind, glradicon_bits, glradicon_width, glradicon_height);
419     XSetWMHints(ourdisplay, gwind, &ourxwmhints);
420     /* set GLX context */
421     glXMakeCurrent(ourdisplay, gwind, gctx);
422     glEnable(GL_DEPTH_TEST);
423     glDepthFunc(GL_LESS);
424     glShadeModel(GL_SMOOTH);
425     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
426 gwlarson 3.7 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
427 gwlarson 3.1 glEnable(GL_LIGHTING);
428 gwlarson 3.9 glFrontFace(GL_CCW);
429     glCullFace(GL_BACK);
430 gwlarson 3.1 if (backvis)
431     glDisable(GL_CULL_FACE);
432 gwlarson 3.9 else
433 gwlarson 3.1 glEnable(GL_CULL_FACE);
434     glDrawBuffer(GL_BACK);
435     /* figure out sensible view */
436     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
437     DisplayWidth(ourdisplay, ourscreen);
438     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
439     DisplayHeight(ourdisplay, ourscreen);
440 gwlarson 3.4 if (stereo) { /* set stereo mode */
441     setstereobuf(STEREO_BUFFER_LEFT);
442     pheight *= 2.;
443     }
444 gwlarson 3.1 /* map the window */
445     XMapWindow(ourdisplay, gwind);
446 gwlarson 3.5 no_render++;
447 gwlarson 3.3 do
448     dev_input(0); /* get resize event */
449     while (hres == 0 & vres == 0);
450 gwlarson 3.5 no_render--;
451 gwlarson 3.1 rgl_checkerr("initializing GLX");
452     }
453    
454    
455     dev_close() /* close our display and free resources */
456     {
457     glXMakeCurrent(ourdisplay, None, NULL);
458     glXDestroyContext(ourdisplay, gctx);
459     XDestroyWindow(ourdisplay, gwind);
460     gwind = 0;
461     XCloseDisplay(ourdisplay);
462     ourdisplay = NULL;
463     }
464    
465    
466     int
467     dev_view(nv) /* assign new driver view */
468     register VIEW *nv;
469     {
470     int newhres = hres, newvres = vres;
471     double wa, va;
472     /* check view legality */
473     if (nv->type != VT_PER) {
474     error(COMMAND, "illegal view type");
475     nv->type = VT_PER;
476     }
477     if (nv->horiz > 160. | nv->vert > 160.) {
478     error(COMMAND, "illegal view angle");
479     if (nv->horiz > 160.)
480     nv->horiz = 160.;
481     if (nv->vert > 160.)
482     nv->vert = 160.;
483     }
484     if (hres != 0 & vres != 0) {
485     wa = (vres*pheight)/(hres*pwidth);
486 gwlarson 3.2 va = viewaspect(nv);
487 gwlarson 3.1 if (va > wa+.05) {
488     newvres = (pwidth/pheight)*va*newhres + .5;
489     if (newvres > maxvres) {
490     newvres = maxvres;
491     newhres = (pheight/pwidth)/va*newvres + .5;
492     }
493     } else if (va < wa-.05) {
494     newhres = (pheight/pwidth)/va*newvres + .5;
495     if (newhres > maxhres) {
496     newhres = maxhres;
497     newvres = (pwidth/pheight)*va*newhres + .5;
498     }
499     }
500     if (newhres != hres | newvres != vres) {
501 gwlarson 3.5 no_render++;
502 gwlarson 3.1 XResizeWindow(ourdisplay, gwind, newhres, newvres);
503     do
504     dev_input(0); /* get resize event */
505     while (newhres != hres | newvres != vres);
506 gwlarson 3.5 no_render--;
507 gwlarson 3.1 }
508     }
509 schorsch 3.17 thisview = *nv;
510 gwlarson 3.1 setglpersp(&thisview);
511     render();
512     return(1);
513     }
514    
515    
516     int
517     dev_input(nsecs) /* get next input event */
518     int nsecs;
519     {
520     #if 0
521     static time_t lasttime = 0;
522     time_t thistime;
523    
524     if (nsecs > 0) {
525     thistime = time(0);
526     nsecs -= (long)(thistime - lasttime);
527     lasttime = thistime;
528     }
529     if (nsecs > 0)
530     alarm(nsecs);
531     #endif
532     XNextEvent(ourdisplay, levptr(XEvent));
533     switch (levptr(XEvent)->type) {
534     case ConfigureNotify:
535     resizewindow(levptr(XConfigureEvent));
536     break;
537     case UnmapNotify:
538     mapped = 0;
539     break;
540     case MapNotify:
541     mapped = 1;
542     break;
543     case Expose:
544     fixwindow(levptr(XExposeEvent));
545     break;
546     case KeyPress:
547     return(getkey(levptr(XKeyPressedEvent)));
548     case ButtonPress:
549     getmove(levptr(XButtonPressedEvent));
550     break;
551     }
552     return(1);
553     }
554    
555    
556     render() /* render our display list and swap buffers */
557     {
558 gwlarson 3.4 double d;
559    
560 gwlarson 3.5 if (!mapped | no_render)
561 gwlarson 3.1 return;
562     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
563     glCallList(displist);
564 gwlarson 3.4 if (stereo) { /* do right eye for stereo */
565     setstereobuf(STEREO_BUFFER_RIGHT);
566     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
567     glMatrixMode(GL_MODELVIEW);
568     glPushMatrix();
569     d = -eyedist / sqrt(thisview.hn2);
570     glTranslated(d*thisview.hvec[0], d*thisview.hvec[1],
571     d*thisview.hvec[2]);
572     glCallList(displist);
573     glMatrixMode(GL_MODELVIEW);
574     glPopMatrix();
575     setstereobuf(STEREO_BUFFER_LEFT);
576     }
577 gwlarson 3.1 glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */
578     rgl_checkerr("rendering display list");
579     }
580    
581    
582     moveview(dx, dy, mov, orb) /* move our view */
583     int dx, dy, mov, orb;
584     {
585     VIEW nv;
586     FVECT odir, v1, wp;
587     double d;
588     /* start with old view */
589 schorsch 3.17 nv = thisview;
590 gwlarson 3.1 /* change view direction */
591     if ((d = viewray(v1, odir, &thisview,
592     (dx+.5)/hres, (dy+.5)/vres)) < -FTINY)
593     return(0); /* outside view */
594     if (mov | orb) {
595     if (!getintersect(wp, v1, odir, d))
596     return(0);
597     VSUM(odir, wp, nv.vp, -1.);
598     } else
599     VCOPY(nv.vdir, odir);
600     if (orb && mov) { /* orbit left/right */
601     spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
602     VSUM(nv.vp, wp, odir, -1.);
603     spinvector(nv.vdir, nv.vdir, nv.vup, d);
604     } else if (orb) { /* orbit up/down */
605     fcross(v1, odir, nv.vup);
606     if (normalize(v1) == 0.)
607     return(0);
608     spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
609     VSUM(nv.vp, wp, odir, -1.);
610     spinvector(nv.vdir, nv.vdir, v1, d);
611     } else if (mov) { /* move forward/backward */
612     d = MOVPCT/100. * mov;
613     VSUM(nv.vp, nv.vp, odir, d);
614     }
615     if (!mov ^ !orb && headlocked) { /* restore head height */
616     VSUM(v1, thisview.vp, nv.vp, -1.);
617     d = DOT(v1, thisview.vup);
618     VSUM(nv.vp, nv.vp, thisview.vup, d);
619     }
620     if (setview(&nv) != NULL)
621     return(0); /* illegal view */
622     dev_view(&nv);
623     return(1);
624     }
625    
626    
627 greg 3.13 static
628     waitabit() /* pause a moment */
629     {
630     struct timespec ts;
631     ts.tv_sec = 0;
632     ts.tv_nsec = 5000000;
633     nanosleep(&ts, NULL);
634     }
635    
636    
637 gwlarson 3.1 getmove(ebut) /* get view change */
638     XButtonPressedEvent *ebut;
639     {
640     int movdir = MOVDIR(ebut->button);
641     int movorb = MOVORB(ebut->state);
642     int moved = 0;
643     Window rootw, childw;
644     int rootx, rooty, wx, wy;
645     unsigned int statemask;
646    
647 gwlarson 3.3 copylastv( movorb ? (movdir ? "left/right" : "up/down") :
648     (movdir ? "fore/back" : "rotate") );
649 gwlarson 3.1 XNoOp(ourdisplay);
650    
651     while (!XCheckMaskEvent(ourdisplay,
652     ButtonReleaseMask, levptr(XEvent))) {
653 greg 3.13 /* pause so as not to move too fast */
654     waitabit();
655 gwlarson 3.1
656     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
657     &rootx, &rooty, &wx, &wy, &statemask))
658     break; /* on another screen */
659    
660     if (!moveview(wx, vres-1-wy, movdir, movorb)) {
661     sleep(1);
662     continue;
663     } else
664     moved++;
665     }
666     if (!moved) { /* do final motion */
667     movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
668     wx = levptr(XButtonReleasedEvent)->x;
669     wy = levptr(XButtonReleasedEvent)->y;
670     moveview(wx, vres-1-wy, movdir, movorb);
671     }
672     }
673    
674    
675     getintersect(wp, org, dir, md) /* intersect ray with scene geometry */
676     FVECT wp; /* returned world intersection point */
677     FVECT org, dir;
678     double md;
679     {
680     float fbuf[6];
681     /* check to see if rtrace is running */
682 schorsch 3.15 /* if (rtpd.pid <= 0) */
683     if (!rtpd.running)
684 gwlarson 3.1 return(0);
685     /* assign origin */
686     fbuf[0] = org[0]; fbuf[1] = org[1]; fbuf[2] = org[2];
687     /* compute clipping distance */
688     if (md <= FTINY) md = FHUGE;
689     fbuf[3] = dir[0]*md; fbuf[4] = dir[1]*md; fbuf[5] = dir[2]*md;
690     /* trace that ray */
691 schorsch 3.15 if (process(&rtpd, (char *)fbuf, (char *)fbuf,
692 greg 3.13 4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float))
693 gwlarson 3.1 error(INTERNAL, "error getting data back from rtrace process");
694     if (fbuf[3] >= .99*FHUGE)
695     return(0); /* missed local objects */
696     wp[0] = fbuf[0]; wp[1] = fbuf[1]; wp[2] = fbuf[2];
697     return(1); /* else return world intersection */
698     }
699    
700    
701     setglpersp(vp) /* set perspective view in GL */
702     register VIEW *vp;
703     {
704     double d, xmin, xmax, ymin, ymax, zmin, zmax;
705    
706 gwlarson 3.2 zmin = 0.1;
707     zmax = 1000.;
708 gwlarson 3.1 if (thisview.vfore > FTINY)
709     zmin = thisview.vfore;
710     if (thisview.vaft > FTINY)
711     zmax = thisview.vaft;
712     xmax = zmin * tan(PI/180./2. * thisview.horiz);
713     xmin = -xmax;
714     d = thisview.hoff * (xmax - xmin);
715     xmin += d; xmax += d;
716     ymax = zmin * tan(PI/180./2. * thisview.vert);
717     ymin = -ymax;
718     d = thisview.voff * (ymax - ymin);
719     ymin += d; ymax += d;
720     /* set view matrix */
721     glMatrixMode(GL_PROJECTION);
722     glLoadIdentity();
723     glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
724     gluLookAt(thisview.vp[0], thisview.vp[1], thisview.vp[2],
725     thisview.vp[0] + thisview.vdir[0],
726     thisview.vp[1] + thisview.vdir[1],
727     thisview.vp[2] + thisview.vdir[2],
728     thisview.vup[0], thisview.vup[1], thisview.vup[2]);
729     rgl_checkerr("setting perspective view");
730     }
731    
732    
733     int
734     getkey(ekey) /* get input key */
735     register XKeyPressedEvent *ekey;
736     {
737     int n;
738     char buf[8];
739    
740     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
741     if (n != 1)
742     return(1);
743     switch (buf[0]) {
744     case 'h': /* turn on height motion lock */
745     headlocked = 1;
746     break;
747     case 'H': /* turn off height motion lock */
748     headlocked = 0;
749     break;
750     case 'l': /* retrieve last (premouse) view */
751 gwlarson 3.2 if (lastview.type) {
752 gwlarson 3.1 VIEW vtmp;
753 schorsch 3.17 vtmp = thisview;
754 gwlarson 3.1 dev_view(&lastview);
755 schorsch 3.17 lastview = vtmp;
756 gwlarson 3.1 } else
757     XBell(ourdisplay, 0);
758     break;
759     case 'n': /* move to next standard view */
760     gotoview(currentview+1);
761     break;
762     case 'p': /* move to last standard view */
763     gotoview(currentview-1);
764     break;
765     case '+': /* zoom in */
766     zoomview(100+ZOOMPCT, ekey->x, vres-1-ekey->y);
767     break;
768     case '-': /* zoom out */
769     zoomview(100-ZOOMPCT, ekey->x, vres-1-ekey->y);
770     break;
771     case 'v': /* spit current view to stdout */
772     fputs(VIEWSTR, stdout);
773     fprintview(&thisview, stdout);
774     fputc('\n', stdout);
775     break;
776     case 'V': /* append view to rad file */
777     appendview(NULL, &thisview);
778     break;
779     case 'q': /* quit the program */
780     return(0);
781     default:
782     XBell(ourdisplay, 0);
783     break;
784     }
785     return(1);
786     }
787    
788    
789     zoomview(pct, dx, dy) /* zoom in or out around (dx,dy) */
790     int pct;
791     int dx, dy;
792     {
793     double h, v;
794     FVECT direc;
795    
796     if (pct == 100 | pct <= 0)
797     return;
798     copylastv("zooming");
799     h = (dx+.5)/hres - 0.5;
800     v = (dy+.5)/vres - 0.5;
801     h *= (1. - 100./pct);
802     v *= (1. - 100./pct);
803     thisview.vdir[0] += h*thisview.hvec[0] + v*thisview.vvec[0];
804     thisview.vdir[1] += h*thisview.hvec[1] + v*thisview.vvec[1];
805     thisview.vdir[2] += h*thisview.hvec[2] + v*thisview.vvec[2];
806     thisview.horiz = 2.*180./PI * atan( 100./pct *
807     tan(PI/180./2.*thisview.horiz) );
808     thisview.vert = 2.*180./PI * atan( 100./pct *
809     tan(PI/180./2.*thisview.vert) );
810     setview(&thisview);
811     dev_view(&thisview);
812     }
813    
814    
815     gotoview(vwnum) /* go to specified view number */
816     int vwnum;
817     {
818     if (vwnum < 0)
819     for (vwnum = currentview; vwl[vwnum+1].v != NULL; vwnum++)
820     ;
821     else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL)
822     vwnum = 0;
823 gwlarson 3.4 copylastv("standard view");
824 gwlarson 3.1 dev_view(vwl[currentview=vwnum].v);
825     }
826    
827    
828     appendview(nm, vp) /* append standard view */
829     char *nm;
830     VIEW *vp;
831     {
832     FILE *fp;
833     /* check if already in there */
834 schorsch 3.16 if (!memcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) {
835 gwlarson 3.1 error(COMMAND, "view already in standard list");
836     return;
837     }
838     /* append to file */
839     if ((fp = fopen(radfile, "a")) == NULL) {
840     error(COMMAND, "cannot append rad input file");
841     return;
842     }
843     fputs("view=", fp);
844     if (nm != NULL) {
845     fputc(' ', fp); fputs(nm, fp);
846     }
847     fprintview(vp, fp); fputc('\n', fp);
848     fclose(fp);
849     /* append to our list */
850     while (vwl[currentview].v != NULL)
851     currentview++;
852     if (currentview >= MAXVIEW)
853     error(INTERNAL, "too many views in appendview");
854     vwl[currentview].v = (VIEW *)bmalloc(sizeof(VIEW));
855 schorsch 3.17 *(vwl[currentview].v) = thisview;
856 gwlarson 3.1 if (nm != NULL)
857     vwl[currentview].nam = savqstr(nm);
858     }
859    
860    
861     copylastv(cause) /* copy last view position */
862     char *cause;
863     {
864 gwlarson 3.2 static char *lastvc;
865    
866 gwlarson 3.1 if (cause == lastvc)
867     return; /* only record one view per cause */
868     lastvc = cause;
869 schorsch 3.17 lastview = thisview;
870 gwlarson 3.1 }
871    
872    
873     fixwindow(eexp) /* repair damage to window */
874     register XExposeEvent *eexp;
875     {
876     if (hres == 0 | vres == 0) { /* first exposure */
877     resizewindow((XConfigureEvent *)eexp);
878     return;
879     }
880     if (eexp->count) /* wait for final exposure */
881     return;
882     /* rerender everything */
883     render();
884     }
885    
886    
887     resizewindow(ersz) /* resize window */
888     register XConfigureEvent *ersz;
889     {
890     static char resizing[] = "resizing window";
891     double wa, va;
892    
893     glViewport(0, 0, hres=ersz->width, vres=ersz->height);
894     if (hres > maxhres) maxhres = hres;
895     if (vres > maxvres) maxvres = vres;
896 gwlarson 3.5 if (no_render)
897 gwlarson 3.2 return;
898 gwlarson 3.1 wa = (vres*pheight)/(hres*pwidth);
899     va = viewaspect(&thisview);
900     if (va > wa+.05) {
901     copylastv(resizing);
902     thisview.vert = 2.*180./PI *
903     atan( tan(PI/180./2. * thisview.horiz) * wa );
904     } else if (va < wa-.05) {
905     copylastv(resizing);
906     thisview.horiz = 2.*180./PI *
907     atan( tan(PI/180./2. * thisview.vert) / wa );
908     } else
909     return;
910     setview(&thisview);
911     dev_view(&thisview);
912     }