9 |
|
|
10 |
|
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "ray.h" |
12 |
> |
#include <string.h> |
13 |
|
|
14 |
+ |
#include "ray.h" |
15 |
|
#include "rpaint.h" |
15 |
– |
|
16 |
|
#include "random.h" |
17 |
|
|
18 |
|
#ifndef WFLUSH |
19 |
< |
#ifdef SPEED |
20 |
< |
#define WFLUSH (5*SPEED) |
21 |
< |
#else |
22 |
< |
#define WFLUSH 100 /* flush after this many rays */ |
19 |
> |
#define WFLUSH 64 /* flush after this many rays */ |
20 |
|
#endif |
24 |
– |
#endif |
21 |
|
|
22 |
|
#ifdef SMLFLT |
23 |
|
#define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3) |
27 |
|
|
28 |
|
|
29 |
|
int |
30 |
< |
getrect(s, r) /* get a box */ |
31 |
< |
char *s; |
32 |
< |
register RECT *r; |
30 |
> |
getrect( /* get a box */ |
31 |
> |
char *s, |
32 |
> |
RECT *r |
33 |
> |
) |
34 |
|
{ |
35 |
|
int x0, y0, x1, y1; |
36 |
|
|
75 |
|
|
76 |
|
|
77 |
|
int |
78 |
< |
getinterest(s, direc, vec, mp) /* get area of interest */ |
79 |
< |
char *s; |
80 |
< |
int direc; |
81 |
< |
FVECT vec; |
82 |
< |
double *mp; |
78 |
> |
getinterest( /* get area of interest */ |
79 |
> |
char *s, |
80 |
> |
int direc, |
81 |
> |
FVECT vec, |
82 |
> |
double *mp |
83 |
> |
) |
84 |
|
{ |
85 |
|
int x, y; |
86 |
|
RAY thisray; |
87 |
< |
register int i; |
87 |
> |
int i; |
88 |
|
|
89 |
|
if (sscanf(s, "%lf", mp) != 1) |
90 |
|
*mp = 1.0; |
106 |
|
return(-1); |
107 |
|
} |
108 |
|
if (!direc || ourview.type == VT_PAR) { |
109 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
109 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
110 |
|
if (!localhit(&thisray, &thescene)) { |
111 |
|
error(COMMAND, "not a local object"); |
112 |
|
return(-1); |
120 |
|
VCOPY(vec, thisray.rdir); |
121 |
|
else |
122 |
|
VCOPY(vec, thisray.rop); |
123 |
< |
} else if (direc) |
124 |
< |
for (i = 0; i < 3; i++) |
125 |
< |
vec[i] -= ourview.vp[i]; |
123 |
> |
} else if (direc) { |
124 |
> |
for (i = 0; i < 3; i++) |
125 |
> |
vec[i] -= ourview.vp[i]; |
126 |
> |
if (normalize(vec) == 0.0) { |
127 |
> |
error(COMMAND, "point at view origin"); |
128 |
> |
return(-1); |
129 |
> |
} |
130 |
> |
} |
131 |
|
return(0); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
float * /* keep consistent with COLOR typedef */ |
136 |
< |
greyof(col) /* convert color to greyscale */ |
137 |
< |
register COLOR col; |
136 |
> |
greyof( /* convert color to greyscale */ |
137 |
> |
COLOR col |
138 |
> |
) |
139 |
|
{ |
140 |
|
static COLOR gcol; |
141 |
|
double b; |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
< |
void |
150 |
< |
paint(p, xmin, ymin, xmax, ymax) /* compute and paint a rectangle */ |
151 |
< |
register PNODE *p; |
152 |
< |
int xmin, ymin, xmax, ymax; |
149 |
> |
int |
150 |
> |
paint( /* compute and paint a rectangle */ |
151 |
> |
PNODE *p |
152 |
> |
) |
153 |
|
{ |
154 |
+ |
extern int ray_pnprocs; |
155 |
|
static unsigned long lastflush = 0; |
156 |
|
static RAY thisray; |
157 |
|
double h, v; |
158 |
|
|
159 |
< |
if (xmax - xmin <= 0 || ymax - ymin <= 0) { /* empty */ |
160 |
< |
p->x = xmin; |
161 |
< |
p->y = ymin; |
159 |
> |
if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0) { /* empty */ |
160 |
> |
p->x = p->xmin; |
161 |
> |
p->y = p->ymin; |
162 |
|
setcolor(p->v, 0.0, 0.0, 0.0); |
163 |
< |
return; |
163 |
> |
return(0); |
164 |
|
} |
165 |
|
/* jitter ray direction */ |
166 |
< |
h = xmin + (xmax-xmin)*frandom(); |
167 |
< |
v = ymin + (ymax-ymin)*frandom(); |
166 |
> |
p->x = h = p->xmin + (p->xmax-p->xmin)*frandom(); |
167 |
> |
p->y = v = p->ymin + (p->ymax-p->ymin)*frandom(); |
168 |
|
|
169 |
|
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview, |
170 |
|
h/hresolu, v/vresolu)) < -FTINY) { |
171 |
|
setcolor(thisray.rcol, 0.0, 0.0, 0.0); |
172 |
|
} else { |
173 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
174 |
< |
samplendx++; |
175 |
< |
rayvalue(&thisray); |
173 |
> |
int rval; |
174 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
175 |
> |
thisray.rno = (unsigned long)p; |
176 |
> |
rval = ray_pqueue(&thisray); |
177 |
> |
if (!rval) |
178 |
> |
return(0); |
179 |
> |
if (rval < 0) |
180 |
> |
return(-1); |
181 |
> |
p = (PNODE *)thisray.rno; |
182 |
|
} |
183 |
|
|
173 |
– |
p->x = h; |
174 |
– |
p->y = v; |
184 |
|
copycolor(p->v, thisray.rcol); |
185 |
|
scalecolor(p->v, exposure); |
186 |
|
|
187 |
< |
(*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax); |
187 |
> |
(*dev->paintr)(greyscale?greyof(p->v):p->v, p->xmin, p->ymin, p->xmax, p->ymax); |
188 |
|
|
189 |
< |
if (dev->flush != NULL && nrays - lastflush >= WFLUSH) { |
190 |
< |
lastflush = nrays; |
189 |
> |
if (dev->flush != NULL && raynum - lastflush >= WFLUSH*ray_pnprocs) { |
190 |
> |
lastflush = raynum; |
191 |
|
(*dev->flush)(); |
192 |
|
} |
193 |
+ |
return(1); |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
+ |
int |
198 |
+ |
waitrays(void) /* finish up pending rays */ |
199 |
+ |
{ |
200 |
+ |
int nwaited = 0; |
201 |
+ |
int rval; |
202 |
+ |
RAY raydone; |
203 |
+ |
|
204 |
+ |
while ((rval = ray_presult(&raydone, 0)) > 0) { |
205 |
+ |
PNODE *p = (PNODE *)raydone.rno; |
206 |
+ |
copycolor(p->v, raydone.rcol); |
207 |
+ |
scalecolor(p->v, exposure); |
208 |
+ |
(*dev->paintr)(greyscale?greyof(p->v):p->v, |
209 |
+ |
p->xmin, p->ymin, p->xmax, p->ymax); |
210 |
+ |
nwaited++; |
211 |
+ |
} |
212 |
+ |
if (rval < 0) |
213 |
+ |
return(-1); |
214 |
+ |
return(nwaited); |
215 |
+ |
} |
216 |
+ |
|
217 |
+ |
|
218 |
|
void |
219 |
< |
newimage() /* start a new image */ |
219 |
> |
newimage( /* start a new image */ |
220 |
> |
char *s |
221 |
> |
) |
222 |
|
{ |
223 |
+ |
int newnp; |
224 |
+ |
/* change in nproc? */ |
225 |
+ |
if (s != NULL && sscanf(s, "%d", &newnp) == 1 && newnp > 0) { |
226 |
+ |
if (!newparam) { |
227 |
+ |
if (newnp < nproc) |
228 |
+ |
ray_pclose(nproc - newnp); |
229 |
+ |
else |
230 |
+ |
ray_popen(newnp - nproc); |
231 |
+ |
} |
232 |
+ |
nproc = newnp; |
233 |
+ |
} |
234 |
|
/* free old image */ |
235 |
|
freepkids(&ptrunk); |
192 |
– |
/* save reserve memory */ |
193 |
– |
fillreserves(); |
236 |
|
/* compute resolution */ |
237 |
|
hresolu = dev->xsiz; |
238 |
|
vresolu = dev->ysiz; |
239 |
|
normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu); |
240 |
< |
pframe.l = pframe.d = 0; |
241 |
< |
pframe.r = hresolu; pframe.u = vresolu; |
240 |
> |
ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0; |
241 |
> |
ptrunk.xmax = pframe.r = hresolu; |
242 |
> |
ptrunk.ymax = pframe.u = vresolu; |
243 |
|
pdepth = 0; |
244 |
|
/* clear device */ |
245 |
|
(*dev->clear)(hresolu, vresolu); |
246 |
+ |
|
247 |
+ |
if (newparam) { /* (re)start rendering procs */ |
248 |
+ |
ray_pclose(0); |
249 |
+ |
ray_popen(nproc); |
250 |
+ |
newparam = 0; |
251 |
+ |
} |
252 |
|
/* get first value */ |
253 |
< |
paint(&ptrunk, 0, 0, hresolu, vresolu); |
253 |
> |
paint(&ptrunk); |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
void |
258 |
< |
redraw() /* redraw the image */ |
258 |
> |
redraw(void) /* redraw the image */ |
259 |
|
{ |
260 |
|
(*dev->clear)(hresolu, vresolu); |
261 |
|
(*dev->comout)("redrawing...\n"); |
265 |
|
|
266 |
|
|
267 |
|
void |
268 |
< |
repaint(xmin, ymin, xmax, ymax) /* repaint a region */ |
269 |
< |
int xmin, ymin, xmax, ymax; |
268 |
> |
repaint( /* repaint a region */ |
269 |
> |
int xmin, |
270 |
> |
int ymin, |
271 |
> |
int xmax, |
272 |
> |
int ymax |
273 |
> |
) |
274 |
|
{ |
275 |
|
RECT reg; |
276 |
|
|
277 |
|
reg.l = xmin; reg.r = xmax; |
278 |
|
reg.d = ymin; reg.u = ymax; |
279 |
|
|
280 |
< |
paintrect(&ptrunk, 0, 0, hresolu, vresolu, ®); |
280 |
> |
paintrect(&ptrunk, ®); |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
void |
285 |
< |
paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */ |
286 |
< |
register PNODE *p; |
287 |
< |
int xmin, ymin, xmax, ymax; |
288 |
< |
register RECT *r; |
285 |
> |
paintrect( /* paint picture rectangle */ |
286 |
> |
PNODE *p, |
287 |
> |
RECT *r |
288 |
> |
) |
289 |
|
{ |
290 |
|
int mx, my; |
291 |
|
|
292 |
< |
if (xmax - xmin <= 0 || ymax - ymin <= 0) |
292 |
> |
if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0) |
293 |
|
return; |
294 |
|
|
295 |
|
if (p->kid == NULL) { |
296 |
|
(*dev->paintr)(greyscale?greyof(p->v):p->v, |
297 |
< |
xmin, ymin, xmax, ymax); /* do this */ |
297 |
> |
p->xmin, p->ymin, p->xmax, p->ymax); /* do this */ |
298 |
|
return; |
299 |
|
} |
300 |
< |
mx = (xmin + xmax) >> 1; /* do kids */ |
301 |
< |
my = (ymin + ymax) >> 1; |
300 |
> |
mx = (p->xmin + p->xmax) >> 1; /* do kids */ |
301 |
> |
my = (p->ymin + p->ymax) >> 1; |
302 |
|
if (mx > r->l) { |
303 |
|
if (my > r->d) |
304 |
< |
paintrect(p->kid+DL, xmin, ymin, mx, my, r); |
304 |
> |
paintrect(p->kid+DL, r); |
305 |
|
if (my < r->u) |
306 |
< |
paintrect(p->kid+UL, xmin, my, mx, ymax, r); |
306 |
> |
paintrect(p->kid+UL, r); |
307 |
|
} |
308 |
|
if (mx < r->r) { |
309 |
|
if (my > r->d) |
310 |
< |
paintrect(p->kid+DR, mx, ymin, xmax, my, r); |
310 |
> |
paintrect(p->kid+DR, r); |
311 |
|
if (my < r->u) |
312 |
< |
paintrect(p->kid+UR, mx, my, xmax, ymax, r); |
312 |
> |
paintrect(p->kid+UR, r); |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
|
PNODE * |
318 |
< |
findrect(x, y, p, r, pd) /* find a rectangle */ |
319 |
< |
int x, y; |
320 |
< |
register PNODE *p; |
321 |
< |
register RECT *r; |
322 |
< |
int pd; |
318 |
> |
findrect( /* find a rectangle */ |
319 |
> |
int x, |
320 |
> |
int y, |
321 |
> |
PNODE *p, |
322 |
> |
int pd |
323 |
> |
) |
324 |
|
{ |
325 |
|
int mx, my; |
326 |
|
|
327 |
|
while (p->kid != NULL && pd--) { |
328 |
|
|
329 |
< |
mx = (r->l + r->r) >> 1; |
330 |
< |
my = (r->d + r->u) >> 1; |
329 |
> |
mx = (p->xmin + p->xmax) >> 1; |
330 |
> |
my = (p->ymin + p->ymax) >> 1; |
331 |
|
|
332 |
|
if (x < mx) { |
279 |
– |
r->r = mx; |
333 |
|
if (y < my) { |
281 |
– |
r->u = my; |
334 |
|
p = p->kid+DL; |
335 |
|
} else { |
284 |
– |
r->d = my; |
336 |
|
p = p->kid+UL; |
337 |
|
} |
338 |
|
} else { |
288 |
– |
r->l = mx; |
339 |
|
if (y < my) { |
290 |
– |
r->u = my; |
340 |
|
p = p->kid+DR; |
341 |
|
} else { |
293 |
– |
r->d = my; |
342 |
|
p = p->kid+UR; |
343 |
|
} |
344 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
void |
351 |
< |
scalepict(p, sf) /* scale picture values */ |
352 |
< |
register PNODE *p; |
353 |
< |
double sf; |
351 |
> |
scalepict( /* scale picture values */ |
352 |
> |
PNODE *p, |
353 |
> |
double sf |
354 |
> |
) |
355 |
|
{ |
356 |
|
scalecolor(p->v, sf); /* do this node */ |
357 |
|
|
366 |
|
|
367 |
|
|
368 |
|
void |
369 |
< |
getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */ |
370 |
< |
int yoff; |
371 |
< |
register COLR *scan; |
372 |
< |
register PNODE *p; |
373 |
< |
int xsiz, ysiz; |
369 |
> |
getpictcolrs( /* get scanline from picture */ |
370 |
> |
int yoff, |
371 |
> |
COLR *scan, |
372 |
> |
PNODE *p, |
373 |
> |
int xsiz, |
374 |
> |
int ysiz |
375 |
> |
) |
376 |
|
{ |
377 |
< |
register int mx; |
377 |
> |
int mx; |
378 |
|
int my; |
379 |
|
|
380 |
|
if (p->kid == NULL) { /* do this node */ |
399 |
|
|
400 |
|
|
401 |
|
void |
402 |
< |
freepkids(p) /* free pnode's children */ |
403 |
< |
register PNODE *p; |
402 |
> |
freepkids( /* free pnode's children */ |
403 |
> |
PNODE *p |
404 |
> |
) |
405 |
|
{ |
406 |
|
if (p->kid == NULL) |
407 |
|
return; |
415 |
|
|
416 |
|
|
417 |
|
void |
418 |
< |
newview(vp) /* change viewing parameters */ |
419 |
< |
register VIEW *vp; |
418 |
> |
newview( /* change viewing parameters */ |
419 |
> |
VIEW *vp |
420 |
> |
) |
421 |
|
{ |
422 |
|
char *err; |
423 |
|
|
424 |
|
if ((err = setview(vp)) != NULL) { |
425 |
|
sprintf(errmsg, "view not set - %s", err); |
426 |
|
error(COMMAND, errmsg); |
427 |
< |
} else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) { |
428 |
< |
copystruct(&oldview, &ourview); |
429 |
< |
copystruct(&ourview, vp); |
430 |
< |
newimage(); |
427 |
> |
} else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) { |
428 |
> |
oldview = ourview; |
429 |
> |
ourview = *vp; |
430 |
> |
newimage(NULL); |
431 |
|
} |
432 |
|
} |
433 |
|
|
434 |
|
|
435 |
|
void |
436 |
< |
moveview(angle, elev, mag, vc) /* move viewpoint */ |
437 |
< |
double angle, elev, mag; |
438 |
< |
FVECT vc; |
436 |
> |
moveview( /* move viewpoint */ |
437 |
> |
double angle, |
438 |
> |
double elev, |
439 |
> |
double mag, |
440 |
> |
FVECT vc |
441 |
> |
) |
442 |
|
{ |
443 |
|
double d; |
444 |
|
FVECT v1; |
445 |
< |
VIEW nv; |
446 |
< |
register int i; |
445 |
> |
VIEW nv = ourview; |
446 |
> |
int i; |
447 |
|
|
392 |
– |
VCOPY(nv.vup, ourview.vup); |
393 |
– |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
448 |
|
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
449 |
|
if (elev != 0.0) { |
450 |
|
fcross(v1, ourview.vup, nv.vdir); |
451 |
|
normalize(v1); |
452 |
|
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
453 |
|
} |
454 |
< |
if ((nv.type = ourview.type) == VT_PAR) { |
455 |
< |
nv.horiz = ourview.horiz / mag; |
456 |
< |
nv.vert = ourview.vert / mag; |
454 |
> |
if (nv.type == VT_PAR) { |
455 |
> |
nv.horiz /= mag; |
456 |
> |
nv.vert /= mag; |
457 |
|
d = 0.0; /* don't move closer */ |
458 |
|
for (i = 0; i < 3; i++) |
459 |
|
d += (vc[i] - ourview.vp[i])*ourview.vdir[i]; |
406 |
– |
nv.vfore = ourview.vfore; |
407 |
– |
nv.vaft = ourview.vaft; |
460 |
|
} else { |
409 |
– |
nv.horiz = ourview.horiz; |
410 |
– |
nv.vert = ourview.vert; |
461 |
|
d = sqrt(dist2(ourview.vp, vc)) / mag; |
462 |
< |
if ((nv.vfore = ourview.vfore) > FTINY) { |
462 |
> |
if (nv.vfore > FTINY) { |
463 |
|
nv.vfore += d - d*mag; |
464 |
|
if (nv.vfore < 0.0) nv.vfore = 0.0; |
465 |
|
} |
466 |
< |
if ((nv.vaft = ourview.vaft) > FTINY) { |
466 |
> |
if (nv.vaft > FTINY) { |
467 |
|
nv.vaft += d - d*mag; |
468 |
|
if (nv.vaft <= nv.vfore) nv.vaft = 0.0; |
469 |
|
} |
470 |
+ |
nv.vdist /= mag; |
471 |
|
} |
472 |
|
for (i = 0; i < 3; i++) |
473 |
|
nv.vp[i] = vc[i] - d*nv.vdir[i]; |
476 |
|
|
477 |
|
|
478 |
|
void |
479 |
< |
pcopy(p1, p2) /* copy paint node p1 into p2 */ |
480 |
< |
register PNODE *p1, *p2; |
479 |
> |
pcopy( /* copy paint node p1 into p2 */ |
480 |
> |
PNODE *p1, |
481 |
> |
PNODE *p2 |
482 |
> |
) |
483 |
|
{ |
484 |
|
copycolor(p2->v, p1->v); |
485 |
|
p2->x = p1->x; |
488 |
|
|
489 |
|
|
490 |
|
void |
491 |
< |
zoomview(vp, zf) /* zoom in or out */ |
492 |
< |
register VIEW *vp; |
493 |
< |
double zf; |
491 |
> |
zoomview( /* zoom in or out */ |
492 |
> |
VIEW *vp, |
493 |
> |
double zf |
494 |
> |
) |
495 |
|
{ |
496 |
|
switch (vp->type) { |
497 |
|
case VT_PAR: /* parallel view */ |
505 |
|
vp->vert /= zf; |
506 |
|
if (vp->vert > 360.) |
507 |
|
vp->vert = 360.; |
508 |
+ |
return; |
509 |
+ |
case VT_PLS: /* planisphere fisheye */ |
510 |
+ |
vp->horiz = sin((PI/180./2.)*vp->horiz) / |
511 |
+ |
(1.0 + cos((PI/180./2.)*vp->horiz)) / zf; |
512 |
+ |
vp->horiz *= vp->horiz; |
513 |
+ |
vp->horiz = (2.*180./PI)*acos((1. - vp->horiz) / |
514 |
+ |
(1. + vp->horiz)); |
515 |
+ |
vp->vert = sin((PI/180./2.)*vp->vert) / |
516 |
+ |
(1.0 + cos((PI/180./2.)*vp->vert)) / zf; |
517 |
+ |
vp->vert *= vp->vert; |
518 |
+ |
vp->vert = (2.*180./PI)*acos((1. - vp->vert) / |
519 |
+ |
(1. + vp->vert)); |
520 |
|
return; |
521 |
|
case VT_CYL: /* cylindrical panorama */ |
522 |
|
vp->horiz /= zf; |