ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.13
Committed: Sat Feb 22 02:07:30 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.12: +17 -6 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Program to display Radiance scene using OpenGL.
6 */
7
8 #include <sys/types.h>
9 #include <GL/glx.h>
10 #ifndef NOSTEREO
11 #include <X11/extensions/SGIStereo.h>
12 #endif
13 #include <ctype.h>
14 #include <time.h>
15 #include "radogl.h"
16 #include "view.h"
17 #include "paths.h"
18 #include "glradicon.h"
19
20 #ifndef MAXVIEW
21 #define MAXVIEW 63 /* maximum number of standard views */
22 #endif
23 #ifndef MAXSCENE
24 #define MAXSCENE 127 /* maximum number of scene files */
25 #endif
26
27 #define ZOOMPCT 9 /* percent to zoom at a time */
28
29 #define MOVPCT 7 /* percent distance to move /frame */
30 #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
31 #define MOVDEG (-5) /* degrees to orbit CW/down /frame */
32 #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
33
34 #define BORWIDTH 5 /* border width */
35
36 #define ourscreen DefaultScreen(ourdisplay)
37 #define ourroot RootWindow(ourdisplay,ourscreen)
38 #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
39 ButtonPressMask|ButtonReleaseMask)
40
41 #define levptr(etype) ((etype *)&currentevent)
42
43 XEvent currentevent; /* current event */
44
45 int mapped = 0; /* window is mapped? */
46 unsigned long ourblack=0, ourwhite=~0;
47
48 Display *ourdisplay = NULL; /* our display */
49 XVisualInfo *ourvinf; /* our visual information */
50 Window gwind = 0; /* our graphics window */
51 int hres, vres; /* rendering window dimensions */
52 int maxhres, maxvres; /* maximum given dimensions */
53 GLXContext gctx; /* our GLX context */
54
55 double pwidth, pheight; /* pixel dimensions (mm) */
56
57 int headlocked = 0; /* lock vertical motion */
58
59 struct {
60 char *nam; /* view name (NULL if none) */
61 VIEW *v; /* parameters (NULL term.) */
62 } vwl[MAXVIEW+1]; /* our list of views */
63
64 int currentview = 0; /* current view number */
65 VIEW thisview = STDVIEW; /* displayed view */
66 double eyedist = 1; /* interocular distance */
67 VIEW lastview; /* last recorded view */
68
69 char *progname; /* global argv[0] */
70 char *radfile; /* rad input file */
71 char *scene[MAXSCENE+1]; /* material and scene file list */
72 int nscenef = 0; /* number of scene files */
73 char *octree; /* octree name (NULL if unnec.) */
74
75 int rtpd[3]; /* rtrace process descriptors */
76
77 int silent = 0; /* run rad silently? */
78 int backvis = 1; /* back faces visible? */
79 int stereo = 0; /* do stereo? */
80
81 #ifdef NOSTEREO
82 #define setstereobuf(bid) 0
83 #else
84 #define setstereobuf(bid) (glXWaitGL(), \
85 XSGISetStereoBuffer(ourdisplay, gwind, bid), \
86 glXWaitX())
87 #endif
88
89 int displist; /* our scene display list */
90
91 int no_render = 0; /* don't rerender */
92
93 #ifdef BSD
94 #define strchr index
95 #endif
96
97 extern char *strchr(), *fgets(), *fgetline(), *atos(), *scan4var();
98 extern int nowarn; /* turn warnings off? */
99 extern time_t time();
100
101
102 main(argc, argv)
103 int argc;
104 char *argv[];
105 {
106 char *viewsel = NULL;
107 long vwintvl = 0;
108 int i;
109
110 progname = argv[0];
111 for (i = 1; i < argc && argv[i][0] == '-'; i++)
112 switch (argv[i][1]) {
113 case 'v':
114 viewsel = argv[++i];
115 break;
116 case 'w':
117 nowarn = !nowarn;
118 break;
119 case 's':
120 silent = !silent;
121 break;
122 case 'S':
123 stereo = !stereo;
124 break;
125 case 'c':
126 vwintvl = atoi(argv[++i]);
127 break;
128 case 'b':
129 backvis = !backvis;
130 break;
131 default:
132 goto userr;
133 }
134 if (i >= argc)
135 goto userr;
136 #ifdef NOSTEREO
137 if (stereo)
138 error(INTERNAL, "stereo not supported in this version");
139 #endif
140 /* run rad and get views */
141 runrad(argc-i, argv+i);
142 /* check view */
143 if (viewsel != NULL)
144 if (viewsel[0] == '-') {
145 char *ve = viewsel;
146 if (!sscanview(&thisview, viewsel) ||
147 (ve = setview(&thisview)) != NULL) {
148 fprintf(stderr, "%s: bad view: %s\n",
149 progname, ve);
150 quit(1);
151 }
152 currentview = -1;
153 } else if ((currentview = findvw(viewsel)) < 0) {
154 fprintf(stderr, "%s: no such view: %s\n",
155 progname, viewsel);
156 quit(1);
157 }
158 /* open GL */
159 dev_open(radfile = argv[i]);
160 /* load octree or scene files */
161 if (octree != NULL) {
162 displist = rgl_octlist(octree, NULL, NULL, NULL);
163 startrtrace(octree);
164 } else
165 displist = rgl_filelist(nscenef, scene, NULL);
166 /* set initial view */
167 dev_view(currentview < 0 ? &thisview : vwl[currentview].v);
168 /* input/render loop */
169 while (dev_input(vwintvl))
170 ;
171 /* all done */
172 quit(0);
173 userr:
174 fprintf(stderr,
175 "Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n",
176 argv[0]);
177 quit(1);
178 }
179
180
181 void
182 quit(code) /* exit gracefully */
183 int code;
184 {
185 if (ourdisplay != NULL)
186 dev_close();
187 if (rtpd[2] > 0) {
188 if (close_process(rtpd) > 0)
189 wputs("bad exit status from rtrace\n");
190 rtpd[2] = 0;
191 }
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 if (open_process(rtpd, av) <= 0)
207 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 if (silent) {
225 strcpy(cp, "-s ");
226 cp += 3;
227 }
228 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 expval *= 0.5; /* compensate for local shading */
249 }
250 /* look for eye separation */
251 if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL)
252 eyedist = atof(cp);
253 /* 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 copystruct(vwl[nvv].v, &stdview);
279 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 /* get relevant options */
294 while (fgets(buf, sizeof(buf), fp) != NULL)
295 if (!strncmp(buf, "-av ", 4))
296 setcolor(ambval, atof(buf+4),
297 atof(sskip2(buf+4,1)),
298 atof(sskip2(buf+4,2)));
299 else if (backvis && !strncmp(buf, "-bv", 3) &&
300 (!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL))
301 backvis = 0;
302 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 #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 /* 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 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
427 glEnable(GL_LIGHTING);
428 glFrontFace(GL_CCW);
429 glCullFace(GL_BACK);
430 if (backvis)
431 glDisable(GL_CULL_FACE);
432 else
433 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 if (stereo) { /* set stereo mode */
441 setstereobuf(STEREO_BUFFER_LEFT);
442 pheight *= 2.;
443 }
444 /* map the window */
445 XMapWindow(ourdisplay, gwind);
446 no_render++;
447 do
448 dev_input(0); /* get resize event */
449 while (hres == 0 & vres == 0);
450 no_render--;
451 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 va = viewaspect(nv);
487 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 no_render++;
502 XResizeWindow(ourdisplay, gwind, newhres, newvres);
503 do
504 dev_input(0); /* get resize event */
505 while (newhres != hres | newvres != vres);
506 no_render--;
507 }
508 }
509 copystruct(&thisview, nv);
510 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 double d;
559
560 if (!mapped | no_render)
561 return;
562 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
563 glCallList(displist);
564 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 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 register int li;
589 /* start with old view */
590 copystruct(&nv, &thisview);
591 /* change view direction */
592 if ((d = viewray(v1, odir, &thisview,
593 (dx+.5)/hres, (dy+.5)/vres)) < -FTINY)
594 return(0); /* outside view */
595 if (mov | orb) {
596 if (!getintersect(wp, v1, odir, d))
597 return(0);
598 VSUM(odir, wp, nv.vp, -1.);
599 } else
600 VCOPY(nv.vdir, odir);
601 if (orb && mov) { /* orbit left/right */
602 spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
603 VSUM(nv.vp, wp, odir, -1.);
604 spinvector(nv.vdir, nv.vdir, nv.vup, d);
605 } else if (orb) { /* orbit up/down */
606 fcross(v1, odir, nv.vup);
607 if (normalize(v1) == 0.)
608 return(0);
609 spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
610 VSUM(nv.vp, wp, odir, -1.);
611 spinvector(nv.vdir, nv.vdir, v1, d);
612 } else if (mov) { /* move forward/backward */
613 d = MOVPCT/100. * mov;
614 VSUM(nv.vp, nv.vp, odir, d);
615 }
616 if (!mov ^ !orb && headlocked) { /* restore head height */
617 VSUM(v1, thisview.vp, nv.vp, -1.);
618 d = DOT(v1, thisview.vup);
619 VSUM(nv.vp, nv.vp, thisview.vup, d);
620 }
621 if (setview(&nv) != NULL)
622 return(0); /* illegal view */
623 dev_view(&nv);
624 return(1);
625 }
626
627
628 static
629 waitabit() /* pause a moment */
630 {
631 struct timespec ts;
632 ts.tv_sec = 0;
633 ts.tv_nsec = 5000000;
634 nanosleep(&ts, NULL);
635 }
636
637
638 getmove(ebut) /* get view change */
639 XButtonPressedEvent *ebut;
640 {
641 int movdir = MOVDIR(ebut->button);
642 int movorb = MOVORB(ebut->state);
643 int moved = 0;
644 Window rootw, childw;
645 int rootx, rooty, wx, wy;
646 unsigned int statemask;
647
648 copylastv( movorb ? (movdir ? "left/right" : "up/down") :
649 (movdir ? "fore/back" : "rotate") );
650 XNoOp(ourdisplay);
651
652 while (!XCheckMaskEvent(ourdisplay,
653 ButtonReleaseMask, levptr(XEvent))) {
654 /* pause so as not to move too fast */
655 waitabit();
656
657 if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
658 &rootx, &rooty, &wx, &wy, &statemask))
659 break; /* on another screen */
660
661 if (!moveview(wx, vres-1-wy, movdir, movorb)) {
662 sleep(1);
663 continue;
664 } else
665 moved++;
666 }
667 if (!moved) { /* do final motion */
668 movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
669 wx = levptr(XButtonReleasedEvent)->x;
670 wy = levptr(XButtonReleasedEvent)->y;
671 moveview(wx, vres-1-wy, movdir, movorb);
672 }
673 }
674
675
676 getintersect(wp, org, dir, md) /* intersect ray with scene geometry */
677 FVECT wp; /* returned world intersection point */
678 FVECT org, dir;
679 double md;
680 {
681 float fbuf[6];
682 /* check to see if rtrace is running */
683 if (rtpd[2] <= 0)
684 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 if (process(rtpd, (char *)fbuf, (char *)fbuf,
692 4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float))
693 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 zmin = 0.1;
707 zmax = 1000.;
708 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 if (lastview.type) {
752 VIEW vtmp;
753 copystruct(&vtmp, &thisview);
754 dev_view(&lastview);
755 copystruct(&lastview, &vtmp);
756 } 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 copylastv("standard view");
824 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 if (!bcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) {
835 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 copystruct(vwl[currentview].v, &thisview);
856 if (nm != NULL)
857 vwl[currentview].nam = savqstr(nm);
858 }
859
860
861 copylastv(cause) /* copy last view position */
862 char *cause;
863 {
864 static char *lastvc;
865
866 if (cause == lastvc)
867 return; /* only record one view per cause */
868 lastvc = cause;
869 copystruct(&lastview, &thisview);
870 }
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 if (no_render)
897 return;
898 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 }