ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.27
Committed: Fri Feb 28 05:18:49 2020 UTC (2 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3, HEAD
Changes since 3.26: +4 -4 lines
Log Message:
Added filtering capabilities to Unix version of open_process()

File Contents

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