1 |
gwlarson |
3.13 |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
2 |
gwlarson |
3.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* OpenGL driver for holodeck display. |
9 |
|
|
* Based on GLX driver using T-mesh. |
10 |
|
|
* |
11 |
|
|
* Define symbol STEREO for stereo viewing. |
12 |
|
|
* Define symbol DOBJ for display object viewing. |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
#ifdef NOSTEREO |
16 |
|
|
#ifdef STEREO |
17 |
|
|
#undef STEREO |
18 |
|
|
#else |
19 |
|
|
#undef NOSTEREO |
20 |
|
|
#endif |
21 |
|
|
#endif |
22 |
|
|
|
23 |
|
|
#include "standard.h" |
24 |
|
|
|
25 |
|
|
#include <GL/glx.h> |
26 |
|
|
#include <GL/glu.h> |
27 |
|
|
#ifdef STEREO |
28 |
|
|
#include <X11/extensions/SGIStereo.h> |
29 |
|
|
#endif |
30 |
|
|
|
31 |
|
|
#include "rhd_odraw.h" |
32 |
|
|
#ifdef DOBJ |
33 |
|
|
#include "rhdobj.h" |
34 |
|
|
#endif |
35 |
|
|
|
36 |
|
|
#include "x11icon.h" |
37 |
|
|
|
38 |
|
|
#ifndef RAYQLEN |
39 |
gwlarson |
3.11 |
#define RAYQLEN 0 /* max. rays to queue before flush */ |
40 |
gwlarson |
3.1 |
#endif |
41 |
gwlarson |
3.15 |
#ifndef MINWIDTH |
42 |
|
|
#define MINWIDTH 480 /* minimum graphics window width */ |
43 |
|
|
#define MINHEIGHT 400 /* minimum graphics window height */ |
44 |
|
|
#endif |
45 |
|
|
#ifndef VIEWDIST |
46 |
|
|
#define VIEWDIST 356 /* assumed viewing distance (mm) */ |
47 |
|
|
#endif |
48 |
|
|
#ifndef BORWIDTH |
49 |
|
|
#define BORWIDTH 5 /* border width */ |
50 |
|
|
#endif |
51 |
gwlarson |
3.1 |
|
52 |
|
|
#ifndef FEQ |
53 |
|
|
#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) |
54 |
|
|
#endif |
55 |
|
|
|
56 |
gwlarson |
3.7 |
#define VWHEADLOCK 01 /* head position is locked flag */ |
57 |
|
|
#define VWPERSP 02 /* perspective view is set */ |
58 |
|
|
#define VWORTHO 04 /* orthographic view is set */ |
59 |
|
|
#define VWCHANGE 010 /* view has changed */ |
60 |
|
|
#define VWSTEADY 020 /* view is now steady */ |
61 |
|
|
#define VWMAPPED 040 /* window is mapped */ |
62 |
|
|
|
63 |
gwlarson |
3.1 |
#define GAMMA 1.4 /* default gamma correction */ |
64 |
|
|
|
65 |
|
|
#define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) |
66 |
|
|
|
67 |
|
|
#define MOVPCT 7 /* percent distance to move /frame */ |
68 |
|
|
#define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) |
69 |
|
|
#define MOVDEG (-5) /* degrees to orbit CW/down /frame */ |
70 |
|
|
#define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) |
71 |
|
|
|
72 |
|
|
#define setstereobuf(bid) (glXWaitGL(), \ |
73 |
|
|
XSGISetStereoBuffer(ourdisplay, gwind, bid), \ |
74 |
|
|
glXWaitX()) |
75 |
|
|
|
76 |
|
|
#define ourscreen DefaultScreen(ourdisplay) |
77 |
|
|
#define ourroot RootWindow(ourdisplay,ourscreen) |
78 |
|
|
#define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\ |
79 |
|
|
ButtonPressMask|ButtonReleaseMask) |
80 |
|
|
|
81 |
|
|
#define levptr(etype) ((etype *)¤tevent) |
82 |
|
|
|
83 |
|
|
struct driver odev; /* global device driver structure */ |
84 |
|
|
|
85 |
|
|
char odev_args[64]; /* command arguments */ |
86 |
|
|
|
87 |
|
|
static GLfloat *depthbuffer = NULL; /* depth buffer */ |
88 |
|
|
|
89 |
|
|
#ifdef STEREO |
90 |
|
|
static VIEW vwright; /* right eye view */ |
91 |
|
|
static GLfloat *depthright = NULL; /* right depth buffer */ |
92 |
|
|
#endif |
93 |
|
|
|
94 |
|
|
static int rayqleft = 0; /* rays left to queue before flush */ |
95 |
|
|
|
96 |
|
|
static XEvent currentevent; /* current event */ |
97 |
|
|
|
98 |
|
|
static unsigned long ourblack=0, ourwhite=~0; |
99 |
|
|
|
100 |
|
|
static Display *ourdisplay = NULL; /* our display */ |
101 |
|
|
static XVisualInfo *ourvinf; /* our visual information */ |
102 |
|
|
static Window gwind = 0; /* our graphics window */ |
103 |
|
|
static GLXContext gctx; /* our GLX context */ |
104 |
|
|
|
105 |
|
|
static double pwidth, pheight; /* pixel dimensions (mm) */ |
106 |
|
|
|
107 |
|
|
static double dev_zmin, dev_zmax; /* fore and aft clipping plane dist. */ |
108 |
|
|
static double dev_zrat; /* (1. - dev_zmin/dev_zmax) */ |
109 |
|
|
|
110 |
|
|
#define setzrat() (dev_zrat = 1. - dev_zmin/dev_zmax) |
111 |
|
|
#define mapdepth(d) ((d)>0.9995 ? FHUGE : dev_zmin/(1.-(d)*dev_zrat)) |
112 |
|
|
|
113 |
|
|
static int inpresflags; /* input result flags */ |
114 |
|
|
|
115 |
gwlarson |
3.7 |
static int viewflags; /* what's happening with view */ |
116 |
gwlarson |
3.1 |
|
117 |
|
|
static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), |
118 |
gwlarson |
3.2 |
xferdepth(), freedepth(), setglortho(), |
119 |
gwlarson |
3.1 |
setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); |
120 |
|
|
|
121 |
|
|
static double getdistance(); |
122 |
|
|
|
123 |
|
|
#ifdef STEREO |
124 |
|
|
static int pushright(), popright(); |
125 |
|
|
#endif |
126 |
|
|
|
127 |
gwlarson |
3.2 |
extern int gmPortals; /* GL portal list id */ |
128 |
|
|
|
129 |
gwlarson |
3.1 |
extern time_t time(); |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
dev_open(id) /* initialize GLX driver */ |
133 |
|
|
char *id; |
134 |
|
|
{ |
135 |
|
|
extern char *getenv(); |
136 |
|
|
static RGBPRIMS myprims = STDPRIMS; |
137 |
gwlarson |
3.6 |
static int atlBest[] = {GLX_RGBA, GLX_DOUBLEBUFFER, |
138 |
|
|
GLX_RED_SIZE,8, GLX_GREEN_SIZE,8, |
139 |
|
|
GLX_BLUE_SIZE,8, GLX_DEPTH_SIZE,15, None}; |
140 |
|
|
static int atlOK[] = {GLX_RGBA, GLX_DOUBLEBUFFER, |
141 |
|
|
GLX_RED_SIZE,4, GLX_GREEN_SIZE,4, |
142 |
|
|
GLX_BLUE_SIZE,4, GLX_DEPTH_SIZE,15, None}; |
143 |
gwlarson |
3.1 |
char *ev; |
144 |
|
|
double gamval = GAMMA; |
145 |
|
|
RGBPRIMP dpri = stdprims; |
146 |
|
|
XSetWindowAttributes ourwinattr; |
147 |
|
|
XWMHints ourxwmhints; |
148 |
|
|
XSizeHints oursizhints; |
149 |
|
|
/* check for unsupported stereo */ |
150 |
|
|
#ifdef NOSTEREO |
151 |
|
|
error(INTERNAL, "stereo display driver unavailable"); |
152 |
|
|
#endif |
153 |
|
|
/* open display server */ |
154 |
|
|
ourdisplay = XOpenDisplay(NULL); |
155 |
gwlarson |
3.5 |
CHECK(ourdisplay==NULL, USER, |
156 |
|
|
"cannot open X-windows; DISPLAY variable set?"); |
157 |
gwlarson |
3.1 |
#ifdef STEREO |
158 |
|
|
switch (XSGIQueryStereoMode(ourdisplay, ourroot)) { |
159 |
|
|
case STEREO_TOP: |
160 |
|
|
case STEREO_BOTTOM: |
161 |
|
|
break; |
162 |
|
|
case STEREO_OFF: |
163 |
|
|
error(USER, |
164 |
gwlarson |
3.5 |
"wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first"); |
165 |
gwlarson |
3.1 |
case X_STEREO_UNSUPPORTED: |
166 |
|
|
error(USER, "stereo mode not supported on this screen"); |
167 |
|
|
default: |
168 |
|
|
error(INTERNAL, "unknown stereo mode"); |
169 |
|
|
} |
170 |
|
|
#endif |
171 |
|
|
/* find a usable visual */ |
172 |
|
|
ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest); |
173 |
gwlarson |
3.6 |
if (ourvinf == NULL) |
174 |
|
|
ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK); |
175 |
gwlarson |
3.5 |
CHECK(ourvinf==NULL, USER, "no suitable visuals available"); |
176 |
gwlarson |
3.1 |
/* get a context */ |
177 |
|
|
gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE); |
178 |
|
|
/* set gamma and tone mapping */ |
179 |
|
|
if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL |
180 |
|
|
|| (ev = getenv("DISPLAY_GAMMA")) != NULL) |
181 |
|
|
gamval = atof(ev); |
182 |
|
|
if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL && |
183 |
|
|
sscanf(ev, "%f %f %f %f %f %f %f %f", |
184 |
|
|
&myprims[RED][CIEX],&myprims[RED][CIEY], |
185 |
|
|
&myprims[GRN][CIEX],&myprims[GRN][CIEY], |
186 |
|
|
&myprims[BLU][CIEX],&myprims[BLU][CIEY], |
187 |
|
|
&myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6) |
188 |
|
|
dpri = myprims; |
189 |
|
|
if (tmInit(mytmflags(), dpri, gamval) == NULL) |
190 |
|
|
error(SYSTEM, "not enough memory in dev_open"); |
191 |
|
|
/* open window */ |
192 |
|
|
ourwinattr.background_pixel = ourblack; |
193 |
|
|
ourwinattr.border_pixel = ourblack; |
194 |
|
|
ourwinattr.event_mask = ourmask; |
195 |
|
|
/* this is stupid */ |
196 |
|
|
ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot, |
197 |
|
|
ourvinf->visual, AllocNone); |
198 |
|
|
gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, |
199 |
|
|
DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, |
200 |
|
|
#ifdef STEREO |
201 |
|
|
(DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2, |
202 |
|
|
#else |
203 |
|
|
DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, |
204 |
|
|
#endif |
205 |
|
|
BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual, |
206 |
|
|
CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr); |
207 |
gwlarson |
3.5 |
CHECK(gwind==0, SYSTEM, "cannot create window"); |
208 |
gwlarson |
3.1 |
XStoreName(ourdisplay, gwind, id); |
209 |
|
|
/* set window manager hints */ |
210 |
|
|
ourxwmhints.flags = InputHint|IconPixmapHint; |
211 |
|
|
ourxwmhints.input = True; |
212 |
|
|
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, |
213 |
|
|
gwind, x11icon_bits, x11icon_width, x11icon_height); |
214 |
|
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
215 |
|
|
oursizhints.min_width = MINWIDTH; |
216 |
|
|
#ifdef STEREO |
217 |
|
|
oursizhints.min_height = MINHEIGHT/2; |
218 |
|
|
oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH; |
219 |
|
|
oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) - |
220 |
|
|
2*BORWIDTH)/2; |
221 |
|
|
oursizhints.flags = PMinSize|PMaxSize; |
222 |
|
|
#else |
223 |
|
|
oursizhints.min_height = MINHEIGHT; |
224 |
|
|
oursizhints.flags = PMinSize; |
225 |
|
|
#endif |
226 |
|
|
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
227 |
|
|
/* set GLX context */ |
228 |
|
|
glXMakeCurrent(ourdisplay, gwind, gctx); |
229 |
|
|
glEnable(GL_DEPTH_TEST); |
230 |
|
|
glDepthFunc(GL_LEQUAL); |
231 |
gwlarson |
3.9 |
glClearColor(0, 0, 0, 0); |
232 |
gwlarson |
3.1 |
glFrontFace(GL_CCW); |
233 |
|
|
glDisable(GL_CULL_FACE); |
234 |
|
|
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
235 |
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
236 |
gwlarson |
3.10 |
glPixelStorei(GL_PACK_ALIGNMENT, 1); |
237 |
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
238 |
gwlarson |
3.1 |
/* figure out sensible view */ |
239 |
|
|
pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / |
240 |
|
|
DisplayWidth(ourdisplay, ourscreen); |
241 |
|
|
pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / |
242 |
|
|
DisplayHeight(ourdisplay, ourscreen); |
243 |
|
|
#ifdef STEREO |
244 |
|
|
pheight *= 2.; |
245 |
|
|
setstereobuf(STEREO_BUFFER_LEFT); |
246 |
|
|
#endif |
247 |
|
|
checkglerr("setting rendering parameters"); |
248 |
|
|
copystruct(&odev.v, &stdview); |
249 |
|
|
odev.v.type = VT_PER; |
250 |
gwlarson |
3.7 |
viewflags = VWSTEADY; /* view starts static */ |
251 |
gwlarson |
3.1 |
/* map the window */ |
252 |
|
|
XMapWindow(ourdisplay, gwind); |
253 |
|
|
dev_input(); /* sets size and view angles */ |
254 |
|
|
if (!odInit(DisplayWidth(ourdisplay,ourscreen) * |
255 |
gwlarson |
3.11 |
DisplayHeight(ourdisplay,ourscreen) / 3)) |
256 |
gwlarson |
3.1 |
error(SYSTEM, "insufficient memory for value storage"); |
257 |
|
|
odev.name = id; |
258 |
|
|
odev.firstuse = 1; /* can't recycle samples */ |
259 |
|
|
odev.ifd = ConnectionNumber(ourdisplay); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
|
263 |
|
|
dev_close() /* close our display and free resources */ |
264 |
|
|
{ |
265 |
|
|
#ifdef DOBJ |
266 |
|
|
dobj_cleanup(); |
267 |
|
|
#endif |
268 |
gwlarson |
3.2 |
freedepth(); |
269 |
|
|
gmEndGeom(); |
270 |
|
|
gmEndPortal(); |
271 |
gwlarson |
3.1 |
odDone(); |
272 |
|
|
glXMakeCurrent(ourdisplay, None, NULL); |
273 |
|
|
glXDestroyContext(ourdisplay, gctx); |
274 |
|
|
XDestroyWindow(ourdisplay, gwind); |
275 |
|
|
gwind = 0; |
276 |
|
|
XCloseDisplay(ourdisplay); |
277 |
|
|
ourdisplay = NULL; |
278 |
|
|
tmDone(NULL); |
279 |
|
|
odev.v.type = 0; |
280 |
|
|
odev.hres = odev.vres = 0; |
281 |
|
|
odev.ifd = -1; |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
|
285 |
|
|
dev_clear() /* clear our representation */ |
286 |
|
|
{ |
287 |
gwlarson |
3.10 |
viewflags |= VWCHANGE; /* pretend our view has changed */ |
288 |
|
|
wipeclean(); /* clean off display and samples */ |
289 |
gwlarson |
3.12 |
dev_flush(); /* redraw geometry & get depth */ |
290 |
gwlarson |
3.1 |
rayqleft = 0; /* hold off update */ |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
|
294 |
|
|
int |
295 |
|
|
dev_view(nv) /* assign new driver view */ |
296 |
|
|
register VIEW *nv; |
297 |
|
|
{ |
298 |
|
|
double d; |
299 |
|
|
|
300 |
|
|
if (nv->type != VT_PER || /* check view legality */ |
301 |
|
|
nv->horiz > 160. || nv->vert > 160.) { |
302 |
|
|
error(COMMAND, "illegal view type/angle"); |
303 |
|
|
nv->type = odev.v.type; |
304 |
|
|
nv->horiz = odev.v.horiz; |
305 |
|
|
nv->vert = odev.v.vert; |
306 |
|
|
return(0); |
307 |
|
|
} |
308 |
|
|
if (nv != &odev.v) { |
309 |
|
|
/* resize window? */ |
310 |
|
|
if (!FEQ(nv->horiz,odev.v.horiz) || |
311 |
|
|
!FEQ(nv->vert,odev.v.vert)) { |
312 |
|
|
int dw = DisplayWidth(ourdisplay,ourscreen); |
313 |
|
|
int dh = DisplayHeight(ourdisplay,ourscreen); |
314 |
|
|
|
315 |
|
|
dw -= 25; /* for window frame */ |
316 |
|
|
dh -= 50; |
317 |
|
|
#ifdef STEREO |
318 |
|
|
dh /= 2; |
319 |
|
|
#endif |
320 |
|
|
odev.hres = 2.*VIEWDIST/pwidth * |
321 |
|
|
tan(PI/180./2.*nv->horiz); |
322 |
|
|
odev.vres = 2.*VIEWDIST/pheight * |
323 |
|
|
tan(PI/180./2.*nv->vert); |
324 |
|
|
if (odev.hres > dw) { |
325 |
|
|
odev.vres = dw * odev.vres / odev.hres; |
326 |
|
|
odev.hres = dw; |
327 |
|
|
} |
328 |
|
|
if (odev.vres > dh) { |
329 |
|
|
odev.hres = dh * odev.hres / odev.vres; |
330 |
|
|
odev.vres = dh; |
331 |
|
|
} |
332 |
|
|
XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); |
333 |
|
|
dev_input(); /* get resize event */ |
334 |
|
|
} |
335 |
|
|
copystruct(&odev.v, nv); /* setview() already called */ |
336 |
gwlarson |
3.14 |
viewflags |= VWCHANGE; |
337 |
|
|
} |
338 |
gwlarson |
3.1 |
#ifdef STEREO |
339 |
gwlarson |
3.14 |
copystruct(&vwright, nv); |
340 |
|
|
d = eyesepdist / sqrt(nv->hn2); |
341 |
|
|
VSUM(vwright.vp, nv->vp, nv->hvec, d); |
342 |
|
|
/* setview(&vwright); -- Unnecessary */ |
343 |
gwlarson |
3.1 |
#endif |
344 |
|
|
wipeclean(); |
345 |
|
|
return(1); |
346 |
|
|
} |
347 |
|
|
|
348 |
|
|
|
349 |
gwlarson |
3.2 |
dev_section(gfn, pfn) /* add octree for geometry rendering */ |
350 |
|
|
char *gfn, *pfn; |
351 |
gwlarson |
3.1 |
{ |
352 |
|
|
extern char *index(); |
353 |
|
|
char *cp; |
354 |
|
|
|
355 |
gwlarson |
3.2 |
if (gfn == NULL) { |
356 |
|
|
gmEndGeom(); |
357 |
|
|
gmEndPortal(); |
358 |
gwlarson |
3.4 |
wipeclean(); /* new geometry, so redraw it */ |
359 |
gwlarson |
3.2 |
return; |
360 |
|
|
} |
361 |
|
|
if (access(gfn, R_OK) == 0) |
362 |
|
|
gmNewGeom(gfn); |
363 |
gwlarson |
3.1 |
#ifdef DEBUG |
364 |
|
|
else { |
365 |
gwlarson |
3.2 |
sprintf(errmsg, "cannot load octree \"%s\"", gfn); |
366 |
gwlarson |
3.1 |
error(WARNING, errmsg); |
367 |
|
|
} |
368 |
|
|
#endif |
369 |
gwlarson |
3.2 |
if (pfn != NULL) |
370 |
|
|
gmNewPortal(pfn); |
371 |
gwlarson |
3.1 |
} |
372 |
|
|
|
373 |
|
|
|
374 |
|
|
dev_auxcom(cmd, args) /* process an auxiliary command */ |
375 |
|
|
char *cmd, *args; |
376 |
|
|
{ |
377 |
|
|
#ifdef DOBJ |
378 |
gwlarson |
3.12 |
int vischange; |
379 |
|
|
|
380 |
|
|
if ((vischange = dobj_command(cmd, args)) >= 0) { |
381 |
|
|
if (vischange) { |
382 |
|
|
imm_mode = beam_sync(1) > 0; |
383 |
|
|
dev_clear(); |
384 |
|
|
} |
385 |
gwlarson |
3.1 |
return; |
386 |
gwlarson |
3.12 |
} |
387 |
gwlarson |
3.1 |
#endif |
388 |
|
|
sprintf(errmsg, "%s: unknown command", cmd); |
389 |
|
|
error(COMMAND, errmsg); |
390 |
|
|
} |
391 |
|
|
|
392 |
|
|
|
393 |
|
|
VIEW * |
394 |
|
|
dev_auxview(n, hvres) /* return nth auxiliary view */ |
395 |
|
|
int n; |
396 |
|
|
int hvres[2]; |
397 |
|
|
{ |
398 |
|
|
hvres[0] = odev.hres; hvres[1] = odev.vres; |
399 |
|
|
if (n == 0) |
400 |
|
|
return(&odev.v); |
401 |
|
|
#ifdef STEREO |
402 |
|
|
if (n == 1) |
403 |
|
|
return(&vwright); |
404 |
|
|
#endif |
405 |
|
|
return(NULL); |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
|
409 |
|
|
int |
410 |
|
|
dev_input() /* get X11 input */ |
411 |
|
|
{ |
412 |
|
|
inpresflags = 0; |
413 |
|
|
|
414 |
|
|
do |
415 |
|
|
getevent(); |
416 |
|
|
|
417 |
|
|
while (XPending(ourdisplay) > 0); |
418 |
|
|
|
419 |
|
|
odev.inpready = 0; |
420 |
|
|
|
421 |
|
|
return(inpresflags); |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
|
425 |
|
|
dev_value(c, d, p) /* add a pixel value to our texture */ |
426 |
|
|
COLR c; |
427 |
|
|
FVECT d, p; |
428 |
|
|
{ |
429 |
|
|
#ifdef DOBJ |
430 |
|
|
if (dobj_lightsamp != NULL) { /* in light source sampling */ |
431 |
|
|
(*dobj_lightsamp)(c, d, p); |
432 |
|
|
return; |
433 |
|
|
} |
434 |
|
|
#endif |
435 |
|
|
odSample(c, d, p); /* add to display representation */ |
436 |
|
|
if (!--rayqleft) |
437 |
|
|
dev_flush(); /* flush output */ |
438 |
|
|
} |
439 |
|
|
|
440 |
|
|
|
441 |
|
|
int |
442 |
|
|
dev_flush() /* flush output as appropriate */ |
443 |
|
|
{ |
444 |
|
|
int ndrawn; |
445 |
|
|
|
446 |
gwlarson |
3.7 |
if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) { |
447 |
gwlarson |
3.1 |
#ifdef STEREO |
448 |
|
|
pushright(); /* draw right eye */ |
449 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
450 |
gwlarson |
3.1 |
#ifdef DOBJ |
451 |
gwlarson |
3.3 |
ndrawn += dobj_render(); |
452 |
gwlarson |
3.1 |
#endif |
453 |
|
|
checkglerr("rendering right eye"); |
454 |
|
|
popright(); /* draw left eye */ |
455 |
|
|
#endif |
456 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
457 |
gwlarson |
3.1 |
#ifdef DOBJ |
458 |
|
|
ndrawn += dobj_render(); |
459 |
|
|
#endif |
460 |
gwlarson |
3.6 |
glXSwapBuffers(ourdisplay, gwind); |
461 |
gwlarson |
3.1 |
checkglerr("rendering base view"); |
462 |
|
|
} |
463 |
gwlarson |
3.7 |
if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == |
464 |
|
|
(VWMAPPED|VWSTEADY|VWPERSP)) { |
465 |
|
|
/* first time after steady */ |
466 |
|
|
if (ndrawn) |
467 |
|
|
xferdepth(); /* transfer and clear depth */ |
468 |
|
|
setglortho(); /* set orthographic view */ |
469 |
|
|
|
470 |
|
|
} |
471 |
|
|
if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == |
472 |
|
|
(VWMAPPED|VWSTEADY|VWORTHO)) { |
473 |
|
|
/* else update cones */ |
474 |
gwlarson |
3.1 |
#ifdef STEREO |
475 |
gwlarson |
3.7 |
pushright(); |
476 |
|
|
odUpdate(1); /* draw right eye */ |
477 |
|
|
popright(); |
478 |
gwlarson |
3.1 |
#endif |
479 |
gwlarson |
3.7 |
odUpdate(0); /* draw left eye */ |
480 |
|
|
glFlush(); /* flush OpenGL */ |
481 |
|
|
} |
482 |
gwlarson |
3.1 |
rayqleft = RAYQLEN; |
483 |
|
|
/* flush X11 and return # pending */ |
484 |
|
|
return(odev.inpready = XPending(ourdisplay)); |
485 |
|
|
} |
486 |
|
|
|
487 |
|
|
|
488 |
|
|
checkglerr(where) /* check for GL or GLU error */ |
489 |
|
|
char *where; |
490 |
|
|
{ |
491 |
|
|
register GLenum errcode; |
492 |
|
|
|
493 |
|
|
while ((errcode = glGetError()) != GL_NO_ERROR) { |
494 |
|
|
sprintf(errmsg, "OpenGL error %s: %s", |
495 |
|
|
where, gluErrorString(errcode)); |
496 |
|
|
error(WARNING, errmsg); |
497 |
|
|
} |
498 |
|
|
} |
499 |
|
|
|
500 |
|
|
|
501 |
|
|
static |
502 |
|
|
xferdepth() /* load and clear depth buffer */ |
503 |
|
|
{ |
504 |
|
|
register GLfloat *dbp; |
505 |
gwlarson |
3.9 |
register GLubyte *pbuf; |
506 |
gwlarson |
3.1 |
|
507 |
gwlarson |
3.3 |
if (depthbuffer == NULL) { /* allocate private depth buffer */ |
508 |
gwlarson |
3.1 |
#ifdef STEREO |
509 |
|
|
depthright = (GLfloat *)malloc( |
510 |
|
|
odev.hres*odev.vres*sizeof(GLfloat)); |
511 |
|
|
#endif |
512 |
|
|
depthbuffer = (GLfloat *)malloc( |
513 |
|
|
odev.hres*odev.vres*sizeof(GLfloat)); |
514 |
gwlarson |
3.5 |
CHECK(depthbuffer==NULL, SYSTEM, "out of memory in xferdepth"); |
515 |
gwlarson |
3.1 |
} |
516 |
gwlarson |
3.3 |
/* allocate alpha buffer for portals */ |
517 |
|
|
if (gmPortals) |
518 |
gwlarson |
3.10 |
pbuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte)); |
519 |
gwlarson |
3.3 |
else |
520 |
gwlarson |
3.9 |
pbuf = NULL; |
521 |
gwlarson |
3.1 |
#ifdef STEREO |
522 |
gwlarson |
3.12 |
pushright(); |
523 |
gwlarson |
3.1 |
glReadPixels(0, 0, odev.hres, odev.vres, |
524 |
|
|
GL_DEPTH_COMPONENT, GL_FLOAT, depthright); |
525 |
gwlarson |
3.9 |
if (pbuf != NULL) { |
526 |
|
|
glClear(GL_COLOR_BUFFER_BIT); |
527 |
|
|
gmDrawPortals(0xff, -1, -1, -1); |
528 |
gwlarson |
3.3 |
glReadPixels(0, 0, odev.hres, odev.vres, |
529 |
gwlarson |
3.10 |
GL_RED, GL_UNSIGNED_BYTE, pbuf); |
530 |
gwlarson |
3.9 |
} |
531 |
gwlarson |
3.3 |
for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; ) |
532 |
gwlarson |
3.10 |
if (pbuf != NULL && pbuf[dbp-depthright]&0x40) |
533 |
gwlarson |
3.3 |
*dbp = FHUGE; |
534 |
|
|
else |
535 |
|
|
*dbp = mapdepth(*dbp); |
536 |
|
|
glClear(GL_DEPTH_BUFFER_BIT); |
537 |
gwlarson |
3.1 |
odDepthMap(1, depthright); |
538 |
gwlarson |
3.12 |
popright(); |
539 |
gwlarson |
3.1 |
#endif |
540 |
gwlarson |
3.2 |
/* read back depth buffer */ |
541 |
gwlarson |
3.1 |
glReadPixels(0, 0, odev.hres, odev.vres, |
542 |
|
|
GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); |
543 |
gwlarson |
3.9 |
if (pbuf != NULL) { |
544 |
|
|
glClear(GL_COLOR_BUFFER_BIT); /* find portals */ |
545 |
|
|
gmDrawPortals(0xff, -1, -1, -1); |
546 |
gwlarson |
3.2 |
glReadPixels(0, 0, odev.hres, odev.vres, |
547 |
gwlarson |
3.10 |
GL_RED, GL_UNSIGNED_BYTE, pbuf); |
548 |
|
|
#ifdef DEBUG |
549 |
|
|
glXSwapBuffers(ourdisplay, gwind); |
550 |
|
|
#endif |
551 |
gwlarson |
3.9 |
} |
552 |
gwlarson |
3.1 |
for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; ) |
553 |
gwlarson |
3.10 |
if (pbuf != NULL && pbuf[dbp-depthbuffer]&0x40) |
554 |
gwlarson |
3.2 |
*dbp = FHUGE; |
555 |
|
|
else |
556 |
|
|
*dbp = mapdepth(*dbp); |
557 |
gwlarson |
3.3 |
glClear(GL_DEPTH_BUFFER_BIT); /* clear system depth buffer */ |
558 |
gwlarson |
3.1 |
odDepthMap(0, depthbuffer); /* transfer depth data */ |
559 |
gwlarson |
3.9 |
if (pbuf != NULL) |
560 |
|
|
free((char *)pbuf); /* free our portal buffer */ |
561 |
gwlarson |
3.1 |
} |
562 |
|
|
|
563 |
|
|
|
564 |
gwlarson |
3.2 |
static |
565 |
|
|
freedepth() /* free recorded depth buffer */ |
566 |
|
|
{ |
567 |
|
|
if (depthbuffer == NULL) |
568 |
|
|
return; |
569 |
|
|
#ifdef STEREO |
570 |
|
|
odDepthMap(1, NULL); |
571 |
|
|
free((char *)depthright); |
572 |
|
|
depthright = NULL; |
573 |
|
|
#endif |
574 |
|
|
odDepthMap(0, NULL); |
575 |
|
|
free((char *)depthbuffer); |
576 |
|
|
depthbuffer = NULL; |
577 |
|
|
} |
578 |
|
|
|
579 |
|
|
|
580 |
gwlarson |
3.1 |
static double |
581 |
|
|
getdistance(dx, dy, direc) /* distance from fore plane along view ray */ |
582 |
|
|
int dx, dy; |
583 |
|
|
FVECT direc; |
584 |
|
|
{ |
585 |
|
|
GLfloat gldepth; |
586 |
|
|
double dist; |
587 |
|
|
|
588 |
|
|
if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres) |
589 |
|
|
return(FHUGE); |
590 |
|
|
if (depthbuffer != NULL) |
591 |
|
|
dist = depthbuffer[dy*odev.hres + dx]; |
592 |
|
|
else { |
593 |
|
|
glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT, |
594 |
|
|
GL_FLOAT, &gldepth); |
595 |
gwlarson |
3.3 |
dist = mapdepth(gldepth); |
596 |
gwlarson |
3.1 |
} |
597 |
|
|
if (dist >= .99*FHUGE) |
598 |
|
|
return(FHUGE); |
599 |
|
|
return((dist-odev.v.vfore)/DOT(direc,odev.v.vdir)); |
600 |
|
|
} |
601 |
|
|
|
602 |
|
|
|
603 |
|
|
#ifdef STEREO |
604 |
|
|
static |
605 |
|
|
pushright() /* push on right view & buffer */ |
606 |
|
|
{ |
607 |
|
|
double d; |
608 |
|
|
|
609 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
610 |
gwlarson |
3.7 |
if (viewflags & VWPERSP) { |
611 |
gwlarson |
3.1 |
glMatrixMode(GL_MODELVIEW); |
612 |
|
|
glPushMatrix(); |
613 |
|
|
d = -eyesepdist / sqrt(odev.v.hn2); |
614 |
|
|
glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], |
615 |
|
|
d*odev.v.hvec[2]); |
616 |
|
|
checkglerr("setting right view"); |
617 |
|
|
} |
618 |
|
|
} |
619 |
|
|
|
620 |
|
|
|
621 |
|
|
static |
622 |
|
|
popright() /* pop off right view & buffer */ |
623 |
|
|
{ |
624 |
gwlarson |
3.7 |
if (viewflags & VWPERSP) { |
625 |
gwlarson |
3.1 |
glMatrixMode(GL_MODELVIEW); |
626 |
|
|
glPopMatrix(); |
627 |
|
|
} |
628 |
|
|
setstereobuf(STEREO_BUFFER_LEFT); |
629 |
|
|
} |
630 |
|
|
#endif |
631 |
|
|
|
632 |
|
|
|
633 |
|
|
static int |
634 |
|
|
mytmflags() /* figure out tone mapping flags */ |
635 |
|
|
{ |
636 |
|
|
extern char *progname; |
637 |
|
|
register char *cp, *tail; |
638 |
|
|
/* find basic name */ |
639 |
|
|
for (cp = tail = progname; *cp; cp++) |
640 |
|
|
if (*cp == '/') |
641 |
|
|
tail = cp+1; |
642 |
|
|
for (cp = tail; *cp && *cp != '.'; cp++) |
643 |
|
|
; |
644 |
|
|
#ifdef DEBUG |
645 |
|
|
if (cp > tail && cp[-1] == 'h') |
646 |
|
|
return(TM_F_HUMAN); |
647 |
|
|
else |
648 |
|
|
return(TM_F_CAMERA); |
649 |
|
|
#else |
650 |
|
|
if (cp > tail && cp[-1] == 'h') |
651 |
|
|
return(TM_F_HUMAN|TM_F_NOSTDERR); |
652 |
|
|
else |
653 |
|
|
return(TM_F_CAMERA|TM_F_NOSTDERR); |
654 |
|
|
#endif |
655 |
|
|
} |
656 |
|
|
|
657 |
|
|
|
658 |
|
|
static |
659 |
|
|
getevent() /* get next event */ |
660 |
|
|
{ |
661 |
|
|
XNextEvent(ourdisplay, levptr(XEvent)); |
662 |
|
|
switch (levptr(XEvent)->type) { |
663 |
|
|
case ConfigureNotify: |
664 |
|
|
resizewindow(levptr(XConfigureEvent)); |
665 |
|
|
break; |
666 |
|
|
case UnmapNotify: |
667 |
gwlarson |
3.7 |
viewflags &= ~VWMAPPED; |
668 |
gwlarson |
3.1 |
break; |
669 |
|
|
case MapNotify: |
670 |
gwlarson |
3.2 |
odRemap(0); |
671 |
gwlarson |
3.7 |
viewflags |= VWMAPPED; |
672 |
gwlarson |
3.1 |
break; |
673 |
|
|
case Expose: |
674 |
|
|
fixwindow(levptr(XExposeEvent)); |
675 |
|
|
break; |
676 |
|
|
case KeyPress: |
677 |
|
|
getkey(levptr(XKeyPressedEvent)); |
678 |
|
|
break; |
679 |
|
|
case ButtonPress: |
680 |
|
|
if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) |
681 |
|
|
getframe(levptr(XButtonPressedEvent)); |
682 |
|
|
else |
683 |
|
|
getmove(levptr(XButtonPressedEvent)); |
684 |
|
|
break; |
685 |
|
|
} |
686 |
|
|
} |
687 |
|
|
|
688 |
|
|
|
689 |
|
|
static |
690 |
|
|
draw3dline(wp) /* draw 3d line in world coordinates */ |
691 |
|
|
register FVECT wp[2]; |
692 |
|
|
{ |
693 |
|
|
glVertex3d(wp[0][0], wp[0][1], wp[0][2]); |
694 |
|
|
glVertex3d(wp[1][0], wp[1][1], wp[1][2]); |
695 |
|
|
} |
696 |
|
|
|
697 |
|
|
|
698 |
|
|
static |
699 |
|
|
draw_grids(fore) /* draw holodeck section grids */ |
700 |
|
|
int fore; |
701 |
|
|
{ |
702 |
|
|
glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT); |
703 |
|
|
glDisable(GL_LIGHTING); |
704 |
|
|
if (fore) |
705 |
gwlarson |
3.8 |
glColor3ub(4, 250, 250); |
706 |
gwlarson |
3.1 |
else |
707 |
|
|
glColor3ub(0, 0, 0); |
708 |
|
|
glBegin(GL_LINES); /* draw each grid line */ |
709 |
|
|
gridlines(draw3dline); |
710 |
|
|
glEnd(); |
711 |
|
|
checkglerr("drawing grid lines"); |
712 |
|
|
glPopAttrib(); |
713 |
|
|
} |
714 |
|
|
|
715 |
|
|
|
716 |
|
|
static |
717 |
|
|
moveview(dx, dy, mov, orb) /* move our view */ |
718 |
|
|
int dx, dy, mov, orb; |
719 |
|
|
{ |
720 |
|
|
VIEW nv; |
721 |
|
|
FVECT odir, v1, wip; |
722 |
|
|
double d, d1; |
723 |
|
|
register int li; |
724 |
|
|
/* start with old view */ |
725 |
|
|
copystruct(&nv, &odev.v); |
726 |
|
|
/* orient our motion */ |
727 |
|
|
if (viewray(v1, odir, &odev.v, |
728 |
|
|
(dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) |
729 |
|
|
return(0); /* outside view */ |
730 |
|
|
if (mov | orb) { /* moving relative to geometry */ |
731 |
|
|
d = getdistance(dx, dy, odir); /* distance from front plane */ |
732 |
|
|
#ifdef DOBJ |
733 |
|
|
d1 = dobj_trace(NULL, v1, odir); |
734 |
|
|
if (d1 < d) |
735 |
|
|
d = d1; |
736 |
|
|
#endif |
737 |
|
|
if (d >= .99*FHUGE) |
738 |
|
|
d = 0.5*(dev_zmax+dev_zmin); /* just guess */ |
739 |
|
|
VSUM(wip, v1, odir, d); |
740 |
|
|
VSUB(odir, wip, odev.v.vp); |
741 |
|
|
} else /* panning with constant viewpoint */ |
742 |
|
|
VCOPY(nv.vdir, odir); |
743 |
|
|
if (orb && mov) { /* orbit left/right */ |
744 |
|
|
spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); |
745 |
|
|
VSUM(nv.vp, wip, odir, -1.); |
746 |
|
|
spinvector(nv.vdir, nv.vdir, nv.vup, d); |
747 |
|
|
} else if (orb) { /* orbit up/down */ |
748 |
|
|
fcross(v1, odir, nv.vup); |
749 |
|
|
if (normalize(v1) == 0.) |
750 |
|
|
return(0); |
751 |
|
|
spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); |
752 |
|
|
VSUM(nv.vp, wip, odir, -1.); |
753 |
|
|
spinvector(nv.vdir, nv.vdir, v1, d); |
754 |
|
|
} else if (mov) { /* move forward/backward */ |
755 |
|
|
d = MOVPCT/100. * mov; |
756 |
|
|
VSUM(nv.vp, nv.vp, odir, d); |
757 |
|
|
} |
758 |
gwlarson |
3.7 |
if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */ |
759 |
gwlarson |
3.1 |
VSUM(v1, odev.v.vp, nv.vp, -1.); |
760 |
|
|
d = DOT(v1, nv.vup); |
761 |
|
|
VSUM(nv.vp, nv.vp, odev.v.vup, d); |
762 |
|
|
} |
763 |
|
|
if (setview(&nv) != NULL) |
764 |
|
|
return(0); /* illegal view */ |
765 |
|
|
dev_view(&nv); |
766 |
gwlarson |
3.2 |
inpresflags |= DFL(DC_SETVIEW); |
767 |
gwlarson |
3.1 |
return(1); |
768 |
|
|
} |
769 |
|
|
|
770 |
|
|
|
771 |
|
|
static |
772 |
|
|
getframe(ebut) /* get focus frame */ |
773 |
|
|
XButtonPressedEvent *ebut; |
774 |
|
|
{ |
775 |
|
|
int startx = ebut->x, starty = ebut->y; |
776 |
|
|
int endx, endy; |
777 |
|
|
|
778 |
|
|
XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); |
779 |
|
|
endx = levptr(XButtonReleasedEvent)->x; |
780 |
|
|
endy = levptr(XButtonReleasedEvent)->y; |
781 |
|
|
if (endx == startx | endy == starty) { |
782 |
|
|
XBell(ourdisplay, 0); |
783 |
|
|
return; |
784 |
|
|
} |
785 |
|
|
if (endx < startx) {register int c = endx; endx = startx; startx = c;} |
786 |
|
|
if (endy < starty) {register int c = endy; endy = starty; starty = c;} |
787 |
|
|
sprintf(odev_args, "%.3f %.3f %.3f %.3f", |
788 |
|
|
(startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres, |
789 |
|
|
(endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres); |
790 |
|
|
inpresflags |= DFL(DC_FOCUS); |
791 |
|
|
} |
792 |
|
|
|
793 |
|
|
|
794 |
|
|
static |
795 |
|
|
getmove(ebut) /* get view change */ |
796 |
|
|
XButtonPressedEvent *ebut; |
797 |
|
|
{ |
798 |
|
|
int movdir = MOVDIR(ebut->button); |
799 |
|
|
int movorb = MOVORB(ebut->state); |
800 |
gwlarson |
3.2 |
int ndrawn; |
801 |
gwlarson |
3.1 |
Window rootw, childw; |
802 |
|
|
int rootx, rooty, wx, wy; |
803 |
|
|
unsigned int statemask; |
804 |
|
|
|
805 |
|
|
XNoOp(ourdisplay); /* makes sure we're not idle */ |
806 |
|
|
|
807 |
gwlarson |
3.7 |
viewflags &= ~VWSTEADY; /* flag moving view */ |
808 |
gwlarson |
3.11 |
setglpersp(); /* start us off in perspective */ |
809 |
gwlarson |
3.1 |
while (!XCheckMaskEvent(ourdisplay, |
810 |
|
|
ButtonReleaseMask, levptr(XEvent))) { |
811 |
|
|
/* get cursor position */ |
812 |
|
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
813 |
|
|
&rootx, &rooty, &wx, &wy, &statemask)) |
814 |
|
|
break; /* on another screen */ |
815 |
|
|
/* compute view motion */ |
816 |
|
|
if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { |
817 |
|
|
sleep(1); |
818 |
|
|
continue; /* cursor in bad place */ |
819 |
|
|
} |
820 |
|
|
draw_grids(1); /* redraw grid */ |
821 |
|
|
#ifdef STEREO |
822 |
|
|
pushright(); |
823 |
|
|
draw_grids(1); |
824 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
825 |
gwlarson |
3.1 |
#ifdef DOBJ |
826 |
gwlarson |
3.3 |
ndrawn += dobj_render(); |
827 |
gwlarson |
3.1 |
#endif |
828 |
|
|
popright(); |
829 |
|
|
#endif |
830 |
|
|
/* redraw octrees */ |
831 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
832 |
gwlarson |
3.1 |
#ifdef DOBJ |
833 |
gwlarson |
3.2 |
ndrawn += dobj_render(); /* redraw objects */ |
834 |
gwlarson |
3.1 |
#endif |
835 |
gwlarson |
3.6 |
glXSwapBuffers(ourdisplay, gwind); |
836 |
|
|
if (!ndrawn) |
837 |
gwlarson |
3.2 |
sleep(1); /* for reasonable interaction */ |
838 |
gwlarson |
3.1 |
} |
839 |
|
|
if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ |
840 |
|
|
movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); |
841 |
|
|
wx = levptr(XButtonReleasedEvent)->x; |
842 |
|
|
wy = levptr(XButtonReleasedEvent)->y; |
843 |
|
|
moveview(wx, odev.vres-1-wy, movdir, movorb); |
844 |
|
|
} |
845 |
gwlarson |
3.7 |
viewflags |= VWSTEADY; /* done goofing around */ |
846 |
gwlarson |
3.1 |
} |
847 |
|
|
|
848 |
|
|
|
849 |
|
|
static |
850 |
gwlarson |
3.11 |
setglpersp() /* set perspective view in GL */ |
851 |
gwlarson |
3.1 |
{ |
852 |
|
|
double d, xmin, xmax, ymin, ymax; |
853 |
|
|
GLfloat vec[4]; |
854 |
|
|
double depthlim[2]; |
855 |
|
|
/* set depth limits */ |
856 |
gwlarson |
3.2 |
gmDepthLimit(depthlim, odev.v.vp, odev.v.vdir); |
857 |
gwlarson |
3.1 |
if (depthlim[0] >= depthlim[1]) { |
858 |
|
|
dev_zmin = 1.; |
859 |
|
|
dev_zmax = 100.; |
860 |
|
|
} else { |
861 |
|
|
dev_zmin = 0.5*depthlim[0]; |
862 |
gwlarson |
3.13 |
dev_zmax = 1.25*depthlim[1]; |
863 |
gwlarson |
3.1 |
if (dev_zmin > dev_zmax/5.) |
864 |
|
|
dev_zmin = dev_zmax/5.; |
865 |
|
|
} |
866 |
|
|
if (odev.v.vfore > FTINY) |
867 |
|
|
dev_zmin = odev.v.vfore; |
868 |
|
|
if (odev.v.vaft > FTINY) |
869 |
|
|
dev_zmax = odev.v.vaft; |
870 |
|
|
if (dev_zmin < dev_zmax/100.) |
871 |
|
|
dev_zmin = dev_zmax/100.; |
872 |
|
|
setzrat(); |
873 |
|
|
xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz); |
874 |
|
|
xmin = -xmax; |
875 |
|
|
d = odev.v.hoff * (xmax - xmin); |
876 |
|
|
xmin += d; xmax += d; |
877 |
|
|
ymax = dev_zmin * tan(PI/180./2. * odev.v.vert); |
878 |
|
|
ymin = -ymax; |
879 |
|
|
d = odev.v.voff * (ymax - ymin); |
880 |
|
|
ymin += d; ymax += d; |
881 |
|
|
/* set view matrix */ |
882 |
|
|
glMatrixMode(GL_PROJECTION); |
883 |
|
|
glLoadIdentity(); |
884 |
|
|
glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax); |
885 |
|
|
gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], |
886 |
|
|
odev.v.vp[0] + odev.v.vdir[0], |
887 |
|
|
odev.v.vp[1] + odev.v.vdir[1], |
888 |
|
|
odev.v.vp[2] + odev.v.vdir[2], |
889 |
|
|
odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); |
890 |
|
|
checkglerr("setting perspective view"); |
891 |
|
|
vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.; |
892 |
|
|
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec); |
893 |
|
|
vec[0] = -odev.v.vdir[0]; |
894 |
|
|
vec[1] = -odev.v.vdir[1]; |
895 |
|
|
vec[2] = -odev.v.vdir[2]; |
896 |
|
|
vec[3] = 0.; |
897 |
|
|
glLightfv(GL_LIGHT0, GL_POSITION, vec); |
898 |
|
|
vec[0] = vec[1] = vec[2] = .7; vec[3] = 1.; |
899 |
|
|
glLightfv(GL_LIGHT0, GL_SPECULAR, vec); |
900 |
|
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, vec); |
901 |
|
|
vec[0] = vec[1] = vec[2] = .3; vec[3] = 1.; |
902 |
|
|
glLightfv(GL_LIGHT0, GL_AMBIENT, vec); |
903 |
|
|
glEnable(GL_LIGHT0); |
904 |
|
|
glEnable(GL_LIGHTING); /* light our GL objects */ |
905 |
|
|
glShadeModel(GL_SMOOTH); |
906 |
gwlarson |
3.7 |
viewflags &= ~VWORTHO; |
907 |
|
|
viewflags |= VWPERSP; |
908 |
gwlarson |
3.1 |
} |
909 |
|
|
|
910 |
|
|
|
911 |
|
|
static |
912 |
|
|
setglortho() /* set up orthographic view for cone drawing */ |
913 |
|
|
{ |
914 |
gwlarson |
3.6 |
glDrawBuffer(GL_FRONT); /* use single-buffer mode */ |
915 |
gwlarson |
3.1 |
/* set view matrix */ |
916 |
|
|
glMatrixMode(GL_PROJECTION); |
917 |
|
|
glLoadIdentity(); |
918 |
|
|
glOrtho(0., (double)odev.hres, 0., (double)odev.vres, |
919 |
|
|
0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH)); |
920 |
|
|
checkglerr("setting orthographic view"); |
921 |
|
|
glDisable(GL_LIGHTING); /* cones are constant color */ |
922 |
|
|
glShadeModel(GL_FLAT); |
923 |
gwlarson |
3.7 |
viewflags &= ~VWPERSP; |
924 |
|
|
viewflags |= VWORTHO; |
925 |
gwlarson |
3.1 |
} |
926 |
|
|
|
927 |
|
|
|
928 |
|
|
static |
929 |
|
|
wipeclean() /* prepare for redraw */ |
930 |
|
|
{ |
931 |
gwlarson |
3.6 |
glDrawBuffer(GL_BACK); /* use double-buffer mode */ |
932 |
gwlarson |
3.9 |
glReadBuffer(GL_BACK); |
933 |
gwlarson |
3.6 |
/* clear buffers */ |
934 |
gwlarson |
3.1 |
#ifdef STEREO |
935 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
936 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
937 |
gwlarson |
3.1 |
setstereobuf(STEREO_BUFFER_LEFT); |
938 |
|
|
#endif |
939 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
940 |
|
|
freedepth(); |
941 |
gwlarson |
3.7 |
if ((viewflags&(VWCHANGE|VWSTEADY)) == |
942 |
|
|
(VWCHANGE|VWSTEADY)) { /* clear samples if new */ |
943 |
gwlarson |
3.1 |
odClean(); |
944 |
gwlarson |
3.7 |
viewflags &= ~VWCHANGE; /* change noted */ |
945 |
gwlarson |
3.10 |
} else if (viewflags & VWSTEADY) |
946 |
|
|
odRedrawAll(); |
947 |
gwlarson |
3.1 |
setglpersp(&odev.v); /* reset view & clipping planes */ |
948 |
|
|
} |
949 |
|
|
|
950 |
|
|
|
951 |
|
|
static |
952 |
|
|
getkey(ekey) /* get input key */ |
953 |
|
|
register XKeyPressedEvent *ekey; |
954 |
|
|
{ |
955 |
|
|
Window rootw, childw; |
956 |
|
|
int rootx, rooty, wx, wy; |
957 |
|
|
unsigned int statemask; |
958 |
|
|
int n; |
959 |
|
|
char buf[8]; |
960 |
|
|
|
961 |
|
|
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
962 |
|
|
if (n != 1) |
963 |
|
|
return; |
964 |
|
|
switch (buf[0]) { |
965 |
|
|
case 'h': /* turn on height motion lock */ |
966 |
gwlarson |
3.7 |
viewflags |= VWHEADLOCK; |
967 |
gwlarson |
3.1 |
return; |
968 |
|
|
case 'H': /* turn off height motion lock */ |
969 |
gwlarson |
3.7 |
viewflags &= ~VWHEADLOCK; |
970 |
gwlarson |
3.1 |
return; |
971 |
|
|
case 'l': /* retrieve last view */ |
972 |
|
|
inpresflags |= DFL(DC_LASTVIEW); |
973 |
|
|
return; |
974 |
|
|
case 'p': /* pause computation */ |
975 |
|
|
inpresflags |= DFL(DC_PAUSE); |
976 |
|
|
return; |
977 |
|
|
case 'v': /* spit out view */ |
978 |
|
|
inpresflags |= DFL(DC_GETVIEW); |
979 |
|
|
return; |
980 |
|
|
case 'f': /* frame view position */ |
981 |
|
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
982 |
|
|
&rootx, &rooty, &wx, &wy, &statemask)) |
983 |
|
|
return; /* on another screen */ |
984 |
|
|
sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres, |
985 |
|
|
1.-(wy+.5)/odev.vres); |
986 |
|
|
inpresflags |= DFL(DC_FOCUS); |
987 |
|
|
return; |
988 |
|
|
case 'F': /* unfocus */ |
989 |
|
|
odev_args[0] = '\0'; |
990 |
|
|
inpresflags |= DFL(DC_FOCUS); |
991 |
|
|
return; |
992 |
|
|
case '\n': |
993 |
|
|
case '\r': /* resume computation */ |
994 |
|
|
inpresflags |= DFL(DC_RESUME); |
995 |
|
|
return; |
996 |
|
|
case CTRL('R'): /* redraw screen */ |
997 |
gwlarson |
3.10 |
odRemap(0); /* new tone mapping */ |
998 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT); |
999 |
gwlarson |
3.1 |
#ifdef STEREO |
1000 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
1001 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT); |
1002 |
gwlarson |
3.1 |
setstereobuf(STEREO_BUFFER_LEFT); |
1003 |
|
|
#endif |
1004 |
|
|
return; |
1005 |
|
|
case CTRL('L'): /* refresh from server */ |
1006 |
|
|
if (inpresflags & DFL(DC_REDRAW)) |
1007 |
gwlarson |
3.12 |
return; /* already called */ |
1008 |
gwlarson |
3.1 |
XRaiseWindow(ourdisplay, gwind); |
1009 |
gwlarson |
3.11 |
XFlush(ourdisplay); /* raise up window */ |
1010 |
gwlarson |
3.10 |
sleep(1); /* wait for restacking */ |
1011 |
gwlarson |
3.11 |
dev_clear(); /* clear buffer and samples */ |
1012 |
gwlarson |
3.10 |
odRemap(1); /* start fresh histogram */ |
1013 |
gwlarson |
3.1 |
inpresflags |= DFL(DC_REDRAW); /* resend values from server */ |
1014 |
|
|
return; |
1015 |
|
|
case 'K': /* kill rtrace process(es) */ |
1016 |
|
|
inpresflags |= DFL(DC_KILL); |
1017 |
|
|
break; |
1018 |
|
|
case 'R': /* restart rtrace */ |
1019 |
|
|
inpresflags |= DFL(DC_RESTART); |
1020 |
|
|
break; |
1021 |
|
|
case 'C': /* clobber holodeck */ |
1022 |
|
|
inpresflags |= DFL(DC_CLOBBER); |
1023 |
|
|
break; |
1024 |
|
|
case 'q': /* quit the program */ |
1025 |
|
|
inpresflags |= DFL(DC_QUIT); |
1026 |
|
|
return; |
1027 |
|
|
default: |
1028 |
|
|
XBell(ourdisplay, 0); |
1029 |
|
|
return; |
1030 |
|
|
} |
1031 |
|
|
} |
1032 |
|
|
|
1033 |
|
|
|
1034 |
|
|
static |
1035 |
|
|
fixwindow(eexp) /* repair damage to window */ |
1036 |
|
|
register XExposeEvent *eexp; |
1037 |
|
|
{ |
1038 |
|
|
int xmin, ymin, xmax, ymax; |
1039 |
|
|
|
1040 |
|
|
if (odev.hres == 0 | odev.vres == 0) { /* first exposure */ |
1041 |
|
|
resizewindow((XConfigureEvent *)eexp); |
1042 |
|
|
return; |
1043 |
|
|
} |
1044 |
|
|
xmin = eexp->x; xmax = eexp->x + eexp->width; |
1045 |
|
|
ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y; |
1046 |
gwlarson |
3.10 |
|
1047 |
|
|
if (xmin <= 0 && xmax >= odev.hres-1 && |
1048 |
|
|
ymin <= 0 && ymax >= odev.vres) { |
1049 |
|
|
DCHECK(eexp->count, WARNING, "multiple clear in fixwindow"); |
1050 |
gwlarson |
3.11 |
wipeclean(); /* make sure we're go */ |
1051 |
gwlarson |
3.10 |
return; |
1052 |
|
|
} |
1053 |
gwlarson |
3.1 |
/* clear portion of depth */ |
1054 |
gwlarson |
3.2 |
glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
1055 |
gwlarson |
3.1 |
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
1056 |
|
|
glDepthFunc(GL_ALWAYS); |
1057 |
|
|
glBegin(GL_POLYGON); |
1058 |
|
|
glVertex3i(xmin, ymin, OMAXDEPTH); |
1059 |
|
|
glVertex3i(xmax, ymin, OMAXDEPTH); |
1060 |
|
|
glVertex3i(xmax, ymax, OMAXDEPTH); |
1061 |
|
|
glVertex3i(xmin, ymax, OMAXDEPTH); |
1062 |
|
|
glEnd(); |
1063 |
|
|
#ifdef STEREO |
1064 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
1065 |
|
|
glBegin(GL_POLYGON); |
1066 |
|
|
glVertex3i(xmin, ymin, OMAXDEPTH); |
1067 |
|
|
glVertex3i(xmax, ymin, OMAXDEPTH); |
1068 |
|
|
glVertex3i(xmax, ymax, OMAXDEPTH); |
1069 |
|
|
glVertex3i(xmin, ymax, OMAXDEPTH); |
1070 |
|
|
glEnd(); |
1071 |
|
|
odRedraw(1, xmin, ymin, xmax, ymax); |
1072 |
|
|
setstereobuf(STEREO_BUFFER_LEFT); |
1073 |
|
|
#endif |
1074 |
gwlarson |
3.2 |
glPopAttrib(); |
1075 |
gwlarson |
3.1 |
odRedraw(0, xmin, ymin, xmax, ymax); |
1076 |
|
|
} |
1077 |
|
|
|
1078 |
|
|
|
1079 |
|
|
static |
1080 |
|
|
resizewindow(ersz) /* resize window */ |
1081 |
|
|
register XConfigureEvent *ersz; |
1082 |
|
|
{ |
1083 |
|
|
glViewport(0, 0, ersz->width, ersz->height); |
1084 |
|
|
|
1085 |
|
|
if (ersz->width == odev.hres && ersz->height == odev.vres) |
1086 |
|
|
return; |
1087 |
|
|
|
1088 |
|
|
odev.hres = ersz->width; |
1089 |
|
|
odev.vres = ersz->height; |
1090 |
|
|
|
1091 |
|
|
odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); |
1092 |
|
|
odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); |
1093 |
|
|
|
1094 |
gwlarson |
3.2 |
inpresflags |= DFL(DC_SETVIEW); |
1095 |
gwlarson |
3.7 |
viewflags |= VWCHANGE; |
1096 |
gwlarson |
3.1 |
} |