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() */ |
36 |
> |
#include "hilbert.h" |
37 |
|
|
38 |
|
|
39 |
|
#define RFTEMPLATE "rfXXXXXX" |
51 |
|
int ndims = 0; /* number of sampling dimensions */ |
52 |
|
int samplendx; /* sample index number */ |
53 |
|
|
49 |
– |
extern void ambnotify(); |
54 |
|
void (*addobjnotify[])() = {ambnotify, NULL}; |
55 |
|
|
56 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
64 |
|
|
65 |
|
double mblur = 0.; /* motion blur parameter */ |
66 |
|
|
67 |
+ |
double dblur = 0.; /* depth-of-field blur parameter */ |
68 |
+ |
|
69 |
|
void (*trace)() = NULL; /* trace call */ |
70 |
|
|
71 |
|
int do_irrad = 0; /* compute irradiance? */ |
72 |
|
|
73 |
+ |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
74 |
+ |
|
75 |
|
double dstrsrc = 0.0; /* square source distribution */ |
76 |
|
double shadthresh = .05; /* shadow threshold */ |
77 |
|
double shadcert = .5; /* shadow certainty */ |
91 |
|
int backvis = 1; /* back face visibility */ |
92 |
|
|
93 |
|
int maxdepth = 7; /* maximum recursion depth */ |
94 |
< |
double minweight = 4e-3; /* minimum ray weight */ |
94 |
> |
double minweight = 1e-3; /* minimum ray weight */ |
95 |
|
|
96 |
|
char *ambfile = NULL; /* ambient file name */ |
97 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
98 |
|
int ambvwt = 0; /* initial weight for ambient value */ |
99 |
< |
double ambacc = 0.15; /* ambient accuracy */ |
99 |
> |
double ambacc = 0.2; /* ambient accuracy */ |
100 |
|
int ambres = 64; /* ambient resolution */ |
101 |
|
int ambdiv = 512; /* ambient divisions */ |
102 |
|
int ambssamp = 128; /* ambient super-samples */ |
103 |
|
int ambounce = 0; /* ambient bounces */ |
104 |
< |
char *amblist[128]; /* ambient include/exclude list */ |
104 |
> |
char *amblist[AMBLLEN]; /* ambient include/exclude list */ |
105 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
106 |
|
|
107 |
|
int ralrm = 0; /* seconds between reports */ |
118 |
|
|
119 |
|
static VIEW lastview; /* the previous view input */ |
120 |
|
|
121 |
< |
extern char *mktemp(); /* XXX should be in stdlib.h or unistd.h */ |
121 |
> |
static void report(int); |
122 |
> |
static int nextview(FILE *fp); |
123 |
> |
static void render(char *zfile, char *oldfile); |
124 |
> |
static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres, |
125 |
> |
int y, int xstep); |
126 |
> |
static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres, |
127 |
> |
int y, int ysize); |
128 |
> |
static int fillsample(COLOR *colline, float *zline, int x, int y, |
129 |
> |
int xlen, int ylen, int b); |
130 |
> |
static double pixvalue(COLOR col, int x, int y); |
131 |
> |
static int salvage(char *oldfile); |
132 |
> |
static int pixnumber(int x, int y, int xres, int yres); |
133 |
|
|
115 |
– |
void report(); |
134 |
|
|
117 |
– |
double pixvalue(); |
135 |
|
|
136 |
|
#ifdef RHAS_STAT |
137 |
|
#include <sys/types.h> |
154 |
|
int code; |
155 |
|
{ |
156 |
|
if (code) /* report status */ |
157 |
< |
report(); |
157 |
> |
report(0); |
158 |
|
#ifndef NON_POSIX |
159 |
|
headclean(); /* delete header file */ |
160 |
|
pfclean(); /* clean up persist files */ |
164 |
|
|
165 |
|
|
166 |
|
#ifndef NON_POSIX |
167 |
< |
void |
168 |
< |
report() /* report progress */ |
167 |
> |
static void |
168 |
> |
report(int dummy) /* report progress */ |
169 |
|
{ |
170 |
|
double u, s; |
171 |
|
#ifdef BSD |
204 |
|
#endif |
205 |
|
} |
206 |
|
#else |
207 |
< |
void |
208 |
< |
report() /* report progress */ |
207 |
> |
static void |
208 |
> |
report(int dummy) /* report progress */ |
209 |
|
{ |
210 |
|
tlastrept = time((time_t *)NULL); |
211 |
|
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
215 |
|
#endif |
216 |
|
|
217 |
|
|
218 |
< |
void |
219 |
< |
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
220 |
< |
int seq; |
221 |
< |
char *pout, *zout, *prvr; |
218 |
> |
extern void |
219 |
> |
rpict( /* generate image(s) */ |
220 |
> |
int seq, |
221 |
> |
char *pout, |
222 |
> |
char *zout, |
223 |
> |
char *prvr |
224 |
> |
) |
225 |
|
/* |
226 |
|
* If seq is greater than zero, then we will render a sequence of |
227 |
|
* images based on view parameter strings read from the standard input. |
236 |
|
{ |
237 |
|
char fbuf[128], fbuf2[128]; |
238 |
|
int npicts; |
239 |
< |
register char *cp; |
239 |
> |
char *cp; |
240 |
|
RESOLU rs; |
241 |
|
double pa; |
242 |
|
/* check sampling */ |
252 |
|
if (seq <= 0) |
253 |
|
seq = 0; |
254 |
|
else if (prvr != NULL && isint(prvr)) { |
255 |
< |
register int rn; /* skip to specified view */ |
255 |
> |
int rn; /* skip to specified view */ |
256 |
|
if ((rn = atoi(prvr)) < seq) |
257 |
|
error(USER, "recover frame less than start frame"); |
258 |
|
if (pout == NULL) |
316 |
|
} |
317 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
318 |
|
if (prvr != NULL) { |
319 |
< |
if (viewfile(prvr, &ourview, &rs) <= 0 |
300 |
< |
|| rs.rt != PIXSTANDARD) { |
319 |
> |
if (viewfile(prvr, &ourview, &rs) <= 0) { |
320 |
|
sprintf(errmsg, |
321 |
|
"cannot recover view parameters from \"%s\"", prvr); |
322 |
|
error(WARNING, errmsg); |
341 |
|
fputs(VIEWSTR, stdout); |
342 |
|
fprintview(&ourview, stdout); |
343 |
|
putchar('\n'); |
344 |
< |
if (pa < .99 || pa > 1.01) |
344 |
> |
if ((pa < .99) | (pa > 1.01)) |
345 |
|
fputaspect(pa, stdout); |
346 |
|
fputnow(stdout); |
347 |
|
fputformat(COLRFMT, stdout); |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
< |
nextview(fp) /* get next view from fp */ |
364 |
< |
FILE *fp; |
363 |
> |
static int |
364 |
> |
nextview( /* get next view from fp */ |
365 |
> |
FILE *fp |
366 |
> |
) |
367 |
|
{ |
368 |
|
char linebuf[256]; |
369 |
|
|
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
< |
render(zfile, oldfile) /* render the scene */ |
379 |
< |
char *zfile, *oldfile; |
378 |
> |
static void |
379 |
> |
render( /* render the scene */ |
380 |
> |
char *zfile, |
381 |
> |
char *oldfile |
382 |
> |
) |
383 |
|
{ |
384 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
385 |
|
float *zbar[MAXDIV+1]; /* z values */ |
390 |
|
int zfd; |
391 |
|
COLOR *colptr; |
392 |
|
float *zptr; |
393 |
< |
register int i; |
393 |
> |
int i; |
394 |
|
/* check for empty image */ |
395 |
< |
if (hres <= 0 || vres <= 0) { |
395 |
> |
if ((hres <= 0) | (vres <= 0)) { |
396 |
|
error(WARNING, "empty output picture"); |
397 |
|
fprtresolu(0, 0, stdout); |
398 |
|
return; |
436 |
|
i = salvage(oldfile); |
437 |
|
if (i >= vres) |
438 |
|
goto alldone; |
439 |
< |
if (zfd != -1 && i > 0 && |
439 |
> |
if ((zfd != -1) & (i > 0) && |
440 |
|
lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0) |
441 |
|
error(SYSTEM, "z-file seek error in render"); |
442 |
|
pctdone = 100.0*i/vres; |
443 |
|
if (ralrm > 0) /* report init stats */ |
444 |
< |
report(); |
444 |
> |
report(0); |
445 |
|
#ifdef SIGCONT |
446 |
|
else |
447 |
|
signal(SIGCONT, report); |
487 |
|
/* record progress */ |
488 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
489 |
|
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
490 |
< |
report(); |
490 |
> |
report(0); |
491 |
|
#ifdef SIGCONT |
492 |
|
else |
493 |
|
signal(SIGCONT, report); |
516 |
|
free(sampdens); |
517 |
|
pctdone = 100.0; |
518 |
|
if (ralrm > 0) |
519 |
< |
report(); |
519 |
> |
report(0); |
520 |
|
#ifdef SIGCONT |
521 |
|
signal(SIGCONT, SIG_DFL); |
522 |
|
#endif |
528 |
|
} |
529 |
|
|
530 |
|
|
531 |
< |
fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ |
532 |
< |
register COLOR *scanline; |
533 |
< |
register float *zline; |
534 |
< |
register char *sd; |
535 |
< |
int xres, y, xstep; |
531 |
> |
static void |
532 |
> |
fillscanline( /* fill scan at y */ |
533 |
> |
COLOR *scanline, |
534 |
> |
float *zline, |
535 |
> |
char *sd, |
536 |
> |
int xres, |
537 |
> |
int y, |
538 |
> |
int xstep |
539 |
> |
) |
540 |
|
{ |
541 |
|
static int nc = 0; /* number of calls */ |
542 |
|
int bl = xstep, b = xstep; |
543 |
|
double z; |
544 |
< |
register int i; |
544 |
> |
int i; |
545 |
|
|
546 |
|
z = pixvalue(scanline[0], 0, y); |
547 |
|
if (zline) zline[0] = z; |
567 |
|
} |
568 |
|
|
569 |
|
|
570 |
< |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
571 |
< |
register COLOR *scanbar[]; |
572 |
< |
register float *zbar[]; |
573 |
< |
int xres, y, ysize; |
570 |
> |
static void |
571 |
> |
fillscanbar( /* fill interior */ |
572 |
> |
COLOR *scanbar[], |
573 |
> |
float *zbar[], |
574 |
> |
int xres, |
575 |
> |
int y, |
576 |
> |
int ysize |
577 |
> |
) |
578 |
|
{ |
579 |
|
COLOR vline[MAXDIV+1]; |
580 |
|
float zline[MAXDIV+1]; |
581 |
|
int b = ysize; |
582 |
< |
register int i, j; |
582 |
> |
int i, j; |
583 |
|
|
584 |
|
for (i = 0; i < xres; i++) { |
585 |
|
copycolor(vline[0], scanbar[0][i]); |
600 |
|
} |
601 |
|
|
602 |
|
|
603 |
< |
int |
604 |
< |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
605 |
< |
register COLOR *colline; |
606 |
< |
register float *zline; |
607 |
< |
int x, y; |
608 |
< |
int xlen, ylen; |
609 |
< |
int b; |
603 |
> |
static int |
604 |
> |
fillsample( /* fill interior points */ |
605 |
> |
COLOR *colline, |
606 |
> |
float *zline, |
607 |
> |
int x, |
608 |
> |
int y, |
609 |
> |
int xlen, |
610 |
> |
int ylen, |
611 |
> |
int b |
612 |
> |
) |
613 |
|
{ |
614 |
|
double ratio; |
615 |
|
double z; |
616 |
|
COLOR ctmp; |
617 |
|
int ncut; |
618 |
< |
register int len; |
618 |
> |
int len; |
619 |
|
|
620 |
|
if (xlen > 0) /* x or y length is zero */ |
621 |
|
len = xlen; |
656 |
|
} |
657 |
|
|
658 |
|
|
659 |
< |
double |
660 |
< |
pixvalue(col, x, y) /* compute pixel value */ |
661 |
< |
COLOR col; /* returned color */ |
662 |
< |
int x, y; /* pixel position */ |
659 |
> |
static double |
660 |
> |
pixvalue( /* compute pixel value */ |
661 |
> |
COLOR col, /* returned color */ |
662 |
> |
int x, /* pixel position */ |
663 |
> |
int y |
664 |
> |
) |
665 |
|
{ |
666 |
+ |
extern void SDsquare2disk(double ds[2], double seedx, double seedy); |
667 |
|
RAY thisray; |
668 |
|
FVECT lorg, ldir; |
669 |
< |
double hpos, vpos, lmax, d; |
669 |
> |
double hpos, vpos, vdist, lmax; |
670 |
> |
int i; |
671 |
|
/* compute view ray */ |
672 |
+ |
setcolor(col, 0.0, 0.0, 0.0); |
673 |
|
hpos = (x+pixjitter())/hres; |
674 |
|
vpos = (y+pixjitter())/vres; |
675 |
|
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
676 |
< |
&ourview, hpos, vpos)) < -FTINY) { |
637 |
< |
setcolor(col, 0.0, 0.0, 0.0); |
676 |
> |
&ourview, hpos, vpos)) < -FTINY) |
677 |
|
return(0.0); |
639 |
– |
} |
678 |
|
|
679 |
< |
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
680 |
< |
|
679 |
> |
vdist = ourview.vdist; |
680 |
> |
/* set pixel index */ |
681 |
> |
samplendx = pixnumber(x,y,hres,vres); |
682 |
|
/* optional motion blur */ |
683 |
|
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
684 |
|
&lastview, hpos, vpos)) >= -FTINY) { |
685 |
< |
register int i; |
647 |
< |
register double d = mblur*(.5-urand(281+samplendx)); |
685 |
> |
double d = mblur*(.5-urand(281+samplendx)); |
686 |
|
|
687 |
|
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
688 |
|
for (i = 3; i--; ) { |
691 |
|
} |
692 |
|
if (normalize(thisray.rdir) == 0.0) |
693 |
|
return(0.0); |
694 |
+ |
vdist = (1.-d)*vdist + d*lastview.vdist; |
695 |
|
} |
696 |
+ |
/* optional depth-of-field */ |
697 |
+ |
if (dblur > FTINY) { |
698 |
+ |
double vc, df[2]; |
699 |
+ |
/* random point on disk */ |
700 |
+ |
SDsquare2disk(df, frandom(), frandom()); |
701 |
+ |
df[0] *= .5*dblur; |
702 |
+ |
df[1] *= .5*dblur; |
703 |
+ |
if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) { |
704 |
+ |
double adj = 1.0; |
705 |
+ |
if (ourview.type == VT_PER) |
706 |
+ |
adj /= DOT(thisray.rdir, ourview.vdir); |
707 |
+ |
df[0] /= sqrt(ourview.hn2); |
708 |
+ |
df[1] /= sqrt(ourview.vn2); |
709 |
+ |
for (i = 3; i--; ) { |
710 |
+ |
vc = ourview.vp[i] + adj*vdist*thisray.rdir[i]; |
711 |
+ |
thisray.rorg[i] += df[0]*ourview.hvec[i] + |
712 |
+ |
df[1]*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 |
+ |
df[0] /= sqrt(ourview.hn2); |
720 |
+ |
df[1] /= sqrt(ourview.vn2); |
721 |
+ |
} |
722 |
+ |
for (i = 3; i--; ) { |
723 |
+ |
vc = ourview.vp[i] + vdist*thisray.rdir[i]; |
724 |
+ |
thisray.rorg[i] += df[0]*ourview.hvec[i] + |
725 |
+ |
df[1]*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; |
770 |
|
goto gotzip; |
771 |
|
} |
772 |
|
|
773 |
< |
if (x != hres || y != vres) { |
773 |
> |
if ((x != hres) | (y != vres)) { |
774 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
775 |
|
oldfile); |
776 |
|
error(USER, errmsg); |
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 (screen door) */ |
806 |
> |
int x, |
807 |
> |
int y, |
808 |
> |
int xres, |
809 |
> |
int yres |
810 |
> |
) |
811 |
|
{ |
812 |
< |
x -= y; |
813 |
< |
while (x < 0) |
814 |
< |
x += xres; |
815 |
< |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
812 |
> |
unsigned nbits = 0; |
813 |
> |
bitmask_t coord[2]; |
814 |
> |
|
815 |
> |
if (xres < yres) xres = yres; |
816 |
> |
while (xres > 0) { |
817 |
> |
xres >>= 1; |
818 |
> |
++nbits; |
819 |
> |
} |
820 |
> |
coord[0] = x; coord[1] = y; |
821 |
> |
return ((int)hilbert_c2i(2, nbits, coord)); |
822 |
|
} |