ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.15
Committed: Thu Jun 26 00:58:11 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.14: +12 -8 lines
Log Message:
Abstracted process and path handling for Windows.
Renamed FLOAT to RREAL because of conflict on Windows.
Added conditional compiles for some signal handlers.

File Contents

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