1 |
|
#ifndef lint |
2 |
< |
static const char RCSid[] = "$Id"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
|
* rpict.c - routines and variables for picture generation. |
7 |
|
|
8 |
|
#include "copyright.h" |
9 |
|
|
10 |
– |
#include "platform.h" |
11 |
– |
#include "ray.h" |
12 |
– |
|
10 |
|
#include <sys/types.h> |
11 |
|
|
12 |
< |
#ifndef NON_POSIX |
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 <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 |
23 |
– |
#endif |
26 |
|
|
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" |
34 |
|
#include "random.h" |
35 |
|
#include "paths.h" |
36 |
+ |
#include "rtmisc.h" /* myhostname() */ |
37 |
|
|
38 |
|
|
39 |
|
#define RFTEMPLATE "rfXXXXXX" |
51 |
|
int ndims = 0; /* number of sampling dimensions */ |
52 |
|
int samplendx; /* sample index number */ |
53 |
|
|
54 |
< |
extern void ambnotify(); |
54 |
> |
//extern void ambnotify(); |
55 |
|
void (*addobjnotify[])() = {ambnotify, NULL}; |
56 |
|
|
57 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
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 */ |
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 = 4e-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 |
|
|
108 |
|
int ralrm = 0; /* seconds between reports */ |
119 |
|
|
120 |
|
static VIEW lastview; /* the previous view input */ |
121 |
|
|
122 |
< |
extern char *mktemp(); /* XXX should be in stdlib.h or unistd.h */ |
122 |
> |
//extern char *mktemp(); /* XXX should be in stdlib.h or unistd.h */ |
123 |
|
|
124 |
< |
void report(); |
124 |
> |
//double pixvalue(); |
125 |
|
|
126 |
< |
double pixvalue(); |
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 |
|
|
139 |
+ |
|
140 |
+ |
|
141 |
|
#ifdef RHAS_STAT |
142 |
|
#include <sys/types.h> |
143 |
|
#include <sys/stat.h> |
159 |
|
int code; |
160 |
|
{ |
161 |
|
if (code) /* report status */ |
162 |
< |
report(); |
162 |
> |
report(0); |
163 |
|
#ifndef NON_POSIX |
164 |
|
headclean(); /* delete header file */ |
165 |
|
pfclean(); /* clean up persist files */ |
169 |
|
|
170 |
|
|
171 |
|
#ifndef NON_POSIX |
172 |
< |
void |
173 |
< |
report() /* report progress */ |
172 |
> |
static void |
173 |
> |
report(int dummy) /* report progress */ |
174 |
|
{ |
152 |
– |
extern char *myhostname(); |
175 |
|
double u, s; |
176 |
|
#ifdef BSD |
177 |
|
struct rusage rubuf; |
209 |
|
#endif |
210 |
|
} |
211 |
|
#else |
212 |
< |
void |
213 |
< |
report() /* report progress */ |
212 |
> |
static void |
213 |
> |
report(int dummy) /* report progress */ |
214 |
|
{ |
215 |
|
tlastrept = time((time_t *)NULL); |
216 |
|
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
220 |
|
#endif |
221 |
|
|
222 |
|
|
223 |
< |
void |
224 |
< |
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
225 |
< |
int seq; |
226 |
< |
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. |
268 |
|
setview(&ourview); |
269 |
|
prvr = fbuf; /* mark for renaming */ |
270 |
|
} |
271 |
< |
if (pout != NULL & prvr != NULL) { |
271 |
> |
if ((pout != NULL) & (prvr != NULL)) { |
272 |
|
sprintf(fbuf, pout, seq); |
273 |
|
if (!strcmp(prvr, fbuf)) { /* rename */ |
274 |
|
strcpy(fbuf2, fbuf); |
278 |
|
cp--; |
279 |
|
strcpy(cp, RFTEMPLATE); |
280 |
|
prvr = mktemp(fbuf2); |
281 |
< |
if (rename(fbuf, prvr) < 0) |
281 |
> |
if (rename(fbuf, prvr) < 0) { |
282 |
|
if (errno == ENOENT) { /* ghost file */ |
283 |
|
sprintf(errmsg, |
284 |
|
"new output file \"%s\"", |
291 |
|
fbuf, prvr); |
292 |
|
error(SYSTEM, errmsg); |
293 |
|
} |
294 |
+ |
} |
295 |
|
} |
296 |
|
} |
297 |
|
npicts = 0; /* render sequence */ |
320 |
|
dupheader(); |
321 |
|
} |
322 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
323 |
< |
if (prvr != NULL) |
324 |
< |
if (viewfile(prvr, &ourview, &rs) <= 0 |
299 |
< |
|| rs.rt != 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); |
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 |
< |
copystruct(&lastview, &ourview); |
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 |
|
{ |
389 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
390 |
|
float *zbar[MAXDIV+1]; /* z values */ |
442 |
|
if (i >= vres) |
443 |
|
goto alldone; |
444 |
|
if (zfd != -1 && i > 0 && |
445 |
< |
lseek(zfd, (off_t)i*hres*sizeof(float), 0) < 0) |
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(); |
449 |
> |
report(0); |
450 |
|
#ifdef SIGCONT |
451 |
|
else |
452 |
|
signal(SIGCONT, report); |
492 |
|
/* record progress */ |
493 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
494 |
|
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
495 |
< |
report(); |
495 |
> |
report(0); |
496 |
|
#ifdef SIGCONT |
497 |
|
else |
498 |
|
signal(SIGCONT, report); |
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 |
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; |
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]; |
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 |
|
double ratio; |
620 |
|
double z; |
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 |
|
RAY thisray; |
672 |
|
FVECT lorg, ldir; |
673 |
< |
double hpos, vpos, lmax, d; |
673 |
> |
double hpos, vpos, vdist, lmax; |
674 |
> |
register int i; |
675 |
|
/* compute view ray */ |
676 |
|
hpos = (x+pixjitter())/hres; |
677 |
|
vpos = (y+pixjitter())/vres; |
680 |
|
setcolor(col, 0.0, 0.0, 0.0); |
681 |
|
return(0.0); |
682 |
|
} |
683 |
< |
|
684 |
< |
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
685 |
< |
|
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) { |
644 |
– |
register int i; |
689 |
|
register double d = mblur*(.5-urand(281+samplendx)); |
690 |
|
|
691 |
|
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
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 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
734 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
735 |
|
|
736 |
|
rayvalue(&thisray); /* trace ray */ |
737 |
|
|
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; |
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) |