ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.8
Committed: Wed Jul 8 18:00:39 1998 UTC (25 years, 9 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.7: +2 -1 lines
Log Message:
fixed usage message

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 #ifndef NOSTEREO
17 #include <X11/extensions/SGIStereo.h>
18 #endif
19 #include <ctype.h>
20 #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 double eyedist = 1; /* interocular distance */
69 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 int silent = 0; /* run rad silently? */
80 int backvis = 1; /* back faces visible? */
81 int stereo = 0; /* do stereo? */
82
83 #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 int displist; /* our scene display list */
92
93 int no_render = 0; /* don't rerender */
94
95 #ifdef BSD
96 #define strchr index
97 #endif
98
99 extern char *strchr(), *fgets(), *fgetline(), *atos(), *scan4var();
100 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 case 's':
122 silent = !silent;
123 break;
124 case 'S':
125 stereo = !stereo;
126 break;
127 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 #ifdef NOSTEREO
139 if (stereo)
140 error(INTERNAL, "stereo not supported in this version");
141 #endif
142 /* run rad and get views */
143 runrad(argc-i, argv+i);
144 /* check view */
145 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 /* 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 dev_view(currentview < 0 ? &thisview : vwl[currentview].v);
170 /* input/render loop */
171 while (dev_input(vwintvl))
172 ;
173 /* all done */
174 quit(0);
175 userr:
176 fprintf(stderr,
177 "Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n",
178 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 if (silent) {
226 strcpy(cp, "-s ");
227 cp += 3;
228 }
229 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 expval *= 0.5; /* compensate for local shading */
250 }
251 /* look for eye separation */
252 if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL)
253 eyedist = atof(cp);
254 /* 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 /* get relevant options */
295 while (fgets(buf, sizeof(buf), fp) != NULL)
296 if (!strncmp(buf, "-av ", 4))
297 setcolor(ambval, atof(buf+4),
298 atof(sskip2(buf+4,1)),
299 atof(sskip2(buf+4,2)));
300 else if (backvis && !strncmp(buf, "-bv", 3) &&
301 (!buf[3] || strchr(" 0-FfNn", buf[3]) != NULL))
302 backvis = 0;
303 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 #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 /* 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 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
428 glEnable(GL_LIGHTING);
429 if (backvis)
430 glDisable(GL_CULL_FACE);
431 else {
432 glFrontFace(GL_CCW);
433 glCullFace(GL_BACK);
434 glEnable(GL_CULL_FACE);
435 }
436 glDrawBuffer(GL_BACK);
437 /* figure out sensible view */
438 pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
439 DisplayWidth(ourdisplay, ourscreen);
440 pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
441 DisplayHeight(ourdisplay, ourscreen);
442 if (stereo) { /* set stereo mode */
443 setstereobuf(STEREO_BUFFER_LEFT);
444 pheight *= 2.;
445 }
446 /* map the window */
447 XMapWindow(ourdisplay, gwind);
448 no_render++;
449 do
450 dev_input(0); /* get resize event */
451 while (hres == 0 & vres == 0);
452 no_render--;
453 rgl_checkerr("initializing GLX");
454 }
455
456
457 dev_close() /* close our display and free resources */
458 {
459 glXMakeCurrent(ourdisplay, None, NULL);
460 glXDestroyContext(ourdisplay, gctx);
461 XDestroyWindow(ourdisplay, gwind);
462 gwind = 0;
463 XCloseDisplay(ourdisplay);
464 ourdisplay = NULL;
465 }
466
467
468 int
469 dev_view(nv) /* assign new driver view */
470 register VIEW *nv;
471 {
472 int newhres = hres, newvres = vres;
473 double wa, va;
474 /* check view legality */
475 if (nv->type != VT_PER) {
476 error(COMMAND, "illegal view type");
477 nv->type = VT_PER;
478 }
479 if (nv->horiz > 160. | nv->vert > 160.) {
480 error(COMMAND, "illegal view angle");
481 if (nv->horiz > 160.)
482 nv->horiz = 160.;
483 if (nv->vert > 160.)
484 nv->vert = 160.;
485 }
486 if (hres != 0 & vres != 0) {
487 wa = (vres*pheight)/(hres*pwidth);
488 va = viewaspect(nv);
489 if (va > wa+.05) {
490 newvres = (pwidth/pheight)*va*newhres + .5;
491 if (newvres > maxvres) {
492 newvres = maxvres;
493 newhres = (pheight/pwidth)/va*newvres + .5;
494 }
495 } else if (va < wa-.05) {
496 newhres = (pheight/pwidth)/va*newvres + .5;
497 if (newhres > maxhres) {
498 newhres = maxhres;
499 newvres = (pwidth/pheight)*va*newhres + .5;
500 }
501 }
502 if (newhres != hres | newvres != vres) {
503 no_render++;
504 XResizeWindow(ourdisplay, gwind, newhres, newvres);
505 do
506 dev_input(0); /* get resize event */
507 while (newhres != hres | newvres != vres);
508 no_render--;
509 }
510 }
511 copystruct(&thisview, nv);
512 setglpersp(&thisview);
513 render();
514 return(1);
515 }
516
517
518 int
519 dev_input(nsecs) /* get next input event */
520 int nsecs;
521 {
522 #if 0
523 static time_t lasttime = 0;
524 time_t thistime;
525
526 if (nsecs > 0) {
527 thistime = time(0);
528 nsecs -= (long)(thistime - lasttime);
529 lasttime = thistime;
530 }
531 if (nsecs > 0)
532 alarm(nsecs);
533 #endif
534 XNextEvent(ourdisplay, levptr(XEvent));
535 switch (levptr(XEvent)->type) {
536 case ConfigureNotify:
537 resizewindow(levptr(XConfigureEvent));
538 break;
539 case UnmapNotify:
540 mapped = 0;
541 break;
542 case MapNotify:
543 mapped = 1;
544 break;
545 case Expose:
546 fixwindow(levptr(XExposeEvent));
547 break;
548 case KeyPress:
549 return(getkey(levptr(XKeyPressedEvent)));
550 case ButtonPress:
551 getmove(levptr(XButtonPressedEvent));
552 break;
553 }
554 return(1);
555 }
556
557
558 render() /* render our display list and swap buffers */
559 {
560 double d;
561
562 if (!mapped | no_render)
563 return;
564 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
565 glCallList(displist);
566 if (stereo) { /* do right eye for stereo */
567 setstereobuf(STEREO_BUFFER_RIGHT);
568 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
569 glMatrixMode(GL_MODELVIEW);
570 glPushMatrix();
571 d = -eyedist / sqrt(thisview.hn2);
572 glTranslated(d*thisview.hvec[0], d*thisview.hvec[1],
573 d*thisview.hvec[2]);
574 glCallList(displist);
575 glMatrixMode(GL_MODELVIEW);
576 glPopMatrix();
577 setstereobuf(STEREO_BUFFER_LEFT);
578 }
579 glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */
580 rgl_checkerr("rendering display list");
581 }
582
583
584 moveview(dx, dy, mov, orb) /* move our view */
585 int dx, dy, mov, orb;
586 {
587 VIEW nv;
588 FVECT odir, v1, wp;
589 double d;
590 register int li;
591 /* start with old view */
592 copystruct(&nv, &thisview);
593 /* change view direction */
594 if ((d = viewray(v1, odir, &thisview,
595 (dx+.5)/hres, (dy+.5)/vres)) < -FTINY)
596 return(0); /* outside view */
597 if (mov | orb) {
598 if (!getintersect(wp, v1, odir, d))
599 return(0);
600 VSUM(odir, wp, nv.vp, -1.);
601 } else
602 VCOPY(nv.vdir, odir);
603 if (orb && mov) { /* orbit left/right */
604 spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
605 VSUM(nv.vp, wp, odir, -1.);
606 spinvector(nv.vdir, nv.vdir, nv.vup, d);
607 } else if (orb) { /* orbit up/down */
608 fcross(v1, odir, nv.vup);
609 if (normalize(v1) == 0.)
610 return(0);
611 spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
612 VSUM(nv.vp, wp, odir, -1.);
613 spinvector(nv.vdir, nv.vdir, v1, d);
614 } else if (mov) { /* move forward/backward */
615 d = MOVPCT/100. * mov;
616 VSUM(nv.vp, nv.vp, odir, d);
617 }
618 if (!mov ^ !orb && headlocked) { /* restore head height */
619 VSUM(v1, thisview.vp, nv.vp, -1.);
620 d = DOT(v1, thisview.vup);
621 VSUM(nv.vp, nv.vp, thisview.vup, d);
622 }
623 if (setview(&nv) != NULL)
624 return(0); /* illegal view */
625 dev_view(&nv);
626 return(1);
627 }
628
629
630 getmove(ebut) /* get view change */
631 XButtonPressedEvent *ebut;
632 {
633 int movdir = MOVDIR(ebut->button);
634 int movorb = MOVORB(ebut->state);
635 int moved = 0;
636 Window rootw, childw;
637 int rootx, rooty, wx, wy;
638 unsigned int statemask;
639
640 copylastv( movorb ? (movdir ? "left/right" : "up/down") :
641 (movdir ? "fore/back" : "rotate") );
642 XNoOp(ourdisplay);
643
644 while (!XCheckMaskEvent(ourdisplay,
645 ButtonReleaseMask, levptr(XEvent))) {
646
647 if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
648 &rootx, &rooty, &wx, &wy, &statemask))
649 break; /* on another screen */
650
651 if (!moveview(wx, vres-1-wy, movdir, movorb)) {
652 sleep(1);
653 continue;
654 } else
655 moved++;
656 }
657 if (!moved) { /* do final motion */
658 movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
659 wx = levptr(XButtonReleasedEvent)->x;
660 wy = levptr(XButtonReleasedEvent)->y;
661 moveview(wx, vres-1-wy, movdir, movorb);
662 }
663 }
664
665
666 getintersect(wp, org, dir, md) /* intersect ray with scene geometry */
667 FVECT wp; /* returned world intersection point */
668 FVECT org, dir;
669 double md;
670 {
671 float fbuf[6];
672 /* check to see if rtrace is running */
673 if (rtpd[2] <= 0)
674 return(0);
675 /* assign origin */
676 fbuf[0] = org[0]; fbuf[1] = org[1]; fbuf[2] = org[2];
677 /* compute clipping distance */
678 if (md <= FTINY) md = FHUGE;
679 fbuf[3] = dir[0]*md; fbuf[4] = dir[1]*md; fbuf[5] = dir[2]*md;
680 /* trace that ray */
681 if (process(rtpd, fbuf, fbuf, 4*sizeof(float), 6*sizeof(float)) !=
682 4*sizeof(float))
683 error(INTERNAL, "error getting data back from rtrace process");
684 if (fbuf[3] >= .99*FHUGE)
685 return(0); /* missed local objects */
686 wp[0] = fbuf[0]; wp[1] = fbuf[1]; wp[2] = fbuf[2];
687 return(1); /* else return world intersection */
688 }
689
690
691 setglpersp(vp) /* set perspective view in GL */
692 register VIEW *vp;
693 {
694 double d, xmin, xmax, ymin, ymax, zmin, zmax;
695
696 zmin = 0.1;
697 zmax = 1000.;
698 if (thisview.vfore > FTINY)
699 zmin = thisview.vfore;
700 if (thisview.vaft > FTINY)
701 zmax = thisview.vaft;
702 xmax = zmin * tan(PI/180./2. * thisview.horiz);
703 xmin = -xmax;
704 d = thisview.hoff * (xmax - xmin);
705 xmin += d; xmax += d;
706 ymax = zmin * tan(PI/180./2. * thisview.vert);
707 ymin = -ymax;
708 d = thisview.voff * (ymax - ymin);
709 ymin += d; ymax += d;
710 /* set view matrix */
711 glMatrixMode(GL_PROJECTION);
712 glLoadIdentity();
713 glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);
714 gluLookAt(thisview.vp[0], thisview.vp[1], thisview.vp[2],
715 thisview.vp[0] + thisview.vdir[0],
716 thisview.vp[1] + thisview.vdir[1],
717 thisview.vp[2] + thisview.vdir[2],
718 thisview.vup[0], thisview.vup[1], thisview.vup[2]);
719 rgl_checkerr("setting perspective view");
720 }
721
722
723 int
724 getkey(ekey) /* get input key */
725 register XKeyPressedEvent *ekey;
726 {
727 int n;
728 char buf[8];
729
730 n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
731 if (n != 1)
732 return(1);
733 switch (buf[0]) {
734 case 'h': /* turn on height motion lock */
735 headlocked = 1;
736 break;
737 case 'H': /* turn off height motion lock */
738 headlocked = 0;
739 break;
740 case 'l': /* retrieve last (premouse) view */
741 if (lastview.type) {
742 VIEW vtmp;
743 copystruct(&vtmp, &thisview);
744 dev_view(&lastview);
745 copystruct(&lastview, &vtmp);
746 } else
747 XBell(ourdisplay, 0);
748 break;
749 case 'n': /* move to next standard view */
750 gotoview(currentview+1);
751 break;
752 case 'p': /* move to last standard view */
753 gotoview(currentview-1);
754 break;
755 case '+': /* zoom in */
756 zoomview(100+ZOOMPCT, ekey->x, vres-1-ekey->y);
757 break;
758 case '-': /* zoom out */
759 zoomview(100-ZOOMPCT, ekey->x, vres-1-ekey->y);
760 break;
761 case 'v': /* spit current view to stdout */
762 fputs(VIEWSTR, stdout);
763 fprintview(&thisview, stdout);
764 fputc('\n', stdout);
765 break;
766 case 'V': /* append view to rad file */
767 appendview(NULL, &thisview);
768 break;
769 case 'q': /* quit the program */
770 return(0);
771 default:
772 XBell(ourdisplay, 0);
773 break;
774 }
775 return(1);
776 }
777
778
779 zoomview(pct, dx, dy) /* zoom in or out around (dx,dy) */
780 int pct;
781 int dx, dy;
782 {
783 double h, v;
784 FVECT direc;
785
786 if (pct == 100 | pct <= 0)
787 return;
788 copylastv("zooming");
789 h = (dx+.5)/hres - 0.5;
790 v = (dy+.5)/vres - 0.5;
791 h *= (1. - 100./pct);
792 v *= (1. - 100./pct);
793 thisview.vdir[0] += h*thisview.hvec[0] + v*thisview.vvec[0];
794 thisview.vdir[1] += h*thisview.hvec[1] + v*thisview.vvec[1];
795 thisview.vdir[2] += h*thisview.hvec[2] + v*thisview.vvec[2];
796 thisview.horiz = 2.*180./PI * atan( 100./pct *
797 tan(PI/180./2.*thisview.horiz) );
798 thisview.vert = 2.*180./PI * atan( 100./pct *
799 tan(PI/180./2.*thisview.vert) );
800 setview(&thisview);
801 dev_view(&thisview);
802 }
803
804
805 gotoview(vwnum) /* go to specified view number */
806 int vwnum;
807 {
808 if (vwnum < 0)
809 for (vwnum = currentview; vwl[vwnum+1].v != NULL; vwnum++)
810 ;
811 else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL)
812 vwnum = 0;
813 copylastv("standard view");
814 dev_view(vwl[currentview=vwnum].v);
815 }
816
817
818 appendview(nm, vp) /* append standard view */
819 char *nm;
820 VIEW *vp;
821 {
822 FILE *fp;
823 /* check if already in there */
824 if (!bcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) {
825 error(COMMAND, "view already in standard list");
826 return;
827 }
828 /* append to file */
829 if ((fp = fopen(radfile, "a")) == NULL) {
830 error(COMMAND, "cannot append rad input file");
831 return;
832 }
833 fputs("view=", fp);
834 if (nm != NULL) {
835 fputc(' ', fp); fputs(nm, fp);
836 }
837 fprintview(vp, fp); fputc('\n', fp);
838 fclose(fp);
839 /* append to our list */
840 while (vwl[currentview].v != NULL)
841 currentview++;
842 if (currentview >= MAXVIEW)
843 error(INTERNAL, "too many views in appendview");
844 vwl[currentview].v = (VIEW *)bmalloc(sizeof(VIEW));
845 copystruct(vwl[currentview].v, &thisview);
846 if (nm != NULL)
847 vwl[currentview].nam = savqstr(nm);
848 }
849
850
851 copylastv(cause) /* copy last view position */
852 char *cause;
853 {
854 static char *lastvc;
855
856 if (cause == lastvc)
857 return; /* only record one view per cause */
858 lastvc = cause;
859 copystruct(&lastview, &thisview);
860 }
861
862
863 fixwindow(eexp) /* repair damage to window */
864 register XExposeEvent *eexp;
865 {
866 if (hres == 0 | vres == 0) { /* first exposure */
867 resizewindow((XConfigureEvent *)eexp);
868 return;
869 }
870 if (eexp->count) /* wait for final exposure */
871 return;
872 /* rerender everything */
873 render();
874 }
875
876
877 resizewindow(ersz) /* resize window */
878 register XConfigureEvent *ersz;
879 {
880 static char resizing[] = "resizing window";
881 double wa, va;
882
883 glViewport(0, 0, hres=ersz->width, vres=ersz->height);
884 if (hres > maxhres) maxhres = hres;
885 if (vres > maxvres) maxvres = vres;
886 if (no_render)
887 return;
888 wa = (vres*pheight)/(hres*pwidth);
889 va = viewaspect(&thisview);
890 if (va > wa+.05) {
891 copylastv(resizing);
892 thisview.vert = 2.*180./PI *
893 atan( tan(PI/180./2. * thisview.horiz) * wa );
894 } else if (va < wa-.05) {
895 copylastv(resizing);
896 thisview.horiz = 2.*180./PI *
897 atan( tan(PI/180./2. * thisview.vert) / wa );
898 } else
899 return;
900 setview(&thisview);
901 dev_view(&thisview);
902 }