ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.3
Committed: Tue Jun 9 15:16:55 1998 UTC (25 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.2: +23 -7 lines
Log Message:
more viewing fixes -- changed -v option to allow views\

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