1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: glrad.c,v 3.19 2004/03/26 23:34:23 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Program to display Radiance scene using OpenGL. |
6 |
*/ |
7 |
|
8 |
#include <sys/types.h> |
9 |
#include <GL/glx.h> |
10 |
#ifndef NOSTEREO |
11 |
#include <X11/extensions/SGIStereo.h> |
12 |
#endif |
13 |
#include <ctype.h> |
14 |
#include <string.h> |
15 |
#include <time.h> |
16 |
|
17 |
#include "radogl.h" |
18 |
#include "view.h" |
19 |
#include "paths.h" |
20 |
#include "glradicon.h" |
21 |
#include "rtprocess.h" |
22 |
|
23 |
#ifndef MAXVIEW |
24 |
#define MAXVIEW 63 /* maximum number of standard views */ |
25 |
#endif |
26 |
#ifndef MAXSCENE |
27 |
#define MAXSCENE 127 /* maximum number of scene files */ |
28 |
#endif |
29 |
|
30 |
#define ZOOMPCT 9 /* percent to zoom 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 *)¤tevent) |
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 |
double eyedist = 1; /* interocular distance */ |
70 |
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 |
SUBPROC rtpd; /* rtrace process descriptors */ |
79 |
|
80 |
int silent = 0; /* run rad silently? */ |
81 |
int backvis = 1; /* back faces visible? */ |
82 |
int stereo = 0; /* do stereo? */ |
83 |
|
84 |
#ifdef NOSTEREO |
85 |
#define setstereobuf(bid) |
86 |
#else |
87 |
#define setstereobuf(bid) (glXWaitGL(), \ |
88 |
XSGISetStereoBuffer(ourdisplay, gwind, bid), \ |
89 |
glXWaitX()) |
90 |
#endif |
91 |
|
92 |
int displist; /* our scene display list */ |
93 |
|
94 |
int no_render = 0; /* don't rerender */ |
95 |
|
96 |
extern int nowarn; /* turn warnings off? */ |
97 |
|
98 |
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 |
|
121 |
|
122 |
int |
123 |
main( |
124 |
int argc, |
125 |
char *argv[] |
126 |
) |
127 |
{ |
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 |
case 's': |
142 |
silent = !silent; |
143 |
break; |
144 |
case 'S': |
145 |
stereo = !stereo; |
146 |
break; |
147 |
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 |
#ifdef NOSTEREO |
159 |
if (stereo) |
160 |
error(INTERNAL, "stereo not supported in this version"); |
161 |
#endif |
162 |
/* run rad and get views */ |
163 |
runrad(argc-i, argv+i); |
164 |
/* check view */ |
165 |
if (viewsel != NULL) { |
166 |
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 |
} |
181 |
/* open GL */ |
182 |
dev_open(radfile = argv[i]); |
183 |
/* load octree or scene files */ |
184 |
if (octree != NULL) { |
185 |
displist = rgl_octlist(octree, NULL, NULL, NULL); |
186 |
startrtrace(octree); |
187 |
} else |
188 |
displist = rgl_filelist(nscenef, scene, NULL); |
189 |
/* set initial view */ |
190 |
dev_view(currentview < 0 ? &thisview : vwl[currentview].v); |
191 |
/* input/render loop */ |
192 |
while (dev_input(vwintvl)) |
193 |
; |
194 |
/* all done */ |
195 |
quit(0); |
196 |
userr: |
197 |
fprintf(stderr, |
198 |
"Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n", |
199 |
argv[0]); |
200 |
quit(1); |
201 |
return 1; /* pro forma return */ |
202 |
} |
203 |
|
204 |
|
205 |
void |
206 |
quit( /* exit gracefully */ |
207 |
int code |
208 |
) |
209 |
{ |
210 |
if (ourdisplay != NULL) |
211 |
dev_close(); |
212 |
/* if (rtpd.pid > 0) { */ |
213 |
if (rtpd.running) { |
214 |
if (close_process(&rtpd) > 0) |
215 |
wputs("bad exit status from rtrace\n"); |
216 |
/* rtpd.pid = 0; */ |
217 |
} |
218 |
exit(code); |
219 |
} |
220 |
|
221 |
|
222 |
static void |
223 |
startrtrace( /* start rtrace on octname */ |
224 |
char *octname |
225 |
) |
226 |
{ |
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 |
if (open_process(&rtpd, av) <= 0) |
235 |
error(SYSTEM, "cannot start rtrace process"); |
236 |
} |
237 |
|
238 |
|
239 |
static void |
240 |
runrad( /* run rad and load variables */ |
241 |
int ac, |
242 |
char **av |
243 |
) |
244 |
{ |
245 |
static char optfile[] = TEMPLATE; |
246 |
int nvn = 0, nvv = 0; |
247 |
FILE *fp; |
248 |
register char *cp; |
249 |
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 |
if (silent) { |
254 |
strcpy(cp, "-s "); |
255 |
cp += 3; |
256 |
} |
257 |
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 |
if ((*cp == '-') | (*cp == '+')) |
276 |
expval = pow(2., expval); |
277 |
expval *= 0.5; /* compensate for local shading */ |
278 |
} |
279 |
/* look for eye separation */ |
280 |
if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL) |
281 |
eyedist = atof(cp); |
282 |
/* look for materials */ |
283 |
while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) { |
284 |
nscenef += wordstring(scene+nscenef, cp); |
285 |
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 |
nscenef += wordstring(scene+nscenef, cp); |
293 |
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 |
*(vwl[nvv].v) = stdview; |
308 |
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 |
/* get relevant options */ |
323 |
while (fgets(buf, sizeof(buf), fp) != NULL) |
324 |
if (!strncmp(buf, "-av ", 4)) |
325 |
setcolor(ambval, atof(buf+4), |
326 |
atof(sskip2(buf+4,1)), |
327 |
atof(sskip2(buf+4,2))); |
328 |
else if (backvis && !strncmp(buf, "-bv", 3) && |
329 |
(!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL)) |
330 |
backvis = 0; |
331 |
fclose(fp); |
332 |
unlink(optfile); /* delete options file */ |
333 |
} |
334 |
|
335 |
|
336 |
static int |
337 |
findvw( /* find named view */ |
338 |
register char *nm |
339 |
) |
340 |
{ |
341 |
register int n; |
342 |
|
343 |
if ((*nm >= '1') & (*nm <= '9') && |
344 |
(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 |
static int |
354 |
varmatch( /* match line to variable */ |
355 |
register char *s, |
356 |
register char *vn |
357 |
) |
358 |
{ |
359 |
register int c; |
360 |
|
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 |
static char * |
374 |
scan4var( /* scan for variable from fp */ |
375 |
char *buf, |
376 |
int buflen, |
377 |
char *vname, |
378 |
FILE *fp |
379 |
) |
380 |
{ |
381 |
int cval; |
382 |
register char *cp; |
383 |
/* 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 |
static void |
400 |
dev_open( /* initialize GLX driver */ |
401 |
char *id |
402 |
) |
403 |
{ |
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 |
#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 |
/* set window manager hints */ |
450 |
ourxwmhints.flags = InputHint|IconPixmapHint; |
451 |
ourxwmhints.input = True; |
452 |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, gwind, |
453 |
(char *)glradicon_bits, glradicon_width, glradicon_height); |
454 |
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 |
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
462 |
glEnable(GL_LIGHTING); |
463 |
glFrontFace(GL_CCW); |
464 |
glCullFace(GL_BACK); |
465 |
if (backvis) |
466 |
glDisable(GL_CULL_FACE); |
467 |
else |
468 |
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 |
if (stereo) { /* set stereo mode */ |
476 |
setstereobuf(STEREO_BUFFER_LEFT); |
477 |
pheight *= 2.; |
478 |
} |
479 |
/* map the window */ |
480 |
XMapWindow(ourdisplay, gwind); |
481 |
no_render++; |
482 |
do |
483 |
dev_input(0); /* get resize event */ |
484 |
while ((hres == 0) & (vres == 0)); |
485 |
no_render--; |
486 |
rgl_checkerr("initializing GLX"); |
487 |
} |
488 |
|
489 |
|
490 |
static void |
491 |
dev_close(void) /* close our display and free resources */ |
492 |
{ |
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 |
static int |
503 |
dev_view( /* assign new driver view */ |
504 |
register VIEW *nv |
505 |
) |
506 |
{ |
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 |
if ((nv->horiz > 160.) | (nv->vert > 160.)) { |
515 |
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 |
if ((hres != 0) & (vres != 0)) { |
522 |
wa = (vres*pheight)/(hres*pwidth); |
523 |
va = viewaspect(nv); |
524 |
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 |
if ((newhres != hres) | (newvres != vres)) { |
538 |
no_render++; |
539 |
XResizeWindow(ourdisplay, gwind, newhres, newvres); |
540 |
do |
541 |
dev_input(0); /* get resize event */ |
542 |
while ((newhres != hres) | (newvres != vres)); |
543 |
no_render--; |
544 |
} |
545 |
} |
546 |
thisview = *nv; |
547 |
setglpersp(&thisview); |
548 |
render(); |
549 |
return(1); |
550 |
} |
551 |
|
552 |
|
553 |
static int |
554 |
dev_input( /* get next input event */ |
555 |
int nsecs |
556 |
) |
557 |
{ |
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 |
getmove(levptr(XButtonPressedEvent)); |
588 |
break; |
589 |
} |
590 |
return(1); |
591 |
} |
592 |
|
593 |
|
594 |
static void |
595 |
render(void) /* render our display list and swap buffers */ |
596 |
{ |
597 |
double d; |
598 |
|
599 |
if (!mapped | no_render) |
600 |
return; |
601 |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
602 |
glCallList(displist); |
603 |
if (stereo) { /* do right eye for stereo */ |
604 |
setstereobuf(STEREO_BUFFER_RIGHT); |
605 |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
606 |
glMatrixMode(GL_MODELVIEW); |
607 |
glPushMatrix(); |
608 |
d = -eyedist / sqrt(thisview.hn2); |
609 |
glTranslated(d*thisview.hvec[0], d*thisview.hvec[1], |
610 |
d*thisview.hvec[2]); |
611 |
glCallList(displist); |
612 |
glMatrixMode(GL_MODELVIEW); |
613 |
glPopMatrix(); |
614 |
setstereobuf(STEREO_BUFFER_LEFT); |
615 |
} |
616 |
glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */ |
617 |
rgl_checkerr("rendering display list"); |
618 |
} |
619 |
|
620 |
|
621 |
static int |
622 |
moveview( /* move our view */ |
623 |
int dx, |
624 |
int dy, |
625 |
int mov, |
626 |
int orb |
627 |
) |
628 |
{ |
629 |
VIEW nv; |
630 |
FVECT odir, v1, wp; |
631 |
double d; |
632 |
/* start with old view */ |
633 |
nv = thisview; |
634 |
/* change view direction */ |
635 |
if ((d = viewray(v1, odir, &thisview, |
636 |
(dx+.5)/hres, (dy+.5)/vres)) < -FTINY) |
637 |
return(0); /* outside view */ |
638 |
if (mov | orb) { |
639 |
if (!getintersect(wp, v1, odir, d)) |
640 |
return(0); |
641 |
VSUM(odir, wp, nv.vp, -1.); |
642 |
} else |
643 |
VCOPY(nv.vdir, odir); |
644 |
if (orb && mov) { /* orbit left/right */ |
645 |
spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); |
646 |
VSUM(nv.vp, wp, odir, -1.); |
647 |
spinvector(nv.vdir, nv.vdir, nv.vup, d); |
648 |
} else if (orb) { /* orbit up/down */ |
649 |
fcross(v1, odir, nv.vup); |
650 |
if (normalize(v1) == 0.) |
651 |
return(0); |
652 |
spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); |
653 |
VSUM(nv.vp, wp, odir, -1.); |
654 |
spinvector(nv.vdir, nv.vdir, v1, d); |
655 |
} else if (mov) { /* move forward/backward */ |
656 |
d = MOVPCT/100. * mov; |
657 |
VSUM(nv.vp, nv.vp, odir, d); |
658 |
} |
659 |
if (!mov ^ !orb && headlocked) { /* restore head height */ |
660 |
VSUM(v1, thisview.vp, nv.vp, -1.); |
661 |
d = DOT(v1, thisview.vup); |
662 |
VSUM(nv.vp, nv.vp, thisview.vup, d); |
663 |
} |
664 |
if (setview(&nv) != NULL) |
665 |
return(0); /* illegal view */ |
666 |
dev_view(&nv); |
667 |
return(1); |
668 |
} |
669 |
|
670 |
|
671 |
static void |
672 |
waitabit(void) /* pause a moment */ |
673 |
{ |
674 |
struct timespec ts; |
675 |
ts.tv_sec = 0; |
676 |
ts.tv_nsec = 5000000; |
677 |
nanosleep(&ts, NULL); |
678 |
} |
679 |
|
680 |
|
681 |
static void |
682 |
getmove( /* get view change */ |
683 |
XButtonPressedEvent *ebut |
684 |
) |
685 |
{ |
686 |
int movdir = MOVDIR(ebut->button); |
687 |
int movorb = MOVORB(ebut->state); |
688 |
int moved = 0; |
689 |
Window rootw, childw; |
690 |
int rootx, rooty, wx, wy; |
691 |
unsigned int statemask; |
692 |
|
693 |
copylastv( movorb ? (movdir ? "left/right" : "up/down") : |
694 |
(movdir ? "fore/back" : "rotate") ); |
695 |
XNoOp(ourdisplay); |
696 |
|
697 |
while (!XCheckMaskEvent(ourdisplay, |
698 |
ButtonReleaseMask, levptr(XEvent))) { |
699 |
/* pause so as not to move too fast */ |
700 |
waitabit(); |
701 |
|
702 |
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
703 |
&rootx, &rooty, &wx, &wy, &statemask)) |
704 |
break; /* on another screen */ |
705 |
|
706 |
if (!moveview(wx, vres-1-wy, movdir, movorb)) { |
707 |
sleep(1); |
708 |
continue; |
709 |
} else |
710 |
moved++; |
711 |
} |
712 |
if (!moved) { /* do final motion */ |
713 |
movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); |
714 |
wx = levptr(XButtonReleasedEvent)->x; |
715 |
wy = levptr(XButtonReleasedEvent)->y; |
716 |
moveview(wx, vres-1-wy, movdir, movorb); |
717 |
} |
718 |
} |
719 |
|
720 |
|
721 |
static int |
722 |
getintersect( /* intersect ray with scene geometry */ |
723 |
FVECT wp, /* returned world intersection point */ |
724 |
FVECT org, |
725 |
FVECT dir, |
726 |
double md |
727 |
) |
728 |
{ |
729 |
float fbuf[6]; |
730 |
/* check to see if rtrace is running */ |
731 |
/* if (rtpd.pid <= 0) */ |
732 |
if (!rtpd.running) |
733 |
return(0); |
734 |
/* assign origin */ |
735 |
fbuf[0] = org[0]; fbuf[1] = org[1]; fbuf[2] = org[2]; |
736 |
/* compute clipping distance */ |
737 |
if (md <= FTINY) md = FHUGE; |
738 |
fbuf[3] = dir[0]*md; fbuf[4] = dir[1]*md; fbuf[5] = dir[2]*md; |
739 |
/* trace that ray */ |
740 |
if (process(&rtpd, (char *)fbuf, (char *)fbuf, |
741 |
4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float)) |
742 |
error(INTERNAL, "error getting data back from rtrace process"); |
743 |
if (fbuf[3] >= .99*FHUGE) |
744 |
return(0); /* missed local objects */ |
745 |
wp[0] = fbuf[0]; wp[1] = fbuf[1]; wp[2] = fbuf[2]; |
746 |
return(1); /* else return world intersection */ |
747 |
} |
748 |
|
749 |
|
750 |
static void |
751 |
setglpersp( /* set perspective view in GL */ |
752 |
register VIEW *vp |
753 |
) |
754 |
{ |
755 |
double d, xmin, xmax, ymin, ymax, zmin, zmax; |
756 |
|
757 |
zmin = 0.1; |
758 |
zmax = 1000.; |
759 |
if (thisview.vfore > FTINY) |
760 |
zmin = thisview.vfore; |
761 |
if (thisview.vaft > FTINY) |
762 |
zmax = thisview.vaft; |
763 |
xmax = zmin * tan(PI/180./2. * thisview.horiz); |
764 |
xmin = -xmax; |
765 |
d = thisview.hoff * (xmax - xmin); |
766 |
xmin += d; xmax += d; |
767 |
ymax = zmin * tan(PI/180./2. * thisview.vert); |
768 |
ymin = -ymax; |
769 |
d = thisview.voff * (ymax - ymin); |
770 |
ymin += d; ymax += d; |
771 |
/* set view matrix */ |
772 |
glMatrixMode(GL_PROJECTION); |
773 |
glLoadIdentity(); |
774 |
glFrustum(xmin, xmax, ymin, ymax, zmin, zmax); |
775 |
gluLookAt(thisview.vp[0], thisview.vp[1], thisview.vp[2], |
776 |
thisview.vp[0] + thisview.vdir[0], |
777 |
thisview.vp[1] + thisview.vdir[1], |
778 |
thisview.vp[2] + thisview.vdir[2], |
779 |
thisview.vup[0], thisview.vup[1], thisview.vup[2]); |
780 |
rgl_checkerr("setting perspective view"); |
781 |
} |
782 |
|
783 |
|
784 |
static int |
785 |
getkey( /* get input key */ |
786 |
register XKeyPressedEvent *ekey |
787 |
) |
788 |
{ |
789 |
int n; |
790 |
char buf[8]; |
791 |
|
792 |
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
793 |
if (n != 1) |
794 |
return(1); |
795 |
switch (buf[0]) { |
796 |
case 'h': /* turn on height motion lock */ |
797 |
headlocked = 1; |
798 |
break; |
799 |
case 'H': /* turn off height motion lock */ |
800 |
headlocked = 0; |
801 |
break; |
802 |
case 'l': /* retrieve last (premouse) view */ |
803 |
if (lastview.type) { |
804 |
VIEW vtmp; |
805 |
vtmp = thisview; |
806 |
dev_view(&lastview); |
807 |
lastview = vtmp; |
808 |
} else |
809 |
XBell(ourdisplay, 0); |
810 |
break; |
811 |
case 'n': /* move to next standard view */ |
812 |
gotoview(currentview+1); |
813 |
break; |
814 |
case 'p': /* move to last standard view */ |
815 |
gotoview(currentview-1); |
816 |
break; |
817 |
case '+': /* zoom in */ |
818 |
zoomview(100+ZOOMPCT, ekey->x, vres-1-ekey->y); |
819 |
break; |
820 |
case '-': /* zoom out */ |
821 |
zoomview(100-ZOOMPCT, ekey->x, vres-1-ekey->y); |
822 |
break; |
823 |
case 'v': /* spit current view to stdout */ |
824 |
fputs(VIEWSTR, stdout); |
825 |
fprintview(&thisview, stdout); |
826 |
fputc('\n', stdout); |
827 |
break; |
828 |
case 'V': /* append view to rad file */ |
829 |
appendview(NULL, &thisview); |
830 |
break; |
831 |
case 'q': /* quit the program */ |
832 |
return(0); |
833 |
default: |
834 |
XBell(ourdisplay, 0); |
835 |
break; |
836 |
} |
837 |
return(1); |
838 |
} |
839 |
|
840 |
|
841 |
static void |
842 |
zoomview( /* zoom in or out around (dx,dy) */ |
843 |
int pct, |
844 |
int dx, |
845 |
int dy |
846 |
) |
847 |
{ |
848 |
double h, v; |
849 |
|
850 |
if ((pct == 100) | (pct <= 0)) |
851 |
return; |
852 |
copylastv("zooming"); |
853 |
h = (dx+.5)/hres - 0.5; |
854 |
v = (dy+.5)/vres - 0.5; |
855 |
h *= (1. - 100./pct); |
856 |
v *= (1. - 100./pct); |
857 |
thisview.vdir[0] += h*thisview.hvec[0] + v*thisview.vvec[0]; |
858 |
thisview.vdir[1] += h*thisview.hvec[1] + v*thisview.vvec[1]; |
859 |
thisview.vdir[2] += h*thisview.hvec[2] + v*thisview.vvec[2]; |
860 |
thisview.horiz = 2.*180./PI * atan( 100./pct * |
861 |
tan(PI/180./2.*thisview.horiz) ); |
862 |
thisview.vert = 2.*180./PI * atan( 100./pct * |
863 |
tan(PI/180./2.*thisview.vert) ); |
864 |
setview(&thisview); |
865 |
dev_view(&thisview); |
866 |
} |
867 |
|
868 |
|
869 |
static void |
870 |
gotoview( /* go to specified view number */ |
871 |
int vwnum |
872 |
) |
873 |
{ |
874 |
if (vwnum < 0) |
875 |
for (vwnum = currentview; vwl[vwnum+1].v != NULL; vwnum++) |
876 |
; |
877 |
else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL) |
878 |
vwnum = 0; |
879 |
copylastv("standard view"); |
880 |
dev_view(vwl[currentview=vwnum].v); |
881 |
} |
882 |
|
883 |
|
884 |
static void |
885 |
appendview( /* append standard view */ |
886 |
char *nm, |
887 |
VIEW *vp |
888 |
) |
889 |
{ |
890 |
FILE *fp; |
891 |
/* check if already in there */ |
892 |
if (!memcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) { |
893 |
error(COMMAND, "view already in standard list"); |
894 |
return; |
895 |
} |
896 |
/* append to file */ |
897 |
if ((fp = fopen(radfile, "a")) == NULL) { |
898 |
error(COMMAND, "cannot append rad input file"); |
899 |
return; |
900 |
} |
901 |
fputs("view=", fp); |
902 |
if (nm != NULL) { |
903 |
fputc(' ', fp); fputs(nm, fp); |
904 |
} |
905 |
fprintview(vp, fp); fputc('\n', fp); |
906 |
fclose(fp); |
907 |
/* append to our list */ |
908 |
while (vwl[currentview].v != NULL) |
909 |
currentview++; |
910 |
if (currentview >= MAXVIEW) |
911 |
error(INTERNAL, "too many views in appendview"); |
912 |
vwl[currentview].v = (VIEW *)bmalloc(sizeof(VIEW)); |
913 |
*(vwl[currentview].v) = thisview; |
914 |
if (nm != NULL) |
915 |
vwl[currentview].nam = savqstr(nm); |
916 |
} |
917 |
|
918 |
|
919 |
static void |
920 |
copylastv( /* copy last view position */ |
921 |
char *cause |
922 |
) |
923 |
{ |
924 |
static char *lastvc; |
925 |
|
926 |
if (cause == lastvc) |
927 |
return; /* only record one view per cause */ |
928 |
lastvc = cause; |
929 |
lastview = thisview; |
930 |
} |
931 |
|
932 |
|
933 |
static void |
934 |
fixwindow( /* repair damage to window */ |
935 |
register XExposeEvent *eexp |
936 |
) |
937 |
{ |
938 |
if ((hres == 0) | (vres == 0)) { /* first exposure */ |
939 |
resizewindow((XConfigureEvent *)eexp); |
940 |
return; |
941 |
} |
942 |
if (eexp->count) /* wait for final exposure */ |
943 |
return; |
944 |
/* rerender everything */ |
945 |
render(); |
946 |
} |
947 |
|
948 |
|
949 |
static void |
950 |
resizewindow( /* resize window */ |
951 |
register XConfigureEvent *ersz |
952 |
) |
953 |
{ |
954 |
static char resizing[] = "resizing window"; |
955 |
double wa, va; |
956 |
|
957 |
glViewport(0, 0, hres=ersz->width, vres=ersz->height); |
958 |
if (hres > maxhres) maxhres = hres; |
959 |
if (vres > maxvres) maxvres = vres; |
960 |
if (no_render) |
961 |
return; |
962 |
wa = (vres*pheight)/(hres*pwidth); |
963 |
va = viewaspect(&thisview); |
964 |
if (va > wa+.05) { |
965 |
copylastv(resizing); |
966 |
thisview.vert = 2.*180./PI * |
967 |
atan( tan(PI/180./2. * thisview.horiz) * wa ); |
968 |
} else if (va < wa-.05) { |
969 |
copylastv(resizing); |
970 |
thisview.horiz = 2.*180./PI * |
971 |
atan( tan(PI/180./2. * thisview.vert) / wa ); |
972 |
} else |
973 |
return; |
974 |
setview(&thisview); |
975 |
dev_view(&thisview); |
976 |
} |