1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Program to display Radiance scene using OpenGL. |
6 |
|
*/ |
7 |
|
|
11 |
– |
#include "radogl.h" |
12 |
– |
#include "view.h" |
13 |
– |
#include "paths.h" |
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 */ |
34 |
|
#define MOVDEG (-5) /* degrees to orbit CW/down /frame */ |
35 |
|
#define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) |
36 |
|
|
33 |
– |
#define MINWIDTH 480 /* minimum graphics window width */ |
34 |
– |
#define MINHEIGHT 400 /* minimum graphics window height */ |
35 |
– |
|
37 |
|
#define BORWIDTH 5 /* border width */ |
38 |
|
|
39 |
|
#define ourscreen DefaultScreen(ourdisplay) |
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 */ |
69 |
– |
char *lastvc = NULL; /* cause of last view change */ |
71 |
|
|
72 |
|
char *progname; /* global argv[0] */ |
73 |
|
char *radfile; /* rad input file */ |
75 |
|
int nscenef = 0; /* number of scene files */ |
76 |
|
char *octree; /* octree name (NULL if unnec.) */ |
77 |
|
|
78 |
< |
int rtpd[3]; /* rtrace process descriptors */ |
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) 0 |
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 |
< |
extern char *fgets(), *fgetline(), *atos(), *scan4var(); |
94 |
> |
int no_render = 0; /* don't rerender */ |
95 |
> |
|
96 |
> |
extern char *fgetline(), *atos(), *scan4var(); |
97 |
|
extern int nowarn; /* turn warnings off? */ |
85 |
– |
extern time_t time(); |
98 |
|
|
99 |
|
|
100 |
|
main(argc, argv) |
114 |
|
case 'w': |
115 |
|
nowarn = !nowarn; |
116 |
|
break; |
117 |
+ |
case 's': |
118 |
+ |
silent = !silent; |
119 |
+ |
break; |
120 |
+ |
case 'S': |
121 |
+ |
stereo = !stereo; |
122 |
+ |
break; |
123 |
|
case 'c': |
124 |
|
vwintvl = atoi(argv[++i]); |
125 |
|
break; |
131 |
|
} |
132 |
|
if (i >= argc) |
133 |
|
goto userr; |
134 |
+ |
#ifdef NOSTEREO |
135 |
+ |
if (stereo) |
136 |
+ |
error(INTERNAL, "stereo not supported in this version"); |
137 |
+ |
#endif |
138 |
|
/* run rad and get views */ |
139 |
|
runrad(argc-i, argv+i); |
140 |
|
/* check view */ |
141 |
< |
if (viewsel != NULL && (currentview = findvw(viewsel)) < 0) { |
142 |
< |
fprintf(stderr, "%s: no such view\n", viewsel); |
143 |
< |
quit(1); |
144 |
< |
} |
141 |
> |
if (viewsel != NULL) |
142 |
> |
if (viewsel[0] == '-') { |
143 |
> |
char *ve = viewsel; |
144 |
> |
if (!sscanview(&thisview, viewsel) || |
145 |
> |
(ve = setview(&thisview)) != NULL) { |
146 |
> |
fprintf(stderr, "%s: bad view: %s\n", |
147 |
> |
progname, ve); |
148 |
> |
quit(1); |
149 |
> |
} |
150 |
> |
currentview = -1; |
151 |
> |
} else if ((currentview = findvw(viewsel)) < 0) { |
152 |
> |
fprintf(stderr, "%s: no such view: %s\n", |
153 |
> |
progname, viewsel); |
154 |
> |
quit(1); |
155 |
> |
} |
156 |
|
/* open GL */ |
157 |
|
dev_open(radfile = argv[i]); |
158 |
|
/* load octree or scene files */ |
159 |
|
if (octree != NULL) { |
160 |
< |
displist = rgl_octlist(octree, NULL, NULL); |
160 |
> |
displist = rgl_octlist(octree, NULL, NULL, NULL); |
161 |
|
startrtrace(octree); |
162 |
|
} else |
163 |
< |
displist = rgl_filelist(nscenef, scene); |
163 |
> |
displist = rgl_filelist(nscenef, scene, NULL); |
164 |
|
/* set initial view */ |
165 |
< |
dev_view(vwl[currentview].v); |
165 |
> |
dev_view(currentview < 0 ? &thisview : vwl[currentview].v); |
166 |
|
/* input/render loop */ |
167 |
|
while (dev_input(vwintvl)) |
168 |
|
; |
169 |
|
/* all done */ |
170 |
|
quit(0); |
171 |
|
userr: |
172 |
< |
fprintf(stderr, "Usage: %s [-w][-c #secs][-v view] rfile [VAR=value]..\n", |
172 |
> |
fprintf(stderr, |
173 |
> |
"Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n", |
174 |
|
argv[0]); |
175 |
|
quit(1); |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
+ |
void |
180 |
|
quit(code) /* exit gracefully */ |
181 |
|
int code; |
182 |
|
{ |
183 |
|
if (ourdisplay != NULL) |
184 |
|
dev_close(); |
185 |
< |
if (rtpd[2] > 0) { |
186 |
< |
if (close_process(rtpd) > 0) |
185 |
> |
/* if (rtpd.pid > 0) { */ |
186 |
> |
if (rtpd.running) { |
187 |
> |
if (close_process(&rtpd) > 0) |
188 |
|
wputs("bad exit status from rtrace\n"); |
189 |
< |
rtpd[2] = 0; |
189 |
> |
/* rtpd.pid = 0; */ |
190 |
|
} |
191 |
|
exit(code); |
192 |
|
} |
202 |
|
if (nowarn) av[ac++] = "-w-"; |
203 |
|
av[ac++] = octname; |
204 |
|
av[ac] = NULL; |
205 |
< |
if (open_process(rtpd, av) <= 0) |
205 |
> |
if (open_process(&rtpd, av) <= 0) |
206 |
|
error(SYSTEM, "cannot start rtrace process"); |
207 |
|
} |
208 |
|
|
220 |
|
/* set rad commmand */ |
221 |
|
strcpy(radcomm, "rad -w -v 0 "); /* look out below! */ |
222 |
|
cp = radcomm + 19; |
223 |
+ |
if (silent) { |
224 |
+ |
strcpy(cp, "-s "); |
225 |
+ |
cp += 3; |
226 |
+ |
} |
227 |
|
while (ac--) { |
228 |
|
strcpy(cp, *av++); |
229 |
|
while (*cp) cp++; |
244 |
|
expval = atof(cp); |
245 |
|
if (*cp == '-' | *cp == '+') |
246 |
|
expval = pow(2., expval); |
247 |
+ |
expval *= 0.5; /* compensate for local shading */ |
248 |
|
} |
249 |
+ |
/* look for eye separation */ |
250 |
+ |
if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL) |
251 |
+ |
eyedist = atof(cp); |
252 |
|
/* look for materials */ |
253 |
|
while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) { |
254 |
|
nscenef += wordstring(scene+nscenef, cp); |
289 |
|
/* open options file */ |
290 |
|
if ((fp = fopen(optfile, "r")) == NULL) |
291 |
|
error(SYSTEM, "cannot open options file"); |
292 |
< |
/* get ambient value */ |
292 |
> |
/* get relevant options */ |
293 |
|
while (fgets(buf, sizeof(buf), fp) != NULL) |
294 |
< |
if (!strncmp(buf, "-av ", 4)) { |
294 |
> |
if (!strncmp(buf, "-av ", 4)) |
295 |
|
setcolor(ambval, atof(buf+4), |
296 |
|
atof(sskip2(buf+4,1)), |
297 |
|
atof(sskip2(buf+4,2))); |
298 |
< |
break; |
299 |
< |
} |
298 |
> |
else if (backvis && !strncmp(buf, "-bv", 3) && |
299 |
> |
(!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL)) |
300 |
> |
backvis = 0; |
301 |
|
fclose(fp); |
302 |
|
unlink(optfile); /* delete options file */ |
303 |
|
} |
370 |
|
GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None}; |
371 |
|
XSetWindowAttributes ourwinattr; |
372 |
|
XWMHints ourxwmhints; |
328 |
– |
XSizeHints oursizhints; |
373 |
|
/* open display server */ |
374 |
|
ourdisplay = XOpenDisplay(NULL); |
375 |
|
if (ourdisplay == NULL) |
395 |
|
if (gwind == 0) |
396 |
|
error(SYSTEM, "cannot create window\n"); |
397 |
|
XStoreName(ourdisplay, gwind, id); |
398 |
+ |
#ifndef NOSTEREO |
399 |
+ |
if (stereo) /* check if stereo working */ |
400 |
+ |
switch (XSGIQueryStereoMode(ourdisplay, gwind)) { |
401 |
+ |
case STEREO_TOP: |
402 |
+ |
case STEREO_BOTTOM: |
403 |
+ |
break; |
404 |
+ |
case STEREO_OFF: |
405 |
+ |
error(USER, |
406 |
+ |
"wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first"); |
407 |
+ |
case X_STEREO_UNSUPPORTED: |
408 |
+ |
error(USER, "stereo not supported on this screen"); |
409 |
+ |
default: |
410 |
+ |
error(INTERNAL, "unknown stereo mode"); |
411 |
+ |
} |
412 |
+ |
#endif |
413 |
|
/* set window manager hints */ |
414 |
|
ourxwmhints.flags = InputHint|IconPixmapHint; |
415 |
|
ourxwmhints.input = True; |
416 |
|
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, |
417 |
|
gwind, glradicon_bits, glradicon_width, glradicon_height); |
418 |
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
360 |
– |
oursizhints.min_width = MINWIDTH; |
361 |
– |
oursizhints.min_height = MINHEIGHT; |
362 |
– |
oursizhints.flags = PMinSize; |
363 |
– |
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
419 |
|
/* set GLX context */ |
420 |
|
glXMakeCurrent(ourdisplay, gwind, gctx); |
421 |
|
glEnable(GL_DEPTH_TEST); |
422 |
|
glDepthFunc(GL_LESS); |
423 |
|
glShadeModel(GL_SMOOTH); |
424 |
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
425 |
< |
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
425 |
> |
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
426 |
|
glEnable(GL_LIGHTING); |
427 |
+ |
glFrontFace(GL_CCW); |
428 |
+ |
glCullFace(GL_BACK); |
429 |
|
if (backvis) |
430 |
|
glDisable(GL_CULL_FACE); |
431 |
< |
else { |
375 |
< |
glFrontFace(GL_CCW); |
376 |
< |
glCullFace(GL_BACK); |
431 |
> |
else |
432 |
|
glEnable(GL_CULL_FACE); |
378 |
– |
} |
433 |
|
glDrawBuffer(GL_BACK); |
434 |
|
/* figure out sensible view */ |
435 |
|
pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / |
436 |
|
DisplayWidth(ourdisplay, ourscreen); |
437 |
|
pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / |
438 |
|
DisplayHeight(ourdisplay, ourscreen); |
439 |
+ |
if (stereo) { /* set stereo mode */ |
440 |
+ |
setstereobuf(STEREO_BUFFER_LEFT); |
441 |
+ |
pheight *= 2.; |
442 |
+ |
} |
443 |
|
/* map the window */ |
444 |
|
XMapWindow(ourdisplay, gwind); |
445 |
+ |
no_render++; |
446 |
+ |
do |
447 |
+ |
dev_input(0); /* get resize event */ |
448 |
+ |
while (hres == 0 & vres == 0); |
449 |
+ |
no_render--; |
450 |
|
rgl_checkerr("initializing GLX"); |
451 |
|
} |
452 |
|
|
482 |
|
} |
483 |
|
if (hres != 0 & vres != 0) { |
484 |
|
wa = (vres*pheight)/(hres*pwidth); |
485 |
< |
va = viewaspect(&thisview); |
485 |
> |
va = viewaspect(nv); |
486 |
|
if (va > wa+.05) { |
487 |
|
newvres = (pwidth/pheight)*va*newhres + .5; |
488 |
|
if (newvres > maxvres) { |
497 |
|
} |
498 |
|
} |
499 |
|
if (newhres != hres | newvres != vres) { |
500 |
+ |
no_render++; |
501 |
|
XResizeWindow(ourdisplay, gwind, newhres, newvres); |
502 |
|
do |
503 |
|
dev_input(0); /* get resize event */ |
504 |
|
while (newhres != hres | newvres != vres); |
505 |
+ |
no_render--; |
506 |
|
} |
507 |
|
} |
508 |
|
copystruct(&thisview, nv); |
554 |
|
|
555 |
|
render() /* render our display list and swap buffers */ |
556 |
|
{ |
557 |
< |
if (!mapped) |
557 |
> |
double d; |
558 |
> |
|
559 |
> |
if (!mapped | no_render) |
560 |
|
return; |
561 |
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
562 |
|
glCallList(displist); |
563 |
+ |
if (stereo) { /* do right eye for stereo */ |
564 |
+ |
setstereobuf(STEREO_BUFFER_RIGHT); |
565 |
+ |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
566 |
+ |
glMatrixMode(GL_MODELVIEW); |
567 |
+ |
glPushMatrix(); |
568 |
+ |
d = -eyedist / sqrt(thisview.hn2); |
569 |
+ |
glTranslated(d*thisview.hvec[0], d*thisview.hvec[1], |
570 |
+ |
d*thisview.hvec[2]); |
571 |
+ |
glCallList(displist); |
572 |
+ |
glMatrixMode(GL_MODELVIEW); |
573 |
+ |
glPopMatrix(); |
574 |
+ |
setstereobuf(STEREO_BUFFER_LEFT); |
575 |
+ |
} |
576 |
|
glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */ |
577 |
|
rgl_checkerr("rendering display list"); |
578 |
|
} |
624 |
|
} |
625 |
|
|
626 |
|
|
627 |
+ |
static |
628 |
+ |
waitabit() /* pause a moment */ |
629 |
+ |
{ |
630 |
+ |
struct timespec ts; |
631 |
+ |
ts.tv_sec = 0; |
632 |
+ |
ts.tv_nsec = 5000000; |
633 |
+ |
nanosleep(&ts, NULL); |
634 |
+ |
} |
635 |
+ |
|
636 |
+ |
|
637 |
|
getmove(ebut) /* get view change */ |
638 |
|
XButtonPressedEvent *ebut; |
639 |
|
{ |
644 |
|
int rootx, rooty, wx, wy; |
645 |
|
unsigned int statemask; |
646 |
|
|
647 |
< |
copylastv("moving"); |
647 |
> |
copylastv( movorb ? (movdir ? "left/right" : "up/down") : |
648 |
> |
(movdir ? "fore/back" : "rotate") ); |
649 |
|
XNoOp(ourdisplay); |
650 |
|
|
651 |
|
while (!XCheckMaskEvent(ourdisplay, |
652 |
|
ButtonReleaseMask, levptr(XEvent))) { |
653 |
+ |
/* pause so as not to move too fast */ |
654 |
+ |
waitabit(); |
655 |
|
|
656 |
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
657 |
|
&rootx, &rooty, &wx, &wy, &statemask)) |
679 |
|
{ |
680 |
|
float fbuf[6]; |
681 |
|
/* check to see if rtrace is running */ |
682 |
< |
if (rtpd[2] <= 0) |
682 |
> |
/* if (rtpd.pid <= 0) */ |
683 |
> |
if (!rtpd.running) |
684 |
|
return(0); |
685 |
|
/* assign origin */ |
686 |
|
fbuf[0] = org[0]; fbuf[1] = org[1]; fbuf[2] = org[2]; |
688 |
|
if (md <= FTINY) md = FHUGE; |
689 |
|
fbuf[3] = dir[0]*md; fbuf[4] = dir[1]*md; fbuf[5] = dir[2]*md; |
690 |
|
/* trace that ray */ |
691 |
< |
if (process(rtpd, fbuf, fbuf, 4*sizeof(float), 6*sizeof(float)) != |
692 |
< |
4*sizeof(float)) |
691 |
> |
if (process(&rtpd, (char *)fbuf, (char *)fbuf, |
692 |
> |
4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float)) |
693 |
|
error(INTERNAL, "error getting data back from rtrace process"); |
694 |
|
if (fbuf[3] >= .99*FHUGE) |
695 |
|
return(0); /* missed local objects */ |
703 |
|
{ |
704 |
|
double d, xmin, xmax, ymin, ymax, zmin, zmax; |
705 |
|
|
706 |
< |
zmin = 0.05; |
707 |
< |
zmax = 5000.; |
706 |
> |
zmin = 0.1; |
707 |
> |
zmax = 1000.; |
708 |
|
if (thisview.vfore > FTINY) |
709 |
|
zmin = thisview.vfore; |
710 |
|
if (thisview.vaft > FTINY) |
748 |
|
headlocked = 0; |
749 |
|
break; |
750 |
|
case 'l': /* retrieve last (premouse) view */ |
751 |
< |
if (lastvc != NULL) { |
751 |
> |
if (lastview.type) { |
752 |
|
VIEW vtmp; |
753 |
|
copystruct(&vtmp, &thisview); |
754 |
|
dev_view(&lastview); |
820 |
|
; |
821 |
|
else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL) |
822 |
|
vwnum = 0; |
823 |
< |
if (vwnum == currentview) |
730 |
< |
return; |
823 |
> |
copylastv("standard view"); |
824 |
|
dev_view(vwl[currentview=vwnum].v); |
825 |
|
} |
826 |
|
|
831 |
|
{ |
832 |
|
FILE *fp; |
833 |
|
/* check if already in there */ |
834 |
< |
if (!bcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) { |
834 |
> |
if (!memcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) { |
835 |
|
error(COMMAND, "view already in standard list"); |
836 |
|
return; |
837 |
|
} |
861 |
|
copylastv(cause) /* copy last view position */ |
862 |
|
char *cause; |
863 |
|
{ |
864 |
+ |
static char *lastvc; |
865 |
+ |
|
866 |
|
if (cause == lastvc) |
867 |
|
return; /* only record one view per cause */ |
868 |
|
lastvc = cause; |
893 |
|
glViewport(0, 0, hres=ersz->width, vres=ersz->height); |
894 |
|
if (hres > maxhres) maxhres = hres; |
895 |
|
if (vres > maxvres) maxvres = vres; |
896 |
+ |
if (no_render) |
897 |
+ |
return; |
898 |
|
wa = (vres*pheight)/(hres*pwidth); |
899 |
|
va = viewaspect(&thisview); |
900 |
|
if (va > wa+.05) { |