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