10 |
|
#include "copyright.h" |
11 |
|
|
12 |
|
#include "ray.h" |
13 |
– |
|
13 |
|
#include "view.h" |
14 |
< |
|
14 |
> |
#include "otypes.h" |
15 |
|
#include "source.h" |
16 |
|
|
17 |
|
|
33 |
|
extern int hres, vres; /* our image resolution */ |
34 |
|
static SPLIST *sphead = NULL; /* our list of source polys */ |
35 |
|
|
36 |
+ |
static int inregion(RREAL p[2], double cv, int crit); |
37 |
+ |
static void clipregion(RREAL a[2], RREAL b[2], double cv, int crit, RREAL r[2]); |
38 |
+ |
static int hp_clip_poly(RREAL vl[][2], int nv, double cv, int crit, |
39 |
+ |
RREAL vlo[][2]); |
40 |
+ |
static int box_clip_poly(RREAL vl[MAXVERT][2], int nv, |
41 |
+ |
double xl, double xr, double yb, double ya, RREAL vlo[MAXVERT][2]); |
42 |
+ |
static double minw2(RREAL vl[][2], int nv, double ar2); |
43 |
+ |
static void convex_center(RREAL vl[][2], int nv, RREAL cv[2]); |
44 |
+ |
static double poly_area(RREAL vl[][2], int nv); |
45 |
+ |
static int convex_hull(RREAL vl[][2], int nv, RREAL vlo[][2]); |
46 |
+ |
static void spinsert(int sn, RREAL vl[][2], int nv); |
47 |
+ |
static int sourcepoly(int sn, RREAL sp[MAXVERT][2]); |
48 |
|
|
49 |
+ |
|
50 |
|
static int |
51 |
< |
inregion(p, cv, crit) /* check if vertex is in region */ |
52 |
< |
RREAL p[2]; |
53 |
< |
double cv; |
54 |
< |
int crit; |
51 |
> |
inregion( /* check if vertex is in region */ |
52 |
> |
RREAL p[2], |
53 |
> |
double cv, |
54 |
> |
int crit |
55 |
> |
) |
56 |
|
{ |
57 |
|
switch (crit) { |
58 |
|
case CLIP_ABOVE: |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
< |
static |
72 |
< |
clipregion(a, b, cv, crit, r) /* find intersection with boundary */ |
73 |
< |
register RREAL a[2], b[2]; |
74 |
< |
double cv; |
75 |
< |
int crit; |
76 |
< |
RREAL r[2]; /* return value */ |
71 |
> |
static void |
72 |
> |
clipregion( /* find intersection with boundary */ |
73 |
> |
RREAL a[2], |
74 |
> |
RREAL b[2], |
75 |
> |
double cv, |
76 |
> |
int crit, |
77 |
> |
RREAL r[2] /* return value */ |
78 |
> |
) |
79 |
|
{ |
80 |
|
switch (crit) { |
81 |
|
case CLIP_ABOVE: |
93 |
|
|
94 |
|
|
95 |
|
static int |
96 |
< |
hp_clip_poly(vl, nv, cv, crit, vlo) /* clip polygon to half-plane */ |
97 |
< |
RREAL vl[][2]; |
98 |
< |
int nv; |
99 |
< |
double cv; |
100 |
< |
int crit; |
101 |
< |
RREAL vlo[][2]; /* return value */ |
96 |
> |
hp_clip_poly( /* clip polygon to half-plane */ |
97 |
> |
RREAL vl[][2], |
98 |
> |
int nv, |
99 |
> |
double cv, |
100 |
> |
int crit, |
101 |
> |
RREAL vlo[][2] /* return value */ |
102 |
> |
) |
103 |
|
{ |
104 |
|
RREAL *s, *p; |
105 |
< |
register int j, nvo; |
105 |
> |
int j, nvo; |
106 |
|
|
107 |
|
s = vl[nv-1]; |
108 |
|
nvo = 0; |
121 |
|
|
122 |
|
|
123 |
|
static int |
124 |
< |
box_clip_poly(vl, nv, xl, xr, yb, ya, vlo) /* clip polygon to box */ |
125 |
< |
RREAL vl[MAXVERT][2]; |
126 |
< |
int nv; |
127 |
< |
double xl, xr, yb, ya; |
128 |
< |
RREAL vlo[MAXVERT][2]; /* return value */ |
124 |
> |
box_clip_poly( /* clip polygon to box */ |
125 |
> |
RREAL vl[MAXVERT][2], |
126 |
> |
int nv, |
127 |
> |
double xl, |
128 |
> |
double xr, |
129 |
> |
double yb, |
130 |
> |
double ya, |
131 |
> |
RREAL vlo[MAXVERT][2] /* return value */ |
132 |
> |
) |
133 |
|
{ |
134 |
|
RREAL vlt[MAXVERT][2]; |
135 |
|
int nvt, nvo; |
144 |
|
|
145 |
|
|
146 |
|
static double |
147 |
< |
minw2(vl, nv, ar2) /* compute square of minimum width */ |
148 |
< |
RREAL vl[][2]; |
149 |
< |
int nv; |
150 |
< |
double ar2; |
147 |
> |
minw2( /* compute square of minimum width */ |
148 |
> |
RREAL vl[][2], |
149 |
> |
int nv, |
150 |
> |
double ar2 |
151 |
> |
) |
152 |
|
{ |
153 |
|
double d2, w2, w2min, w2max; |
154 |
< |
register RREAL *p0, *p1, *p2; |
154 |
> |
RREAL *p0, *p1, *p2; |
155 |
|
int i, j; |
156 |
|
/* find minimum for all widths */ |
157 |
|
w2min = FHUGE; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
< |
static |
181 |
< |
convex_center(vl, nv, cv) /* compute center of convex polygon */ |
182 |
< |
register RREAL vl[][2]; |
183 |
< |
int nv; |
184 |
< |
RREAL cv[2]; /* return value */ |
180 |
> |
static void |
181 |
> |
convex_center( /* compute center of convex polygon */ |
182 |
> |
RREAL vl[][2], |
183 |
> |
int nv, |
184 |
> |
RREAL cv[2] /* return value */ |
185 |
> |
) |
186 |
|
{ |
187 |
< |
register int i; |
187 |
> |
int i; |
188 |
|
/* simple average (suboptimal) */ |
189 |
|
cv[0] = cv[1] = 0.; |
190 |
|
for (i = 0; i < nv; i++) { |
197 |
|
|
198 |
|
|
199 |
|
static double |
200 |
< |
poly_area(vl, nv) /* compute area of polygon */ |
201 |
< |
register RREAL vl[][2]; |
202 |
< |
int nv; |
200 |
> |
poly_area( /* compute area of polygon */ |
201 |
> |
RREAL vl[][2], |
202 |
> |
int nv |
203 |
> |
) |
204 |
|
{ |
205 |
|
double a; |
206 |
|
RREAL v0[2], v1[2]; |
207 |
< |
register int i; |
207 |
> |
int i; |
208 |
|
|
209 |
|
a = 0.; |
210 |
|
v0[0] = vl[1][0] - vl[0][0]; |
220 |
|
|
221 |
|
|
222 |
|
static int |
223 |
< |
convex_hull(vl, nv, vlo) /* compute polygon's convex hull */ |
224 |
< |
RREAL vl[][2]; |
225 |
< |
int nv; |
226 |
< |
RREAL vlo[][2]; /* return value */ |
223 |
> |
convex_hull( /* compute polygon's convex hull */ |
224 |
> |
RREAL vl[][2], |
225 |
> |
int nv, |
226 |
> |
RREAL vlo[][2] /* return value */ |
227 |
> |
) |
228 |
|
{ |
229 |
|
int nvo, nvt; |
230 |
|
RREAL vlt[MAXVERT][2]; |
231 |
|
double voa, vta; |
232 |
< |
register int i, j; |
232 |
> |
int i, j; |
233 |
|
/* start with original polygon */ |
234 |
|
for (i = nvo = nv; i--; ) { |
235 |
|
vlo[i][0] = vl[i][0]; vlo[i][1] = vl[i][1]; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
< |
static |
260 |
< |
spinsert(sn, vl, nv) /* insert new source polygon */ |
261 |
< |
int sn; |
262 |
< |
RREAL vl[][2]; |
263 |
< |
int nv; |
259 |
> |
static void |
260 |
> |
spinsert( /* insert new source polygon */ |
261 |
> |
int sn, |
262 |
> |
RREAL vl[][2], |
263 |
> |
int nv |
264 |
> |
) |
265 |
|
{ |
266 |
< |
register SPLIST *spn; |
267 |
< |
register int i; |
266 |
> |
SPLIST *spn; |
267 |
> |
int i; |
268 |
|
|
269 |
|
if (nv < 3) |
270 |
|
return; |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
< |
int |
287 |
< |
sourcepoly(sn, sp) /* compute image polygon for source */ |
288 |
< |
int sn; |
289 |
< |
RREAL sp[MAXVERT][2]; |
286 |
> |
static int |
287 |
> |
sourcepoly( /* compute image polygon for source */ |
288 |
> |
int sn, |
289 |
> |
RREAL sp[MAXVERT][2] |
290 |
> |
) |
291 |
|
{ |
292 |
< |
static char cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2}, |
292 |
> |
static short cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2}, |
293 |
|
{0,1,3,7,6,4},{0,1,5,7,6,2}, |
294 |
|
{0,2,6,7,5,1},{0,4,6,7,3,1}, |
295 |
|
{0,2,3,7,5,4},{1,5,4,6,2,3}}; |
296 |
< |
register SRCREC *s = source + sn; |
296 |
> |
SRCREC *s = source + sn; |
297 |
|
FVECT ap, ip; |
298 |
|
RREAL pt[6][2]; |
299 |
|
int dir; |
300 |
< |
register int i, j; |
300 |
> |
int i, j; |
301 |
|
|
302 |
|
if (s->sflags & (SDISTANT|SFLAT)) { |
303 |
< |
if (s->sflags & SDISTANT && ourview.type == VT_PAR) |
304 |
< |
return(0); /* all or nothing case */ |
303 |
> |
if (s->sflags & SDISTANT) { |
304 |
> |
if (ourview.type == VT_PAR) |
305 |
> |
return(0); /* all or nothing case */ |
306 |
> |
if (s->srad >= 0.05) |
307 |
> |
return(0); /* should never be a problem */ |
308 |
> |
} |
309 |
|
if (s->sflags & SFLAT) { |
310 |
|
for (i = 0; i < 3; i++) |
311 |
|
ap[i] = s->sloc[i] - ourview.vp[i]; |
315 |
|
for (j = 0; j < 4; j++) { /* four corners */ |
316 |
|
for (i = 0; i < 3; i++) { |
317 |
|
ap[i] = s->sloc[i]; |
318 |
< |
if (j==1|j==2) ap[i] += s->ss[SU][i]; |
318 |
> |
if ((j==1)|(j==2)) ap[i] += s->ss[SU][i]; |
319 |
|
else ap[i] -= s->ss[SU][i]; |
320 |
< |
if (j==2|j==3) ap[i] += s->ss[SV][i]; |
320 |
> |
if ((j==2)|(j==3)) ap[i] += s->ss[SV][i]; |
321 |
|
else ap[i] -= s->ss[SV][i]; |
322 |
|
if (s->sflags & SDISTANT) { |
323 |
|
ap[i] *= 1. + ourview.vfore; |
324 |
|
ap[i] += ourview.vp[i]; |
325 |
|
} |
326 |
|
} |
327 |
< |
viewloc(ip, &ourview, ap); /* find image point */ |
328 |
< |
if (ip[2] <= 0.) |
327 |
> |
/* find image point */ |
328 |
> |
if (viewloc(ip, &ourview, ap) <= 0) |
329 |
|
return(0); /* in front of view */ |
330 |
|
sp[j][0] = ip[0]; sp[j][1] = ip[1]; |
331 |
|
} |
348 |
|
if (cubeord[dir][j] & 4) ap[i] += s->ss[SW][i]; |
349 |
|
else ap[i] -= s->ss[SW][i]; |
350 |
|
} |
351 |
< |
viewloc(ip, &ourview, ap); /* find image point */ |
352 |
< |
if (ip[2] <= 0.) |
351 |
> |
/* find image point */ |
352 |
> |
if (viewloc(ip, &ourview, ap) <= 0) |
353 |
|
return(0); /* in front of view */ |
354 |
|
pt[j][0] = ip[0]; pt[j][1] = ip[1]; |
355 |
|
} |
358 |
|
|
359 |
|
|
360 |
|
/* initialize by finding sources smaller than rad */ |
361 |
< |
init_drawsources(rad) |
362 |
< |
int rad; /* source sample size */ |
361 |
> |
void |
362 |
> |
init_drawsources( |
363 |
> |
int rad /* source sample size */ |
364 |
> |
) |
365 |
|
{ |
366 |
|
RREAL spoly[MAXVERT][2]; |
367 |
|
int nsv; |
368 |
< |
register SPLIST *sp; |
369 |
< |
register int i; |
368 |
> |
SPLIST *sp; |
369 |
> |
int i; |
370 |
|
/* free old source list if one */ |
371 |
|
for (sp = sphead; sp != NULL; sp = sphead) { |
372 |
|
sphead = sp->next; |
374 |
|
} |
375 |
|
/* loop through all sources */ |
376 |
|
for (i = nsources; i--; ) { |
377 |
+ |
/* skip illum's */ |
378 |
+ |
if (findmaterial(source[i].so)->otype == MAT_ILLUM) |
379 |
+ |
continue; |
380 |
|
/* compute image polygon for source */ |
381 |
|
if (!(nsv = sourcepoly(i, spoly))) |
382 |
|
continue; |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
< |
void /* add sources smaller than rad to computed subimage */ |
396 |
< |
drawsources(pic, zbf, x0, xsiz, y0, ysiz) |
397 |
< |
COLOR *pic[]; /* subimage pixel value array */ |
398 |
< |
float *zbf[]; /* subimage distance array (opt.) */ |
399 |
< |
int x0, xsiz, y0, ysiz; /* origin and size of subimage */ |
395 |
> |
void /* add sources smaller than rad to computed subimage */ |
396 |
> |
drawsources( |
397 |
> |
COLOR *pic[], /* subimage pixel value array */ |
398 |
> |
float *zbf[], /* subimage distance array (opt.) */ |
399 |
> |
int x0, /* origin and size of subimage */ |
400 |
> |
int xsiz, |
401 |
> |
int y0, |
402 |
> |
int ysiz |
403 |
> |
) |
404 |
|
{ |
405 |
|
RREAL spoly[MAXVERT][2], ppoly[MAXVERT][2]; |
406 |
|
int nsv, npv; |
408 |
|
RREAL cxy[2]; |
409 |
|
double w; |
410 |
|
RAY sr; |
411 |
< |
register SPLIST *sp; |
412 |
< |
register int i; |
411 |
> |
SPLIST *sp; |
412 |
> |
int i; |
413 |
|
/* check each source in our list */ |
414 |
|
for (sp = sphead; sp != NULL; sp = sp->next) { |
415 |
|
/* clip source poly to subimage */ |
448 |
|
if (source[sp->sn].sflags & SSPOT && |
449 |
|
spotout(&sr, source[sp->sn].sl.s)) |
450 |
|
continue; /* outside spot */ |
451 |
< |
rayorigin(&sr, NULL, SHADOW, 1.0); |
451 |
> |
rayorigin(&sr, SHADOW, NULL, NULL); |
452 |
|
sr.rsrc = sp->sn; |
453 |
|
rayvalue(&sr); /* compute value */ |
454 |
|
if (bright(sr.rcol) <= FTINY) |
455 |
|
continue; /* missed/blocked */ |
456 |
|
/* modify pixel */ |
457 |
+ |
w = poly_area(ppoly, npv) * hres * vres; |
458 |
|
if (zbf[y-y0] != NULL && |
459 |
< |
sr.rt < 0.999*zbf[y-y0][x-x0]) |
459 |
> |
sr.rt < 0.99*zbf[y-y0][x-x0]) { |
460 |
|
zbf[y-y0][x-x0] = sr.rt; |
461 |
< |
else if (!bigdiff(sr.rcol, pic[y-y0][x-x0], |
462 |
< |
0.001)) /* source sample */ |
463 |
< |
setcolor(pic[y-y0][x-x0], 0., 0., 0.); |
464 |
< |
w = poly_area(ppoly, npv) * hres * vres; |
461 |
> |
} else if (!bigdiff(sr.rcol, pic[y-y0][x-x0], |
462 |
> |
0.01)) { /* source sample */ |
463 |
> |
scalecolor(pic[y-y0][x-x0], w); |
464 |
> |
continue; |
465 |
> |
} |
466 |
|
scalecolor(sr.rcol, w); |
467 |
|
scalecolor(pic[y-y0][x-x0], 1.-w); |
468 |
|
addcolor(pic[y-y0][x-x0], sr.rcol); |