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 "copyright.h" |
9 |
+ |
|
10 |
|
#include "ray.h" |
11 |
|
|
12 |
|
#include <sys/types.h> |
27 |
|
|
28 |
|
#include "view.h" |
29 |
|
|
33 |
– |
#include "resolu.h" |
34 |
– |
|
30 |
|
#include "random.h" |
31 |
|
|
32 |
|
#include "paths.h" |
37 |
|
#define SIGCONT SIGIO |
38 |
|
#endif |
39 |
|
|
40 |
+ |
CUBE thescene; /* our scene */ |
41 |
+ |
OBJECT nsceneobjs; /* number of objects in our scene */ |
42 |
+ |
|
43 |
|
int dimlist[MAXDIM]; /* sampling dimensions */ |
44 |
|
int ndims = 0; /* number of sampling dimensions */ |
45 |
|
int samplendx; /* sample index number */ |
46 |
|
|
47 |
+ |
extern void ambnotify(); |
48 |
+ |
void (*addobjnotify[])() = {ambnotify, NULL}; |
49 |
+ |
|
50 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
51 |
|
int hresolu = 512; /* horizontal resolution */ |
52 |
|
int vresolu = 512; /* vertical resolution */ |
56 |
|
double maxdiff = .05; /* max. difference for interpolation */ |
57 |
|
double dstrpix = 0.67; /* square pixel distribution */ |
58 |
|
|
59 |
+ |
double mblur = 0.; /* motion blur parameter */ |
60 |
+ |
|
61 |
+ |
void (*trace)() = NULL; /* trace call */ |
62 |
+ |
|
63 |
+ |
int do_irrad = 0; /* compute irradiance? */ |
64 |
+ |
|
65 |
|
double dstrsrc = 0.0; /* square source distribution */ |
66 |
|
double shadthresh = .05; /* shadow threshold */ |
67 |
|
double shadcert = .5; /* shadow certainty */ |
71 |
|
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
72 |
|
|
73 |
|
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
74 |
< |
double salbedo = 0.; /* global scattering albedo */ |
74 |
> |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
75 |
|
double seccg = 0.; /* global scattering eccentricity */ |
76 |
|
double ssampdist = 0.; /* scatter sampling distance */ |
77 |
|
|
83 |
|
int maxdepth = 6; /* maximum recursion depth */ |
84 |
|
double minweight = 5e-3; /* minimum ray weight */ |
85 |
|
|
86 |
+ |
char *ambfile = NULL; /* ambient file name */ |
87 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
88 |
+ |
int ambvwt = 0; /* initial weight for ambient value */ |
89 |
|
double ambacc = 0.2; /* ambient accuracy */ |
90 |
|
int ambres = 32; /* ambient resolution */ |
91 |
|
int ambdiv = 128; /* ambient divisions */ |
94 |
|
char *amblist[128]; /* ambient include/exclude list */ |
95 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
96 |
|
|
88 |
– |
#ifdef MSDOS |
89 |
– |
int ralrm = 60; /* seconds between reports */ |
90 |
– |
#else |
97 |
|
int ralrm = 0; /* seconds between reports */ |
92 |
– |
#endif |
98 |
|
|
99 |
|
double pctdone = 0.0; /* percentage done */ |
95 |
– |
|
100 |
|
time_t tlastrept = 0L; /* time at last report */ |
101 |
+ |
time_t tstart; /* starting time */ |
102 |
|
|
98 |
– |
extern time_t tstart; /* starting time */ |
99 |
– |
|
100 |
– |
extern unsigned long nrays; /* number of rays traced */ |
101 |
– |
|
103 |
|
#define MAXDIV 16 /* maximum sample size */ |
104 |
|
|
105 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
106 |
|
|
107 |
< |
static int hres, vres; /* resolution for this frame */ |
107 |
> |
int hres, vres; /* resolution for this frame */ |
108 |
|
|
109 |
+ |
static VIEW lastview; /* the previous view input */ |
110 |
+ |
|
111 |
|
extern char *mktemp(); |
112 |
|
|
113 |
+ |
void report(); |
114 |
+ |
|
115 |
|
double pixvalue(); |
116 |
|
|
117 |
|
#ifdef NIX |
130 |
|
#endif |
131 |
|
|
132 |
|
|
133 |
+ |
void |
134 |
|
quit(code) /* quit program */ |
135 |
|
int code; |
136 |
|
{ |
137 |
|
if (code) /* report status */ |
138 |
|
report(); |
139 |
+ |
#ifndef NIX |
140 |
|
headclean(); /* delete header file */ |
141 |
|
pfclean(); /* clean up persist files */ |
142 |
+ |
#endif |
143 |
|
exit(code); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
#ifndef NIX |
148 |
+ |
void |
149 |
|
report() /* report progress */ |
150 |
|
{ |
151 |
|
extern char *myhostname(); |
186 |
|
#endif |
187 |
|
} |
188 |
|
#else |
189 |
+ |
void |
190 |
|
report() /* report progress */ |
191 |
|
{ |
192 |
|
tlastrept = time((time_t *)NULL); |
197 |
|
#endif |
198 |
|
|
199 |
|
|
200 |
+ |
void |
201 |
|
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
202 |
|
int seq; |
203 |
|
char *pout, *zout, *prvr; |
240 |
|
for ( ; seq < rn; seq++) |
241 |
|
if (nextview(stdin) == EOF) |
242 |
|
error(USER, "unexpected EOF on view input"); |
243 |
+ |
setview(&ourview); |
244 |
|
prvr = fbuf; /* mark for renaming */ |
245 |
|
} |
246 |
|
if (pout != NULL & prvr != NULL) { |
282 |
|
fbuf); |
283 |
|
error(USER, errmsg); |
284 |
|
} |
285 |
+ |
setview(&ourview); |
286 |
|
continue; /* don't clobber */ |
287 |
|
} |
288 |
|
if (freopen(fbuf, "w", stdout) == NULL) { |
298 |
|
hres = hresolu; vres = vresolu; pa = pixaspect; |
299 |
|
if (prvr != NULL) |
300 |
|
if (viewfile(prvr, &ourview, &rs) <= 0 |
301 |
< |
|| rs.or != PIXSTANDARD) { |
301 |
> |
|| rs.rt != PIXSTANDARD) { |
302 |
|
sprintf(errmsg, |
303 |
|
"cannot recover view parameters from \"%s\"", prvr); |
304 |
|
error(WARNING, errmsg); |
345 |
|
{ |
346 |
|
char linebuf[256]; |
347 |
|
|
348 |
+ |
copystruct(&lastview, &ourview); |
349 |
|
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) |
350 |
|
if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) |
351 |
|
return(0); |
356 |
|
render(zfile, oldfile) /* render the scene */ |
357 |
|
char *zfile, *oldfile; |
358 |
|
{ |
345 |
– |
extern long lseek(); |
359 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
360 |
|
float *zbar[MAXDIV+1]; /* z values */ |
361 |
|
char *sampdens; /* previous sample density */ |
366 |
|
COLOR *colptr; |
367 |
|
float *zptr; |
368 |
|
register int i; |
369 |
+ |
/* check for empty image */ |
370 |
+ |
if (hres <= 0 || vres <= 0) { |
371 |
+ |
error(WARNING, "empty output picture"); |
372 |
+ |
fprtresolu(0, 0, stdout); |
373 |
+ |
return; |
374 |
+ |
} |
375 |
|
/* allocate scanlines */ |
376 |
|
for (i = 0; i <= psample; i++) { |
377 |
|
scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR)); |
382 |
|
ystep = (psample*99+70)/140; |
383 |
|
if (hstep > 2) { |
384 |
|
i = hres/hstep + 2; |
385 |
< |
if ((sampdens = malloc(i)) == NULL) |
385 |
> |
if ((sampdens = (char *)malloc(i)) == NULL) |
386 |
|
goto memerr; |
387 |
|
while (i--) |
388 |
|
sampdens[i] = hstep; |
414 |
|
if (i >= vres) |
415 |
|
goto alldone; |
416 |
|
if (zfd != -1 && i > 0 && |
417 |
< |
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
417 |
> |
lseek(zfd, (off_t)i*hres*sizeof(float), 0) < 0) |
418 |
|
error(SYSTEM, "z-file seek error in render"); |
419 |
|
pctdone = 100.0*i/vres; |
420 |
|
if (ralrm > 0) /* report init stats */ |
423 |
|
else |
424 |
|
#endif |
425 |
|
signal(SIGCONT, report); |
426 |
< |
ypos = vres-1 - i; |
426 |
> |
ypos = vres-1 - i; /* initialize sampling */ |
427 |
> |
if (directvis) |
428 |
> |
init_drawsources(psample); |
429 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
430 |
|
/* compute scanlines */ |
431 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
445 |
|
hres, ypos, hstep); |
446 |
|
/* fill bar */ |
447 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
448 |
+ |
if (directvis) /* add bitty sources */ |
449 |
+ |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
450 |
|
/* write it out */ |
451 |
|
#ifndef BSD |
452 |
|
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
483 |
|
if (close(zfd) == -1) |
484 |
|
goto writerr; |
485 |
|
for (i = 0; i <= psample; i++) |
486 |
< |
free((char *)zbar[i]); |
486 |
> |
free((void *)zbar[i]); |
487 |
|
} |
488 |
|
for (i = 0; i <= psample; i++) |
489 |
< |
free((char *)scanbar[i]); |
489 |
> |
free((void *)scanbar[i]); |
490 |
|
if (sampdens != NULL) |
491 |
|
free(sampdens); |
492 |
|
pctdone = 100.0; |
511 |
|
int bl = xstep, b = xstep; |
512 |
|
double z; |
513 |
|
register int i; |
514 |
< |
|
514 |
> |
|
515 |
|
z = pixvalue(scanline[0], 0, y); |
516 |
|
if (zline) zline[0] = z; |
517 |
|
/* zig-zag start for quincunx pattern */ |
545 |
|
float zline[MAXDIV+1]; |
546 |
|
int b = ysize; |
547 |
|
register int i, j; |
548 |
< |
|
548 |
> |
|
549 |
|
for (i = 0; i < xres; i++) { |
527 |
– |
|
550 |
|
copycolor(vline[0], scanbar[0][i]); |
551 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
552 |
|
if (zbar[0]) { |
553 |
|
zline[0] = zbar[0][i]; |
554 |
|
zline[ysize] = zbar[ysize][i]; |
555 |
|
} |
534 |
– |
|
556 |
|
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
557 |
|
i, y, 0, ysize, b/2); |
558 |
< |
|
558 |
> |
|
559 |
|
for (j = 1; j < ysize; j++) |
560 |
|
copycolor(scanbar[j][i], vline[j]); |
561 |
|
if (zbar[0]) |
578 |
|
COLOR ctmp; |
579 |
|
int ncut; |
580 |
|
register int len; |
581 |
< |
|
581 |
> |
|
582 |
|
if (xlen > 0) /* x or y length is zero */ |
583 |
|
len = xlen; |
584 |
|
else |
585 |
|
len = ylen; |
586 |
< |
|
586 |
> |
|
587 |
|
if (len <= 1) /* limit recursion */ |
588 |
|
return(0); |
589 |
< |
|
590 |
< |
if (b > 0 |
591 |
< |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
589 |
> |
|
590 |
> |
if (b > 0 || |
591 |
> |
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
592 |
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
593 |
< |
|
593 |
> |
|
594 |
|
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
595 |
|
if (zline) zline[len>>1] = z; |
596 |
|
ncut = 1; |
576 |
– |
|
597 |
|
} else { /* interpolate */ |
578 |
– |
|
598 |
|
copycolor(colline[len>>1], colline[len]); |
599 |
|
ratio = (double)(len>>1) / len; |
600 |
|
scalecolor(colline[len>>1], ratio); |
608 |
|
} |
609 |
|
/* recurse */ |
610 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
611 |
< |
|
611 |
> |
|
612 |
|
ncut += fillsample(colline+(len>>1), |
613 |
|
zline ? zline+(len>>1) : (float *)NULL, |
614 |
|
x+(xlen>>1), y+(ylen>>1), |
623 |
|
COLOR col; /* returned color */ |
624 |
|
int x, y; /* pixel position */ |
625 |
|
{ |
626 |
< |
static RAY thisray; |
627 |
< |
|
628 |
< |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview, |
629 |
< |
(x+pixjitter())/hres, (y+pixjitter())/vres)) < -FTINY) { |
626 |
> |
RAY thisray; |
627 |
> |
FVECT lorg, ldir; |
628 |
> |
double hpos, vpos, lmax, d; |
629 |
> |
/* compute view ray */ |
630 |
> |
hpos = (x+pixjitter())/hres; |
631 |
> |
vpos = (y+pixjitter())/vres; |
632 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
633 |
> |
&ourview, hpos, vpos)) < -FTINY) { |
634 |
|
setcolor(col, 0.0, 0.0, 0.0); |
635 |
|
return(0.0); |
636 |
|
} |
637 |
|
|
615 |
– |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
616 |
– |
|
638 |
|
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
639 |
|
|
640 |
+ |
/* optional motion blur */ |
641 |
+ |
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
642 |
+ |
&lastview, hpos, vpos)) >= -FTINY) { |
643 |
+ |
register int i; |
644 |
+ |
register double d = mblur*(.5-urand(281+samplendx)); |
645 |
+ |
|
646 |
+ |
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
647 |
+ |
for (i = 3; i--; ) { |
648 |
+ |
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; |
649 |
+ |
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; |
650 |
+ |
} |
651 |
+ |
if (normalize(thisray.rdir) == 0.0) |
652 |
+ |
return(0.0); |
653 |
+ |
} |
654 |
+ |
|
655 |
+ |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
656 |
+ |
|
657 |
|
rayvalue(&thisray); /* trace ray */ |
658 |
|
|
659 |
|
copycolor(col, thisray.rcol); /* return color */ |
660 |
< |
|
660 |
> |
|
661 |
|
return(thisray.rt); /* return distance */ |
662 |
|
} |
663 |
|
|
709 |
|
} |
710 |
|
if (fflush(stdout) == EOF) |
711 |
|
goto writerr; |
712 |
< |
free((char *)scanline); |
712 |
> |
free((void *)scanline); |
713 |
|
fclose(fp); |
714 |
|
unlink(oldfile); |
715 |
|
return(y); |