ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.18
Committed: Fri Jan 2 12:51:54 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.17: +136 -73 lines
Log Message:
Ansification.

File Contents

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