ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.2
Committed: Tue Jun 9 12:46:45 1998 UTC (25 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.1: +13 -5 lines
Log Message:
minor viewing fixes

File Contents

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