ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glrad.c
Revision: 3.29
Committed: Tue Jun 3 21:31:51 2025 UTC (5 days, 7 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.28: +3 -4 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

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