1 |
– |
/* Copyright (c) 1992 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 NON_POSIX |
13 |
|
#ifdef BSD |
14 |
|
#include <sys/time.h> |
15 |
|
#include <sys/resource.h> |
16 |
+ |
#else |
17 |
+ |
#ifndef MINGW |
18 |
+ |
#include <sys/times.h> |
19 |
+ |
#else |
20 |
+ |
#include <sys/time.h> |
21 |
|
#endif |
22 |
+ |
#include <unistd.h> |
23 |
+ |
#endif |
24 |
+ |
#endif |
25 |
|
|
26 |
+ |
#include <time.h> |
27 |
|
#include <signal.h> |
21 |
– |
#include <fcntl.h> |
28 |
|
|
29 |
+ |
#include "platform.h" |
30 |
+ |
#include "ray.h" |
31 |
+ |
#include "paths.h" |
32 |
+ |
#include "ambient.h" |
33 |
|
#include "view.h" |
34 |
+ |
#include "random.h" |
35 |
+ |
#include "paths.h" |
36 |
+ |
#include "rtmisc.h" /* myhostname() */ |
37 |
|
|
25 |
– |
#include "resolu.h" |
38 |
|
|
39 |
< |
#include "random.h" |
39 |
> |
#define RFTEMPLATE "rfXXXXXX" |
40 |
|
|
41 |
< |
#include "paths.h" |
41 |
> |
#ifndef SIGCONT |
42 |
> |
#ifdef SIGIO /* XXX can we live without this? */ |
43 |
> |
#define SIGCONT SIGIO |
44 |
> |
#endif |
45 |
> |
#endif |
46 |
|
|
47 |
+ |
CUBE thescene; /* our scene */ |
48 |
+ |
OBJECT nsceneobjs; /* number of objects in our scene */ |
49 |
+ |
|
50 |
|
int dimlist[MAXDIM]; /* sampling dimensions */ |
51 |
|
int ndims = 0; /* number of sampling dimensions */ |
52 |
|
int samplendx; /* sample index number */ |
53 |
|
|
54 |
+ |
//extern void ambnotify(); |
55 |
+ |
void (*addobjnotify[])() = {ambnotify, NULL}; |
56 |
+ |
|
57 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
58 |
|
int hresolu = 512; /* horizontal resolution */ |
59 |
|
int vresolu = 512; /* vertical resolution */ |
60 |
< |
double pixaspect = 1.0; /* pixel aspect ratio */ |
60 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
61 |
|
|
62 |
|
int psample = 4; /* pixel sample size */ |
63 |
< |
double maxdiff = .05; /* max. difference for interpolation */ |
64 |
< |
double dstrpix = 0.67; /* square pixel distribution */ |
63 |
> |
double maxdiff = .05; /* max. difference for interpolation */ |
64 |
> |
double dstrpix = 0.67; /* square pixel distribution */ |
65 |
|
|
66 |
< |
double dstrsrc = 0.0; /* square source distribution */ |
67 |
< |
double shadthresh = .05; /* shadow threshold */ |
68 |
< |
double shadcert = .5; /* shadow certainty */ |
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 */ |
79 |
|
int directrelay = 1; /* number of source relays */ |
80 |
|
int vspretest = 512; /* virtual source pretest density */ |
81 |
< |
int directinvis = 0; /* sources invisible? */ |
82 |
< |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
81 |
> |
int directvis = 1; /* sources visible? */ |
82 |
> |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
83 |
|
|
84 |
< |
double specthresh = .15; /* specular sampling threshold */ |
85 |
< |
double specjitter = 1.; /* specular sampling jitter */ |
84 |
> |
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
85 |
> |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
86 |
> |
double seccg = 0.; /* global scattering eccentricity */ |
87 |
> |
double ssampdist = 0.; /* scatter sampling distance */ |
88 |
|
|
89 |
< |
int maxdepth = 6; /* maximum recursion depth */ |
90 |
< |
double minweight = 5e-3; /* minimum ray weight */ |
89 |
> |
double specthresh = .15; /* specular sampling threshold */ |
90 |
> |
double specjitter = 1.; /* specular sampling jitter */ |
91 |
|
|
92 |
+ |
int backvis = 1; /* back face visibility */ |
93 |
+ |
|
94 |
+ |
int maxdepth = 7; /* maximum recursion depth */ |
95 |
+ |
double minweight = 4e-3; /* minimum ray weight */ |
96 |
+ |
|
97 |
+ |
char *ambfile = NULL; /* ambient file name */ |
98 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
99 |
< |
double ambacc = 0.2; /* ambient accuracy */ |
100 |
< |
int ambres = 32; /* ambient resolution */ |
101 |
< |
int ambdiv = 128; /* ambient divisions */ |
102 |
< |
int ambssamp = 0; /* ambient super-samples */ |
99 |
> |
int ambvwt = 0; /* initial weight for ambient value */ |
100 |
> |
double ambacc = 0.2; /* ambient accuracy */ |
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 |
|
|
108 |
|
int ralrm = 0; /* seconds between reports */ |
109 |
|
|
110 |
< |
double pctdone = 0.0; /* percentage done */ |
110 |
> |
double pctdone = 0.0; /* percentage done */ |
111 |
> |
time_t tlastrept = 0L; /* time at last report */ |
112 |
> |
time_t tstart; /* starting time */ |
113 |
|
|
114 |
< |
long tlastrept = 0L; /* time at last report */ |
114 |
> |
#define MAXDIV 16 /* maximum sample size */ |
115 |
|
|
116 |
< |
extern long time(); |
74 |
< |
extern long tstart; /* starting time */ |
116 |
> |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
117 |
|
|
118 |
< |
extern long nrays; /* number of rays traced */ |
118 |
> |
int hres, vres; /* resolution for this frame */ |
119 |
|
|
120 |
< |
#define MAXDIV 16 /* maximum sample size */ |
120 |
> |
static VIEW lastview; /* the previous view input */ |
121 |
|
|
122 |
< |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
122 |
> |
//extern char *mktemp(); /* XXX should be in stdlib.h or unistd.h */ |
123 |
|
|
124 |
< |
#define RFTEMPLATE "rfXXXXXX" |
83 |
< |
#define HFTEMPLATE TEMPLATE |
124 |
> |
//double pixvalue(); |
125 |
|
|
126 |
< |
static char *hfname = NULL; /* header file name */ |
127 |
< |
static FILE *hfp = NULL; /* header file pointer */ |
126 |
> |
static void report(int); |
127 |
> |
static int nextview(FILE *fp); |
128 |
> |
static void render(char *zfile, char *oldfile); |
129 |
> |
static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres, |
130 |
> |
int y, int xstep); |
131 |
> |
static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres, |
132 |
> |
int y, int ysize); |
133 |
> |
static int fillsample(COLOR *colline, float *zline, int x, int y, |
134 |
> |
int xlen, int ylen, int b); |
135 |
> |
static double pixvalue(COLOR col, int x, int y); |
136 |
> |
static int salvage(char *oldfile); |
137 |
> |
static int pixnumber(int x, int y, int xres, int yres); |
138 |
|
|
88 |
– |
static int hres, vres; /* resolution for this frame */ |
139 |
|
|
90 |
– |
extern char *mktemp(); |
140 |
|
|
141 |
< |
double pixvalue(); |
141 |
> |
#ifdef RHAS_STAT |
142 |
> |
#include <sys/types.h> |
143 |
> |
#include <sys/stat.h> |
144 |
> |
int |
145 |
> |
file_exists(fname) /* ordinary file exists? */ |
146 |
> |
char *fname; |
147 |
> |
{ |
148 |
> |
struct stat sbuf; |
149 |
> |
if (stat(fname, &sbuf) < 0) return(0); |
150 |
> |
return((sbuf.st_mode & S_IFREG) != 0); |
151 |
> |
} |
152 |
> |
#else |
153 |
> |
#define file_exists(f) (access(f,F_OK)==0) |
154 |
> |
#endif |
155 |
|
|
156 |
|
|
157 |
+ |
void |
158 |
|
quit(code) /* quit program */ |
159 |
|
int code; |
160 |
|
{ |
161 |
|
if (code) /* report status */ |
162 |
< |
report(); |
163 |
< |
if (hfname != NULL) { /* delete header file */ |
164 |
< |
if (hfp != NULL) |
165 |
< |
fclose(hfp); |
166 |
< |
unlink(hfname); |
104 |
< |
} |
162 |
> |
report(0); |
163 |
> |
#ifndef NON_POSIX |
164 |
> |
headclean(); /* delete header file */ |
165 |
> |
pfclean(); /* clean up persist files */ |
166 |
> |
#endif |
167 |
|
exit(code); |
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
< |
#ifdef BSD |
172 |
< |
report() /* report progress */ |
171 |
> |
#ifndef NON_POSIX |
172 |
> |
static void |
173 |
> |
report(int dummy) /* report progress */ |
174 |
|
{ |
175 |
+ |
double u, s; |
176 |
+ |
#ifdef BSD |
177 |
|
struct rusage rubuf; |
178 |
< |
double t; |
178 |
> |
#else |
179 |
> |
struct tms tbuf; |
180 |
> |
double period; |
181 |
> |
#endif |
182 |
|
|
183 |
+ |
tlastrept = time((time_t *)NULL); |
184 |
+ |
#ifdef BSD |
185 |
|
getrusage(RUSAGE_SELF, &rubuf); |
186 |
< |
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
187 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
186 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
187 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
188 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
189 |
< |
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
190 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
189 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
190 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
191 |
> |
#else |
192 |
> |
times(&tbuf); |
193 |
> |
#ifdef _SC_CLK_TCK |
194 |
> |
period = 1.0 / sysconf(_SC_CLK_TCK); |
195 |
> |
#else |
196 |
> |
period = 1.0 / 60.0; |
197 |
> |
#endif |
198 |
> |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
199 |
> |
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
200 |
> |
#endif |
201 |
|
|
202 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
203 |
< |
nrays, pctdone, t/3600.0); |
202 |
> |
sprintf(errmsg, |
203 |
> |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
204 |
> |
nrays, pctdone, u/3600., s/3600., |
205 |
> |
(tlastrept-tstart)/3600., myhostname()); |
206 |
|
eputs(errmsg); |
207 |
< |
tlastrept = time((long *)0); |
207 |
> |
#ifdef SIGCONT |
208 |
> |
signal(SIGCONT, report); |
209 |
> |
#endif |
210 |
|
} |
211 |
|
#else |
212 |
< |
report() /* report progress */ |
212 |
> |
static void |
213 |
> |
report(int dummy) /* report progress */ |
214 |
|
{ |
215 |
< |
tlastrept = time((long *)0); |
216 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
215 |
> |
tlastrept = time((time_t *)NULL); |
216 |
> |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
217 |
|
nrays, pctdone, (tlastrept-tstart)/3600.0); |
218 |
|
eputs(errmsg); |
134 |
– |
signal(SIGALRM, report); |
219 |
|
} |
220 |
|
#endif |
221 |
|
|
222 |
|
|
223 |
< |
openheader() /* save standard output to header file */ |
224 |
< |
{ |
225 |
< |
hfname = mktemp(HFTEMPLATE); |
226 |
< |
if (freopen(hfname, "w", stdout) == NULL) { |
227 |
< |
sprintf(errmsg, "cannot open header file \"%s\"", hfname); |
228 |
< |
error(SYSTEM, errmsg); |
229 |
< |
} |
146 |
< |
} |
147 |
< |
|
148 |
< |
|
149 |
< |
closeheader() /* done with header output */ |
150 |
< |
{ |
151 |
< |
if (hfname == NULL) |
152 |
< |
return; |
153 |
< |
if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL) |
154 |
< |
error(SYSTEM, "error reopening header file"); |
155 |
< |
} |
156 |
< |
|
157 |
< |
|
158 |
< |
dupheader() /* repeat header on standard output */ |
159 |
< |
{ |
160 |
< |
register int c; |
161 |
< |
|
162 |
< |
if (fseek(hfp, 0L, 0) < 0) |
163 |
< |
error(SYSTEM, "seek error on header file"); |
164 |
< |
while ((c = getc(hfp)) != EOF) |
165 |
< |
putchar(c); |
166 |
< |
} |
167 |
< |
|
168 |
< |
|
169 |
< |
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
170 |
< |
int seq; |
171 |
< |
char *pout, *zout, *prvr; |
223 |
> |
extern void |
224 |
> |
rpict( /* generate image(s) */ |
225 |
> |
int seq, |
226 |
> |
char *pout, |
227 |
> |
char *zout, |
228 |
> |
char *prvr |
229 |
> |
) |
230 |
|
/* |
231 |
|
* If seq is greater than zero, then we will render a sequence of |
232 |
|
* images based on view parameter strings read from the standard input. |
239 |
|
* sequenced file naming. |
240 |
|
*/ |
241 |
|
{ |
184 |
– |
extern char *rindex(), *strncpy(), *strcat(); |
242 |
|
char fbuf[128], fbuf2[128]; |
243 |
+ |
int npicts; |
244 |
|
register char *cp; |
245 |
< |
RESOLU rs; |
246 |
< |
double pa; |
189 |
< |
/* finished writing header */ |
190 |
< |
closeheader(); |
245 |
> |
RESOLU rs; |
246 |
> |
double pa; |
247 |
|
/* check sampling */ |
248 |
|
if (psample < 1) |
249 |
|
psample = 1; |
265 |
|
for ( ; seq < rn; seq++) |
266 |
|
if (nextview(stdin) == EOF) |
267 |
|
error(USER, "unexpected EOF on view input"); |
268 |
+ |
setview(&ourview); |
269 |
|
prvr = fbuf; /* mark for renaming */ |
270 |
|
} |
271 |
< |
if (pout != NULL) { |
271 |
> |
if ((pout != NULL) & (prvr != NULL)) { |
272 |
|
sprintf(fbuf, pout, seq); |
273 |
< |
if (prvr != NULL && !strcmp(prvr, fbuf)) { /* rename */ |
274 |
< |
fbuf2[0] = '\0'; |
275 |
< |
if ((cp = rindex(fbuf, '/')) != NULL) |
276 |
< |
strncpy(fbuf2, fbuf, cp-fbuf+1); |
277 |
< |
strcat(fbuf2, RFTEMPLATE); |
273 |
> |
if (!strcmp(prvr, fbuf)) { /* rename */ |
274 |
> |
strcpy(fbuf2, fbuf); |
275 |
> |
for (cp = fbuf2; *cp; cp++) |
276 |
> |
; |
277 |
> |
while (cp > fbuf2 && !ISDIRSEP(cp[-1])) |
278 |
> |
cp--; |
279 |
> |
strcpy(cp, RFTEMPLATE); |
280 |
|
prvr = mktemp(fbuf2); |
281 |
< |
if (rename(fbuf, prvr) < 0 && errno != ENOENT) { |
282 |
< |
sprintf(errmsg, |
281 |
> |
if (rename(fbuf, prvr) < 0) { |
282 |
> |
if (errno == ENOENT) { /* ghost file */ |
283 |
> |
sprintf(errmsg, |
284 |
> |
"new output file \"%s\"", |
285 |
> |
fbuf); |
286 |
> |
error(WARNING, errmsg); |
287 |
> |
prvr = NULL; |
288 |
> |
} else { /* serious error */ |
289 |
> |
sprintf(errmsg, |
290 |
|
"cannot rename \"%s\" to \"%s\"", |
291 |
|
fbuf, prvr); |
292 |
< |
error(SYSTEM, errmsg); |
292 |
> |
error(SYSTEM, errmsg); |
293 |
> |
} |
294 |
|
} |
295 |
|
} |
296 |
|
} |
297 |
< |
/* render sequence */ |
297 |
> |
npicts = 0; /* render sequence */ |
298 |
|
do { |
299 |
|
if (seq && nextview(stdin) == EOF) |
300 |
|
break; |
301 |
+ |
pctdone = 0.0; |
302 |
|
if (pout != NULL) { |
303 |
|
sprintf(fbuf, pout, seq); |
304 |
+ |
if (file_exists(fbuf)) { |
305 |
+ |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
306 |
+ |
sprintf(errmsg, |
307 |
+ |
"output file \"%s\" exists", |
308 |
+ |
fbuf); |
309 |
+ |
error(USER, errmsg); |
310 |
+ |
} |
311 |
+ |
setview(&ourview); |
312 |
+ |
continue; /* don't clobber */ |
313 |
+ |
} |
314 |
|
if (freopen(fbuf, "w", stdout) == NULL) { |
315 |
|
sprintf(errmsg, |
316 |
|
"cannot open output file \"%s\"", fbuf); |
317 |
|
error(SYSTEM, errmsg); |
318 |
|
} |
319 |
+ |
SET_FILE_BINARY(stdout); |
320 |
|
dupheader(); |
321 |
|
} |
322 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
323 |
< |
if (prvr != NULL) |
324 |
< |
if (viewfile(prvr, &ourview, &rs) <= 0 |
246 |
< |
|| rs.or != PIXSTANDARD) { |
323 |
> |
if (prvr != NULL) { |
324 |
> |
if (viewfile(prvr, &ourview, &rs) <= 0) { |
325 |
|
sprintf(errmsg, |
326 |
|
"cannot recover view parameters from \"%s\"", prvr); |
327 |
|
error(WARNING, errmsg); |
330 |
|
hres = scanlen(&rs); |
331 |
|
vres = numscans(&rs); |
332 |
|
} |
333 |
+ |
} |
334 |
|
if ((cp = setview(&ourview)) != NULL) |
335 |
|
error(USER, cp); |
336 |
|
normaspect(viewaspect(&ourview), &pa, &hres, &vres); |
348 |
|
putchar('\n'); |
349 |
|
if (pa < .99 || pa > 1.01) |
350 |
|
fputaspect(pa, stdout); |
351 |
+ |
fputnow(stdout); |
352 |
|
fputformat(COLRFMT, stdout); |
353 |
|
putchar('\n'); |
354 |
|
if (zout != NULL) |
357 |
|
cp = NULL; |
358 |
|
render(cp, prvr); |
359 |
|
prvr = NULL; |
360 |
+ |
npicts++; |
361 |
|
} while (seq++); |
362 |
+ |
/* check that we did something */ |
363 |
+ |
if (npicts == 0) |
364 |
+ |
error(WARNING, "no output produced"); |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
< |
nextview(fp) /* get next view from fp */ |
369 |
< |
FILE *fp; |
368 |
> |
static int |
369 |
> |
nextview( /* get next view from fp */ |
370 |
> |
FILE *fp |
371 |
> |
) |
372 |
|
{ |
373 |
|
char linebuf[256]; |
374 |
|
|
375 |
+ |
lastview = ourview; |
376 |
|
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) |
377 |
|
if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) |
378 |
|
return(0); |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
< |
render(zfile, oldfile) /* render the scene */ |
384 |
< |
char *zfile, *oldfile; |
383 |
> |
static void |
384 |
> |
render( /* render the scene */ |
385 |
> |
char *zfile, |
386 |
> |
char *oldfile |
387 |
> |
) |
388 |
|
{ |
299 |
– |
extern long lseek(); |
389 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
390 |
|
float *zbar[MAXDIV+1]; /* z values */ |
391 |
|
char *sampdens; /* previous sample density */ |
396 |
|
COLOR *colptr; |
397 |
|
float *zptr; |
398 |
|
register int i; |
399 |
+ |
/* check for empty image */ |
400 |
+ |
if (hres <= 0 || vres <= 0) { |
401 |
+ |
error(WARNING, "empty output picture"); |
402 |
+ |
fprtresolu(0, 0, stdout); |
403 |
+ |
return; |
404 |
+ |
} |
405 |
|
/* allocate scanlines */ |
406 |
|
for (i = 0; i <= psample; i++) { |
407 |
|
scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR)); |
412 |
|
ystep = (psample*99+70)/140; |
413 |
|
if (hstep > 2) { |
414 |
|
i = hres/hstep + 2; |
415 |
< |
if ((sampdens = malloc(i)) == NULL) |
415 |
> |
if ((sampdens = (char *)malloc(i)) == NULL) |
416 |
|
goto memerr; |
417 |
|
while (i--) |
418 |
|
sampdens[i] = hstep; |
419 |
|
} else |
420 |
|
sampdens = NULL; |
421 |
< |
/* open z file */ |
421 |
> |
/* open z-file */ |
422 |
|
if (zfile != NULL) { |
423 |
|
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
424 |
< |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
424 |
> |
sprintf(errmsg, "cannot open z-file \"%s\"", zfile); |
425 |
|
error(SYSTEM, errmsg); |
426 |
|
} |
427 |
+ |
SET_FD_BINARY(zfd); |
428 |
|
for (i = 0; i <= psample; i++) { |
429 |
|
zbar[i] = (float *)malloc(hres*sizeof(float)); |
430 |
|
if (zbar[i] == NULL) |
439 |
|
fprtresolu(hres, vres, stdout); |
440 |
|
/* recover file and compute first */ |
441 |
|
i = salvage(oldfile); |
442 |
+ |
if (i >= vres) |
443 |
+ |
goto alldone; |
444 |
|
if (zfd != -1 && i > 0 && |
445 |
< |
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
446 |
< |
error(SYSTEM, "z file seek error in render"); |
445 |
> |
lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0) |
446 |
> |
error(SYSTEM, "z-file seek error in render"); |
447 |
|
pctdone = 100.0*i/vres; |
448 |
|
if (ralrm > 0) /* report init stats */ |
449 |
< |
report(); |
450 |
< |
#ifndef BSD |
449 |
> |
report(0); |
450 |
> |
#ifdef SIGCONT |
451 |
|
else |
452 |
+ |
signal(SIGCONT, report); |
453 |
|
#endif |
454 |
< |
signal(SIGALRM, report); |
455 |
< |
ypos = vres-1 - i; |
454 |
> |
ypos = vres-1 - i; /* initialize sampling */ |
455 |
> |
if (directvis) |
456 |
> |
init_drawsources(psample); |
457 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
458 |
|
/* compute scanlines */ |
459 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
473 |
|
hres, ypos, hstep); |
474 |
|
/* fill bar */ |
475 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
476 |
+ |
if (directvis) /* add bitty sources */ |
477 |
+ |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
478 |
|
/* write it out */ |
479 |
< |
#ifndef BSD |
480 |
< |
signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ |
479 |
> |
#ifdef SIGCONT |
480 |
> |
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
481 |
|
#endif |
482 |
|
for (i = ystep; i > 0; i--) { |
483 |
|
if (zfd != -1 && write(zfd, (char *)zbar[i], |
491 |
|
goto writerr; |
492 |
|
/* record progress */ |
493 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
494 |
< |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
495 |
< |
report(); |
496 |
< |
#ifndef BSD |
494 |
> |
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
495 |
> |
report(0); |
496 |
> |
#ifdef SIGCONT |
497 |
|
else |
498 |
< |
signal(SIGALRM, report); |
498 |
> |
signal(SIGCONT, report); |
499 |
|
#endif |
500 |
|
} |
501 |
|
/* clean up */ |
502 |
< |
signal(SIGALRM, SIG_IGN); |
503 |
< |
if (zfd != -1) { |
504 |
< |
if (write(zfd, (char *)zbar[0], hres*sizeof(float)) |
502 |
> |
#ifdef SIGCONT |
503 |
> |
signal(SIGCONT, SIG_IGN); |
504 |
> |
#endif |
505 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) |
506 |
|
< hres*sizeof(float)) |
507 |
< |
goto writerr; |
507 |
> |
goto writerr; |
508 |
> |
fwritescan(scanbar[0], hres, stdout); |
509 |
> |
if (fflush(stdout) == EOF) |
510 |
> |
goto writerr; |
511 |
> |
alldone: |
512 |
> |
if (zfd != -1) { |
513 |
|
if (close(zfd) == -1) |
514 |
|
goto writerr; |
515 |
|
for (i = 0; i <= psample; i++) |
516 |
< |
free((char *)zbar[i]); |
516 |
> |
free((void *)zbar[i]); |
517 |
|
} |
410 |
– |
fwritescan(scanbar[0], hres, stdout); |
411 |
– |
if (fflush(stdout) == EOF) |
412 |
– |
goto writerr; |
518 |
|
for (i = 0; i <= psample; i++) |
519 |
< |
free((char *)scanbar[i]); |
519 |
> |
free((void *)scanbar[i]); |
520 |
|
if (sampdens != NULL) |
521 |
|
free(sampdens); |
522 |
|
pctdone = 100.0; |
523 |
|
if (ralrm > 0) |
524 |
< |
report(); |
524 |
> |
report(0); |
525 |
> |
#ifdef SIGCONT |
526 |
> |
signal(SIGCONT, SIG_DFL); |
527 |
> |
#endif |
528 |
|
return; |
529 |
|
writerr: |
530 |
|
error(SYSTEM, "write error in render"); |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
< |
fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ |
537 |
< |
register COLOR *scanline; |
538 |
< |
register float *zline; |
539 |
< |
register char *sd; |
540 |
< |
int xres, y, xstep; |
536 |
> |
static void |
537 |
> |
fillscanline( /* fill scan at y */ |
538 |
> |
register COLOR *scanline, |
539 |
> |
register float *zline, |
540 |
> |
register char *sd, |
541 |
> |
int xres, |
542 |
> |
int y, |
543 |
> |
int xstep |
544 |
> |
) |
545 |
|
{ |
546 |
|
static int nc = 0; /* number of calls */ |
547 |
|
int bl = xstep, b = xstep; |
548 |
< |
double z; |
548 |
> |
double z; |
549 |
|
register int i; |
550 |
< |
|
550 |
> |
|
551 |
|
z = pixvalue(scanline[0], 0, y); |
552 |
|
if (zline) zline[0] = z; |
553 |
|
/* zig-zag start for quincunx pattern */ |
563 |
|
b = fillsample(scanline, zline, 0, y, i, 0, b/2); |
564 |
|
else |
565 |
|
b = fillsample(scanline+i-xstep, |
566 |
< |
zline ? zline+i-xstep : NULL, |
566 |
> |
zline ? zline+i-xstep : (float *)NULL, |
567 |
|
i-xstep, y, xstep, 0, b/2); |
568 |
|
if (sd) *sd++ = nc & 1 ? bl : b; |
569 |
|
bl = b; |
572 |
|
} |
573 |
|
|
574 |
|
|
575 |
< |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
576 |
< |
register COLOR *scanbar[]; |
577 |
< |
register float *zbar[]; |
578 |
< |
int xres, y, ysize; |
575 |
> |
static void |
576 |
> |
fillscanbar( /* fill interior */ |
577 |
> |
register COLOR *scanbar[], |
578 |
> |
register float *zbar[], |
579 |
> |
int xres, |
580 |
> |
int y, |
581 |
> |
int ysize |
582 |
> |
) |
583 |
|
{ |
584 |
|
COLOR vline[MAXDIV+1]; |
585 |
|
float zline[MAXDIV+1]; |
586 |
|
int b = ysize; |
587 |
|
register int i, j; |
588 |
< |
|
588 |
> |
|
589 |
|
for (i = 0; i < xres; i++) { |
474 |
– |
|
590 |
|
copycolor(vline[0], scanbar[0][i]); |
591 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
592 |
|
if (zbar[0]) { |
593 |
|
zline[0] = zbar[0][i]; |
594 |
|
zline[ysize] = zbar[ysize][i]; |
595 |
|
} |
596 |
< |
|
482 |
< |
b = fillsample(vline, zbar[0] ? zline : NULL, |
596 |
> |
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
597 |
|
i, y, 0, ysize, b/2); |
598 |
< |
|
598 |
> |
|
599 |
|
for (j = 1; j < ysize; j++) |
600 |
|
copycolor(scanbar[j][i], vline[j]); |
601 |
|
if (zbar[0]) |
605 |
|
} |
606 |
|
|
607 |
|
|
608 |
< |
int |
609 |
< |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
610 |
< |
register COLOR *colline; |
611 |
< |
register float *zline; |
612 |
< |
int x, y; |
613 |
< |
int xlen, ylen; |
614 |
< |
int b; |
608 |
> |
static int |
609 |
> |
fillsample( /* fill interior points */ |
610 |
> |
register COLOR *colline, |
611 |
> |
register float *zline, |
612 |
> |
int x, |
613 |
> |
int y, |
614 |
> |
int xlen, |
615 |
> |
int ylen, |
616 |
> |
int b |
617 |
> |
) |
618 |
|
{ |
619 |
< |
extern double fabs(); |
620 |
< |
double ratio; |
504 |
< |
double z; |
619 |
> |
double ratio; |
620 |
> |
double z; |
621 |
|
COLOR ctmp; |
622 |
|
int ncut; |
623 |
|
register int len; |
624 |
< |
|
624 |
> |
|
625 |
|
if (xlen > 0) /* x or y length is zero */ |
626 |
|
len = xlen; |
627 |
|
else |
628 |
|
len = ylen; |
629 |
< |
|
629 |
> |
|
630 |
|
if (len <= 1) /* limit recursion */ |
631 |
|
return(0); |
632 |
< |
|
633 |
< |
if (b > 0 |
634 |
< |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
632 |
> |
|
633 |
> |
if (b > 0 || |
634 |
> |
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
635 |
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
636 |
< |
|
636 |
> |
|
637 |
|
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
638 |
|
if (zline) zline[len>>1] = z; |
639 |
|
ncut = 1; |
524 |
– |
|
640 |
|
} else { /* interpolate */ |
526 |
– |
|
641 |
|
copycolor(colline[len>>1], colline[len]); |
642 |
|
ratio = (double)(len>>1) / len; |
643 |
|
scalecolor(colline[len>>1], ratio); |
651 |
|
} |
652 |
|
/* recurse */ |
653 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
654 |
< |
|
655 |
< |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
654 |
> |
|
655 |
> |
ncut += fillsample(colline+(len>>1), |
656 |
> |
zline ? zline+(len>>1) : (float *)NULL, |
657 |
|
x+(xlen>>1), y+(ylen>>1), |
658 |
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
659 |
|
|
661 |
|
} |
662 |
|
|
663 |
|
|
664 |
< |
double |
665 |
< |
pixvalue(col, x, y) /* compute pixel value */ |
666 |
< |
COLOR col; /* returned color */ |
667 |
< |
int x, y; /* pixel position */ |
664 |
> |
static double |
665 |
> |
pixvalue( /* compute pixel value */ |
666 |
> |
COLOR col, /* returned color */ |
667 |
> |
int x, /* pixel position */ |
668 |
> |
int y |
669 |
> |
) |
670 |
|
{ |
671 |
< |
static RAY thisray; |
672 |
< |
|
673 |
< |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
674 |
< |
(x+pixjitter())/hres, (y+pixjitter())/vres) < 0) { |
671 |
> |
RAY thisray; |
672 |
> |
FVECT lorg, ldir; |
673 |
> |
double hpos, vpos, vdist, lmax; |
674 |
> |
register int i; |
675 |
> |
/* compute view ray */ |
676 |
> |
hpos = (x+pixjitter())/hres; |
677 |
> |
vpos = (y+pixjitter())/vres; |
678 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
679 |
> |
&ourview, hpos, vpos)) < -FTINY) { |
680 |
|
setcolor(col, 0.0, 0.0, 0.0); |
681 |
|
return(0.0); |
682 |
|
} |
683 |
+ |
vdist = ourview.vdist; |
684 |
+ |
/* set pixel index */ |
685 |
+ |
samplendx = pixnumber(x,y,hres,vres); |
686 |
+ |
/* optional motion blur */ |
687 |
+ |
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
688 |
+ |
&lastview, hpos, vpos)) >= -FTINY) { |
689 |
+ |
register double d = mblur*(.5-urand(281+samplendx)); |
690 |
|
|
691 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
691 |
> |
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
692 |
> |
for (i = 3; i--; ) { |
693 |
> |
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; |
694 |
> |
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; |
695 |
> |
} |
696 |
> |
if (normalize(thisray.rdir) == 0.0) |
697 |
> |
return(0.0); |
698 |
> |
vdist = (1.-d)*vdist + d*lastview.vdist; |
699 |
> |
} |
700 |
> |
/* optional depth-of-field */ |
701 |
> |
if (dblur > FTINY && vdist > FTINY) { |
702 |
> |
double vc, dfh, dfv; |
703 |
> |
/* PI/4. square/circle conv. */ |
704 |
> |
dfh = PI/4.*dblur*(.5 - frandom()); |
705 |
> |
dfv = PI/4.*dblur*(.5 - frandom()); |
706 |
> |
if (ourview.type == VT_PER || ourview.type == VT_PAR) { |
707 |
> |
dfh /= sqrt(ourview.hn2); |
708 |
> |
dfv /= sqrt(ourview.vn2); |
709 |
> |
for (i = 3; i--; ) { |
710 |
> |
vc = thisray.rorg[i] + vdist*thisray.rdir[i]; |
711 |
> |
thisray.rorg[i] += dfh*ourview.hvec[i] + |
712 |
> |
dfv*ourview.vvec[i] ; |
713 |
> |
thisray.rdir[i] = vc - thisray.rorg[i]; |
714 |
> |
} |
715 |
> |
} else { /* non-standard view case */ |
716 |
> |
double dfd = PI/4.*dblur*(.5 - frandom()); |
717 |
> |
if (ourview.type != VT_ANG && ourview.type != VT_PLS) { |
718 |
> |
if (ourview.type != VT_CYL) |
719 |
> |
dfh /= sqrt(ourview.hn2); |
720 |
> |
dfv /= sqrt(ourview.vn2); |
721 |
> |
} |
722 |
> |
for (i = 3; i--; ) { |
723 |
> |
vc = thisray.rorg[i] + vdist*thisray.rdir[i]; |
724 |
> |
thisray.rorg[i] += dfh*ourview.hvec[i] + |
725 |
> |
dfv*ourview.vvec[i] + |
726 |
> |
dfd*ourview.vdir[i] ; |
727 |
> |
thisray.rdir[i] = vc - thisray.rorg[i]; |
728 |
> |
} |
729 |
> |
} |
730 |
> |
if (normalize(thisray.rdir) == 0.0) |
731 |
> |
return(0.0); |
732 |
> |
} |
733 |
|
|
734 |
< |
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
734 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
735 |
|
|
736 |
|
rayvalue(&thisray); /* trace ray */ |
737 |
|
|
738 |
|
copycolor(col, thisray.rcol); /* return color */ |
739 |
< |
|
739 |
> |
|
740 |
|
return(thisray.rt); /* return distance */ |
741 |
|
} |
742 |
|
|
743 |
|
|
744 |
< |
int |
745 |
< |
salvage(oldfile) /* salvage scanlines from killed program */ |
746 |
< |
char *oldfile; |
744 |
> |
static int |
745 |
> |
salvage( /* salvage scanlines from killed program */ |
746 |
> |
char *oldfile |
747 |
> |
) |
748 |
|
{ |
749 |
|
COLR *scanline; |
750 |
|
FILE *fp; |
751 |
|
int x, y; |
752 |
|
|
753 |
|
if (oldfile == NULL) |
754 |
< |
return(0); |
755 |
< |
|
754 |
> |
goto gotzip; |
755 |
> |
|
756 |
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
757 |
|
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
758 |
|
error(WARNING, errmsg); |
759 |
< |
return(0); |
759 |
> |
goto gotzip; |
760 |
|
} |
761 |
+ |
SET_FILE_BINARY(fp); |
762 |
|
/* discard header */ |
763 |
< |
getheader(fp, NULL); |
763 |
> |
getheader(fp, NULL, NULL); |
764 |
|
/* get picture size */ |
765 |
|
if (!fscnresolu(&x, &y, fp)) { |
766 |
< |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
766 |
> |
sprintf(errmsg, "bad recover file \"%s\" - not removed", |
767 |
> |
oldfile); |
768 |
|
error(WARNING, errmsg); |
769 |
|
fclose(fp); |
770 |
< |
return(0); |
770 |
> |
goto gotzip; |
771 |
|
} |
772 |
|
|
773 |
|
if (x != hres || y != vres) { |
787 |
|
} |
788 |
|
if (fflush(stdout) == EOF) |
789 |
|
goto writerr; |
790 |
< |
free((char *)scanline); |
790 |
> |
free((void *)scanline); |
791 |
|
fclose(fp); |
792 |
|
unlink(oldfile); |
793 |
|
return(y); |
794 |
+ |
gotzip: |
795 |
+ |
if (fflush(stdout) == EOF) |
796 |
+ |
error(SYSTEM, "error writing picture header"); |
797 |
+ |
return(0); |
798 |
|
writerr: |
799 |
|
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); |
800 |
|
error(SYSTEM, errmsg); |
801 |
+ |
return -1; /* pro forma return */ |
802 |
|
} |
803 |
|
|
804 |
< |
|
805 |
< |
int |
806 |
< |
pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ |
807 |
< |
register int x, y; |
808 |
< |
int xres, yres; |
804 |
> |
static int |
805 |
> |
pixnumber( /* compute pixel index (brushed) */ |
806 |
> |
register int x, |
807 |
> |
register int y, |
808 |
> |
int xres, |
809 |
> |
int yres |
810 |
> |
) |
811 |
|
{ |
812 |
|
x -= y; |
813 |
|
while (x < 0) |