ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.16
Committed: Mon Jun 30 14:59:13 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.15: +3 -2 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 schorsch 3.16 static const char RCSid[] = "$Id: glrad.c,v 3.15 2003/06/26 00:58:11 schorsch Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Program to display Radiance scene using OpenGL.
6     */
7    
8     #include <sys/types.h>
9     #include <GL/glx.h>
10 gwlarson 3.4 #ifndef NOSTEREO
11     #include <X11/extensions/SGIStereo.h>
12     #endif
13 gwlarson 3.1 #include <ctype.h>
14 schorsch 3.16 #include <string.h>
15 greg 3.13 #include <time.h>
16 schorsch 3.15
17 gwlarson 3.11 #include "radogl.h"
18     #include "view.h"
19     #include "paths.h"
20 gwlarson 3.1 #include "glradicon.h"
21 schorsch 3.15 #include "rtprocess.h"
22 gwlarson 3.1
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 at a time */
31    
32     #define MOVPCT 7 /* percent distance to move /frame */
33     #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
34     #define MOVDEG (-5) /* degrees to orbit CW/down /frame */
35     #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
36    
37     #define BORWIDTH 5 /* border width */
38    
39     #define ourscreen DefaultScreen(ourdisplay)
40     #define ourroot RootWindow(ourdisplay,ourscreen)
41     #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
42     ButtonPressMask|ButtonReleaseMask)
43    
44     #define levptr(etype) ((etype *)&currentevent)
45    
46     XEvent currentevent; /* current event */
47    
48     int mapped = 0; /* window is mapped? */
49     unsigned long ourblack=0, ourwhite=~0;
50    
51     Display *ourdisplay = NULL; /* our display */
52     XVisualInfo *ourvinf; /* our visual information */
53     Window gwind = 0; /* our graphics window */
54     int hres, vres; /* rendering window dimensions */
55     int maxhres, maxvres; /* maximum given dimensions */
56     GLXContext gctx; /* our GLX context */
57    
58     double pwidth, pheight; /* pixel dimensions (mm) */
59    
60     int headlocked = 0; /* lock vertical motion */
61    
62     struct {
63     char *nam; /* view name (NULL if none) */
64     VIEW *v; /* parameters (NULL term.) */
65     } vwl[MAXVIEW+1]; /* our list of views */
66    
67     int currentview = 0; /* current view number */
68     VIEW thisview = STDVIEW; /* displayed view */
69 gwlarson 3.4 double eyedist = 1; /* interocular distance */
70 gwlarson 3.1 VIEW lastview; /* last recorded view */
71    
72     char *progname; /* global argv[0] */
73     char *radfile; /* rad input file */
74     char *scene[MAXSCENE+1]; /* material and scene file list */
75     int nscenef = 0; /* number of scene files */
76     char *octree; /* octree name (NULL if unnec.) */
77    
78 schorsch 3.15 SUBPROC rtpd; /* rtrace process descriptors */
79 gwlarson 3.1
80 gwlarson 3.4 int silent = 0; /* run rad silently? */
81 gwlarson 3.1 int backvis = 1; /* back faces visible? */
82 gwlarson 3.4 int stereo = 0; /* do stereo? */
83 gwlarson 3.1
84 gwlarson 3.4 #ifdef NOSTEREO
85     #define setstereobuf(bid) 0
86     #else
87     #define setstereobuf(bid) (glXWaitGL(), \
88     XSGISetStereoBuffer(ourdisplay, gwind, bid), \
89     glXWaitX())
90     #endif
91    
92 gwlarson 3.1 int displist; /* our scene display list */
93    
94 gwlarson 3.5 int no_render = 0; /* don't rerender */
95 gwlarson 3.2
96 greg 3.14 extern char *fgetline(), *atos(), *scan4var();
97 gwlarson 3.1 extern int nowarn; /* turn warnings off? */
98    
99    
100     main(argc, argv)
101     int argc;
102     char *argv[];
103     {
104     char *viewsel = NULL;
105     long vwintvl = 0;
106     int i;
107    
108     progname = argv[0];
109     for (i = 1; i < argc && argv[i][0] == '-'; i++)
110     switch (argv[i][1]) {
111     case 'v':
112     viewsel = argv[++i];
113     break;
114     case 'w':
115     nowarn = !nowarn;
116     break;
117 gwlarson 3.4 case 's':
118     silent = !silent;
119     break;
120     case 'S':
121     stereo = !stereo;
122     break;
123 gwlarson 3.1 case 'c':
124     vwintvl = atoi(argv[++i]);
125     break;
126     case 'b':
127     backvis = !backvis;
128     break;
129     default:
130     goto userr;
131     }
132     if (i >= argc)
133     goto userr;
134 gwlarson 3.4 #ifdef NOSTEREO
135     if (stereo)
136     error(INTERNAL, "stereo not supported in this version");
137     #endif
138 gwlarson 3.1 /* run rad and get views */
139     runrad(argc-i, argv+i);
140     /* check view */
141 gwlarson 3.3 if (viewsel != NULL)
142     if (viewsel[0] == '-') {
143     char *ve = viewsel;
144     if (!sscanview(&thisview, viewsel) ||
145     (ve = setview(&thisview)) != NULL) {
146     fprintf(stderr, "%s: bad view: %s\n",
147     progname, ve);
148     quit(1);
149     }
150     currentview = -1;
151     } else if ((currentview = findvw(viewsel)) < 0) {
152     fprintf(stderr, "%s: no such view: %s\n",
153     progname, viewsel);
154     quit(1);
155     }
156 gwlarson 3.1 /* open GL */
157     dev_open(radfile = argv[i]);
158     /* load octree or scene files */
159     if (octree != NULL) {
160 gwlarson 3.12 displist = rgl_octlist(octree, NULL, NULL, NULL);
161 gwlarson 3.1 startrtrace(octree);
162     } else
163 gwlarson 3.12 displist = rgl_filelist(nscenef, scene, NULL);
164 gwlarson 3.1 /* set initial view */
165 gwlarson 3.3 dev_view(currentview < 0 ? &thisview : vwl[currentview].v);
166 gwlarson 3.1 /* input/render loop */
167     while (dev_input(vwintvl))
168     ;
169     /* all done */
170     quit(0);
171     userr:
172 gwlarson 3.8 fprintf(stderr,
173     "Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n",
174 gwlarson 3.1 argv[0]);
175     quit(1);
176     }
177    
178    
179 greg 3.13 void
180 gwlarson 3.1 quit(code) /* exit gracefully */
181     int code;
182     {
183     if (ourdisplay != NULL)
184     dev_close();
185 schorsch 3.15 /* if (rtpd.pid > 0) { */
186     if (rtpd.running) {
187     if (close_process(&rtpd) > 0)
188 gwlarson 3.1 wputs("bad exit status from rtrace\n");
189 schorsch 3.15 /* rtpd.pid = 0; */
190 gwlarson 3.1 }
191     exit(code);
192     }
193    
194    
195     startrtrace(octname) /* start rtrace on octname */
196     char *octname;
197     {
198     static char *av[12] = {"rtrace", "-h", "-fff", "-ld+",
199     "-opL", "-x", "1"};
200     int ac = 7;
201    
202     if (nowarn) av[ac++] = "-w-";
203     av[ac++] = octname;
204     av[ac] = NULL;
205 schorsch 3.15 if (open_process(&rtpd, av) <= 0)
206 gwlarson 3.1 error(SYSTEM, "cannot start rtrace process");
207     }
208    
209    
210     runrad(ac, av) /* run rad and load variables */
211     int ac;
212     char **av;
213     {
214     static char optfile[] = TEMPLATE;
215     int nvn = 0, nvv = 0;
216     FILE *fp;
217     int cval;
218     register char *cp;
219     char radcomm[256], buf[128], nam[32];
220     /* set rad commmand */
221     strcpy(radcomm, "rad -w -v 0 "); /* look out below! */
222     cp = radcomm + 19;
223 gwlarson 3.4 if (silent) {
224     strcpy(cp, "-s ");
225     cp += 3;
226     }
227 gwlarson 3.1 while (ac--) {
228     strcpy(cp, *av++);
229     while (*cp) cp++;
230     *cp++ = ' ';
231     }
232     strcpy(cp, "OPTFILE="); /* create temporary options file */
233     strcpy(cp+8, mktemp(optfile));
234     if (system(radcomm)) /* update octree */
235     error(USER, "error executing rad command");
236     /* replace "-v 0" with "-n -e -s -V" */
237     strcpy(radcomm+7, "-n -e -s -V");
238     radcomm[18] = ' ';
239     if ((fp = popen(radcomm, "r")) == NULL)
240     error(SYSTEM, "cannot start rad command");
241     buf[0] = '\0'; /* read variables alphabetically */
242     /* get exposure */
243     if ((cp = scan4var(buf, sizeof(buf), "EXPOSURE", fp)) != NULL) {
244     expval = atof(cp);
245     if (*cp == '-' | *cp == '+')
246     expval = pow(2., expval);
247 gwlarson 3.3 expval *= 0.5; /* compensate for local shading */
248 gwlarson 3.1 }
249 gwlarson 3.4 /* look for eye separation */
250     if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL)
251     eyedist = atof(cp);
252 gwlarson 3.1 /* look for materials */
253     while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) {
254     nscenef += wordstring(scene+nscenef, cp);
255     buf[0] = '\0';
256     }
257     /* look for octree */
258     if ((cp = scan4var(buf, sizeof(buf), "OCTREE", fp)) != NULL)
259     octree = savqstr(cp);
260     /* look for scene files */
261     while ((cp = scan4var(buf, sizeof(buf), "scene", fp)) != NULL) {
262     nscenef += wordstring(scene+nscenef, cp);
263     buf[0] = '\0';
264     }
265     /* load view names */
266     while ((cp = scan4var(buf, sizeof(buf), "view", fp)) != NULL) {
267     if (nvn >= MAXVIEW)
268     error(INTERNAL, "too many views in rad file");
269     vwl[nvn++].nam = *cp == '-' ? (char *)NULL :
270     savqstr(atos(nam, sizeof(nam), cp));
271     buf[0] = '\0';
272     }
273     /* load actual views */
274     do
275     if (isview(buf)) {
276     vwl[nvv].v = (VIEW *)bmalloc(sizeof(VIEW));
277     copystruct(vwl[nvv].v, &stdview);
278     sscanview(vwl[nvv].v, buf);
279     if ((cp = setview(vwl[nvv++].v)) != NULL) {
280     fprintf(stderr, "%s: bad view %d - %s\n",
281     progname, nvv, cp);
282     quit(1);
283     }
284     }
285     while (fgets(buf, sizeof(buf), fp) != NULL);
286     if (nvv != nvn)
287     error(INTERNAL, "view miscount in runrad");
288     pclose(fp);
289     /* open options file */
290     if ((fp = fopen(optfile, "r")) == NULL)
291     error(SYSTEM, "cannot open options file");
292 gwlarson 3.4 /* get relevant options */
293 gwlarson 3.1 while (fgets(buf, sizeof(buf), fp) != NULL)
294 gwlarson 3.4 if (!strncmp(buf, "-av ", 4))
295 gwlarson 3.1 setcolor(ambval, atof(buf+4),
296     atof(sskip2(buf+4,1)),
297     atof(sskip2(buf+4,2)));
298 gwlarson 3.4 else if (backvis && !strncmp(buf, "-bv", 3) &&
299 gwlarson 3.10 (!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL))
300 gwlarson 3.4 backvis = 0;
301 gwlarson 3.1 fclose(fp);
302     unlink(optfile); /* delete options file */
303     }
304    
305    
306     int
307     findvw(nm) /* find named view */
308     register char *nm;
309     {
310     register int n;
311    
312     if (*nm >= '1' & *nm <= '9' &&
313     (n = atoi(nm)-1) <= MAXVIEW && vwl[n].v != NULL)
314     return(n);
315     for (n = 0; vwl[n].v != NULL; n++)
316     if (vwl[n].nam != NULL && !strcmp(nm, vwl[n].nam))
317     return(n);
318     return(-1);
319     }
320    
321    
322     int
323     varmatch(s, vn) /* match line to variable */
324     register char *s, *vn;
325     {
326     register int c;
327    
328     for ( ; *vn && *s == *vn; s++, vn++)
329     ;
330     while (isspace(*s))
331     s++;
332     if (*s == '=')
333     return(*vn);
334     while (!(c = toupper(*s++) - toupper(*vn)) && *vn++)
335     ;
336     return(c);
337     }
338    
339    
340     char *
341     scan4var(buf, buflen, vname, fp) /* scan for variable from fp */
342     char *buf;
343     int buflen;
344     char *vname;
345     FILE *fp;
346     {
347     int cval;
348     register char *cp;
349     /* search out matching line */
350     while ((cval = varmatch(buf, vname))) {
351     if (cval > 0) /* gone too far? */
352     return(NULL);
353     buf[0] = '\0'; /* else get next line */
354     if (fgetline(buf, buflen, fp) == NULL)
355     return(NULL);
356     }
357     /* skip variable name and '=' */
358     for (cp = buf; *cp++ != '='; )
359     ;
360     while (isspace(*cp)) cp++;
361     return(cp);
362     }
363    
364    
365     dev_open(id) /* initialize GLX driver */
366     char *id;
367     {
368     static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,4,
369     GLX_GREEN_SIZE,4, GLX_BLUE_SIZE,4,
370     GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None};
371     XSetWindowAttributes ourwinattr;
372     XWMHints ourxwmhints;
373     /* open display server */
374     ourdisplay = XOpenDisplay(NULL);
375     if (ourdisplay == NULL)
376     error(USER, "cannot open X-windows; DISPLAY variable set?\n");
377     /* find a usable visual */
378     ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest);
379     if (ourvinf == NULL)
380     error(USER, "no suitable visuals available");
381     /* get a context */
382     gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE);
383     /* open window */
384     ourwinattr.background_pixel = ourblack;
385     ourwinattr.border_pixel = ourblack;
386     ourwinattr.event_mask = ourmask;
387     /* this is stupid */
388     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
389     ourvinf->visual, AllocNone);
390     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
391     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
392     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
393     BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual,
394     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
395     if (gwind == 0)
396     error(SYSTEM, "cannot create window\n");
397     XStoreName(ourdisplay, gwind, id);
398 gwlarson 3.4 #ifndef NOSTEREO
399     if (stereo) /* check if stereo working */
400     switch (XSGIQueryStereoMode(ourdisplay, gwind)) {
401     case STEREO_TOP:
402     case STEREO_BOTTOM:
403     break;
404     case STEREO_OFF:
405     error(USER,
406     "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first");
407     case X_STEREO_UNSUPPORTED:
408     error(USER, "stereo not supported on this screen");
409     default:
410     error(INTERNAL, "unknown stereo mode");
411     }
412     #endif
413 gwlarson 3.1 /* set window manager hints */
414     ourxwmhints.flags = InputHint|IconPixmapHint;
415     ourxwmhints.input = True;
416     ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
417     gwind, glradicon_bits, glradicon_width, glradicon_height);
418     XSetWMHints(ourdisplay, gwind, &ourxwmhints);
419     /* set GLX context */
420     glXMakeCurrent(ourdisplay, gwind, gctx);
421     glEnable(GL_DEPTH_TEST);
422     glDepthFunc(GL_LESS);
423     glShadeModel(GL_SMOOTH);
424     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
425 gwlarson 3.7 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
426 gwlarson 3.1 glEnable(GL_LIGHTING);
427 gwlarson 3.9 glFrontFace(GL_CCW);
428     glCullFace(GL_BACK);
429 gwlarson 3.1 if (backvis)
430     glDisable(GL_CULL_FACE);
431 gwlarson 3.9 else
432 gwlarson 3.1 glEnable(GL_CULL_FACE);
433     glDrawBuffer(GL_BACK);
434     /* figure out sensible view */
435     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
436     DisplayWidth(ourdisplay, ourscreen);
437     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
438     DisplayHeight(ourdisplay, ourscreen);
439 gwlarson 3.4 if (stereo) { /* set stereo mode */
440     setstereobuf(STEREO_BUFFER_LEFT);
441     pheight *= 2.;
442     }
443 gwlarson 3.1 /* map the window */
444     XMapWindow(ourdisplay, gwind);
445 gwlarson 3.5 no_render++;
446 gwlarson 3.3 do
447     dev_input(0); /* get resize event */
448     while (hres == 0 & vres == 0);
449 gwlarson 3.5 no_render--;
450 gwlarson 3.1 rgl_checkerr("initializing GLX");
451     }
452    
453    
454     dev_close() /* close our display and free resources */
455     {
456     glXMakeCurrent(ourdisplay, None, NULL);
457     glXDestroyContext(ourdisplay, gctx);
458     XDestroyWindow(ourdisplay, gwind);
459     gwind = 0;
460     XCloseDisplay(ourdisplay);
461     ourdisplay = NULL;
462     }
463    
464    
465     int
466     dev_view(nv) /* assign new driver view */
467     register VIEW *nv;
468     {
469     int newhres = hres, newvres = vres;
470     double wa, va;
471     /* check view legality */
472     if (nv->type != VT_PER) {
473     error(COMMAND, "illegal view type");
474     nv->type = VT_PER;
475     }
476     if (nv->horiz > 160. | nv->vert > 160.) {
477     error(COMMAND, "illegal view angle");
478     if (nv->horiz > 160.)
479     nv->horiz = 160.;
480     if (nv->vert > 160.)
481     nv->vert = 160.;
482     }
483     if (hres != 0 & vres != 0) {
484     wa = (vres*pheight)/(hres*pwidth);
485 gwlarson 3.2 va = viewaspect(nv);
486 gwlarson 3.1 if (va > wa+.05) {
487     newvres = (pwidth/pheight)*va*newhres + .5;
488     if (newvres > maxvres) {
489     newvres = maxvres;
490     newhres = (pheight/pwidth)/va*newvres + .5;
491     }
492     } else if (va < wa-.05) {
493     newhres = (pheight/pwidth)/va*newvres + .5;
494     if (newhres > maxhres) {
495     newhres = maxhres;
496     newvres = (pwidth/pheight)*va*newhres + .5;
497     }
498     }
499     if (newhres != hres | newvres != vres) {
500 gwlarson 3.5 no_render++;
501 gwlarson 3.1 XResizeWindow(ourdisplay, gwind, newhres, newvres);
502     do
503     dev_input(0); /* get resize event */
504     while (newhres != hres | newvres != vres);
505 gwlarson 3.5 no_render--;
506 gwlarson 3.1 }
507     }
508     copystruct(&thisview, nv);
509     setglpersp(&thisview);
510     render();
511     return(1);
512     }
513    
514    
515     int
516     dev_input(nsecs) /* get next input event */
517     int nsecs;
518     {
519     #if 0
520     static time_t lasttime = 0;
521     time_t thistime;
522    
523     if (nsecs > 0) {
524     thistime = time(0);
525     nsecs -= (long)(thistime - lasttime);
526     lasttime = thistime;
527     }
528     if (nsecs > 0)
529     alarm(nsecs);
530     #endif
531     XNextEvent(ourdisplay, levptr(XEvent));
532     switch (levptr(XEvent)->type) {
533     case ConfigureNotify:
534     resizewindow(levptr(XConfigureEvent));
535     break;
536     case UnmapNotify:
537     mapped = 0;
538     break;
539     case MapNotify:
540     mapped = 1;
541     break;
542     case Expose:
543     fixwindow(levptr(XExposeEvent));
544     break;
545     case KeyPress:
546     return(getkey(levptr(XKeyPressedEvent)));
547     case ButtonPress:
548     getmove(levptr(XButtonPressedEvent));
549     break;
550     }
551     return(1);
552     }
553    
554    
555     render() /* render our display list and swap buffers */
556     {
557 gwlarson 3.4 double d;
558    
559 gwlarson 3.5 if (!mapped | no_render)
560 gwlarson 3.1 return;
561     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
562     glCallList(displist);
563 gwlarson 3.4 if (stereo) { /* do right eye for stereo */
564     setstereobuf(STEREO_BUFFER_RIGHT);
565     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
566     glMatrixMode(GL_MODELVIEW);
567     glPushMatrix();
568     d = -eyedist / sqrt(thisview.hn2);
569     glTranslated(d*thisview.hvec[0], d*thisview.hvec[1],
570     d*thisview.hvec[2]);
571     glCallList(displist);
572     glMatrixMode(GL_MODELVIEW);
573     glPopMatrix();
574     setstereobuf(STEREO_BUFFER_LEFT);
575     }
576 gwlarson 3.1 glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */
577     rgl_checkerr("rendering display list");
578     }
579    
580    
581     moveview(dx, dy, mov, orb) /* move our view */
582     int dx, dy, mov, orb;
583     {
584     VIEW nv;
585     FVECT odir, v1, wp;
586     double d;
587     register int li;
588     /* start with old view */
589     copystruct(&nv, &thisview);
590     /* change view direction */
591     if ((d = viewray(v1, odir, &thisview,
592     (dx+.5)/hres, (dy+.5)/vres)) < -FTINY)
593     return(0); /* outside view */
594     if (mov | orb) {
595     if (!getintersect(wp, v1, odir, d))
596     return(0);
597     VSUM(odir, wp, nv.vp, -1.);
598     } else
599     VCOPY(nv.vdir, odir);
600     if (orb && mov) { /* orbit left/right */
601     spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
602     VSUM(nv.vp, wp, odir, -1.);
603     spinvector(nv.vdir, nv.vdir, nv.vup, d);
604     } else if (orb) { /* orbit up/down */
605     fcross(v1, odir, nv.vup);
606     if (normalize(v1) == 0.)
607     return(0);
608     spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
609     VSUM(nv.vp, wp, odir, -1.);
610     spinvector(nv.vdir, nv.vdir, v1, d);
611     } else if (mov) { /* move forward/backward */
612     d = MOVPCT/100. * mov;
613     VSUM(nv.vp, nv.vp, odir, d);
614     }
615     if (!mov ^ !orb && headlocked) { /* restore head height */
616     VSUM(v1, thisview.vp, nv.vp, -1.);
617     d = DOT(v1, thisview.vup);
618     VSUM(nv.vp, nv.vp, thisview.vup, d);
619     }
620     if (setview(&nv) != NULL)
621     return(0); /* illegal view */
622     dev_view(&nv);
623     return(1);
624     }
625    
626    
627 greg 3.13 static
628     waitabit() /* pause a moment */
629     {
630     struct timespec ts;
631     ts.tv_sec = 0;
632     ts.tv_nsec = 5000000;
633     nanosleep(&ts, NULL);
634     }
635    
636    
637 gwlarson 3.1 getmove(ebut) /* get view change */
638     XButtonPressedEvent *ebut;
639     {
640     int movdir = MOVDIR(ebut->button);
641     int movorb = MOVORB(ebut->state);
642     int moved = 0;
643     Window rootw, childw;
644     int rootx, rooty, wx, wy;
645     unsigned int statemask;
646    
647 gwlarson 3.3 copylastv( movorb ? (movdir ? "left/right" : "up/down") :
648     (movdir ? "fore/back" : "rotate") );
649 gwlarson 3.1 XNoOp(ourdisplay);
650    
651     while (!XCheckMaskEvent(ourdisplay,
652     ButtonReleaseMask, levptr(XEvent))) {
653 greg 3.13 /* pause so as not to move too fast */
654     waitabit();
655 gwlarson 3.1
656     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
657     &rootx, &rooty, &wx, &wy, &statemask))
658     break; /* on another screen */
659    
660     if (!moveview(wx, vres-1-wy, movdir, movorb)) {
661     sleep(1);
662     continue;
663     } else
664     moved++;
665     }
666     if (!moved) { /* do final motion */
667     movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
668     wx = levptr(XButtonReleasedEvent)->x;
669     wy = levptr(XButtonReleasedEvent)->y;
670     moveview(wx, vres-1-wy, movdir, movorb);
671     }
672     }
673    
674    
675     getintersect(wp, org, dir, md) /* intersect ray with scene geometry */
676     FVECT wp; /* returned world intersection point */
677     FVECT org, dir;
678     double md;
679     {
680     float fbuf[6];
681     /* check to see if rtrace is running */
682 schorsch 3.15 /* if (rtpd.pid <= 0) */
683     if (!rtpd.running)
684 gwlarson 3.1 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 schorsch 3.15 if (process(&rtpd, (char *)fbuf, (char *)fbuf,
692 greg 3.13 4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float))
693 gwlarson 3.1 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 gwlarson 3.2 zmin = 0.1;
707     zmax = 1000.;
708 gwlarson 3.1 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 gwlarson 3.2 if (lastview.type) {
752 gwlarson 3.1 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 gwlarson 3.4 copylastv("standard view");
824 gwlarson 3.1 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 schorsch 3.16 if (!memcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) {
835 gwlarson 3.1 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 gwlarson 3.2 static char *lastvc;
865    
866 gwlarson 3.1 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 gwlarson 3.5 if (no_render)
897 gwlarson 3.2 return;
898 gwlarson 3.1 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     }