ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.11
Committed: Wed Oct 21 17:48:51 1998 UTC (25 years, 5 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.10: +3 -3 lines
Log Message:
modifications to avoid problems with redeclared BYTE type

File Contents

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