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 <time.h> |
15 |
+ |
#include "radogl.h" |
16 |
+ |
#include "view.h" |
17 |
+ |
#include "paths.h" |
18 |
|
#include "glradicon.h" |
19 |
|
|
20 |
|
#ifndef MAXVIEW |
31 |
|
#define MOVDEG (-5) /* degrees to orbit CW/down /frame */ |
32 |
|
#define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) |
33 |
|
|
33 |
– |
#define MINWIDTH 480 /* minimum graphics window width */ |
34 |
– |
#define MINHEIGHT 400 /* minimum graphics window height */ |
35 |
– |
|
34 |
|
#define BORWIDTH 5 /* border width */ |
35 |
|
|
36 |
|
#define ourscreen DefaultScreen(ourdisplay) |
63 |
|
|
64 |
|
int currentview = 0; /* current view number */ |
65 |
|
VIEW thisview = STDVIEW; /* displayed view */ |
66 |
+ |
double eyedist = 1; /* interocular distance */ |
67 |
|
VIEW lastview; /* last recorded view */ |
68 |
|
|
69 |
|
char *progname; /* global argv[0] */ |
74 |
|
|
75 |
|
int rtpd[3]; /* rtrace process descriptors */ |
76 |
|
|
77 |
+ |
int silent = 0; /* run rad silently? */ |
78 |
|
int backvis = 1; /* back faces visible? */ |
79 |
+ |
int stereo = 0; /* do stereo? */ |
80 |
|
|
81 |
+ |
#ifdef NOSTEREO |
82 |
+ |
#define setstereobuf(bid) 0 |
83 |
+ |
#else |
84 |
+ |
#define setstereobuf(bid) (glXWaitGL(), \ |
85 |
+ |
XSGISetStereoBuffer(ourdisplay, gwind, bid), \ |
86 |
+ |
glXWaitX()) |
87 |
+ |
#endif |
88 |
+ |
|
89 |
|
int displist; /* our scene display list */ |
90 |
|
|
91 |
< |
int in_dev_view = 0; /* currently in dev_view() */ |
91 |
> |
int no_render = 0; /* don't rerender */ |
92 |
|
|
93 |
< |
extern char *fgets(), *fgetline(), *atos(), *scan4var(); |
93 |
> |
extern char *fgetline(), *atos(), *scan4var(); |
94 |
|
extern int nowarn; /* turn warnings off? */ |
86 |
– |
extern time_t time(); |
95 |
|
|
96 |
|
|
97 |
|
main(argc, argv) |
111 |
|
case 'w': |
112 |
|
nowarn = !nowarn; |
113 |
|
break; |
114 |
+ |
case 's': |
115 |
+ |
silent = !silent; |
116 |
+ |
break; |
117 |
+ |
case 'S': |
118 |
+ |
stereo = !stereo; |
119 |
+ |
break; |
120 |
|
case 'c': |
121 |
|
vwintvl = atoi(argv[++i]); |
122 |
|
break; |
128 |
|
} |
129 |
|
if (i >= argc) |
130 |
|
goto userr; |
131 |
+ |
#ifdef NOSTEREO |
132 |
+ |
if (stereo) |
133 |
+ |
error(INTERNAL, "stereo not supported in this version"); |
134 |
+ |
#endif |
135 |
|
/* run rad and get views */ |
136 |
|
runrad(argc-i, argv+i); |
137 |
|
/* check view */ |
154 |
|
dev_open(radfile = argv[i]); |
155 |
|
/* load octree or scene files */ |
156 |
|
if (octree != NULL) { |
157 |
< |
displist = rgl_octlist(octree, NULL, NULL); |
157 |
> |
displist = rgl_octlist(octree, NULL, NULL, NULL); |
158 |
|
startrtrace(octree); |
159 |
|
} else |
160 |
< |
displist = rgl_filelist(nscenef, scene); |
160 |
> |
displist = rgl_filelist(nscenef, scene, NULL); |
161 |
|
/* set initial view */ |
162 |
|
dev_view(currentview < 0 ? &thisview : vwl[currentview].v); |
163 |
|
/* input/render loop */ |
166 |
|
/* all done */ |
167 |
|
quit(0); |
168 |
|
userr: |
169 |
< |
fprintf(stderr, "Usage: %s [-w][-b][-v view] rfile [VAR=value]..\n", |
169 |
> |
fprintf(stderr, |
170 |
> |
"Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n", |
171 |
|
argv[0]); |
172 |
|
quit(1); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
+ |
void |
177 |
|
quit(code) /* exit gracefully */ |
178 |
|
int code; |
179 |
|
{ |
216 |
|
/* set rad commmand */ |
217 |
|
strcpy(radcomm, "rad -w -v 0 "); /* look out below! */ |
218 |
|
cp = radcomm + 19; |
219 |
+ |
if (silent) { |
220 |
+ |
strcpy(cp, "-s "); |
221 |
+ |
cp += 3; |
222 |
+ |
} |
223 |
|
while (ac--) { |
224 |
|
strcpy(cp, *av++); |
225 |
|
while (*cp) cp++; |
242 |
|
expval = pow(2., expval); |
243 |
|
expval *= 0.5; /* compensate for local shading */ |
244 |
|
} |
245 |
+ |
/* look for eye separation */ |
246 |
+ |
if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL) |
247 |
+ |
eyedist = atof(cp); |
248 |
|
/* look for materials */ |
249 |
|
while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) { |
250 |
|
nscenef += wordstring(scene+nscenef, cp); |
285 |
|
/* open options file */ |
286 |
|
if ((fp = fopen(optfile, "r")) == NULL) |
287 |
|
error(SYSTEM, "cannot open options file"); |
288 |
< |
/* get ambient value */ |
288 |
> |
/* get relevant options */ |
289 |
|
while (fgets(buf, sizeof(buf), fp) != NULL) |
290 |
< |
if (!strncmp(buf, "-av ", 4)) { |
290 |
> |
if (!strncmp(buf, "-av ", 4)) |
291 |
|
setcolor(ambval, atof(buf+4), |
292 |
|
atof(sskip2(buf+4,1)), |
293 |
|
atof(sskip2(buf+4,2))); |
294 |
< |
break; |
295 |
< |
} |
294 |
> |
else if (backvis && !strncmp(buf, "-bv", 3) && |
295 |
> |
(!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL)) |
296 |
> |
backvis = 0; |
297 |
|
fclose(fp); |
298 |
|
unlink(optfile); /* delete options file */ |
299 |
|
} |
366 |
|
GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None}; |
367 |
|
XSetWindowAttributes ourwinattr; |
368 |
|
XWMHints ourxwmhints; |
341 |
– |
XSizeHints oursizhints; |
369 |
|
/* open display server */ |
370 |
|
ourdisplay = XOpenDisplay(NULL); |
371 |
|
if (ourdisplay == NULL) |
391 |
|
if (gwind == 0) |
392 |
|
error(SYSTEM, "cannot create window\n"); |
393 |
|
XStoreName(ourdisplay, gwind, id); |
394 |
+ |
#ifndef NOSTEREO |
395 |
+ |
if (stereo) /* check if stereo working */ |
396 |
+ |
switch (XSGIQueryStereoMode(ourdisplay, gwind)) { |
397 |
+ |
case STEREO_TOP: |
398 |
+ |
case STEREO_BOTTOM: |
399 |
+ |
break; |
400 |
+ |
case STEREO_OFF: |
401 |
+ |
error(USER, |
402 |
+ |
"wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first"); |
403 |
+ |
case X_STEREO_UNSUPPORTED: |
404 |
+ |
error(USER, "stereo not supported on this screen"); |
405 |
+ |
default: |
406 |
+ |
error(INTERNAL, "unknown stereo mode"); |
407 |
+ |
} |
408 |
+ |
#endif |
409 |
|
/* set window manager hints */ |
410 |
|
ourxwmhints.flags = InputHint|IconPixmapHint; |
411 |
|
ourxwmhints.input = True; |
412 |
|
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, |
413 |
|
gwind, glradicon_bits, glradicon_width, glradicon_height); |
414 |
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
373 |
– |
oursizhints.min_width = MINWIDTH; |
374 |
– |
oursizhints.min_height = MINHEIGHT; |
375 |
– |
oursizhints.flags = PMinSize; |
376 |
– |
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
415 |
|
/* set GLX context */ |
416 |
|
glXMakeCurrent(ourdisplay, gwind, gctx); |
417 |
|
glEnable(GL_DEPTH_TEST); |
418 |
|
glDepthFunc(GL_LESS); |
419 |
|
glShadeModel(GL_SMOOTH); |
420 |
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
421 |
< |
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
421 |
> |
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
422 |
|
glEnable(GL_LIGHTING); |
423 |
+ |
glFrontFace(GL_CCW); |
424 |
+ |
glCullFace(GL_BACK); |
425 |
|
if (backvis) |
426 |
|
glDisable(GL_CULL_FACE); |
427 |
< |
else { |
388 |
< |
glFrontFace(GL_CCW); |
389 |
< |
glCullFace(GL_BACK); |
427 |
> |
else |
428 |
|
glEnable(GL_CULL_FACE); |
391 |
– |
} |
429 |
|
glDrawBuffer(GL_BACK); |
430 |
|
/* figure out sensible view */ |
431 |
|
pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / |
432 |
|
DisplayWidth(ourdisplay, ourscreen); |
433 |
|
pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / |
434 |
|
DisplayHeight(ourdisplay, ourscreen); |
435 |
+ |
if (stereo) { /* set stereo mode */ |
436 |
+ |
setstereobuf(STEREO_BUFFER_LEFT); |
437 |
+ |
pheight *= 2.; |
438 |
+ |
} |
439 |
|
/* map the window */ |
440 |
|
XMapWindow(ourdisplay, gwind); |
441 |
+ |
no_render++; |
442 |
|
do |
443 |
|
dev_input(0); /* get resize event */ |
444 |
|
while (hres == 0 & vres == 0); |
445 |
+ |
no_render--; |
446 |
|
rgl_checkerr("initializing GLX"); |
447 |
|
} |
448 |
|
|
493 |
|
} |
494 |
|
} |
495 |
|
if (newhres != hres | newvres != vres) { |
496 |
< |
in_dev_view++; |
496 |
> |
no_render++; |
497 |
|
XResizeWindow(ourdisplay, gwind, newhres, newvres); |
498 |
|
do |
499 |
|
dev_input(0); /* get resize event */ |
500 |
|
while (newhres != hres | newvres != vres); |
501 |
< |
in_dev_view--; |
501 |
> |
no_render--; |
502 |
|
} |
503 |
|
} |
504 |
|
copystruct(&thisview, nv); |
550 |
|
|
551 |
|
render() /* render our display list and swap buffers */ |
552 |
|
{ |
553 |
< |
if (!mapped) |
553 |
> |
double d; |
554 |
> |
|
555 |
> |
if (!mapped | no_render) |
556 |
|
return; |
557 |
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
558 |
|
glCallList(displist); |
559 |
+ |
if (stereo) { /* do right eye for stereo */ |
560 |
+ |
setstereobuf(STEREO_BUFFER_RIGHT); |
561 |
+ |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
562 |
+ |
glMatrixMode(GL_MODELVIEW); |
563 |
+ |
glPushMatrix(); |
564 |
+ |
d = -eyedist / sqrt(thisview.hn2); |
565 |
+ |
glTranslated(d*thisview.hvec[0], d*thisview.hvec[1], |
566 |
+ |
d*thisview.hvec[2]); |
567 |
+ |
glCallList(displist); |
568 |
+ |
glMatrixMode(GL_MODELVIEW); |
569 |
+ |
glPopMatrix(); |
570 |
+ |
setstereobuf(STEREO_BUFFER_LEFT); |
571 |
+ |
} |
572 |
|
glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */ |
573 |
|
rgl_checkerr("rendering display list"); |
574 |
|
} |
620 |
|
} |
621 |
|
|
622 |
|
|
623 |
+ |
static |
624 |
+ |
waitabit() /* pause a moment */ |
625 |
+ |
{ |
626 |
+ |
struct timespec ts; |
627 |
+ |
ts.tv_sec = 0; |
628 |
+ |
ts.tv_nsec = 5000000; |
629 |
+ |
nanosleep(&ts, NULL); |
630 |
+ |
} |
631 |
+ |
|
632 |
+ |
|
633 |
|
getmove(ebut) /* get view change */ |
634 |
|
XButtonPressedEvent *ebut; |
635 |
|
{ |
646 |
|
|
647 |
|
while (!XCheckMaskEvent(ourdisplay, |
648 |
|
ButtonReleaseMask, levptr(XEvent))) { |
649 |
+ |
/* pause so as not to move too fast */ |
650 |
+ |
waitabit(); |
651 |
|
|
652 |
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
653 |
|
&rootx, &rooty, &wx, &wy, &statemask)) |
683 |
|
if (md <= FTINY) md = FHUGE; |
684 |
|
fbuf[3] = dir[0]*md; fbuf[4] = dir[1]*md; fbuf[5] = dir[2]*md; |
685 |
|
/* trace that ray */ |
686 |
< |
if (process(rtpd, fbuf, fbuf, 4*sizeof(float), 6*sizeof(float)) != |
687 |
< |
4*sizeof(float)) |
686 |
> |
if (process(rtpd, (char *)fbuf, (char *)fbuf, |
687 |
> |
4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float)) |
688 |
|
error(INTERNAL, "error getting data back from rtrace process"); |
689 |
|
if (fbuf[3] >= .99*FHUGE) |
690 |
|
return(0); /* missed local objects */ |
815 |
|
; |
816 |
|
else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL) |
817 |
|
vwnum = 0; |
818 |
< |
if (vwnum == currentview) |
749 |
< |
return; |
750 |
< |
/* copylastv("change view"); */ |
818 |
> |
copylastv("standard view"); |
819 |
|
dev_view(vwl[currentview=vwnum].v); |
820 |
|
} |
821 |
|
|
888 |
|
glViewport(0, 0, hres=ersz->width, vres=ersz->height); |
889 |
|
if (hres > maxhres) maxhres = hres; |
890 |
|
if (vres > maxvres) maxvres = vres; |
891 |
< |
if (in_dev_view) |
891 |
> |
if (no_render) |
892 |
|
return; |
893 |
|
wa = (vres*pheight)/(hres*pwidth); |
894 |
|
va = viewaspect(&thisview); |