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 |
> |
#ifdef BSD |
94 |
> |
#define strchr index |
95 |
> |
#endif |
96 |
> |
|
97 |
> |
extern char *strchr(), *fgets(), *fgetline(), *atos(), *scan4var(); |
98 |
|
extern int nowarn; /* turn warnings off? */ |
99 |
|
extern time_t time(); |
100 |
|
|
116 |
|
case 'w': |
117 |
|
nowarn = !nowarn; |
118 |
|
break; |
119 |
+ |
case 's': |
120 |
+ |
silent = !silent; |
121 |
+ |
break; |
122 |
+ |
case 'S': |
123 |
+ |
stereo = !stereo; |
124 |
+ |
break; |
125 |
|
case 'c': |
126 |
|
vwintvl = atoi(argv[++i]); |
127 |
|
break; |
133 |
|
} |
134 |
|
if (i >= argc) |
135 |
|
goto userr; |
136 |
+ |
#ifdef NOSTEREO |
137 |
+ |
if (stereo) |
138 |
+ |
error(INTERNAL, "stereo not supported in this version"); |
139 |
+ |
#endif |
140 |
|
/* run rad and get views */ |
141 |
|
runrad(argc-i, argv+i); |
142 |
|
/* check view */ |
159 |
|
dev_open(radfile = argv[i]); |
160 |
|
/* load octree or scene files */ |
161 |
|
if (octree != NULL) { |
162 |
< |
displist = rgl_octlist(octree, NULL, NULL); |
162 |
> |
displist = rgl_octlist(octree, NULL, NULL, NULL); |
163 |
|
startrtrace(octree); |
164 |
|
} else |
165 |
< |
displist = rgl_filelist(nscenef, scene); |
165 |
> |
displist = rgl_filelist(nscenef, scene, NULL); |
166 |
|
/* set initial view */ |
167 |
|
dev_view(currentview < 0 ? &thisview : vwl[currentview].v); |
168 |
|
/* input/render loop */ |
171 |
|
/* all done */ |
172 |
|
quit(0); |
173 |
|
userr: |
174 |
< |
fprintf(stderr, "Usage: %s [-w][-b][-v view] rfile [VAR=value]..\n", |
174 |
> |
fprintf(stderr, |
175 |
> |
"Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n", |
176 |
|
argv[0]); |
177 |
|
quit(1); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
+ |
void |
182 |
|
quit(code) /* exit gracefully */ |
183 |
|
int code; |
184 |
|
{ |
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++; |
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); |
290 |
|
/* open options file */ |
291 |
|
if ((fp = fopen(optfile, "r")) == NULL) |
292 |
|
error(SYSTEM, "cannot open options file"); |
293 |
< |
/* get ambient value */ |
293 |
> |
/* get relevant options */ |
294 |
|
while (fgets(buf, sizeof(buf), fp) != NULL) |
295 |
< |
if (!strncmp(buf, "-av ", 4)) { |
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 |
< |
break; |
300 |
< |
} |
299 |
> |
else if (backvis && !strncmp(buf, "-bv", 3) && |
300 |
> |
(!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL)) |
301 |
> |
backvis = 0; |
302 |
|
fclose(fp); |
303 |
|
unlink(optfile); /* delete options file */ |
304 |
|
} |
371 |
|
GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None}; |
372 |
|
XSetWindowAttributes ourwinattr; |
373 |
|
XWMHints ourxwmhints; |
341 |
– |
XSizeHints oursizhints; |
374 |
|
/* open display server */ |
375 |
|
ourdisplay = XOpenDisplay(NULL); |
376 |
|
if (ourdisplay == NULL) |
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); |
373 |
– |
oursizhints.min_width = MINWIDTH; |
374 |
– |
oursizhints.min_height = MINHEIGHT; |
375 |
– |
oursizhints.flags = PMinSize; |
376 |
– |
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
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); |
426 |
> |
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
427 |
|
glEnable(GL_LIGHTING); |
428 |
+ |
glFrontFace(GL_CCW); |
429 |
+ |
glCullFace(GL_BACK); |
430 |
|
if (backvis) |
431 |
|
glDisable(GL_CULL_FACE); |
432 |
< |
else { |
388 |
< |
glFrontFace(GL_CCW); |
389 |
< |
glCullFace(GL_BACK); |
432 |
> |
else |
433 |
|
glEnable(GL_CULL_FACE); |
391 |
– |
} |
434 |
|
glDrawBuffer(GL_BACK); |
435 |
|
/* figure out sensible view */ |
436 |
|
pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / |
437 |
|
DisplayWidth(ourdisplay, ourscreen); |
438 |
|
pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / |
439 |
|
DisplayHeight(ourdisplay, ourscreen); |
440 |
+ |
if (stereo) { /* set stereo mode */ |
441 |
+ |
setstereobuf(STEREO_BUFFER_LEFT); |
442 |
+ |
pheight *= 2.; |
443 |
+ |
} |
444 |
|
/* map the window */ |
445 |
|
XMapWindow(ourdisplay, gwind); |
446 |
+ |
no_render++; |
447 |
|
do |
448 |
|
dev_input(0); /* get resize event */ |
449 |
|
while (hres == 0 & vres == 0); |
450 |
+ |
no_render--; |
451 |
|
rgl_checkerr("initializing GLX"); |
452 |
|
} |
453 |
|
|
498 |
|
} |
499 |
|
} |
500 |
|
if (newhres != hres | newvres != vres) { |
501 |
< |
in_dev_view++; |
501 |
> |
no_render++; |
502 |
|
XResizeWindow(ourdisplay, gwind, newhres, newvres); |
503 |
|
do |
504 |
|
dev_input(0); /* get resize event */ |
505 |
|
while (newhres != hres | newvres != vres); |
506 |
< |
in_dev_view--; |
506 |
> |
no_render--; |
507 |
|
} |
508 |
|
} |
509 |
|
copystruct(&thisview, nv); |
555 |
|
|
556 |
|
render() /* render our display list and swap buffers */ |
557 |
|
{ |
558 |
< |
if (!mapped) |
558 |
> |
double d; |
559 |
> |
|
560 |
> |
if (!mapped | no_render) |
561 |
|
return; |
562 |
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
563 |
|
glCallList(displist); |
564 |
+ |
if (stereo) { /* do right eye for stereo */ |
565 |
+ |
setstereobuf(STEREO_BUFFER_RIGHT); |
566 |
+ |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
567 |
+ |
glMatrixMode(GL_MODELVIEW); |
568 |
+ |
glPushMatrix(); |
569 |
+ |
d = -eyedist / sqrt(thisview.hn2); |
570 |
+ |
glTranslated(d*thisview.hvec[0], d*thisview.hvec[1], |
571 |
+ |
d*thisview.hvec[2]); |
572 |
+ |
glCallList(displist); |
573 |
+ |
glMatrixMode(GL_MODELVIEW); |
574 |
+ |
glPopMatrix(); |
575 |
+ |
setstereobuf(STEREO_BUFFER_LEFT); |
576 |
+ |
} |
577 |
|
glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */ |
578 |
|
rgl_checkerr("rendering display list"); |
579 |
|
} |
625 |
|
} |
626 |
|
|
627 |
|
|
628 |
+ |
static |
629 |
+ |
waitabit() /* pause a moment */ |
630 |
+ |
{ |
631 |
+ |
struct timespec ts; |
632 |
+ |
ts.tv_sec = 0; |
633 |
+ |
ts.tv_nsec = 5000000; |
634 |
+ |
nanosleep(&ts, NULL); |
635 |
+ |
} |
636 |
+ |
|
637 |
+ |
|
638 |
|
getmove(ebut) /* get view change */ |
639 |
|
XButtonPressedEvent *ebut; |
640 |
|
{ |
651 |
|
|
652 |
|
while (!XCheckMaskEvent(ourdisplay, |
653 |
|
ButtonReleaseMask, levptr(XEvent))) { |
654 |
+ |
/* pause so as not to move too fast */ |
655 |
+ |
waitabit(); |
656 |
|
|
657 |
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
658 |
|
&rootx, &rooty, &wx, &wy, &statemask)) |
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 */ |
820 |
|
; |
821 |
|
else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL) |
822 |
|
vwnum = 0; |
823 |
< |
if (vwnum == currentview) |
749 |
< |
return; |
750 |
< |
/* copylastv("change view"); */ |
823 |
> |
copylastv("standard view"); |
824 |
|
dev_view(vwl[currentview=vwnum].v); |
825 |
|
} |
826 |
|
|
893 |
|
glViewport(0, 0, hres=ersz->width, vres=ersz->height); |
894 |
|
if (hres > maxhres) maxhres = hres; |
895 |
|
if (vres > maxvres) maxvres = vres; |
896 |
< |
if (in_dev_view) |
896 |
> |
if (no_render) |
897 |
|
return; |
898 |
|
wa = (vres*pheight)/(hres*pwidth); |
899 |
|
va = viewaspect(&thisview); |