1 |
gwlarson |
3.1 |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Routines for drawing samples using depth buffer checks. |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include "standard.h" |
12 |
|
|
|
13 |
|
|
#include <sys/types.h> |
14 |
|
|
#include <GL/glx.h> |
15 |
|
|
#include <GL/glu.h> |
16 |
|
|
|
17 |
|
|
#include "random.h" |
18 |
|
|
#include "rhd_odraw.h" |
19 |
|
|
|
20 |
|
|
#ifndef DEPTHEPS |
21 |
|
|
#define DEPTHEPS 0.02 /* depth epsilon */ |
22 |
|
|
#endif |
23 |
|
|
#ifndef SAMPSPERBLOCK |
24 |
|
|
#define SAMPSPERBLOCK 1024 /* target samples per image block */ |
25 |
|
|
#endif |
26 |
|
|
#ifndef SFREEFRAC |
27 |
|
|
#define SFREEFRAC 0.2 /* fraction to free at a time */ |
28 |
|
|
#endif |
29 |
|
|
#ifndef MAXFAN |
30 |
|
|
#define MAXFAN 32 /* maximum arms in a triangle fan */ |
31 |
|
|
#endif |
32 |
|
|
#ifndef MINFAN |
33 |
|
|
#define MINFAN 4 /* minimum arms in a triangle fan */ |
34 |
|
|
#endif |
35 |
|
|
#ifndef FANSIZE |
36 |
|
|
#define FANSIZE 3.5 /* fan sizing factor */ |
37 |
|
|
#endif |
38 |
|
|
|
39 |
|
|
#define NEWMAP 01 /* need to recompute mapping */ |
40 |
|
|
#define NEWRGB 02 /* need to remap RGB values */ |
41 |
gwlarson |
3.2 |
#define NEWHIST 04 /* clear histogram as well */ |
42 |
gwlarson |
3.1 |
|
43 |
|
|
struct ODview *odView; /* our view list */ |
44 |
|
|
int odNViews; /* number of views in our list */ |
45 |
|
|
|
46 |
|
|
struct ODsamp odS; /* sample values */ |
47 |
|
|
|
48 |
|
|
static int needmapping; /* what needs doing with tone map */ |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
#define SAMP32 (32*(2*sizeof(short)+sizeof(union ODfunion)+sizeof(TMbright)+\ |
52 |
|
|
6*sizeof(BYTE))+sizeof(int4)) |
53 |
|
|
|
54 |
|
|
int |
55 |
|
|
odInit(n) /* initialize drawing routines */ |
56 |
|
|
int n; |
57 |
|
|
{ |
58 |
|
|
int nbytes, i, j, k, nextsamp, count, blockdiv; |
59 |
|
|
int res[2]; |
60 |
|
|
|
61 |
gwlarson |
3.3 |
if (odNViews > 0) { /* deallocate view structures */ |
62 |
|
|
for (i = odNViews; i--; ) { |
63 |
gwlarson |
3.1 |
free((char *)odView[i].bmap); |
64 |
gwlarson |
3.3 |
free((char *)odView[i].pmap); |
65 |
gwlarson |
3.1 |
if (odView[i].emap != NULL) |
66 |
|
|
free((char *)odView[i].emap); |
67 |
|
|
} |
68 |
|
|
free((char *)odView); |
69 |
|
|
odView = NULL; |
70 |
|
|
odNViews = 0; |
71 |
|
|
} |
72 |
|
|
if (n && n != odS.nsamp) { |
73 |
|
|
/* round space up to nearest power of 2 */ |
74 |
|
|
nbytes = (n+31)/32 * SAMP32; |
75 |
|
|
for (i = 1024; nbytes > i-8; i <<= 1) |
76 |
|
|
; |
77 |
|
|
n = (i-8)/SAMP32 * 32; |
78 |
gwlarson |
3.2 |
needmapping = NEWHIST; |
79 |
gwlarson |
3.1 |
} |
80 |
|
|
if (n != odS.nsamp) { /* (re)allocate sample array */ |
81 |
|
|
if (odS.nsamp) |
82 |
|
|
free(odS.base); |
83 |
|
|
odS.nsamp = 0; |
84 |
|
|
if (!n) |
85 |
|
|
return(0); |
86 |
|
|
nbytes = (n+31)/32 * SAMP32; |
87 |
|
|
odS.base = (char *)malloc(nbytes); |
88 |
|
|
if (odS.base == NULL) |
89 |
|
|
return(0); |
90 |
|
|
/* assign larger alignment types earlier */ |
91 |
|
|
odS.f = (union ODfunion *)odS.base; |
92 |
|
|
odS.redraw = (int4 *)(odS.f + n); |
93 |
|
|
odS.ip = (short (*)[2])(odS.redraw + n/32); |
94 |
|
|
odS.brt = (TMbright *)(odS.ip + n); |
95 |
|
|
odS.chr = (BYTE (*)[3])(odS.brt + n); |
96 |
|
|
odS.rgb = (BYTE (*)[3])(odS.chr + n); |
97 |
|
|
odS.nsamp = n; |
98 |
|
|
} |
99 |
|
|
if (!n) |
100 |
|
|
return(0); |
101 |
|
|
/* allocate view information */ |
102 |
|
|
count = 0; /* count pixels */ |
103 |
|
|
for (i = 0; dev_auxview(i, res) != NULL; i++) |
104 |
|
|
count += res[0]*res[1]; |
105 |
|
|
odView = (struct ODview *)malloc(i*sizeof(struct ODview)); |
106 |
|
|
if (odView == NULL) |
107 |
|
|
return(0); |
108 |
|
|
odNViews = i; |
109 |
|
|
blockdiv = sqrt(count/(n/SAMPSPERBLOCK)) + 0.5; |
110 |
|
|
if (blockdiv < 8) blockdiv = 8; |
111 |
|
|
nextsamp = 0; count /= blockdiv*blockdiv; /* # blocks */ |
112 |
|
|
while (i--) { /* initialize each view */ |
113 |
|
|
dev_auxview(i, res); |
114 |
|
|
odView[i].hhi = res[0]; |
115 |
|
|
odView[i].hlow = (res[0] + blockdiv/2) / blockdiv; |
116 |
|
|
if (odView[i].hlow < 1) odView[i].hlow = 1; |
117 |
|
|
odView[i].vhi = res[1]; |
118 |
|
|
odView[i].vlow = (res[1] + blockdiv/2) / blockdiv; |
119 |
|
|
if (odView[i].vlow < 1) odView[i].vlow = 1; |
120 |
gwlarson |
3.3 |
odView[i].emap = NULL; |
121 |
|
|
odView[i].dmap = NULL; |
122 |
|
|
odView[i].pmap = (int4 *)calloc(FL4NELS(res[0]*res[1]), |
123 |
|
|
sizeof(int4)); |
124 |
|
|
if (odView[i].pmap == NULL) |
125 |
|
|
return(0); |
126 |
gwlarson |
3.1 |
j = odView[i].hlow*odView[i].vlow; |
127 |
|
|
odView[i].bmap = (struct ODblock *)malloc( |
128 |
|
|
j * sizeof(struct ODblock)); |
129 |
|
|
if (odView[i].bmap == NULL) |
130 |
|
|
return(0); |
131 |
|
|
DCHECK(count<=0 | nextsamp>=n, |
132 |
|
|
CONSISTENCY, "counter botch in odInit"); |
133 |
|
|
if (!i) count = j; |
134 |
gwlarson |
3.2 |
while (j--) { /* initialize blocks & free lists */ |
135 |
|
|
odView[i].bmap[j].pthresh = FHUGE; |
136 |
gwlarson |
3.1 |
odView[i].bmap[j].first = k = nextsamp; |
137 |
|
|
nextsamp += odView[i].bmap[j].nsamp = |
138 |
|
|
(n - nextsamp)/count--; |
139 |
|
|
odView[i].bmap[j].free = k; |
140 |
|
|
while (++k < nextsamp) |
141 |
|
|
odS.nextfree(k-1) = k; |
142 |
|
|
odS.nextfree(k-1) = ENDFREE; |
143 |
|
|
odView[i].bmap[j].nused = 0; |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
CLR4ALL(odS.redraw, odS.nsamp); /* clear redraw flags */ |
147 |
gwlarson |
3.2 |
for (i = odS.nsamp; i--; ) { /* clear values */ |
148 |
gwlarson |
3.1 |
odS.ip[i][0] = odS.ip[i][1] = -1; |
149 |
gwlarson |
3.2 |
odS.brt[i] = TM_NOBRT; |
150 |
|
|
} |
151 |
|
|
needmapping |= NEWMAP; /* compute new map on update */ |
152 |
gwlarson |
3.1 |
return(odS.nsamp); /* return number of samples */ |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
#undef SAMP32 |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
int |
159 |
|
|
sampcmp(s0, s1) /* sample order, descending proximity */ |
160 |
|
|
int *s0, *s1; |
161 |
|
|
{ |
162 |
|
|
register double diff = odS.closeness(*s1) - odS.closeness(*s0); |
163 |
|
|
|
164 |
|
|
return (diff > FTINY ? 1 : diff < -FTINY ? -1 : 0); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
int |
169 |
gwlarson |
3.2 |
odAllocBlockSamp(vn, hh, vh, prox) /* allocate sample from block */ |
170 |
|
|
int vn, hh, vh; |
171 |
|
|
double prox; |
172 |
gwlarson |
3.1 |
{ |
173 |
|
|
int si[SAMPSPERBLOCK+SAMPSPERBLOCK/4]; |
174 |
gwlarson |
3.2 |
int hl, vl; |
175 |
gwlarson |
3.1 |
VIEW *vw; |
176 |
|
|
FVECT ro, rd; |
177 |
|
|
int res[2]; |
178 |
|
|
register struct ODblock *bp; |
179 |
gwlarson |
3.2 |
register int i, j; |
180 |
|
|
/* get block */ |
181 |
|
|
hl = hh*odView[vn].hlow/odView[vn].hhi; |
182 |
|
|
vl = vh*odView[vn].vlow/odView[vn].vhi; |
183 |
gwlarson |
3.1 |
bp = odView[vn].bmap + vl*odView[vn].hlow + hl; |
184 |
gwlarson |
3.2 |
if (prox > bp->pthresh) |
185 |
|
|
return(-1); /* worse than free list occupants */ |
186 |
|
|
/* check for duplicate pixel */ |
187 |
gwlarson |
3.3 |
if (CHK4(odView[vn].pmap, vh*odView[vn].hhi + hh)) |
188 |
|
|
i = bp->first + bp->nsamp; |
189 |
|
|
else |
190 |
|
|
i = -1; |
191 |
|
|
while (i-- > bp->first) |
192 |
gwlarson |
3.2 |
if (hh == odS.ip[i][0] && vh == odS.ip[i][1]) { /* found it! */ |
193 |
|
|
/* search free list for it */ |
194 |
|
|
if (i == bp->free) |
195 |
|
|
break; /* special case */ |
196 |
|
|
if (bp->free != ENDFREE) |
197 |
|
|
for (j = bp->free; odS.nextfree(j) != ENDFREE; |
198 |
|
|
j = odS.nextfree(j)) |
199 |
|
|
if (odS.nextfree(j) == i) { |
200 |
|
|
odS.nextfree(j) = |
201 |
|
|
odS.nextfree(i); |
202 |
|
|
bp->nused++; |
203 |
|
|
goto gotit; |
204 |
|
|
} |
205 |
|
|
if (prox >= 0.999*odS.closeness(i)) |
206 |
|
|
return(-1); /* previous sample is fine */ |
207 |
|
|
goto gotit; |
208 |
|
|
} |
209 |
gwlarson |
3.3 |
DCHECK(i>=-1, WARNING, "pixel in presence map not found in block"); |
210 |
gwlarson |
3.2 |
if (bp->free != ENDFREE) { /* allocate from free list */ |
211 |
gwlarson |
3.1 |
i = bp->free; |
212 |
gwlarson |
3.3 |
if (odS.ip[i][0] >= 0 & odS.ip[i][1] >= 0) |
213 |
|
|
CLR4(odView[vn].pmap, odS.ip[i][1]*odView[vn].hhi + |
214 |
|
|
odS.ip[i][0]); |
215 |
gwlarson |
3.1 |
bp->free = odS.nextfree(i); |
216 |
|
|
bp->nused++; |
217 |
gwlarson |
3.2 |
goto gotit; |
218 |
gwlarson |
3.1 |
} |
219 |
|
|
DCHECK(bp->nsamp<=0, CONSISTENCY, |
220 |
|
|
"no available samples in odAllocBlockSamp"); |
221 |
|
|
DCHECK(bp->nsamp > sizeof(si)/sizeof(si[0]), CONSISTENCY, |
222 |
|
|
"too many samples in odAllocBlockSamp"); |
223 |
|
|
/* free some samples */ |
224 |
|
|
if ((vw = dev_auxview(vn, res)) == NULL) |
225 |
|
|
error(CONSISTENCY, "bad view number in odAllocBlockSamp"); |
226 |
|
|
for (i = bp->nsamp; i--; ) /* figure out which are worse */ |
227 |
|
|
si[i] = bp->first + i; |
228 |
|
|
qsort((char *)si, bp->nsamp, sizeof(int), sampcmp); |
229 |
gwlarson |
3.2 |
i = bp->nsamp*SFREEFRAC + .5; /* put them into free list */ |
230 |
|
|
if (i >= bp->nsamp) i = bp->nsamp-1; /* paranoia */ |
231 |
|
|
bp->pthresh = odS.closeness(si[i]); /* new proximity threshold */ |
232 |
gwlarson |
3.1 |
while (--i > 0) { |
233 |
|
|
odS.nextfree(si[i]) = bp->free; |
234 |
|
|
bp->free = si[i]; |
235 |
|
|
bp->nused--; |
236 |
|
|
} |
237 |
gwlarson |
3.2 |
i = si[0]; /* use worst sample */ |
238 |
gwlarson |
3.3 |
CLR4(odView[vn].pmap, odS.ip[i][1]*odView[vn].hhi + odS.ip[i][0]); |
239 |
gwlarson |
3.2 |
gotit: |
240 |
|
|
odS.ip[i][0] = hh; |
241 |
|
|
odS.ip[i][1] = vh; |
242 |
|
|
odS.closeness(i) = prox; |
243 |
gwlarson |
3.3 |
SET4(odView[vn].pmap, vh*odView[vn].hhi + hh); |
244 |
gwlarson |
3.2 |
return(i); |
245 |
gwlarson |
3.1 |
} |
246 |
|
|
|
247 |
|
|
|
248 |
|
|
odSample(c, d, p) /* add a sample value */ |
249 |
|
|
COLR c; |
250 |
|
|
FVECT d, p; |
251 |
|
|
{ |
252 |
|
|
FVECT disp; |
253 |
gwlarson |
3.2 |
double d0, d1, h, v, prox; |
254 |
gwlarson |
3.1 |
register VIEW *vw; |
255 |
gwlarson |
3.2 |
int hh, vh; |
256 |
gwlarson |
3.1 |
int res[2]; |
257 |
|
|
register int i, id; |
258 |
|
|
|
259 |
|
|
DCHECK(odS.nsamp<=0, CONSISTENCY, "no samples allocated in odSample"); |
260 |
|
|
/* add value to each view */ |
261 |
|
|
for (i = 0; (vw = dev_auxview(i, res)) != NULL; i++) { |
262 |
|
|
DCHECK(i>=odNViews, CONSISTENCY, "too many views in odSample"); |
263 |
|
|
CHECK(vw->type!=VT_PER, INTERNAL, |
264 |
|
|
"cannot handle non-perspective views"); |
265 |
|
|
if (p != NULL) { /* compute view position */ |
266 |
|
|
VSUB(disp, p, vw->vp); |
267 |
|
|
d0 = DOT(disp, vw->vdir); |
268 |
|
|
if (d0 <= vw->vfore+FTINY) |
269 |
|
|
continue; /* too close */ |
270 |
|
|
} else { |
271 |
|
|
VCOPY(disp, d); |
272 |
|
|
d0 = DOT(disp, vw->vdir); |
273 |
|
|
if (d0 <= FTINY) /* behind view */ |
274 |
|
|
continue; |
275 |
|
|
} |
276 |
|
|
h = DOT(disp,vw->hvec)/(d0*vw->hn2) + 0.5 - vw->hoff; |
277 |
|
|
if (h < 0. || h >= 1.) |
278 |
|
|
continue; /* left or right */ |
279 |
|
|
v = DOT(disp,vw->vvec)/(d0*vw->vn2) + 0.5 - vw->voff; |
280 |
|
|
if (v < 0. || v >= 1.) |
281 |
|
|
continue; /* above or below */ |
282 |
|
|
hh = h * res[0]; |
283 |
|
|
vh = v * res[1]; |
284 |
|
|
if (odView[i].dmap != NULL) { /* check depth */ |
285 |
|
|
d1 = odView[i].dmap[vh*res[0] + hh]; |
286 |
|
|
if (d1 < 0.99*FHUGE && (d0 > (1.+DEPTHEPS)*d1 || |
287 |
|
|
(1.+DEPTHEPS)*d0 < d1)) |
288 |
|
|
continue; /* occlusion error */ |
289 |
|
|
} |
290 |
|
|
if (p != NULL) { /* compute closeness (sin^2) */ |
291 |
|
|
d1 = DOT(disp, d); |
292 |
gwlarson |
3.2 |
prox = 1. - d1*d1/DOT(disp,disp); |
293 |
gwlarson |
3.1 |
} else |
294 |
gwlarson |
3.2 |
prox = 0.; |
295 |
|
|
/* allocate sample */ |
296 |
|
|
id = odAllocBlockSamp(i, hh, vh, prox); |
297 |
|
|
if (id < 0) |
298 |
|
|
continue; /* not good enough */ |
299 |
gwlarson |
3.1 |
/* convert color */ |
300 |
|
|
tmCvColrs(&odS.brt[id], odS.chr[id], c, 1); |
301 |
|
|
if (imm_mode | needmapping) /* if immediate mode */ |
302 |
|
|
needmapping |= NEWRGB; /* map it later */ |
303 |
|
|
else /* else map it now */ |
304 |
|
|
tmMapPixels(odS.rgb[id], &odS.brt[id], odS.chr[id], 1); |
305 |
|
|
SET4(odS.redraw, id); /* mark for redraw */ |
306 |
|
|
} |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
|
310 |
gwlarson |
3.2 |
odRemap(newhist) /* recompute tone mapping */ |
311 |
|
|
int newhist; |
312 |
gwlarson |
3.1 |
{ |
313 |
|
|
needmapping |= NEWMAP|NEWRGB; |
314 |
gwlarson |
3.2 |
if (newhist) |
315 |
|
|
needmapping |= NEWHIST; |
316 |
gwlarson |
3.1 |
} |
317 |
|
|
|
318 |
|
|
|
319 |
|
|
odRedraw(vn, hmin, vmin, hmax, vmax) /* redraw view region */ |
320 |
|
|
int vn, hmin, vmin, hmax, vmax; |
321 |
|
|
{ |
322 |
|
|
int i, j; |
323 |
|
|
register struct ODblock *bp; |
324 |
|
|
register int k; |
325 |
|
|
|
326 |
|
|
if (vn<0 | vn>=odNViews) |
327 |
|
|
return; |
328 |
|
|
/* check view limits */ |
329 |
|
|
if (hmin < 0) hmin = 0; |
330 |
|
|
if (hmax >= odView[vn].hhi) hmax = odView[vn].hhi-1; |
331 |
|
|
if (vmin < 0) vmin = 0; |
332 |
|
|
if (vmax >= odView[vn].vhi) vmax = odView[vn].vhi-1; |
333 |
|
|
if (hmax <= hmin | vmax <= vmin) |
334 |
|
|
return; |
335 |
|
|
/* convert to low resolution */ |
336 |
|
|
hmin = hmin * odView[vn].hlow / odView[vn].hhi; |
337 |
|
|
hmax = hmax * odView[vn].hlow / odView[vn].hhi; |
338 |
|
|
vmin = vmin * odView[vn].vlow / odView[vn].vhi; |
339 |
|
|
vmax = vmax * odView[vn].vlow / odView[vn].vhi; |
340 |
|
|
/* mark block samples for redraw, inclusive */ |
341 |
|
|
for (i = hmin; i <= hmax; i++) |
342 |
|
|
for (j = vmin; j <= vmax; j++) { |
343 |
|
|
bp = odView[vn].bmap + j*odView[vn].hlow + i; |
344 |
|
|
for (k = bp->nsamp; k--; ) |
345 |
|
|
if (odS.ip[bp->first+k][0] >= 0) |
346 |
|
|
SET4(odS.redraw, bp->first+k); |
347 |
|
|
} |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
|
351 |
|
|
odDepthMap(vn, dm) /* assign depth map for view */ |
352 |
|
|
int vn; |
353 |
|
|
GLfloat *dm; |
354 |
|
|
{ |
355 |
|
|
double d0, d1; |
356 |
|
|
int i, j, hmin, hmax, vmin, vmax; |
357 |
|
|
register int k, l; |
358 |
|
|
|
359 |
|
|
if (dm == NULL) { /* free edge map */ |
360 |
gwlarson |
3.2 |
if (vn<0 | vn>=odNViews) |
361 |
|
|
return; /* too late -- they're gone! */ |
362 |
gwlarson |
3.1 |
if (odView[vn].emap != NULL) |
363 |
|
|
free((char *)odView[vn].emap); |
364 |
|
|
odView[vn].emap = NULL; |
365 |
|
|
odView[vn].dmap = NULL; |
366 |
|
|
return; |
367 |
|
|
} |
368 |
gwlarson |
3.2 |
DCHECK(vn<0 | vn>=odNViews, CONSISTENCY, |
369 |
|
|
"bad view number in odDepthMap"); |
370 |
gwlarson |
3.1 |
odView[vn].dmap = dm; /* initialize edge map */ |
371 |
|
|
if (odView[vn].emap == NULL) { |
372 |
|
|
odView[vn].emap = (int4 *)malloc( |
373 |
|
|
FL4NELS(odView[vn].hlow*odView[vn].vlow)*sizeof(int4)); |
374 |
|
|
if (odView[vn].emap == NULL) |
375 |
|
|
error(SYSTEM, "out of memory in odDepthMap"); |
376 |
|
|
} |
377 |
|
|
CLR4ALL(odView[vn].emap, odView[vn].hlow*odView[vn].vlow); |
378 |
|
|
/* compute edge map */ |
379 |
|
|
vmin = odView[vn].vhi; /* enter loopsville */ |
380 |
|
|
for (j = odView[vn].vlow; j--; ) { |
381 |
|
|
vmax = vmin; |
382 |
|
|
vmin = j*odView[vn].vhi/odView[vn].vlow; |
383 |
|
|
hmin = odView[vn].hhi; |
384 |
|
|
for (i = odView[vn].hlow; i--; ) { |
385 |
|
|
hmax = hmin; |
386 |
|
|
hmin = i*odView[vn].hhi/odView[vn].hlow; |
387 |
|
|
for (l = vmin; l < vmax; l++) { /* vertical edges */ |
388 |
|
|
d1 = dm[l*odView[vn].hhi+hmin]; |
389 |
|
|
for (k = hmin+1; k < hmax; k++) { |
390 |
|
|
d0 = d1; |
391 |
|
|
d1 = dm[l*odView[vn].hhi+k]; |
392 |
|
|
if (d0 > (1.+DEPTHEPS)*d1 || |
393 |
|
|
(1.+DEPTHEPS)*d0 < d1) { |
394 |
|
|
SET4(odView[vn].emap, |
395 |
|
|
j*odView[vn].hlow + i); |
396 |
|
|
break; |
397 |
|
|
} |
398 |
|
|
} |
399 |
|
|
if (k < hmax) |
400 |
|
|
break; |
401 |
|
|
} |
402 |
|
|
if (l < vmax) |
403 |
|
|
continue; |
404 |
|
|
for (k = hmin; k < hmax; k++) { /* horizontal edges */ |
405 |
|
|
d1 = dm[vmin*odView[vn].hhi+k]; |
406 |
|
|
for (l = vmin+1; l < vmax; l++) { |
407 |
|
|
d0 = d1; |
408 |
|
|
d1 = dm[l*odView[vn].hhi+k]; |
409 |
|
|
if (d0 > (1.+DEPTHEPS)*d1 || |
410 |
|
|
(1.+DEPTHEPS)*d0 < d1) { |
411 |
|
|
SET4(odView[vn].emap, |
412 |
|
|
j*odView[vn].hlow + i); |
413 |
|
|
break; |
414 |
|
|
} |
415 |
|
|
} |
416 |
|
|
if (l < vmax) |
417 |
|
|
break; |
418 |
|
|
} |
419 |
|
|
} |
420 |
|
|
} |
421 |
|
|
} |
422 |
|
|
|
423 |
|
|
|
424 |
|
|
odUpdate(vn) /* update this view */ |
425 |
|
|
int vn; |
426 |
|
|
{ |
427 |
|
|
int i, j; |
428 |
|
|
register struct ODblock *bp; |
429 |
|
|
register int k; |
430 |
|
|
|
431 |
|
|
DCHECK(vn<0 | vn>=odNViews, CONSISTENCY, |
432 |
|
|
"bad view number in odUpdate"); |
433 |
|
|
/* need to do some tone mapping? */ |
434 |
|
|
if (needmapping & NEWRGB) { |
435 |
|
|
if (needmapping & NEWMAP) { |
436 |
gwlarson |
3.2 |
if (needmapping & NEWHIST) |
437 |
|
|
tmClearHisto(); |
438 |
gwlarson |
3.1 |
if (tmAddHisto(odS.brt,odS.nsamp,1) != TM_E_OK) |
439 |
|
|
return; |
440 |
|
|
if (tmComputeMapping(0.,0.,0.) != TM_E_OK) |
441 |
|
|
return; |
442 |
|
|
for (k = odS.nsamp; k--; ) /* redraw all */ |
443 |
|
|
if (odS.ip[k][0] >= 0) |
444 |
|
|
SET4(odS.redraw, k); |
445 |
|
|
} |
446 |
|
|
if (tmMapPixels(odS.rgb,odS.brt,odS.chr,odS.nsamp) != TM_E_OK) |
447 |
|
|
return; |
448 |
|
|
needmapping = 0; /* reset flag */ |
449 |
|
|
} |
450 |
|
|
/* draw each block in view */ |
451 |
gwlarson |
3.2 |
for (j = odView[vn].vlow; j--; ) |
452 |
|
|
for (i = 0; i < odView[vn].hlow; i++) { |
453 |
gwlarson |
3.1 |
/* get block */ |
454 |
|
|
bp = odView[vn].bmap + j*odView[vn].hlow + i; |
455 |
|
|
/* do quick, conservative flag check */ |
456 |
|
|
for (k = (bp->first+bp->nsamp+31)>>5; |
457 |
|
|
k-- > bp->first>>5; ) |
458 |
|
|
if (odS.redraw[k]) |
459 |
|
|
break; /* non-zero flag */ |
460 |
|
|
if (k < bp->first>>5) |
461 |
|
|
continue; /* no flags set */ |
462 |
|
|
for (k = bp->nsamp; k--; ) /* sample by sample */ |
463 |
|
|
if (CHK4(odS.redraw, bp->first+k)) { |
464 |
|
|
odDrawBlockSamp(vn, i, j, bp->first+k); |
465 |
|
|
CLR4(odS.redraw, bp->first+k); |
466 |
|
|
} |
467 |
|
|
} |
468 |
|
|
} |
469 |
|
|
|
470 |
|
|
|
471 |
|
|
#if 0 |
472 |
|
|
static |
473 |
|
|
clip_end(p, o, vp) /* clip line segment to view */ |
474 |
|
|
GLshort p[3]; |
475 |
|
|
short o[2]; |
476 |
|
|
register struct ODview *vp; |
477 |
|
|
{ |
478 |
|
|
if (p[0] < 0) { |
479 |
|
|
p[1] = -o[0]*(p[1]-o[1])/(p[0]-o[0]) + o[1]; |
480 |
|
|
p[2] = -o[0]*p[2]/(p[0]-o[0]); |
481 |
|
|
p[0] = 0; |
482 |
|
|
} else if (p[0] >= vp->hhi) { |
483 |
|
|
p[1] = (vp->hhi-1-o[0])*(p[1]-o[1])/(p[0]-o[0]) + o[1]; |
484 |
|
|
p[2] = (vp->hhi-1-o[0])*p[2]/(p[0]-o[0]); |
485 |
|
|
p[0] = vp->hhi-1; |
486 |
|
|
} |
487 |
|
|
if (p[1] < 0) { |
488 |
|
|
p[0] = -o[1]*(p[0]-o[0])/(p[1]-o[1]) + o[0]; |
489 |
|
|
p[2] = -o[1]*p[2]/(p[1]-o[1]); |
490 |
|
|
p[1] = 0; |
491 |
|
|
} else if (p[1] >= vp->vhi) { |
492 |
|
|
p[0] = (vp->vhi-1-o[1])*(p[0]-o[0])/(p[1]-o[1]) + o[0]; |
493 |
|
|
p[2] = (vp->vhi-1-o[1])*p[2]/(p[1]-o[1]); |
494 |
|
|
p[1] = vp->vhi-1; |
495 |
|
|
} |
496 |
|
|
} |
497 |
|
|
#endif |
498 |
|
|
|
499 |
|
|
|
500 |
|
|
static int |
501 |
|
|
make_arms(ar, cp, vp, sz) /* make arms for triangle fan */ |
502 |
|
|
GLshort ar[MAXFAN][3]; |
503 |
|
|
short cp[2]; |
504 |
|
|
register struct ODview *vp; |
505 |
|
|
double sz; |
506 |
|
|
{ |
507 |
|
|
int na, dv; |
508 |
|
|
double hrad, vrad, phi0, phi; |
509 |
|
|
register int i; |
510 |
|
|
|
511 |
|
|
DCHECK(sz > 1, CONSISTENCY, "super-unary size in make_arms"); |
512 |
|
|
na = MAXFAN*sz*sz + 0.5; /* keep area constant */ |
513 |
|
|
if (na < MINFAN) na = MINFAN; |
514 |
|
|
hrad = FANSIZE*sz*vp->hhi/vp->hlow; |
515 |
|
|
vrad = FANSIZE*sz*vp->vhi/vp->vlow; |
516 |
|
|
if (hrad*vrad < 2.25) |
517 |
|
|
hrad = vrad = 1.5; |
518 |
|
|
phi0 = (2.*PI) * frandom(); |
519 |
|
|
dv = OMAXDEPTH*sz + 0.5; |
520 |
|
|
for (i = 0; i < na; i++) { |
521 |
|
|
phi = phi0 + (2.*PI)*i/na; |
522 |
|
|
ar[i][0] = cp[0] + tcos(phi)*hrad + 0.5; |
523 |
|
|
ar[i][1] = cp[1] + tsin(phi)*vrad + 0.5; |
524 |
|
|
ar[i][2] = dv; |
525 |
|
|
/* clip_end(ar[i], cp, vp); */ |
526 |
|
|
} |
527 |
|
|
return(na); |
528 |
|
|
} |
529 |
|
|
|
530 |
|
|
|
531 |
|
|
static int |
532 |
|
|
depthchange(vp, x0, y0, x1, y1) /* check depth discontinuity */ |
533 |
|
|
register struct ODview *vp; |
534 |
|
|
int x0, y0, x1, y1; |
535 |
|
|
{ |
536 |
|
|
register double d0, d1; |
537 |
|
|
|
538 |
|
|
DCHECK(x0<0 | x0>=vp->hhi | y0<0 | y0>=vp->vhi, |
539 |
|
|
CONSISTENCY, "coordinates off view in depthchange"); |
540 |
|
|
|
541 |
|
|
if (x1<0 | x1>=vp->hhi | y1<0 | y1>=vp->vhi) |
542 |
|
|
return(1); |
543 |
|
|
|
544 |
|
|
d0 = vp->dmap[y0*vp->hhi + x0]; |
545 |
|
|
d1 = vp->dmap[y1*vp->hhi + x1]; |
546 |
|
|
|
547 |
|
|
return((1.+DEPTHEPS)*d0 < d1 || d0 > (1.+DEPTHEPS)*d1); |
548 |
|
|
} |
549 |
|
|
|
550 |
|
|
|
551 |
|
|
static |
552 |
|
|
clip_edge(p, o, vp) /* clip line segment to depth edge */ |
553 |
|
|
GLshort p[3]; |
554 |
|
|
short o[2]; |
555 |
|
|
register struct ODview *vp; |
556 |
|
|
{ |
557 |
|
|
int x, y, xstep, ystep, rise, rise2, run, run2, n; |
558 |
|
|
|
559 |
|
|
DCHECK(vp->dmap==NULL, CONSISTENCY, |
560 |
|
|
"clip_edge called with no depth map"); |
561 |
|
|
x = o[0]; y = o[1]; |
562 |
|
|
run = p[0] - x; |
563 |
|
|
xstep = run > 0 ? 1 : -1; |
564 |
|
|
run *= xstep; |
565 |
|
|
rise = p[1] - y; |
566 |
|
|
ystep = rise > 0 ? 1 : -1; |
567 |
|
|
rise *= ystep; |
568 |
|
|
rise2 = run2 = 0; |
569 |
|
|
if (rise > run) rise2 = 1; |
570 |
|
|
else run2 = 1; |
571 |
|
|
n = rise + run; |
572 |
|
|
while (n--) /* run out arm, checking depth */ |
573 |
|
|
if (run2 > rise2) { |
574 |
|
|
if (depthchange(vp, x, y, x+xstep, y)) |
575 |
|
|
break; |
576 |
|
|
x += xstep; |
577 |
|
|
rise2 += rise; |
578 |
|
|
} else { |
579 |
|
|
if (depthchange(vp, x, y, x, y+ystep)) |
580 |
|
|
break; |
581 |
|
|
y += ystep; |
582 |
|
|
run2 += run; |
583 |
|
|
} |
584 |
|
|
if (n < 0) /* found something? */ |
585 |
|
|
return; |
586 |
|
|
if (run > rise) |
587 |
|
|
p[2] = (x - o[0])*p[2]/(p[0] - o[0]); |
588 |
|
|
else |
589 |
|
|
p[2] = (y - o[1])*p[2]/(p[1] - o[1]); |
590 |
|
|
p[0] = x; |
591 |
|
|
p[1] = y; |
592 |
|
|
} |
593 |
|
|
|
594 |
|
|
|
595 |
|
|
static int |
596 |
|
|
getblock(vp, h, v) /* get block index */ |
597 |
|
|
register struct ODview *vp; |
598 |
|
|
register int h, v; |
599 |
|
|
{ |
600 |
|
|
if (h<0 | h>=vp->hhi | v<0 | v>=vp->vhi) |
601 |
|
|
return(-1); |
602 |
|
|
return(h*vp->hlow/vp->hhi + v*vp->vlow/vp->vhi*vp->hlow); |
603 |
|
|
} |
604 |
|
|
|
605 |
|
|
|
606 |
|
|
odDrawBlockSamp(vn, h, v, id) /* draw sample in view block */ |
607 |
|
|
int vn, h, v; |
608 |
|
|
register int id; |
609 |
|
|
{ |
610 |
|
|
GLshort arm[MAXFAN][3]; |
611 |
|
|
int narms, blockindex, bi1; |
612 |
|
|
register struct ODview *vp; |
613 |
|
|
double size; |
614 |
|
|
int home_edges; |
615 |
|
|
register int i; |
616 |
|
|
|
617 |
|
|
vp = odView + vn; |
618 |
|
|
blockindex = v*vp->hlow + h; |
619 |
|
|
DCHECK(odS.ip[id][0]*vp->hlow/vp->hhi != h | |
620 |
|
|
odS.ip[id][1]*vp->vlow/vp->vhi != v, |
621 |
|
|
CONSISTENCY, "bad sample position in odDrawBlockSamp"); |
622 |
|
|
DCHECK(vp->bmap[blockindex].nused <= 0, |
623 |
|
|
CONSISTENCY, "bad in-use count in odDrawBlockSamp"); |
624 |
|
|
/* create triangle fan */ |
625 |
|
|
size = 1./sqrt((double)vp->bmap[blockindex].nused); |
626 |
|
|
narms = make_arms(arm, odS.ip[id], vp, size); |
627 |
|
|
if (vp->emap != NULL) { /* check for edge collisions */ |
628 |
|
|
home_edges = CHK4(vp->emap, blockindex); |
629 |
|
|
for (i = 0; i < narms; i++) |
630 |
|
|
/* the following test is flawed, because we could |
631 |
|
|
* be passing through a block on a diagonal run */ |
632 |
|
|
if (home_edges || |
633 |
|
|
( (bi1 = getblock(vp, arm[i][0], arm[i][1])) |
634 |
|
|
!= blockindex && |
635 |
|
|
(bi1 < 0 || CHK4(vp->emap, bi1)) )) |
636 |
|
|
clip_edge(arm[i], odS.ip[id], vp); |
637 |
|
|
} |
638 |
|
|
/* draw triangle fan */ |
639 |
|
|
glColor3ub(odS.rgb[id][0], odS.rgb[id][1], odS.rgb[id][2]); |
640 |
|
|
glBegin(GL_TRIANGLE_FAN); |
641 |
|
|
glVertex3s((GLshort)odS.ip[id][0], (GLshort)odS.ip[id][1], (GLshort)0); |
642 |
|
|
for (i = 0; i < narms; i++) |
643 |
|
|
glVertex3sv(arm[i]); |
644 |
|
|
glVertex3sv(arm[0]); /* connect last to first */ |
645 |
|
|
glEnd(); |
646 |
|
|
} |