1 |
– |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* rpict.c - routines and variables for picture generation. |
9 |
– |
* |
10 |
– |
* 8/14/85 |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "ray.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
|
#include <sys/types.h> |
11 |
|
|
12 |
< |
#ifndef NIX |
13 |
< |
#ifdef BSD |
14 |
< |
#include <sys/time.h> |
15 |
< |
#include <sys/resource.h> |
12 |
> |
#include "platform.h" |
13 |
> |
#ifdef NON_POSIX |
14 |
> |
#ifdef MINGW |
15 |
> |
#include <sys/time.h> |
16 |
> |
#endif |
17 |
|
#else |
18 |
< |
#include <sys/times.h> |
19 |
< |
#include <sys/utsname.h> |
20 |
< |
#include <unistd.h> |
18 |
> |
#ifdef BSD |
19 |
> |
#include <sys/time.h> |
20 |
> |
#include <sys/resource.h> |
21 |
> |
#else |
22 |
> |
#include <sys/times.h> |
23 |
> |
#include <unistd.h> |
24 |
> |
#endif |
25 |
|
#endif |
26 |
– |
#endif |
26 |
|
|
27 |
< |
extern time_t time(); |
29 |
< |
|
27 |
> |
#include <time.h> |
28 |
|
#include <signal.h> |
29 |
|
|
30 |
+ |
#include "ray.h" |
31 |
+ |
#include "paths.h" |
32 |
+ |
#include "ambient.h" |
33 |
|
#include "view.h" |
33 |
– |
|
34 |
– |
#include "resolu.h" |
35 |
– |
|
34 |
|
#include "random.h" |
37 |
– |
|
35 |
|
#include "paths.h" |
36 |
+ |
#include "hilbert.h" |
37 |
+ |
#include "pmapbias.h" |
38 |
+ |
#include "pmapdiag.h" |
39 |
|
|
40 |
|
#define RFTEMPLATE "rfXXXXXX" |
41 |
|
|
42 |
|
#ifndef SIGCONT |
43 |
+ |
#ifdef SIGIO /* XXX can we live without this? */ |
44 |
|
#define SIGCONT SIGIO |
45 |
|
#endif |
46 |
+ |
#endif |
47 |
|
|
48 |
+ |
CUBE thescene; /* our scene */ |
49 |
+ |
OBJECT nsceneobjs; /* number of objects in our scene */ |
50 |
+ |
|
51 |
|
int dimlist[MAXDIM]; /* sampling dimensions */ |
52 |
|
int ndims = 0; /* number of sampling dimensions */ |
53 |
|
int samplendx; /* sample index number */ |
54 |
|
|
55 |
+ |
void (*addobjnotify[])() = {ambnotify, NULL}; |
56 |
+ |
|
57 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
58 |
|
int hresolu = 512; /* horizontal resolution */ |
59 |
|
int vresolu = 512; /* vertical resolution */ |
63 |
|
double maxdiff = .05; /* max. difference for interpolation */ |
64 |
|
double dstrpix = 0.67; /* square pixel distribution */ |
65 |
|
|
66 |
+ |
double mblur = 0.; /* motion blur parameter */ |
67 |
+ |
|
68 |
+ |
double dblur = 0.; /* depth-of-field blur parameter */ |
69 |
+ |
|
70 |
+ |
void (*trace)() = NULL; /* trace call */ |
71 |
+ |
|
72 |
+ |
int do_irrad = 0; /* compute irradiance? */ |
73 |
+ |
|
74 |
+ |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
75 |
+ |
|
76 |
|
double dstrsrc = 0.0; /* square source distribution */ |
77 |
|
double shadthresh = .05; /* shadow threshold */ |
78 |
|
double shadcert = .5; /* shadow certainty */ |
82 |
|
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
83 |
|
|
84 |
|
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
85 |
< |
double salbedo = 0.; /* global scattering albedo */ |
85 |
> |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
86 |
|
double seccg = 0.; /* global scattering eccentricity */ |
87 |
|
double ssampdist = 0.; /* scatter sampling distance */ |
88 |
|
|
91 |
|
|
92 |
|
int backvis = 1; /* back face visibility */ |
93 |
|
|
94 |
< |
int maxdepth = 6; /* maximum recursion depth */ |
95 |
< |
double minweight = 5e-3; /* minimum ray weight */ |
94 |
> |
int maxdepth = 7; /* maximum recursion depth */ |
95 |
> |
double minweight = 1e-3; /* minimum ray weight */ |
96 |
|
|
97 |
+ |
char *ambfile = NULL; /* ambient file name */ |
98 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
99 |
+ |
int ambvwt = 0; /* initial weight for ambient value */ |
100 |
|
double ambacc = 0.2; /* ambient accuracy */ |
101 |
< |
int ambres = 32; /* ambient resolution */ |
102 |
< |
int ambdiv = 128; /* ambient divisions */ |
103 |
< |
int ambssamp = 0; /* ambient super-samples */ |
101 |
> |
int ambres = 64; /* ambient resolution */ |
102 |
> |
int ambdiv = 512; /* ambient divisions */ |
103 |
> |
int ambssamp = 128; /* ambient super-samples */ |
104 |
|
int ambounce = 0; /* ambient bounces */ |
105 |
< |
char *amblist[128]; /* ambient include/exclude list */ |
105 |
> |
char *amblist[AMBLLEN]; /* ambient include/exclude list */ |
106 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
107 |
|
|
89 |
– |
#ifdef MSDOS |
90 |
– |
int ralrm = 60; /* seconds between reports */ |
91 |
– |
#else |
108 |
|
int ralrm = 0; /* seconds between reports */ |
93 |
– |
#endif |
109 |
|
|
110 |
|
double pctdone = 0.0; /* percentage done */ |
96 |
– |
|
111 |
|
time_t tlastrept = 0L; /* time at last report */ |
112 |
+ |
time_t tstart; /* starting time */ |
113 |
|
|
99 |
– |
extern time_t tstart; /* starting time */ |
100 |
– |
|
101 |
– |
extern unsigned long nrays; /* number of rays traced */ |
102 |
– |
|
114 |
|
#define MAXDIV 16 /* maximum sample size */ |
115 |
|
|
116 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
117 |
|
|
118 |
< |
static int hres, vres; /* resolution for this frame */ |
118 |
> |
int hres, vres; /* resolution for this frame */ |
119 |
|
|
120 |
< |
extern char *mktemp(); |
120 |
> |
static VIEW lastview; /* the previous view input */ |
121 |
|
|
122 |
< |
double pixvalue(); |
122 |
> |
static void report(int); |
123 |
> |
static int nextview(FILE *fp); |
124 |
> |
static void render(char *zfile, char *oldfile); |
125 |
> |
static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres, |
126 |
> |
int y, int xstep); |
127 |
> |
static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres, |
128 |
> |
int y, int ysize); |
129 |
> |
static int fillsample(COLOR *colline, float *zline, int x, int y, |
130 |
> |
int xlen, int ylen, int b); |
131 |
> |
static double pixvalue(COLOR col, int x, int y); |
132 |
> |
static int salvage(char *oldfile); |
133 |
> |
static int pixnumber(int x, int y, int xres, int yres); |
134 |
|
|
113 |
– |
#ifdef NIX |
114 |
– |
#define file_exists(f) (access(f,F_OK)==0) |
115 |
– |
#else |
116 |
– |
#include <sys/types.h> |
117 |
– |
#include <sys/stat.h> |
118 |
– |
int |
119 |
– |
file_exists(fname) /* ordinary file exists? */ |
120 |
– |
char *fname; |
121 |
– |
{ |
122 |
– |
struct stat sbuf; |
123 |
– |
if (stat(fname, &sbuf) < 0) return(0); |
124 |
– |
return((sbuf.st_mode & S_IFREG) != 0); |
125 |
– |
} |
126 |
– |
#endif |
135 |
|
|
136 |
|
|
137 |
< |
quit(code) /* quit program */ |
138 |
< |
int code; |
137 |
> |
void |
138 |
> |
quit(int code) /* quit program */ |
139 |
|
{ |
140 |
|
if (code) /* report status */ |
141 |
< |
report(); |
141 |
> |
report(0); |
142 |
> |
#ifndef NON_POSIX |
143 |
|
headclean(); /* delete header file */ |
144 |
|
pfclean(); /* clean up persist files */ |
145 |
+ |
#endif |
146 |
|
exit(code); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
< |
#ifndef NIX |
151 |
< |
report() /* report progress */ |
150 |
> |
#ifndef NON_POSIX |
151 |
> |
static void |
152 |
> |
report(int dummy) /* report progress */ |
153 |
|
{ |
154 |
< |
double u, s; |
154 |
> |
char bcStat [128]; |
155 |
> |
double u, s; |
156 |
|
#ifdef BSD |
157 |
< |
char hostname[257]; |
146 |
< |
struct rusage rubuf; |
157 |
> |
struct rusage rubuf; |
158 |
|
#else |
159 |
< |
struct tms tbuf; |
160 |
< |
struct utsname nambuf; |
150 |
< |
double period; |
151 |
< |
#define hostname nambuf.nodename |
159 |
> |
double period = 1.0 / 60.0; |
160 |
> |
struct tms tbuf; |
161 |
|
#endif |
162 |
|
|
163 |
|
tlastrept = time((time_t *)NULL); |
164 |
|
#ifdef BSD |
165 |
|
getrusage(RUSAGE_SELF, &rubuf); |
166 |
< |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
167 |
< |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
166 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; |
167 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; |
168 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
169 |
< |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
170 |
< |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
162 |
< |
gethostname(hostname, sizeof(hostname)); |
169 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; |
170 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; |
171 |
|
#else |
172 |
|
times(&tbuf); |
173 |
|
#ifdef _SC_CLK_TCK |
174 |
|
period = 1.0 / sysconf(_SC_CLK_TCK); |
167 |
– |
#else |
168 |
– |
period = 1.0 / 60.0; |
175 |
|
#endif |
176 |
|
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
177 |
|
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
172 |
– |
uname(&nambuf); |
178 |
|
#endif |
179 |
|
|
180 |
+ |
/* PMAP: Get photon map bias compensation statistics */ |
181 |
+ |
pmapBiasCompReport(bcStat); |
182 |
+ |
|
183 |
|
sprintf(errmsg, |
184 |
< |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
185 |
< |
nrays, pctdone, u/3600., s/3600., |
186 |
< |
(tlastrept-tstart)/3600., hostname); |
184 |
> |
"%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n", |
185 |
> |
nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.), |
186 |
> |
(tlastrept-tstart)*(1./3600.), myhostname(), getpid()); |
187 |
|
eputs(errmsg); |
188 |
< |
#ifndef BSD |
188 |
> |
#ifdef SIGCONT |
189 |
|
signal(SIGCONT, report); |
182 |
– |
#undef hostname |
190 |
|
#endif |
191 |
|
} |
192 |
|
#else |
193 |
< |
report() /* report progress */ |
193 |
> |
static void |
194 |
> |
report(int dummy) /* report progress */ |
195 |
|
{ |
196 |
+ |
char bcStat [128]; |
197 |
+ |
|
198 |
|
tlastrept = time((time_t *)NULL); |
199 |
< |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
200 |
< |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
199 |
> |
|
200 |
> |
/* PMAP: Get photon map bias compensation statistics */ |
201 |
> |
pmapBiasCompReport(bcStat); |
202 |
> |
|
203 |
> |
sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n", |
204 |
> |
nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0); |
205 |
|
eputs(errmsg); |
206 |
|
} |
207 |
|
#endif |
208 |
|
|
209 |
|
|
210 |
< |
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
211 |
< |
int seq; |
212 |
< |
char *pout, *zout, *prvr; |
210 |
> |
void |
211 |
> |
rpict( /* generate image(s) */ |
212 |
> |
int seq, |
213 |
> |
char *pout, |
214 |
> |
char *zout, |
215 |
> |
char *prvr |
216 |
> |
) |
217 |
|
/* |
218 |
|
* If seq is greater than zero, then we will render a sequence of |
219 |
|
* images based on view parameter strings read from the standard input. |
226 |
|
* sequenced file naming. |
227 |
|
*/ |
228 |
|
{ |
211 |
– |
extern char *rindex(), *strncpy(), *strcat(), *strcpy(); |
229 |
|
char fbuf[128], fbuf2[128]; |
230 |
|
int npicts; |
231 |
< |
register char *cp; |
231 |
> |
char *cp; |
232 |
|
RESOLU rs; |
233 |
|
double pa; |
234 |
|
/* check sampling */ |
244 |
|
if (seq <= 0) |
245 |
|
seq = 0; |
246 |
|
else if (prvr != NULL && isint(prvr)) { |
247 |
< |
register int rn; /* skip to specified view */ |
247 |
> |
int rn; /* skip to specified view */ |
248 |
|
if ((rn = atoi(prvr)) < seq) |
249 |
|
error(USER, "recover frame less than start frame"); |
250 |
|
if (pout == NULL) |
252 |
|
for ( ; seq < rn; seq++) |
253 |
|
if (nextview(stdin) == EOF) |
254 |
|
error(USER, "unexpected EOF on view input"); |
255 |
+ |
setview(&ourview); |
256 |
|
prvr = fbuf; /* mark for renaming */ |
257 |
|
} |
258 |
< |
if (pout != NULL & prvr != NULL) { |
258 |
> |
if ((pout != NULL) & (prvr != NULL)) { |
259 |
|
sprintf(fbuf, pout, seq); |
260 |
|
if (!strcmp(prvr, fbuf)) { /* rename */ |
261 |
|
strcpy(fbuf2, fbuf); |
265 |
|
cp--; |
266 |
|
strcpy(cp, RFTEMPLATE); |
267 |
|
prvr = mktemp(fbuf2); |
268 |
< |
if (rename(fbuf, prvr) < 0) |
268 |
> |
if (rename(fbuf, prvr) < 0) { |
269 |
|
if (errno == ENOENT) { /* ghost file */ |
270 |
|
sprintf(errmsg, |
271 |
|
"new output file \"%s\"", |
278 |
|
fbuf, prvr); |
279 |
|
error(SYSTEM, errmsg); |
280 |
|
} |
281 |
+ |
} |
282 |
|
} |
283 |
|
} |
284 |
|
npicts = 0; /* render sequence */ |
287 |
|
break; |
288 |
|
pctdone = 0.0; |
289 |
|
if (pout != NULL) { |
290 |
+ |
int myfd; |
291 |
+ |
close(1); /* reassign stdout */ |
292 |
|
sprintf(fbuf, pout, seq); |
293 |
< |
if (file_exists(fbuf)) { |
294 |
< |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
293 |
> |
tryagain: |
294 |
> |
errno = 0; /* exclusive open */ |
295 |
> |
if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { |
296 |
> |
if ((errno != EEXIST) | (prvr != NULL) || |
297 |
> |
!strcmp(fbuf, pout)) { |
298 |
|
sprintf(errmsg, |
299 |
< |
"output file \"%s\" exists", |
299 |
> |
"cannot open output file \"%s\"", |
300 |
|
fbuf); |
301 |
< |
error(USER, errmsg); |
301 |
> |
error(SYSTEM, errmsg); |
302 |
|
} |
303 |
+ |
setview(&ourview); |
304 |
|
continue; /* don't clobber */ |
305 |
|
} |
306 |
< |
if (freopen(fbuf, "w", stdout) == NULL) { |
307 |
< |
sprintf(errmsg, |
308 |
< |
"cannot open output file \"%s\"", fbuf); |
284 |
< |
error(SYSTEM, errmsg); |
306 |
> |
if (myfd != 1) { |
307 |
> |
unlink(fbuf); |
308 |
> |
goto tryagain; /* leave it open */ |
309 |
|
} |
310 |
< |
#ifdef MSDOS |
287 |
< |
setmode(fileno(stdout), O_BINARY); |
288 |
< |
#endif |
310 |
> |
SET_FD_BINARY(1); |
311 |
|
dupheader(); |
312 |
|
} |
313 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
314 |
< |
if (prvr != NULL) |
315 |
< |
if (viewfile(prvr, &ourview, &rs) <= 0 |
294 |
< |
|| rs.or != PIXSTANDARD) { |
314 |
> |
if (prvr != NULL) { |
315 |
> |
if (viewfile(prvr, &ourview, &rs) <= 0) { |
316 |
|
sprintf(errmsg, |
317 |
|
"cannot recover view parameters from \"%s\"", prvr); |
318 |
|
error(WARNING, errmsg); |
321 |
|
hres = scanlen(&rs); |
322 |
|
vres = numscans(&rs); |
323 |
|
} |
324 |
+ |
} |
325 |
|
if ((cp = setview(&ourview)) != NULL) |
326 |
|
error(USER, cp); |
327 |
|
normaspect(viewaspect(&ourview), &pa, &hres, &vres); |
337 |
|
fputs(VIEWSTR, stdout); |
338 |
|
fprintview(&ourview, stdout); |
339 |
|
putchar('\n'); |
340 |
< |
if (pa < .99 || pa > 1.01) |
340 |
> |
if ((pa < .99) | (pa > 1.01)) |
341 |
|
fputaspect(pa, stdout); |
342 |
+ |
fputnow(stdout); |
343 |
+ |
fputprims(stdprims, stdout); |
344 |
|
fputformat(COLRFMT, stdout); |
345 |
|
putchar('\n'); |
346 |
|
if (zout != NULL) |
357 |
|
} |
358 |
|
|
359 |
|
|
360 |
< |
nextview(fp) /* get next view from fp */ |
361 |
< |
FILE *fp; |
360 |
> |
static int |
361 |
> |
nextview( /* get next view from fp */ |
362 |
> |
FILE *fp |
363 |
> |
) |
364 |
|
{ |
365 |
|
char linebuf[256]; |
366 |
|
|
367 |
+ |
lastview = ourview; |
368 |
|
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) |
369 |
|
if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) |
370 |
|
return(0); |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
< |
render(zfile, oldfile) /* render the scene */ |
376 |
< |
char *zfile, *oldfile; |
375 |
> |
static void |
376 |
> |
render( /* render the scene */ |
377 |
> |
char *zfile, |
378 |
> |
char *oldfile |
379 |
> |
) |
380 |
|
{ |
351 |
– |
extern long lseek(); |
381 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
382 |
|
float *zbar[MAXDIV+1]; /* z values */ |
383 |
|
char *sampdens; /* previous sample density */ |
387 |
|
int zfd; |
388 |
|
COLOR *colptr; |
389 |
|
float *zptr; |
390 |
< |
register int i; |
390 |
> |
int i; |
391 |
> |
/* check for empty image */ |
392 |
> |
if ((hres <= 0) | (vres <= 0)) { |
393 |
> |
error(WARNING, "empty output picture"); |
394 |
> |
fprtresolu(0, 0, stdout); |
395 |
> |
return; |
396 |
> |
} |
397 |
|
/* allocate scanlines */ |
398 |
|
for (i = 0; i <= psample; i++) { |
399 |
|
scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR)); |
404 |
|
ystep = (psample*99+70)/140; |
405 |
|
if (hstep > 2) { |
406 |
|
i = hres/hstep + 2; |
407 |
< |
if ((sampdens = malloc(i)) == NULL) |
407 |
> |
if ((sampdens = (char *)malloc(i)) == NULL) |
408 |
|
goto memerr; |
409 |
|
while (i--) |
410 |
|
sampdens[i] = hstep; |
416 |
|
sprintf(errmsg, "cannot open z-file \"%s\"", zfile); |
417 |
|
error(SYSTEM, errmsg); |
418 |
|
} |
419 |
< |
#ifdef MSDOS |
385 |
< |
setmode(zfd, O_BINARY); |
386 |
< |
#endif |
419 |
> |
SET_FD_BINARY(zfd); |
420 |
|
for (i = 0; i <= psample; i++) { |
421 |
|
zbar[i] = (float *)malloc(hres*sizeof(float)); |
422 |
|
if (zbar[i] == NULL) |
433 |
|
i = salvage(oldfile); |
434 |
|
if (i >= vres) |
435 |
|
goto alldone; |
436 |
< |
if (zfd != -1 && i > 0 && |
437 |
< |
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
436 |
> |
if ((zfd != -1) & (i > 0) && |
437 |
> |
lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0) |
438 |
|
error(SYSTEM, "z-file seek error in render"); |
439 |
|
pctdone = 100.0*i/vres; |
440 |
|
if (ralrm > 0) /* report init stats */ |
441 |
< |
report(); |
442 |
< |
#ifndef BSD |
441 |
> |
report(0); |
442 |
> |
#ifdef SIGCONT |
443 |
|
else |
411 |
– |
#endif |
444 |
|
signal(SIGCONT, report); |
445 |
< |
ypos = vres-1 - i; |
445 |
> |
#endif |
446 |
> |
ypos = vres-1 - i; /* initialize sampling */ |
447 |
> |
if (directvis) |
448 |
> |
init_drawsources(psample); |
449 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
450 |
|
/* compute scanlines */ |
451 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
465 |
|
hres, ypos, hstep); |
466 |
|
/* fill bar */ |
467 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
468 |
+ |
if (directvis) /* add bitty sources */ |
469 |
+ |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
470 |
|
/* write it out */ |
471 |
< |
#ifndef BSD |
471 |
> |
#ifdef SIGCONT |
472 |
|
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
473 |
|
#endif |
474 |
|
for (i = ystep; i > 0; i--) { |
484 |
|
/* record progress */ |
485 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
486 |
|
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
487 |
< |
report(); |
488 |
< |
#ifndef BSD |
487 |
> |
report(0); |
488 |
> |
#ifdef SIGCONT |
489 |
|
else |
490 |
|
signal(SIGCONT, report); |
491 |
|
#endif |
492 |
|
} |
493 |
|
/* clean up */ |
494 |
+ |
#ifdef SIGCONT |
495 |
|
signal(SIGCONT, SIG_IGN); |
496 |
+ |
#endif |
497 |
|
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) |
498 |
|
< hres*sizeof(float)) |
499 |
|
goto writerr; |
505 |
|
if (close(zfd) == -1) |
506 |
|
goto writerr; |
507 |
|
for (i = 0; i <= psample; i++) |
508 |
< |
free((char *)zbar[i]); |
508 |
> |
free((void *)zbar[i]); |
509 |
|
} |
510 |
|
for (i = 0; i <= psample; i++) |
511 |
< |
free((char *)scanbar[i]); |
511 |
> |
free((void *)scanbar[i]); |
512 |
|
if (sampdens != NULL) |
513 |
|
free(sampdens); |
514 |
|
pctdone = 100.0; |
515 |
|
if (ralrm > 0) |
516 |
< |
report(); |
516 |
> |
report(0); |
517 |
> |
#ifdef SIGCONT |
518 |
|
signal(SIGCONT, SIG_DFL); |
519 |
+ |
#endif |
520 |
|
return; |
521 |
|
writerr: |
522 |
|
error(SYSTEM, "write error in render"); |
525 |
|
} |
526 |
|
|
527 |
|
|
528 |
< |
fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ |
529 |
< |
register COLOR *scanline; |
530 |
< |
register float *zline; |
531 |
< |
register char *sd; |
532 |
< |
int xres, y, xstep; |
528 |
> |
static void |
529 |
> |
fillscanline( /* fill scan at y */ |
530 |
> |
COLOR *scanline, |
531 |
> |
float *zline, |
532 |
> |
char *sd, |
533 |
> |
int xres, |
534 |
> |
int y, |
535 |
> |
int xstep |
536 |
> |
) |
537 |
|
{ |
538 |
|
static int nc = 0; /* number of calls */ |
539 |
|
int bl = xstep, b = xstep; |
540 |
|
double z; |
541 |
< |
register int i; |
542 |
< |
|
541 |
> |
int i; |
542 |
> |
|
543 |
|
z = pixvalue(scanline[0], 0, y); |
544 |
|
if (zline) zline[0] = z; |
545 |
|
/* zig-zag start for quincunx pattern */ |
564 |
|
} |
565 |
|
|
566 |
|
|
567 |
< |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
568 |
< |
register COLOR *scanbar[]; |
569 |
< |
register float *zbar[]; |
570 |
< |
int xres, y, ysize; |
567 |
> |
static void |
568 |
> |
fillscanbar( /* fill interior */ |
569 |
> |
COLOR *scanbar[], |
570 |
> |
float *zbar[], |
571 |
> |
int xres, |
572 |
> |
int y, |
573 |
> |
int ysize |
574 |
> |
) |
575 |
|
{ |
576 |
|
COLOR vline[MAXDIV+1]; |
577 |
|
float zline[MAXDIV+1]; |
578 |
|
int b = ysize; |
579 |
< |
register int i, j; |
580 |
< |
|
579 |
> |
int i, j; |
580 |
> |
|
581 |
|
for (i = 0; i < xres; i++) { |
533 |
– |
|
582 |
|
copycolor(vline[0], scanbar[0][i]); |
583 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
584 |
|
if (zbar[0]) { |
585 |
|
zline[0] = zbar[0][i]; |
586 |
|
zline[ysize] = zbar[ysize][i]; |
587 |
|
} |
540 |
– |
|
588 |
|
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
589 |
|
i, y, 0, ysize, b/2); |
590 |
< |
|
590 |
> |
|
591 |
|
for (j = 1; j < ysize; j++) |
592 |
|
copycolor(scanbar[j][i], vline[j]); |
593 |
|
if (zbar[0]) |
597 |
|
} |
598 |
|
|
599 |
|
|
600 |
< |
int |
601 |
< |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
602 |
< |
register COLOR *colline; |
603 |
< |
register float *zline; |
604 |
< |
int x, y; |
605 |
< |
int xlen, ylen; |
606 |
< |
int b; |
600 |
> |
static int |
601 |
> |
fillsample( /* fill interior points */ |
602 |
> |
COLOR *colline, |
603 |
> |
float *zline, |
604 |
> |
int x, |
605 |
> |
int y, |
606 |
> |
int xlen, |
607 |
> |
int ylen, |
608 |
> |
int b |
609 |
> |
) |
610 |
|
{ |
611 |
|
double ratio; |
612 |
|
double z; |
613 |
|
COLOR ctmp; |
614 |
|
int ncut; |
615 |
< |
register int len; |
616 |
< |
|
615 |
> |
int len; |
616 |
> |
|
617 |
|
if (xlen > 0) /* x or y length is zero */ |
618 |
|
len = xlen; |
619 |
|
else |
620 |
|
len = ylen; |
621 |
< |
|
621 |
> |
|
622 |
|
if (len <= 1) /* limit recursion */ |
623 |
|
return(0); |
624 |
< |
|
625 |
< |
if (b > 0 |
626 |
< |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
624 |
> |
|
625 |
> |
if (b > 0 || |
626 |
> |
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
627 |
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
628 |
< |
|
628 |
> |
|
629 |
|
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
630 |
|
if (zline) zline[len>>1] = z; |
631 |
|
ncut = 1; |
582 |
– |
|
632 |
|
} else { /* interpolate */ |
584 |
– |
|
633 |
|
copycolor(colline[len>>1], colline[len]); |
634 |
|
ratio = (double)(len>>1) / len; |
635 |
|
scalecolor(colline[len>>1], ratio); |
643 |
|
} |
644 |
|
/* recurse */ |
645 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
646 |
< |
|
646 |
> |
|
647 |
|
ncut += fillsample(colline+(len>>1), |
648 |
|
zline ? zline+(len>>1) : (float *)NULL, |
649 |
|
x+(xlen>>1), y+(ylen>>1), |
653 |
|
} |
654 |
|
|
655 |
|
|
656 |
< |
double |
657 |
< |
pixvalue(col, x, y) /* compute pixel value */ |
658 |
< |
COLOR col; /* returned color */ |
659 |
< |
int x, y; /* pixel position */ |
656 |
> |
static double |
657 |
> |
pixvalue( /* compute pixel value */ |
658 |
> |
COLOR col, /* returned color */ |
659 |
> |
int x, /* pixel position */ |
660 |
> |
int y |
661 |
> |
) |
662 |
|
{ |
663 |
< |
static RAY thisray; |
664 |
< |
|
665 |
< |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview, |
666 |
< |
(x+pixjitter())/hres, (y+pixjitter())/vres)) < -FTINY) { |
667 |
< |
setcolor(col, 0.0, 0.0, 0.0); |
663 |
> |
RAY thisray; |
664 |
> |
FVECT lorg, ldir; |
665 |
> |
double hpos, vpos, lmax; |
666 |
> |
int i; |
667 |
> |
/* compute view ray */ |
668 |
> |
setcolor(col, 0.0, 0.0, 0.0); |
669 |
> |
hpos = (x+pixjitter())/hres; |
670 |
> |
vpos = (y+pixjitter())/vres; |
671 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
672 |
> |
&ourview, hpos, vpos)) < -FTINY) |
673 |
|
return(0.0); |
674 |
+ |
/* set pixel index */ |
675 |
+ |
samplendx = pixnumber(x,y,hres,vres); |
676 |
+ |
/* optional motion blur */ |
677 |
+ |
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
678 |
+ |
&lastview, hpos, vpos)) >= -FTINY) { |
679 |
+ |
double d = mblur*(.5-urand(281+samplendx)); |
680 |
+ |
|
681 |
+ |
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
682 |
+ |
for (i = 3; i--; ) { |
683 |
+ |
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; |
684 |
+ |
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; |
685 |
+ |
} |
686 |
+ |
if (normalize(thisray.rdir) == 0.0) |
687 |
+ |
return(0.0); |
688 |
|
} |
689 |
+ |
/* depth-of-field */ |
690 |
+ |
if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur)) |
691 |
+ |
return(0.0); |
692 |
|
|
693 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
693 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
694 |
|
|
623 |
– |
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
624 |
– |
|
695 |
|
rayvalue(&thisray); /* trace ray */ |
696 |
|
|
697 |
|
copycolor(col, thisray.rcol); /* return color */ |
698 |
< |
|
699 |
< |
return(thisray.rt); /* return distance */ |
698 |
> |
|
699 |
> |
return(raydistance(&thisray)); /* return distance */ |
700 |
|
} |
701 |
|
|
702 |
|
|
703 |
< |
int |
704 |
< |
salvage(oldfile) /* salvage scanlines from killed program */ |
705 |
< |
char *oldfile; |
703 |
> |
static int |
704 |
> |
salvage( /* salvage scanlines from killed program */ |
705 |
> |
char *oldfile |
706 |
> |
) |
707 |
|
{ |
708 |
|
COLR *scanline; |
709 |
|
FILE *fp; |
717 |
|
error(WARNING, errmsg); |
718 |
|
goto gotzip; |
719 |
|
} |
720 |
< |
#ifdef MSDOS |
650 |
< |
setmode(fileno(fp), O_BINARY); |
651 |
< |
#endif |
720 |
> |
SET_FILE_BINARY(fp); |
721 |
|
/* discard header */ |
722 |
|
getheader(fp, NULL, NULL); |
723 |
|
/* get picture size */ |
729 |
|
goto gotzip; |
730 |
|
} |
731 |
|
|
732 |
< |
if (x != hres || y != vres) { |
732 |
> |
if ((x != hres) | (y != vres)) { |
733 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
734 |
|
oldfile); |
735 |
|
error(USER, errmsg); |
746 |
|
} |
747 |
|
if (fflush(stdout) == EOF) |
748 |
|
goto writerr; |
749 |
< |
free((char *)scanline); |
749 |
> |
free((void *)scanline); |
750 |
|
fclose(fp); |
751 |
|
unlink(oldfile); |
752 |
|
return(y); |
757 |
|
writerr: |
758 |
|
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); |
759 |
|
error(SYSTEM, errmsg); |
760 |
+ |
return -1; /* pro forma return */ |
761 |
|
} |
762 |
|
|
763 |
< |
|
764 |
< |
int |
765 |
< |
pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ |
766 |
< |
register int x, y; |
767 |
< |
int xres, yres; |
763 |
> |
static int |
764 |
> |
pixnumber( /* compute pixel index (screen door) */ |
765 |
> |
int x, |
766 |
> |
int y, |
767 |
> |
int xres, |
768 |
> |
int yres |
769 |
> |
) |
770 |
|
{ |
771 |
< |
x -= y; |
772 |
< |
while (x < 0) |
773 |
< |
x += xres; |
774 |
< |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
771 |
> |
unsigned nbits = 0; |
772 |
> |
bitmask_t coord[2]; |
773 |
> |
|
774 |
> |
if (xres < yres) xres = yres; |
775 |
> |
while (xres > 0) { |
776 |
> |
xres >>= 1; |
777 |
> |
++nbits; |
778 |
> |
} |
779 |
> |
coord[0] = x; coord[1] = y; |
780 |
> |
return ((int)hilbert_c2i(2, nbits, coord)); |
781 |
|
} |