1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: srcdraw.c,v 2.10 2003/10/24 05:29:43 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Draw small sources into image in case we missed them. |
6 |
* |
7 |
* External symbols declared in ray.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
#include "ray.h" |
13 |
#include "view.h" |
14 |
#include "source.h" |
15 |
|
16 |
|
17 |
#define CLIP_ABOVE 1 |
18 |
#define CLIP_BELOW 2 |
19 |
#define CLIP_RIGHT 3 |
20 |
#define CLIP_LEFT 4 |
21 |
|
22 |
#define MAXVERT 10 |
23 |
|
24 |
typedef struct splist { |
25 |
struct splist *next; /* next source in list */ |
26 |
int sn; /* source number */ |
27 |
short nv; /* number of vertices */ |
28 |
RREAL vl[3][2]; /* vertex array (last) */ |
29 |
} SPLIST; /* source polygon list */ |
30 |
|
31 |
extern VIEW ourview; /* our view parameters */ |
32 |
extern int hres, vres; /* our image resolution */ |
33 |
static SPLIST *sphead = NULL; /* our list of source polys */ |
34 |
|
35 |
static int inregion(RREAL p[2], double cv, int crit); |
36 |
static void clipregion(RREAL a[2], RREAL b[2], double cv, int crit, RREAL r[2]); |
37 |
static int hp_clip_poly(RREAL vl[][2], int nv, double cv, int crit, |
38 |
RREAL vlo[][2]); |
39 |
static int box_clip_poly(RREAL vl[MAXVERT][2], int nv, |
40 |
double xl, double xr, double yb, double ya, RREAL vlo[MAXVERT][2]); |
41 |
static double minw2(RREAL vl[][2], int nv, double ar2); |
42 |
static void convex_center(RREAL vl[][2], int nv, RREAL cv[2]); |
43 |
static double poly_area(RREAL vl[][2], int nv); |
44 |
static int convex_hull(RREAL vl[][2], int nv, RREAL vlo[][2]); |
45 |
static void spinsert(int sn, RREAL vl[][2], int nv); |
46 |
static int sourcepoly(int sn, RREAL sp[MAXVERT][2]); |
47 |
|
48 |
|
49 |
static int |
50 |
inregion( /* check if vertex is in region */ |
51 |
RREAL p[2], |
52 |
double cv, |
53 |
int crit |
54 |
) |
55 |
{ |
56 |
switch (crit) { |
57 |
case CLIP_ABOVE: |
58 |
return(p[1] < cv); |
59 |
case CLIP_BELOW: |
60 |
return(p[1] >= cv); |
61 |
case CLIP_RIGHT: |
62 |
return(p[0] < cv); |
63 |
case CLIP_LEFT: |
64 |
return(p[0] >= cv); |
65 |
} |
66 |
return(-1); |
67 |
} |
68 |
|
69 |
|
70 |
static void |
71 |
clipregion( /* find intersection with boundary */ |
72 |
register RREAL a[2], |
73 |
register RREAL b[2], |
74 |
double cv, |
75 |
int crit, |
76 |
RREAL r[2] /* return value */ |
77 |
) |
78 |
{ |
79 |
switch (crit) { |
80 |
case CLIP_ABOVE: |
81 |
case CLIP_BELOW: |
82 |
r[1] = cv; |
83 |
r[0] = a[0] + (cv-a[1])/(b[1]-a[1])*(b[0]-a[0]); |
84 |
return; |
85 |
case CLIP_RIGHT: |
86 |
case CLIP_LEFT: |
87 |
r[0] = cv; |
88 |
r[1] = a[1] + (cv-a[0])/(b[0]-a[0])*(b[1]-a[1]); |
89 |
return; |
90 |
} |
91 |
} |
92 |
|
93 |
|
94 |
static int |
95 |
hp_clip_poly( /* clip polygon to half-plane */ |
96 |
RREAL vl[][2], |
97 |
int nv, |
98 |
double cv, |
99 |
int crit, |
100 |
RREAL vlo[][2] /* return value */ |
101 |
) |
102 |
{ |
103 |
RREAL *s, *p; |
104 |
register int j, nvo; |
105 |
|
106 |
s = vl[nv-1]; |
107 |
nvo = 0; |
108 |
for (j = 0; j < nv; j++) { |
109 |
p = vl[j]; |
110 |
if (inregion(p, cv, crit)) { |
111 |
if (!inregion(s, cv, crit)) |
112 |
clipregion(s, p, cv, crit, vlo[nvo++]); |
113 |
vlo[nvo][0] = p[0]; vlo[nvo++][1] = p[1]; |
114 |
} else if (inregion(s, cv, crit)) |
115 |
clipregion(s, p, cv, crit, vlo[nvo++]); |
116 |
s = p; |
117 |
} |
118 |
return(nvo); |
119 |
} |
120 |
|
121 |
|
122 |
static int |
123 |
box_clip_poly( /* clip polygon to box */ |
124 |
RREAL vl[MAXVERT][2], |
125 |
int nv, |
126 |
double xl, |
127 |
double xr, |
128 |
double yb, |
129 |
double ya, |
130 |
RREAL vlo[MAXVERT][2] /* return value */ |
131 |
) |
132 |
{ |
133 |
RREAL vlt[MAXVERT][2]; |
134 |
int nvt, nvo; |
135 |
|
136 |
nvt = hp_clip_poly(vl, nv, yb, CLIP_BELOW, vlt); |
137 |
nvo = hp_clip_poly(vlt, nvt, ya, CLIP_ABOVE, vlo); |
138 |
nvt = hp_clip_poly(vlo, nvo, xl, CLIP_LEFT, vlt); |
139 |
nvo = hp_clip_poly(vlt, nvt, xr, CLIP_RIGHT, vlo); |
140 |
|
141 |
return(nvo); |
142 |
} |
143 |
|
144 |
|
145 |
static double |
146 |
minw2( /* compute square of minimum width */ |
147 |
RREAL vl[][2], |
148 |
int nv, |
149 |
double ar2 |
150 |
) |
151 |
{ |
152 |
double d2, w2, w2min, w2max; |
153 |
register RREAL *p0, *p1, *p2; |
154 |
int i, j; |
155 |
/* find minimum for all widths */ |
156 |
w2min = FHUGE; |
157 |
p0 = vl[nv-1]; |
158 |
for (i = 0; i < nv; i++) { /* for each edge */ |
159 |
p1 = vl[i]; |
160 |
d2 = (p1[0]-p0[0])*(p1[0]-p0[0]) + |
161 |
(p1[1]-p0[1])*(p1[1]-p0[1])*ar2; |
162 |
w2max = 0.; /* find maximum for this side */ |
163 |
for (j = 1; j < nv-1; j++) { |
164 |
p2 = vl[(i+j)%nv]; |
165 |
w2 = (p1[0]-p0[0])*(p2[1]-p0[1]) - |
166 |
(p1[1]-p0[1])*(p2[0]-p0[0]); |
167 |
w2 = w2*w2*ar2/d2; /* triangle height squared */ |
168 |
if (w2 > w2max) |
169 |
w2max = w2; |
170 |
} |
171 |
if (w2max < w2min) /* global min. based on local max.'s */ |
172 |
w2min = w2max; |
173 |
p0 = p1; |
174 |
} |
175 |
return(w2min); |
176 |
} |
177 |
|
178 |
|
179 |
static void |
180 |
convex_center( /* compute center of convex polygon */ |
181 |
register RREAL vl[][2], |
182 |
int nv, |
183 |
RREAL cv[2] /* return value */ |
184 |
) |
185 |
{ |
186 |
register int i; |
187 |
/* simple average (suboptimal) */ |
188 |
cv[0] = cv[1] = 0.; |
189 |
for (i = 0; i < nv; i++) { |
190 |
cv[0] += vl[i][0]; |
191 |
cv[1] += vl[i][1]; |
192 |
} |
193 |
cv[0] /= (double)nv; |
194 |
cv[1] /= (double)nv; |
195 |
} |
196 |
|
197 |
|
198 |
static double |
199 |
poly_area( /* compute area of polygon */ |
200 |
register RREAL vl[][2], |
201 |
int nv |
202 |
) |
203 |
{ |
204 |
double a; |
205 |
RREAL v0[2], v1[2]; |
206 |
register int i; |
207 |
|
208 |
a = 0.; |
209 |
v0[0] = vl[1][0] - vl[0][0]; |
210 |
v0[1] = vl[1][1] - vl[0][1]; |
211 |
for (i = 2; i < nv; i++) { |
212 |
v1[0] = vl[i][0] - vl[0][0]; |
213 |
v1[1] = vl[i][1] - vl[0][1]; |
214 |
a += v0[0]*v1[1] - v0[1]*v1[0]; |
215 |
v0[0] = v1[0]; v0[1] = v1[1]; |
216 |
} |
217 |
return(a * (a >= 0. ? .5 : -.5)); |
218 |
} |
219 |
|
220 |
|
221 |
static int |
222 |
convex_hull( /* compute polygon's convex hull */ |
223 |
RREAL vl[][2], |
224 |
int nv, |
225 |
RREAL vlo[][2] /* return value */ |
226 |
) |
227 |
{ |
228 |
int nvo, nvt; |
229 |
RREAL vlt[MAXVERT][2]; |
230 |
double voa, vta; |
231 |
register int i, j; |
232 |
/* start with original polygon */ |
233 |
for (i = nvo = nv; i--; ) { |
234 |
vlo[i][0] = vl[i][0]; vlo[i][1] = vl[i][1]; |
235 |
} |
236 |
voa = poly_area(vlo, nvo); /* compute its area */ |
237 |
for (i = 0; i < nvo; i++) { /* for each output vertex */ |
238 |
for (j = 0; j < i; j++) { |
239 |
vlt[j][0] = vlo[j][0]; vlt[j][1] = vlo[j][1]; |
240 |
} |
241 |
nvt = nvo - 1; /* make poly w/o vertex */ |
242 |
for (j = i; j < nvt; j++) { |
243 |
vlt[j][0] = vlo[j+1][0]; vlt[j][1] = vlo[j+1][1]; |
244 |
} |
245 |
vta = poly_area(vlt, nvt); |
246 |
if (vta >= voa) { /* is simpler poly bigger? */ |
247 |
voa = vta; /* then use it */ |
248 |
for (j = nvo = nvt; j--; ) { |
249 |
vlo[j][0] = vlt[j][0]; vlo[j][1] = vlt[j][1]; |
250 |
} |
251 |
i--; /* next adjust */ |
252 |
} |
253 |
} |
254 |
return(nvo); |
255 |
} |
256 |
|
257 |
|
258 |
static void |
259 |
spinsert( /* insert new source polygon */ |
260 |
int sn, |
261 |
RREAL vl[][2], |
262 |
int nv |
263 |
) |
264 |
{ |
265 |
register SPLIST *spn; |
266 |
register int i; |
267 |
|
268 |
if (nv < 3) |
269 |
return; |
270 |
if (nv > 3) |
271 |
spn = (SPLIST *)malloc(sizeof(SPLIST)+sizeof(RREAL)*2*(nv-3)); |
272 |
else |
273 |
spn = (SPLIST *)malloc(sizeof(SPLIST)); |
274 |
if (spn == NULL) |
275 |
error(SYSTEM, "out of memory in spinsert"); |
276 |
spn->sn = sn; |
277 |
for (i = spn->nv = nv; i--; ) { |
278 |
spn->vl[i][0] = vl[i][0]; spn->vl[i][1] = vl[i][1]; |
279 |
} |
280 |
spn->next = sphead; /* push onto global list */ |
281 |
sphead = spn; |
282 |
} |
283 |
|
284 |
|
285 |
static int |
286 |
sourcepoly( /* compute image polygon for source */ |
287 |
int sn, |
288 |
RREAL sp[MAXVERT][2] |
289 |
) |
290 |
{ |
291 |
static short cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2}, |
292 |
{0,1,3,7,6,4},{0,1,5,7,6,2}, |
293 |
{0,2,6,7,5,1},{0,4,6,7,3,1}, |
294 |
{0,2,3,7,5,4},{1,5,4,6,2,3}}; |
295 |
register SRCREC *s = source + sn; |
296 |
FVECT ap, ip; |
297 |
RREAL pt[6][2]; |
298 |
int dir; |
299 |
register int i, j; |
300 |
|
301 |
if (s->sflags & (SDISTANT|SFLAT)) { |
302 |
if (s->sflags & SDISTANT && ourview.type == VT_PAR) |
303 |
return(0); /* all or nothing case */ |
304 |
if (s->sflags & SFLAT) { |
305 |
for (i = 0; i < 3; i++) |
306 |
ap[i] = s->sloc[i] - ourview.vp[i]; |
307 |
if (DOT(ap, s->snorm) >= 0.) |
308 |
return(0); /* source faces away */ |
309 |
} |
310 |
for (j = 0; j < 4; j++) { /* four corners */ |
311 |
for (i = 0; i < 3; i++) { |
312 |
ap[i] = s->sloc[i]; |
313 |
if ((j==1)|(j==2)) ap[i] += s->ss[SU][i]; |
314 |
else ap[i] -= s->ss[SU][i]; |
315 |
if ((j==2)|(j==3)) ap[i] += s->ss[SV][i]; |
316 |
else ap[i] -= s->ss[SV][i]; |
317 |
if (s->sflags & SDISTANT) { |
318 |
ap[i] *= 1. + ourview.vfore; |
319 |
ap[i] += ourview.vp[i]; |
320 |
} |
321 |
} |
322 |
viewloc(ip, &ourview, ap); /* find image point */ |
323 |
if (ip[2] <= 0.) |
324 |
return(0); /* in front of view */ |
325 |
sp[j][0] = ip[0]; sp[j][1] = ip[1]; |
326 |
} |
327 |
return(4); |
328 |
} |
329 |
/* identify furthest corner */ |
330 |
for (i = 0; i < 3; i++) |
331 |
ap[i] = s->sloc[i] - ourview.vp[i]; |
332 |
dir = (DOT(ap,s->ss[SU])>0.) | |
333 |
(DOT(ap,s->ss[SV])>0.)<<1 | |
334 |
(DOT(ap,s->ss[SW])>0.)<<2 ; |
335 |
/* order vertices based on this */ |
336 |
for (j = 0; j < 6; j++) { |
337 |
for (i = 0; i < 3; i++) { |
338 |
ap[i] = s->sloc[i]; |
339 |
if (cubeord[dir][j] & 1) ap[i] += s->ss[SU][i]; |
340 |
else ap[i] -= s->ss[SU][i]; |
341 |
if (cubeord[dir][j] & 2) ap[i] += s->ss[SV][i]; |
342 |
else ap[i] -= s->ss[SV][i]; |
343 |
if (cubeord[dir][j] & 4) ap[i] += s->ss[SW][i]; |
344 |
else ap[i] -= s->ss[SW][i]; |
345 |
} |
346 |
viewloc(ip, &ourview, ap); /* find image point */ |
347 |
if (ip[2] <= 0.) |
348 |
return(0); /* in front of view */ |
349 |
pt[j][0] = ip[0]; pt[j][1] = ip[1]; |
350 |
} |
351 |
return(convex_hull(pt, 6, sp)); /* make sure it's convex */ |
352 |
} |
353 |
|
354 |
|
355 |
/* initialize by finding sources smaller than rad */ |
356 |
extern void |
357 |
init_drawsources( |
358 |
int rad /* source sample size */ |
359 |
) |
360 |
{ |
361 |
RREAL spoly[MAXVERT][2]; |
362 |
int nsv; |
363 |
register SPLIST *sp; |
364 |
register int i; |
365 |
/* free old source list if one */ |
366 |
for (sp = sphead; sp != NULL; sp = sphead) { |
367 |
sphead = sp->next; |
368 |
free((void *)sp); |
369 |
} |
370 |
/* loop through all sources */ |
371 |
for (i = nsources; i--; ) { |
372 |
/* compute image polygon for source */ |
373 |
if (!(nsv = sourcepoly(i, spoly))) |
374 |
continue; |
375 |
/* clip to image boundaries */ |
376 |
if (!(nsv = box_clip_poly(spoly, nsv, 0., 1., 0., 1., spoly))) |
377 |
continue; |
378 |
/* big enough for standard sampling? */ |
379 |
if (minw2(spoly, nsv, ourview.vn2/ourview.hn2) > |
380 |
(double)rad*rad/hres/hres) |
381 |
continue; |
382 |
/* OK, add to our list */ |
383 |
spinsert(i, spoly, nsv); |
384 |
} |
385 |
} |
386 |
|
387 |
extern void /* add sources smaller than rad to computed subimage */ |
388 |
drawsources( |
389 |
COLOR *pic[], /* subimage pixel value array */ |
390 |
float *zbf[], /* subimage distance array (opt.) */ |
391 |
int x0, /* origin and size of subimage */ |
392 |
int xsiz, |
393 |
int y0, |
394 |
int ysiz |
395 |
) |
396 |
{ |
397 |
RREAL spoly[MAXVERT][2], ppoly[MAXVERT][2]; |
398 |
int nsv, npv; |
399 |
int xmin, xmax, ymin, ymax, x, y; |
400 |
RREAL cxy[2]; |
401 |
double w; |
402 |
RAY sr; |
403 |
register SPLIST *sp; |
404 |
register int i; |
405 |
/* check each source in our list */ |
406 |
for (sp = sphead; sp != NULL; sp = sp->next) { |
407 |
/* clip source poly to subimage */ |
408 |
nsv = box_clip_poly(sp->vl, sp->nv, |
409 |
(double)x0/hres, (double)(x0+xsiz)/hres, |
410 |
(double)y0/vres, (double)(y0+ysiz)/vres, spoly); |
411 |
if (!nsv) |
412 |
continue; |
413 |
/* find common subimage (BBox) */ |
414 |
xmin = x0 + xsiz; xmax = x0; |
415 |
ymin = y0 + ysiz; ymax = y0; |
416 |
for (i = 0; i < nsv; i++) { |
417 |
if ((double)xmin/hres > spoly[i][0]) |
418 |
xmin = spoly[i][0]*hres + FTINY; |
419 |
if ((double)xmax/hres < spoly[i][0]) |
420 |
xmax = spoly[i][0]*hres - FTINY; |
421 |
if ((double)ymin/vres > spoly[i][1]) |
422 |
ymin = spoly[i][1]*vres + FTINY; |
423 |
if ((double)ymax/vres < spoly[i][1]) |
424 |
ymax = spoly[i][1]*vres - FTINY; |
425 |
} |
426 |
/* evaluate each pixel in BBox */ |
427 |
for (y = ymin; y <= ymax; y++) |
428 |
for (x = xmin; x <= xmax; x++) { |
429 |
/* subarea for pixel */ |
430 |
npv = box_clip_poly(spoly, nsv, |
431 |
(double)x/hres, (x+1.)/hres, |
432 |
(double)y/vres, (y+1.)/vres, |
433 |
ppoly); |
434 |
if (!npv) |
435 |
continue; /* no overlap */ |
436 |
convex_center(ppoly, npv, cxy); |
437 |
if ((sr.rmax = viewray(sr.rorg,sr.rdir,&ourview, |
438 |
cxy[0],cxy[1])) < -FTINY) |
439 |
continue; /* not in view */ |
440 |
if (source[sp->sn].sflags & SSPOT && |
441 |
spotout(&sr, source[sp->sn].sl.s)) |
442 |
continue; /* outside spot */ |
443 |
w = poly_area(ppoly, npv) * hres * vres; |
444 |
if (w < .95) { /* subpixel source */ |
445 |
rayorigin(&sr, NULL, SHADOW, 1.0); |
446 |
sr.rsrc = sp->sn; |
447 |
} else |
448 |
rayorigin(&sr, NULL, PRIMARY, 1.0); |
449 |
rayvalue(&sr); /* compute value */ |
450 |
if (bright(sr.rcol) <= FTINY) |
451 |
continue; /* missed/blocked */ |
452 |
/* modify pixel */ |
453 |
if (zbf[y-y0] != NULL && |
454 |
sr.rt < 0.99*zbf[y-y0][x-x0]) |
455 |
zbf[y-y0][x-x0] = sr.rt; |
456 |
else if (!bigdiff(sr.rcol, pic[y-y0][x-x0], |
457 |
0.01)) /* source sample */ |
458 |
setcolor(pic[y-y0][x-x0], 0., 0., 0.); |
459 |
scalecolor(sr.rcol, w); |
460 |
scalecolor(pic[y-y0][x-x0], 1.-w); |
461 |
addcolor(pic[y-y0][x-x0], sr.rcol); |
462 |
} |
463 |
} |
464 |
} |