10 |
|
*/ |
11 |
|
|
12 |
|
#include "standard.h" |
13 |
< |
#include "tonemap.h" |
14 |
< |
#include "rhdriver.h" |
13 |
> |
#include "rhd_qtree.h" |
14 |
|
|
15 |
|
#include <GL/glx.h> |
16 |
|
|
17 |
|
#include "x11icon.h" |
18 |
|
|
19 |
+ |
#ifndef RAYQLEN |
20 |
+ |
#define RAYQLEN 50000 /* max. rays to queue before flush */ |
21 |
+ |
#endif |
22 |
+ |
|
23 |
|
#ifndef FEQ |
24 |
|
#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) |
25 |
|
#endif |
26 |
|
|
27 |
< |
#ifndef int4 |
28 |
< |
#define int4 int |
27 |
> |
#ifndef MAXCONE |
28 |
> |
#define MAXCONE 16 /* number of different cone sizes */ |
29 |
|
#endif |
30 |
< |
|
31 |
< |
#ifndef FREEPCT |
29 |
< |
#define FREEPCT 10 /* percentage of values to free */ |
30 |
> |
#ifndef MAXVERT |
31 |
> |
#define MAXVERT 32 /* maximum number of cone vertices */ |
32 |
|
#endif |
33 |
< |
|
34 |
< |
#ifndef NCONEV |
33 |
< |
#define NCONEV 7 /* number of cone base vertices */ |
33 |
> |
#ifndef MINVERT |
34 |
> |
#define MINVERT 4 /* minimum number of cone vertices */ |
35 |
|
#endif |
36 |
< |
#ifndef CONEH |
37 |
< |
#define CONEH 3. /* cone height (fraction of depth) */ |
36 |
> |
#ifndef DEPTHFACT |
37 |
> |
#define DEPTHFACT 16. /* multiplier for depth tests */ |
38 |
|
#endif |
38 |
– |
#ifndef CONEW |
39 |
– |
#define CONEW 0.05 /* cone width (fraction of screen) */ |
40 |
– |
#endif |
41 |
– |
#ifndef DIRPEN |
42 |
– |
#define DIRPEN 0.001 /* direction penalty factor */ |
43 |
– |
#endif |
44 |
– |
#ifndef VALUA |
45 |
– |
#define VALUA 16 /* target value area (pixels) */ |
46 |
– |
#endif |
39 |
|
|
40 |
|
#define GAMMA 1.4 /* default gamma correction */ |
41 |
|
|
70 |
|
static Window gwind = 0; /* our graphics window */ |
71 |
|
static GLXContext gctx; /* our GLX context */ |
72 |
|
|
81 |
– |
static double mindepth = FHUGE; /* minimum depth value so far */ |
82 |
– |
static double maxdepth = 0.; /* maximum depth value so far */ |
83 |
– |
|
73 |
|
static double pwidth, pheight; /* pixel dimensions (mm) */ |
74 |
|
|
75 |
< |
static FVECT conev[NCONEV]; /* drawing cone */ |
76 |
< |
static double coneh; /* cone height */ |
75 |
> |
static double curzmax = 1e4; /* current depth upper limit */ |
76 |
> |
static double nxtzmax = 0.; /* maximum (finite) depth so far */ |
77 |
|
|
78 |
+ |
static struct { |
79 |
+ |
double rad; /* cone radius */ |
80 |
+ |
int nverts; /* number of vertices */ |
81 |
+ |
FVECT *va; /* allocated vertex array */ |
82 |
+ |
} cone[MAXCONE]; /* precomputed cones for drawing */ |
83 |
+ |
|
84 |
|
static int inpresflags; /* input result flags */ |
85 |
|
|
86 |
|
static int headlocked = 0; /* lock vertical motion */ |
87 |
|
|
93 |
– |
static int quicken = 0; /* quicker, sloppier update rate? */ |
94 |
– |
|
95 |
– |
static struct { |
96 |
– |
float (*wp)[3]; /* world intersection point array */ |
97 |
– |
int4 *wd; /* world direction array */ |
98 |
– |
TMbright *brt; /* encoded brightness array */ |
99 |
– |
BYTE (*chr)[3]; /* encoded chrominance array */ |
100 |
– |
BYTE (*rgb)[3]; /* tone-mapped color array */ |
101 |
– |
BYTE *alpha; /* alpha values */ |
102 |
– |
int nl; /* count of values */ |
103 |
– |
int bl, tl; /* bottom and top (next) value index */ |
104 |
– |
int tml; /* next value needing tone-mapping */ |
105 |
– |
int drl; /* next value in need of drawing */ |
106 |
– |
char *base; /* base of allocated memory */ |
107 |
– |
} rV; /* our collection of values */ |
108 |
– |
|
109 |
– |
static int *valmap = NULL; /* sorted map of screen values */ |
110 |
– |
static int vmaplen = 0; /* value map length */ |
111 |
– |
|
112 |
– |
#define redraw() (rV.drl = rV.bl) |
113 |
– |
|
88 |
|
static int resizewindow(), getevent(), getkey(), moveview(), |
89 |
< |
setGLview(), getmove(), fixwindow(), mytmflags(), |
90 |
< |
drawvalue(), valcmp(), clralphas(), setalphas(), mergalphas(), |
117 |
< |
IndexValue(), Compost(), FindValue(), TMapValues(), |
118 |
< |
AllocValues(), FreeValues(); |
89 |
> |
initcones(), freecones(), |
90 |
> |
getmove(), fixwindow(), mytmflags(); |
91 |
|
|
120 |
– |
extern int4 encodedir(); |
121 |
– |
extern double fdir2diff(), dir2diff(); |
92 |
|
|
123 |
– |
|
93 |
|
dev_open(id) /* initialize X11 driver */ |
94 |
|
char *id; |
95 |
|
{ |
96 |
|
extern char *getenv(); |
97 |
|
static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, |
98 |
|
GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, |
99 |
< |
GLX_ALPHA_SIZE,8, GLX_DEPTH_SIZE,15, |
131 |
< |
None}; |
99 |
> |
GLX_DEPTH_SIZE,15, None}; |
100 |
|
char *gv; |
101 |
|
double gamval = GAMMA; |
102 |
|
XSetWindowAttributes ourwinattr; |
103 |
|
XWMHints ourxwmhints; |
104 |
|
XSizeHints oursizhints; |
105 |
+ |
/* set quadtree globals */ |
106 |
+ |
qtMinNodesiz = 3; |
107 |
+ |
qtDepthEps = 0.07; |
108 |
|
/* open display server */ |
109 |
|
ourdisplay = XOpenDisplay(NULL); |
110 |
|
if (ourdisplay == NULL) |
152 |
|
glDepthFunc(GL_LEQUAL); |
153 |
|
glShadeModel(GL_FLAT); |
154 |
|
glDisable(GL_DITHER); |
155 |
+ |
glDisable(GL_CULL_FACE); |
156 |
+ |
glMatrixMode(GL_PROJECTION); |
157 |
+ |
glOrtho(0., 1., 0., 1., -.01, 1.01); |
158 |
+ |
glTranslated(0., 0., -1.01); |
159 |
|
/* figure out sensible view */ |
160 |
|
pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / |
161 |
|
DisplayWidth(ourdisplay, ourscreen); |
166 |
|
/* map the window */ |
167 |
|
XMapWindow(ourdisplay, gwind); |
168 |
|
dev_input(); /* sets size and view angles */ |
169 |
< |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
170 |
< |
/* allocate our value list */ |
171 |
< |
if (!AllocValues(DisplayWidth(ourdisplay,ourscreen) * |
172 |
< |
DisplayHeight(ourdisplay,ourscreen) / VALUA)) |
169 |
> |
/* allocate our leaf pile */ |
170 |
> |
if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) * |
171 |
> |
DisplayHeight(ourdisplay,ourscreen) / |
172 |
> |
(qtMinNodesiz*qtMinNodesiz))) |
173 |
|
error(SYSTEM, "insufficient memory for value storage"); |
174 |
|
odev.name = id; |
175 |
|
odev.ifd = ConnectionNumber(ourdisplay); |
176 |
+ |
/* initialize cone array */ |
177 |
+ |
initcones(); |
178 |
|
} |
179 |
|
|
180 |
|
|
186 |
|
gwind = 0; |
187 |
|
XCloseDisplay(ourdisplay); |
188 |
|
ourdisplay = NULL; |
189 |
+ |
qtFreeLeaves(); |
190 |
|
tmDone(NULL); |
191 |
< |
FreeValues(); |
191 |
> |
freecones(); |
192 |
|
odev.v.type = 0; |
193 |
|
odev.hres = odev.vres = 0; |
194 |
|
odev.ifd = -1; |
199 |
|
dev_view(nv) /* assign new driver view */ |
200 |
|
register VIEW *nv; |
201 |
|
{ |
202 |
< |
if (nv->type != VT_PER || /* check view legality */ |
203 |
< |
nv->horiz > 120. || nv->vert > 120.) { |
202 |
> |
if (nv->type == VT_PAR || /* check view legality */ |
203 |
> |
nv->horiz > 160. || nv->vert > 160.) { |
204 |
|
error(COMMAND, "illegal view type/angle"); |
205 |
< |
nv->type = VT_PER; |
205 |
> |
nv->type = odev.v.type; |
206 |
|
nv->horiz = odev.v.horiz; |
207 |
|
nv->vert = odev.v.vert; |
208 |
|
return(0); |
209 |
|
} |
210 |
+ |
if (nv->vfore > FTINY) { |
211 |
+ |
error(COMMAND, "cannot handle fore clipping"); |
212 |
+ |
nv->vfore = 0.; |
213 |
+ |
return(0); |
214 |
+ |
} |
215 |
|
if (nv != &odev.v) { |
216 |
|
if (!FEQ(nv->horiz,odev.v.horiz) || /* resize window? */ |
217 |
|
!FEQ(nv->vert,odev.v.vert)) { |
236 |
|
dev_input(); /* get resize event */ |
237 |
|
} |
238 |
|
copystruct(&odev.v, nv); |
256 |
– |
setGLview(); |
257 |
– |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
258 |
– |
redraw(); |
239 |
|
} |
240 |
+ |
if (nxtzmax > FTINY) { |
241 |
+ |
curzmax = nxtzmax; |
242 |
+ |
nxtzmax = 0.; |
243 |
+ |
} |
244 |
+ |
glClear(GL_DEPTH_BUFFER_BIT); |
245 |
+ |
qtReplant(); |
246 |
|
return(1); |
247 |
|
} |
248 |
|
|
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
< |
dev_value(c, p, v) /* add a pixel value to our list */ |
265 |
< |
COLR c; |
280 |
< |
FVECT p, v; |
264 |
> |
int |
265 |
> |
dev_flush() /* flush output */ |
266 |
|
{ |
267 |
< |
register int li; |
268 |
< |
|
269 |
< |
li = rV.tl++; |
270 |
< |
if (rV.tl >= rV.nl) /* get next leaf in ring */ |
286 |
< |
rV.tl = 0; |
287 |
< |
if (rV.tl == rV.bl) /* need to shake some free */ |
288 |
< |
Compost(FREEPCT); |
289 |
< |
VCOPY(rV.wp[li], p); |
290 |
< |
rV.wd[li] = encodedir(v); |
291 |
< |
tmCvColrs(&rV.brt[li], rV.chr[li], c, 1); |
267 |
> |
qtUpdate(); |
268 |
> |
glFlush(); |
269 |
> |
rayqleft = RAYQLEN; |
270 |
> |
return(XPending(ourdisplay)); |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
< |
int |
275 |
< |
dev_flush() /* flush output */ |
274 |
> |
dev_cone(rgb, ip, rad) /* render a cone in view coordinates */ |
275 |
> |
BYTE rgb[3]; |
276 |
> |
FVECT ip; |
277 |
> |
double rad; |
278 |
|
{ |
279 |
< |
if (mapped) { |
280 |
< |
TMapValues(0); |
281 |
< |
while (rV.drl != rV.tl) { |
282 |
< |
drawvalue(rV.drl); |
283 |
< |
if (++rV.drl >= rV.nl) |
284 |
< |
rV.drl = 0; |
285 |
< |
} |
286 |
< |
glFlush(); |
279 |
> |
register int ci, j; |
280 |
> |
double apexh, basez; |
281 |
> |
/* compute apex height (0. to 1.) */ |
282 |
> |
if (ip[2] > 1e6) |
283 |
> |
apexh = 1. - 1./DEPTHFACT; |
284 |
> |
else { |
285 |
> |
if (ip[2] > nxtzmax) |
286 |
> |
nxtzmax = ip[2]; |
287 |
> |
if (ip[2] >= curzmax) |
288 |
> |
apexh = 1. - 1./DEPTHFACT; |
289 |
> |
else |
290 |
> |
apexh = 1. - ip[2]/(curzmax*DEPTHFACT); |
291 |
|
} |
292 |
< |
return(XPending(ourdisplay)); |
292 |
> |
rad *= 1.25; /* find conservative cone match */ |
293 |
> |
for (ci = 0; ci < MAXCONE-1; ci++) |
294 |
> |
if (cone[ci].rad >= rad) |
295 |
> |
break; |
296 |
> |
/* draw it */ |
297 |
> |
glColor3ub(rgb[0], rgb[1], rgb[2]); |
298 |
> |
glBegin(GL_TRIANGLE_FAN); |
299 |
> |
glVertex3d(ip[0], ip[1], apexh); /* start with apex */ |
300 |
> |
basez = apexh*cone[ci].va[0][2]; /* base z's all the same */ |
301 |
> |
for (j = 0; j < cone[ci].nverts; j++) /* draw each face */ |
302 |
> |
glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1], |
303 |
> |
basez); |
304 |
> |
/* connect last to first */ |
305 |
> |
glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez); |
306 |
> |
glEnd(); /* all done */ |
307 |
|
} |
308 |
|
|
309 |
|
|
319 |
|
for (cp = tail; *cp && *cp != '.'; cp++) |
320 |
|
; |
321 |
|
if (cp-tail == 3 && !strncmp(tail, "glx", 3)) |
322 |
< |
return(TM_F_CAMERA); |
322 |
> |
return(TM_F_CAMERA|TM_F_NOSTDERR); |
323 |
|
if (cp-tail == 4 && !strncmp(tail, "glxh", 4)) |
324 |
< |
return(TM_F_HUMAN); |
324 |
> |
return(TM_F_HUMAN|TM_F_NOSTDERR); |
325 |
|
error(USER, "illegal driver name"); |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
|
static |
330 |
< |
setGLview() /* set our GL view */ |
330 |
> |
initcones() /* initialize cone vertices */ |
331 |
|
{ |
333 |
– |
double xmin, xmax, ymin, ymax, zmin, zmax; |
334 |
– |
double d, cx, sx, crad; |
335 |
– |
FVECT vx, vy; |
332 |
|
register int i, j; |
333 |
< |
/* compute view frustum */ |
338 |
< |
if (normalize(odev.v.vdir) == 0.0) |
339 |
< |
return; |
340 |
< |
if (mindepth < maxdepth) { |
341 |
< |
zmin = 0.25*mindepth; |
342 |
< |
zmax = 4.0*(1.+CONEH)*maxdepth; |
343 |
< |
} else { |
344 |
< |
zmin = 0.01; |
345 |
< |
zmax = 1000.; |
346 |
< |
} |
347 |
< |
if (odev.v.vfore > FTINY) |
348 |
< |
zmin = odev.v.vfore; |
349 |
< |
if (odev.v.vaft > FTINY) |
350 |
< |
zmax = odev.v.vaft; |
351 |
< |
xmax = zmin * tan(PI/180./2. * odev.v.horiz); |
352 |
< |
xmin = -xmax; |
353 |
< |
d = odev.v.hoff * (xmax - xmin); |
354 |
< |
xmin += d; xmax += d; |
355 |
< |
ymax = zmin * tan(PI/180./2. * odev.v.vert); |
356 |
< |
ymin = -ymax; |
357 |
< |
d = odev.v.voff * (ymax - ymin); |
358 |
< |
ymin += d; ymax += d; |
359 |
< |
/* set view matrix */ |
360 |
< |
glMatrixMode(GL_PROJECTION); |
361 |
< |
glLoadIdentity(); |
362 |
< |
glFrustum(xmin, xmax, ymin, ymax, zmin, zmax); |
363 |
< |
gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], |
364 |
< |
odev.v.vp[0] + odev.v.vdir[0], |
365 |
< |
odev.v.vp[1] + odev.v.vdir[1], |
366 |
< |
odev.v.vp[2] + odev.v.vdir[2], |
367 |
< |
odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); |
368 |
< |
/* set viewport */ |
369 |
< |
glViewport(0, 0, odev.hres, odev.vres); |
370 |
< |
/* initialize cone for Vornoi polys */ |
371 |
< |
coneh = CONEH*(zmax - zmin); |
372 |
< |
crad = 0.5 * CONEW * 0.5*(xmax-xmin + ymax-ymin) * (zmin+coneh)/zmin; |
373 |
< |
vy[0] = vy[1] = vy[2] = 0.; |
374 |
< |
for (i = 0; i < 3; i++) |
375 |
< |
if (odev.v.vdir[i] < 0.6 && odev.v.vdir[i] > -0.6) |
376 |
< |
break; |
377 |
< |
vy[i] = 1.; |
378 |
< |
fcross(vx, vy, odev.v.vdir); |
379 |
< |
normalize(vx); |
380 |
< |
fcross(vy, odev.v.vdir, vx); |
381 |
< |
for (j = 0, d = 0.; j < NCONEV; j++, d += 2.*PI/NCONEV) { |
382 |
< |
cx = crad*cos(d); sx = crad*sin(d); |
383 |
< |
for (i = 0; i < 3; i++) |
384 |
< |
conev[j][i] = coneh*odev.v.vdir[i] + |
385 |
< |
cx*vx[i] + sx*vy[i]; |
386 |
< |
} |
387 |
< |
} |
333 |
> |
double minrad, d; |
334 |
|
|
335 |
< |
|
336 |
< |
#define SUCCSTEP 8 /* skip step when successful */ |
337 |
< |
#define MAXSTEP 64 |
338 |
< |
|
339 |
< |
static |
340 |
< |
drawvalue(li) /* draw a pixel value as a cone */ |
341 |
< |
register int li; |
342 |
< |
{ |
343 |
< |
static int skipstep = 1; |
344 |
< |
static FVECT disp; |
345 |
< |
FVECT apex; |
346 |
< |
double d, dorg, dnew, h, v; |
347 |
< |
register int i; |
348 |
< |
/* check for quicker update */ |
349 |
< |
if (quicken) { |
350 |
< |
if (li % skipstep) |
405 |
< |
return; |
406 |
< |
if (skipstep < MAXSTEP) |
407 |
< |
skipstep++; |
408 |
< |
} |
409 |
< |
/* compute cone coordinates */ |
410 |
< |
disp[0] = rV.wp[li][0] - odev.v.vp[0]; |
411 |
< |
disp[1] = rV.wp[li][1] - odev.v.vp[1]; |
412 |
< |
disp[2] = rV.wp[li][2] - odev.v.vp[2]; |
413 |
< |
dorg = DOT(disp,odev.v.vdir); |
414 |
< |
if (dorg <= odev.v.vfore) |
415 |
< |
return; /* clipped too near */ |
416 |
< |
if (odev.v.vaft > FTINY && dorg > odev.v.vaft) |
417 |
< |
return; /* clipped too far */ |
418 |
< |
if (dorg > 1e5) { /* background pixel */ |
419 |
< |
dnew = maxdepth; |
420 |
< |
d = dnew/dorg; |
421 |
< |
dorg = maxdepth; |
422 |
< |
} else { /* foreground pixel, compute penalty */ |
423 |
< |
normalize(disp); |
424 |
< |
d = dnew = dorg + coneh*fdir2diff(rV.wd[li],disp)*DIRPEN; |
425 |
< |
} |
426 |
< |
/* compute adjusted apex position */ |
427 |
< |
disp[0] *= d; disp[1] *= d; disp[2] *= d; |
428 |
< |
apex[0] = odev.v.vp[0] + disp[0]; |
429 |
< |
apex[1] = odev.v.vp[1] + disp[1]; |
430 |
< |
apex[2] = odev.v.vp[2] + disp[2]; |
431 |
< |
/* compute view position and base offset */ |
432 |
< |
h = DOT(disp,odev.v.hvec)/(dnew*odev.v.hn2); |
433 |
< |
v = DOT(disp,odev.v.vvec)/(dnew*odev.v.vn2); |
434 |
< |
if (fabs(h - odev.v.hoff) > 0.5 || fabs(v - odev.v.voff) > 0.5) |
435 |
< |
return; /* clipped off screen */ |
436 |
< |
if (dorg < mindepth) |
437 |
< |
mindepth = dorg; |
438 |
< |
if (dorg > maxdepth) |
439 |
< |
maxdepth = dorg; |
440 |
< |
for (i = 0; i < 3; i++) |
441 |
< |
disp[i] = apex[i] + coneh*(h*odev.v.hvec[i] + v*odev.v.vvec[i]); |
442 |
< |
/* draw cone (pyramid approx.) */ |
443 |
< |
glColor4ub(rV.rgb[li][0], rV.rgb[li][1], rV.rgb[li][2], rV.alpha[li]); |
444 |
< |
glBegin(GL_TRIANGLE_FAN); |
445 |
< |
glVertex3f(apex[0], apex[1], apex[2]); |
446 |
< |
for (i = 0; i < NCONEV; i++) |
447 |
< |
glVertex3d(conev[i][0] + disp[0], conev[i][1] + disp[1], |
448 |
< |
conev[i][2] + disp[2]); |
449 |
< |
/* connect last face to first */ |
450 |
< |
glVertex3d(conev[0][0] + disp[0], conev[0][1] + disp[1], |
451 |
< |
conev[0][2] + disp[2]); |
452 |
< |
glEnd(); /* done */ |
453 |
< |
skipstep = SUCCSTEP; |
454 |
< |
} |
455 |
< |
|
456 |
< |
#undef SUCCSTEP |
457 |
< |
#undef MAXSTEP |
458 |
< |
|
459 |
< |
|
460 |
< |
#define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\ |
461 |
< |
sizeof(TMbright)+7*sizeof(BYTE)) |
462 |
< |
|
463 |
< |
static |
464 |
< |
AllocValues(n) /* allocate space for n values */ |
465 |
< |
register int n; |
466 |
< |
{ |
467 |
< |
unsigned nbytes; |
468 |
< |
register unsigned i; |
469 |
< |
|
470 |
< |
if (n <= 0) |
471 |
< |
return(0); |
472 |
< |
if (rV.nl >= n) |
473 |
< |
return(rV.nl); |
474 |
< |
else if (rV.nl > 0) |
475 |
< |
free(rV.base); |
476 |
< |
/* round space up to nearest power of 2 */ |
477 |
< |
nbytes = n*LEAFSIZ + 8; |
478 |
< |
for (i = 1024; nbytes > i; i <<= 1) |
479 |
< |
; |
480 |
< |
n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */ |
481 |
< |
rV.base = (char *)malloc(n*LEAFSIZ); |
482 |
< |
if (rV.base == NULL) |
483 |
< |
return(0); |
484 |
< |
/* assign larger alignment types earlier */ |
485 |
< |
rV.wp = (float (*)[3])rV.base; |
486 |
< |
rV.wd = (int4 *)(rV.wp + n); |
487 |
< |
rV.brt = (TMbright *)(rV.wd + n); |
488 |
< |
rV.chr = (BYTE (*)[3])(rV.brt + n); |
489 |
< |
rV.rgb = (BYTE (*)[3])(rV.chr + n); |
490 |
< |
rV.alpha = (BYTE *)(rV.rgb + n); |
491 |
< |
rV.nl = n; |
492 |
< |
rV.drl = rV.tml = rV.bl = rV.tl = 0; |
493 |
< |
return(n); |
494 |
< |
} |
495 |
< |
|
496 |
< |
#undef LEAFSIZ |
497 |
< |
|
498 |
< |
|
499 |
< |
static |
500 |
< |
FreeValues() /* free our allocated values */ |
501 |
< |
{ |
502 |
< |
if (rV.nl <= 0) |
503 |
< |
return; |
504 |
< |
free(rV.base); |
505 |
< |
rV.base = NULL; |
506 |
< |
rV.nl = 0; |
507 |
< |
} |
508 |
< |
|
509 |
< |
|
510 |
< |
static |
511 |
< |
clralphas() /* prepare for new alpha values */ |
512 |
< |
{ |
513 |
< |
if (!vmaplen) |
514 |
< |
return; |
515 |
< |
free((char *)valmap); |
516 |
< |
valmap = NULL; |
517 |
< |
vmaplen = 0; |
518 |
< |
} |
519 |
< |
|
520 |
< |
|
521 |
< |
static int |
522 |
< |
valcmp(v1p, v2p) /* compare two pixel values */ |
523 |
< |
int *v1p, *v2p; |
524 |
< |
{ |
525 |
< |
register int v1 = *v1p, v2 = *v2p; |
526 |
< |
register int c; |
527 |
< |
|
528 |
< |
if ((c = rV.rgb[v1][0] - rV.rgb[v2][0])) return(c); |
529 |
< |
if ((c = rV.rgb[v1][1] - rV.rgb[v2][1])) return(c); |
530 |
< |
if ((c = rV.rgb[v1][2] - rV.rgb[v2][2])) return(c); |
531 |
< |
return(rV.alpha[v1] - rV.alpha[v2]); |
532 |
< |
} |
533 |
< |
|
534 |
< |
|
535 |
< |
static |
536 |
< |
mergalphas(adest, al1, n1, al2, n2) /* merge two sorted alpha lists */ |
537 |
< |
register int *adest, *al1, *al2; |
538 |
< |
int n1, n2; |
539 |
< |
{ |
540 |
< |
register int cmp; |
541 |
< |
|
542 |
< |
while (n1 | n2) { |
543 |
< |
if (!n1) cmp = 1; |
544 |
< |
else if (!n2) cmp = -1; |
545 |
< |
else cmp = valcmp(al1, al2); |
546 |
< |
if (cmp > 0) { |
547 |
< |
*adest++ = *al2++; |
548 |
< |
n2--; |
549 |
< |
} else { |
550 |
< |
*adest++ = *al1++; |
551 |
< |
n1--; |
335 |
> |
if (cone[0].nverts) |
336 |
> |
freecones(); |
337 |
> |
minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) + |
338 |
> |
DisplayHeight(ourdisplay,ourscreen)); |
339 |
> |
for (i = 0; i < MAXCONE; i++) { |
340 |
> |
d = (double)i/(MAXCONE-1); d *= d; /* x^2 distribution */ |
341 |
> |
cone[i].rad = minrad + (1.-minrad)*d; |
342 |
> |
cone[i].nverts = MINVERT + (MAXVERT-MINVERT)*d; |
343 |
> |
cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT)); |
344 |
> |
if (cone[i].va == NULL) |
345 |
> |
error(SYSTEM, "out of memory in initcones"); |
346 |
> |
for (j = cone[i].nverts; j--; ) { |
347 |
> |
d = 2.*PI * (j+.5) / (cone[i].nverts); |
348 |
> |
cone[i].va[j][0] = cos(d) * cone[i].rad; |
349 |
> |
cone[i].va[j][1] = sin(d) * cone[i].rad; |
350 |
> |
cone[i].va[j][2] = 1. - cone[i].rad; |
351 |
|
} |
352 |
|
} |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
static |
357 |
< |
setalphas(vbeg, nvals) /* add values to our map and set alphas */ |
559 |
< |
int vbeg, nvals; |
357 |
> |
freecones() /* free cone vertices */ |
358 |
|
{ |
359 |
< |
register int *newmap; |
562 |
< |
short ccmp[3], lastalpha; |
563 |
< |
int newmaplen; |
359 |
> |
register int i; |
360 |
|
|
361 |
< |
if (nvals <= 0) |
362 |
< |
return; |
363 |
< |
newmaplen = vmaplen + nvals; /* allocate new map */ |
364 |
< |
newmap = (int *)malloc(newmaplen*sizeof(int)); |
365 |
< |
if (newmap == NULL) |
570 |
< |
error(SYSTEM, "out of memory in setalphas"); |
571 |
< |
while (nvals--) { /* add new values to end */ |
572 |
< |
rV.alpha[vbeg] = 255; |
573 |
< |
newmap[vmaplen+nvals] = vbeg++; |
574 |
< |
} |
575 |
< |
if (nvals >= 3*vmaplen) { /* resort the combined array */ |
576 |
< |
while (vmaplen--) |
577 |
< |
newmap[vmaplen] = valmap[vmaplen]; |
578 |
< |
qsort((char *)newmap, newmaplen, sizeof(int), valcmp); |
579 |
< |
} else { /* perform merge sort */ |
580 |
< |
qsort((char *)(newmap+vmaplen), newmaplen-vmaplen, |
581 |
< |
sizeof(int), valcmp); |
582 |
< |
mergalphas(newmap, valmap, vmaplen, |
583 |
< |
newmap+vmaplen, newmaplen-vmaplen); |
584 |
< |
} |
585 |
< |
if (valmap != NULL) /* free old map and assign new one */ |
586 |
< |
free((char *)valmap); |
587 |
< |
valmap = newmap; |
588 |
< |
vmaplen = newmaplen; |
589 |
< |
lastalpha = 0; /* set new alpha values */ |
590 |
< |
ccmp[0] = ccmp[1] = ccmp[2] = 256; |
591 |
< |
while (newmaplen--) |
592 |
< |
if (rV.rgb[*newmap][0] == ccmp[0] && |
593 |
< |
rV.rgb[*newmap][1] == ccmp[1] && |
594 |
< |
rV.rgb[*newmap][2] == ccmp[2]) { |
595 |
< |
if (lastalpha >= 255) |
596 |
< |
newmap++; |
597 |
< |
else if (rV.alpha[*newmap] < 255) |
598 |
< |
lastalpha = rV.alpha[*newmap++]; |
599 |
< |
else |
600 |
< |
rV.alpha[*newmap++] = ++lastalpha; |
601 |
< |
} else { |
602 |
< |
ccmp[0] = rV.rgb[*newmap][0]; |
603 |
< |
ccmp[1] = rV.rgb[*newmap][1]; |
604 |
< |
ccmp[2] = rV.rgb[*newmap][2]; |
605 |
< |
if (rV.alpha[*newmap] < 255) |
606 |
< |
lastalpha = rV.alpha[*newmap++]; |
607 |
< |
else |
608 |
< |
rV.alpha[*newmap++] = lastalpha = 1; |
361 |
> |
for (i = MAXCONE; i--; ) |
362 |
> |
if (cone[i].nverts) { |
363 |
> |
free((char *)cone[i].va); |
364 |
> |
cone[i].va = NULL; |
365 |
> |
cone[i].nverts = 0; |
366 |
|
} |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
static |
614 |
– |
TMapValues(redo) /* map our values to RGB */ |
615 |
– |
int redo; |
616 |
– |
{ |
617 |
– |
int aorg, alen, borg, blen; |
618 |
– |
/* recompute mapping? */ |
619 |
– |
if (redo) |
620 |
– |
rV.tml = rV.bl; |
621 |
– |
/* already done? */ |
622 |
– |
if (rV.tml == rV.tl) |
623 |
– |
return(1); |
624 |
– |
/* compute segments */ |
625 |
– |
aorg = rV.tml; |
626 |
– |
if (rV.tl >= aorg) { |
627 |
– |
alen = rV.tl - aorg; |
628 |
– |
blen = 0; |
629 |
– |
} else { |
630 |
– |
alen = rV.nl - aorg; |
631 |
– |
borg = 0; |
632 |
– |
blen = rV.tl; |
633 |
– |
} |
634 |
– |
/* (re)compute tone mapping? */ |
635 |
– |
if (rV.tml == rV.bl) { |
636 |
– |
tmClearHisto(); |
637 |
– |
tmAddHisto(rV.brt+aorg, alen, 1); |
638 |
– |
if (blen > 0) |
639 |
– |
tmAddHisto(rV.brt+borg, blen, 1); |
640 |
– |
if (tmComputeMapping(0., 0., 0.) != TM_E_OK) |
641 |
– |
return(0); |
642 |
– |
clralphas(); /* restart value list */ |
643 |
– |
rV.drl = rV.bl; /* need to redraw */ |
644 |
– |
} |
645 |
– |
if (tmMapPixels(rV.rgb+aorg, rV.brt+aorg, |
646 |
– |
rV.chr+aorg, alen) != TM_E_OK) |
647 |
– |
return(0); |
648 |
– |
if (blen > 0) |
649 |
– |
tmMapPixels(rV.rgb+borg, rV.brt+borg, |
650 |
– |
rV.chr+borg, blen); |
651 |
– |
setalphas(aorg, alen); /* compute add'l alpha values */ |
652 |
– |
if (blen > 0) |
653 |
– |
setalphas(borg, blen); |
654 |
– |
rV.tml = rV.tl; /* we're all up to date */ |
655 |
– |
return(1); |
656 |
– |
} |
657 |
– |
|
658 |
– |
|
659 |
– |
static int |
660 |
– |
Compost(pct) /* free up some values */ |
661 |
– |
int pct; |
662 |
– |
{ |
663 |
– |
int nused, nclear, nmapped, ndrawn; |
664 |
– |
/* figure out how many values to clear */ |
665 |
– |
nclear = rV.nl * pct / 100; |
666 |
– |
nused = rV.tl - rV.bl; |
667 |
– |
if (nused <= 0) nused += rV.nl; |
668 |
– |
nclear -= rV.nl - nused; |
669 |
– |
if (nclear <= 0) |
670 |
– |
return(0); |
671 |
– |
if (nclear >= nused) { /* clear them all? */ |
672 |
– |
rV.drl = rV.tml = rV.bl = rV.tl = 0; |
673 |
– |
return(nused); |
674 |
– |
} |
675 |
– |
/* else clear values from bottom */ |
676 |
– |
ndrawn = rV.drl - rV.bl; |
677 |
– |
if (ndrawn < 0) ndrawn += rV.nl; |
678 |
– |
nmapped = rV.tml - rV.bl; |
679 |
– |
if (nmapped < 0) nmapped += rV.nl; |
680 |
– |
rV.bl += nclear; |
681 |
– |
if (rV.bl >= rV.nl) rV.bl -= rV.nl; |
682 |
– |
if (ndrawn < nclear) rV.drl = rV.bl; |
683 |
– |
if (nmapped < nclear) rV.tml = rV.bl; |
684 |
– |
return(nclear); |
685 |
– |
} |
686 |
– |
|
687 |
– |
|
688 |
– |
static |
371 |
|
getevent() /* get next event */ |
372 |
|
{ |
373 |
|
XNextEvent(ourdisplay, levptr(XEvent)); |
407 |
|
draw_grids() /* draw holodeck section grids */ |
408 |
|
{ |
409 |
|
static BYTE gridrgba[4] = {0x0, 0xff, 0xff, 0x00}; |
410 |
< |
|
411 |
< |
if (!mapped) |
410 |
> |
double xmin, xmax, ymin, ymax, zmin, zmax; |
411 |
> |
double d, cx, sx, crad; |
412 |
> |
FVECT vx, vy; |
413 |
> |
register int i, j; |
414 |
> |
/* can we even do it? */ |
415 |
> |
if (!mapped || odev.v.type != VT_PER) |
416 |
|
return; |
417 |
+ |
/* compute view frustum */ |
418 |
+ |
if (normalize(odev.v.vdir) == 0.0) |
419 |
+ |
return; |
420 |
+ |
zmin = 0.01; |
421 |
+ |
zmax = 10000.; |
422 |
+ |
if (odev.v.vfore > FTINY) |
423 |
+ |
zmin = odev.v.vfore; |
424 |
+ |
if (odev.v.vaft > FTINY) |
425 |
+ |
zmax = odev.v.vaft; |
426 |
+ |
xmax = zmin * tan(PI/180./2. * odev.v.horiz); |
427 |
+ |
xmin = -xmax; |
428 |
+ |
d = odev.v.hoff * (xmax - xmin); |
429 |
+ |
xmin += d; xmax += d; |
430 |
+ |
ymax = zmin * tan(PI/180./2. * odev.v.vert); |
431 |
+ |
ymin = -ymax; |
432 |
+ |
d = odev.v.voff * (ymax - ymin); |
433 |
+ |
ymin += d; ymax += d; |
434 |
+ |
/* set view matrix */ |
435 |
+ |
glMatrixMode(GL_PROJECTION); |
436 |
+ |
glPushMatrix(); |
437 |
+ |
glLoadIdentity(); |
438 |
+ |
glFrustum(xmin, xmax, ymin, ymax, zmin, zmax); |
439 |
+ |
gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], |
440 |
+ |
odev.v.vp[0] + odev.v.vdir[0], |
441 |
+ |
odev.v.vp[1] + odev.v.vdir[1], |
442 |
+ |
odev.v.vp[2] + odev.v.vdir[2], |
443 |
+ |
odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); |
444 |
+ |
glDisable(GL_DEPTH_TEST); /* write no depth values */ |
445 |
|
glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]); |
446 |
< |
/* draw each grid line */ |
733 |
< |
glBegin(GL_LINES); |
446 |
> |
glBegin(GL_LINES); /* draw each grid line */ |
447 |
|
gridlines(draw3dline); |
448 |
|
glEnd(); |
449 |
+ |
glEnable(GL_DEPTH_TEST); /* restore rendering params */ |
450 |
+ |
glPopMatrix(); |
451 |
|
} |
452 |
|
|
453 |
|
|
739 |
– |
static int |
740 |
– |
IndexValue(rgba) /* locate a pixel by it's framebuffer value */ |
741 |
– |
register BYTE rgba[4]; |
742 |
– |
{ |
743 |
– |
register int *vp; |
744 |
– |
/* check legality */ |
745 |
– |
if (rgba[3] == 0 || rgba[3] == 255) |
746 |
– |
return(-1); |
747 |
– |
/* borrow a value slot */ |
748 |
– |
rV.rgb[rV.tl][0] = rgba[0]; |
749 |
– |
rV.rgb[rV.tl][1] = rgba[1]; |
750 |
– |
rV.rgb[rV.tl][2] = rgba[2]; |
751 |
– |
rV.alpha[rV.tl] = rgba[3]; |
752 |
– |
/* find it */ |
753 |
– |
vp = (int *)bsearch((char *)&rV.tl, (char *)valmap, vmaplen, |
754 |
– |
sizeof(int), valcmp); |
755 |
– |
if (vp == NULL) |
756 |
– |
return(-1); |
757 |
– |
return(*vp); |
758 |
– |
} |
759 |
– |
|
760 |
– |
|
761 |
– |
static int |
762 |
– |
FindValue(dx, dy) /* find a value on the display */ |
763 |
– |
int dx, dy; |
764 |
– |
{ |
765 |
– |
BYTE rgba[4]; |
766 |
– |
|
767 |
– |
if (dx < 0 || dy < 0 || dx >= odev.hres || dy >= odev.vres) |
768 |
– |
return(-1); |
769 |
– |
glReadPixels(dx, dy, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba); |
770 |
– |
return(IndexValue(rgba)); |
771 |
– |
} |
772 |
– |
|
773 |
– |
|
454 |
|
static |
455 |
|
moveview(dx, dy, mov, orb) /* move our view */ |
456 |
|
int dx, dy, mov, orb; |
463 |
|
copystruct(&nv, &odev.v); |
464 |
|
/* change view direction */ |
465 |
|
if (mov | orb) { |
466 |
< |
if ((li = FindValue(dx, dy)) < 0) |
466 |
> |
if ((li = qtFindLeaf(dx, dy)) < 0) |
467 |
|
return(0); /* not on window */ |
468 |
< |
VSUM(odir, rV.wp[li], nv.vp, -1.); |
468 |
> |
VSUM(odir, qtL.wp[li], nv.vp, -1.); |
469 |
|
} else { |
470 |
|
if (viewray(nv.vp, nv.vdir, &odev.v, |
471 |
|
(dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) |
473 |
|
} |
474 |
|
if (orb && mov) { /* orbit left/right */ |
475 |
|
spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); |
476 |
< |
VSUM(nv.vp, rV.wp[li], odir, -1.); |
476 |
> |
VSUM(nv.vp, qtL.wp[li], odir, -1.); |
477 |
|
spinvector(nv.vdir, nv.vdir, nv.vup, d); |
478 |
|
} else if (orb) { /* orbit up/down */ |
479 |
|
fcross(v1, odir, nv.vup); |
480 |
|
if (normalize(v1) == 0.) |
481 |
|
return(0); |
482 |
|
spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); |
483 |
< |
VSUM(nv.vp, rV.wp[li], odir, -1.); |
483 |
> |
VSUM(nv.vp, qtL.wp[li], odir, -1.); |
484 |
|
spinvector(nv.vdir, nv.vdir, v1, d); |
485 |
|
} else if (mov) { /* move forward/backward */ |
486 |
|
d = MOVPCT/100. * mov; |
505 |
|
{ |
506 |
|
int movdir = MOVDIR(ebut->button); |
507 |
|
int movorb = MOVORB(ebut->state); |
508 |
+ |
int oldnodesiz = qtMinNodesiz; |
509 |
|
Window rootw, childw; |
510 |
|
int rootx, rooty, wx, wy; |
511 |
|
unsigned int statemask; |
512 |
|
|
513 |
< |
quicken = 1; /* accelerate update rate */ |
513 |
> |
qtMinNodesiz = 24; /* accelerate update rate */ |
514 |
|
XNoOp(ourdisplay); |
515 |
|
|
516 |
|
while (!XCheckMaskEvent(ourdisplay, |
524 |
|
sleep(1); |
525 |
|
continue; |
526 |
|
} |
527 |
+ |
glClear(GL_COLOR_BUFFER_BIT); |
528 |
+ |
qtUpdate(); |
529 |
|
draw_grids(); |
530 |
< |
dev_flush(); |
530 |
> |
glFlush(); |
531 |
|
} |
532 |
|
if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ |
533 |
|
movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); |
534 |
|
wx = levptr(XButtonReleasedEvent)->x; |
535 |
|
wy = levptr(XButtonReleasedEvent)->y; |
536 |
|
moveview(wx, odev.vres-1-wy, movdir, movorb); |
854 |
– |
} else { |
855 |
– |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
856 |
– |
redraw(); |
537 |
|
} |
858 |
– |
quicken = 0; |
538 |
|
dev_flush(); |
539 |
+ |
|
540 |
+ |
qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */ |
541 |
|
} |
542 |
|
|
543 |
|
|
572 |
|
inpresflags |= DFL(DC_RESUME); |
573 |
|
return; |
574 |
|
case CTRL('R'): /* redraw screen */ |
575 |
< |
TMapValues(1); |
575 |
> |
if (nxtzmax > FTINY) { |
576 |
> |
curzmax = nxtzmax; |
577 |
> |
nxtzmax = 0.; |
578 |
> |
} |
579 |
|
glClear(GL_DEPTH_BUFFER_BIT); |
580 |
< |
redraw(); |
580 |
> |
qtRedraw(0, 0, odev.hres, odev.vres); |
581 |
|
return; |
582 |
|
case CTRL('L'): /* refresh from server */ |
583 |
|
if (inpresflags & DFL(DC_REDRAW)) |
584 |
|
return; |
585 |
+ |
if (nxtzmax > FTINY) { |
586 |
+ |
curzmax = nxtzmax; |
587 |
+ |
nxtzmax = 0.; |
588 |
+ |
} |
589 |
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
902 |
– |
glDisable(GL_DEPTH_TEST); |
590 |
|
draw_grids(); |
904 |
– |
glEnable(GL_DEPTH_TEST); |
591 |
|
glFlush(); |
592 |
< |
Compost(100); /* get rid of old values */ |
592 |
> |
qtCompost(100); /* get rid of old values */ |
593 |
|
inpresflags |= DFL(DC_REDRAW); /* resend values from server */ |
594 |
|
return; |
595 |
|
case 'K': /* kill rtrace process(es) */ |
615 |
|
fixwindow(eexp) /* repair damage to window */ |
616 |
|
register XExposeEvent *eexp; |
617 |
|
{ |
618 |
+ |
int xmin, xmax, ymin, ymax; |
619 |
+ |
|
620 |
|
if (odev.hres == 0 || odev.vres == 0) /* first exposure */ |
621 |
|
resizewindow((XConfigureEvent *)eexp); |
622 |
< |
if (eexp->width == odev.hres && eexp->height == odev.vres) |
623 |
< |
TMapValues(1); |
624 |
< |
if (!eexp->count) { |
625 |
< |
glClear(GL_DEPTH_BUFFER_BIT); |
626 |
< |
redraw(); |
627 |
< |
} |
622 |
> |
xmin = eexp->x; xmax = eexp->x + eexp->width; |
623 |
> |
ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y; |
624 |
> |
/* clear portion of depth */ |
625 |
> |
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
626 |
> |
glDepthFunc(GL_ALWAYS); |
627 |
> |
glBegin(GL_POLYGON); |
628 |
> |
glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.); |
629 |
> |
glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.); |
630 |
> |
glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.); |
631 |
> |
glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.); |
632 |
> |
glEnd(); |
633 |
> |
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
634 |
> |
glDepthFunc(GL_LEQUAL); |
635 |
> |
qtRedraw(xmin, ymin, xmax, ymax); |
636 |
|
} |
637 |
|
|
638 |
|
|
640 |
|
resizewindow(ersz) /* resize window */ |
641 |
|
register XConfigureEvent *ersz; |
642 |
|
{ |
643 |
+ |
glViewport(0, 0, ersz->width, ersz->height); |
644 |
+ |
|
645 |
|
if (ersz->width == odev.hres && ersz->height == odev.vres) |
646 |
|
return; |
647 |
|
|
650 |
|
|
651 |
|
odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); |
652 |
|
odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); |
955 |
– |
|
956 |
– |
setGLview(); |
653 |
|
|
654 |
|
inpresflags |= DFL(DC_SETVIEW); |
655 |
|
} |