ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.16
Committed: Tue Oct 6 12:30:07 1992 UTC (31 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +4 -0 lines
Log Message:
Changes for 32-bit PC port

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * rpict.c - routines and variables for picture generation.
9 *
10 * 8/14/85
11 */
12
13 #include "ray.h"
14
15 #ifdef BSD
16 #include <sys/time.h>
17 #include <sys/resource.h>
18 #endif
19
20 #include <signal.h>
21
22 #include "view.h"
23
24 #include "resolu.h"
25
26 #include "random.h"
27
28 #include "paths.h"
29
30 int dimlist[MAXDIM]; /* sampling dimensions */
31 int ndims = 0; /* number of sampling dimensions */
32 int samplendx; /* sample index number */
33
34 VIEW ourview = STDVIEW; /* view parameters */
35 int hresolu = 512; /* horizontal resolution */
36 int vresolu = 512; /* vertical resolution */
37 double pixaspect = 1.0; /* pixel aspect ratio */
38
39 int psample = 4; /* pixel sample size */
40 double maxdiff = .05; /* max. difference for interpolation */
41 double dstrpix = 0.67; /* square pixel distribution */
42
43 double dstrsrc = 0.0; /* square source distribution */
44 double shadthresh = .05; /* shadow threshold */
45 double shadcert = .5; /* shadow certainty */
46 int directrelay = 1; /* number of source relays */
47 int vspretest = 512; /* virtual source pretest density */
48 int directinvis = 0; /* sources invisible? */
49 double srcsizerat = .25; /* maximum ratio source size/dist. */
50
51 double specthresh = .15; /* specular sampling threshold */
52 double specjitter = 1.; /* specular sampling jitter */
53
54 int maxdepth = 6; /* maximum recursion depth */
55 double minweight = 5e-3; /* minimum ray weight */
56
57 COLOR ambval = BLKCOLOR; /* ambient value */
58 double ambacc = 0.2; /* ambient accuracy */
59 int ambres = 32; /* ambient resolution */
60 int ambdiv = 128; /* ambient divisions */
61 int ambssamp = 0; /* ambient super-samples */
62 int ambounce = 0; /* ambient bounces */
63 char *amblist[128]; /* ambient include/exclude list */
64 int ambincl = -1; /* include == 1, exclude == 0 */
65
66 #ifdef MSDOS
67 int ralrm = 60; /* seconds between reports */
68 #else
69 int ralrm = 0; /* seconds between reports */
70 #endif
71
72 double pctdone = 0.0; /* percentage done */
73
74 long tlastrept = 0L; /* time at last report */
75
76 extern long time();
77 extern long tstart; /* starting time */
78
79 extern long nrays; /* number of rays traced */
80
81 #define MAXDIV 16 /* maximum sample size */
82
83 #define pixjitter() (.5+dstrpix*(.5-frandom()))
84
85 #define RFTEMPLATE "rfXXXXXX"
86 #define HFTEMPLATE TEMPLATE
87
88 static char *hfname = NULL; /* header file name */
89 static FILE *hfp = NULL; /* header file pointer */
90
91 static int hres, vres; /* resolution for this frame */
92
93 extern char *mktemp();
94
95 double pixvalue();
96
97
98 quit(code) /* quit program */
99 int code;
100 {
101 if (code) /* report status */
102 report();
103 if (hfname != NULL) { /* delete header file */
104 if (hfp != NULL)
105 fclose(hfp);
106 unlink(hfname);
107 }
108 exit(code);
109 }
110
111
112 #ifdef BSD
113 report() /* report progress */
114 {
115 struct rusage rubuf;
116 double t;
117
118 getrusage(RUSAGE_SELF, &rubuf);
119 t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
120 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
121 getrusage(RUSAGE_CHILDREN, &rubuf);
122 t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
123 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
124
125 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
126 nrays, pctdone, t/3600.0);
127 eputs(errmsg);
128 tlastrept = time((long *)0);
129 }
130 #else
131 report() /* report progress */
132 {
133 tlastrept = time((long *)0);
134 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
135 nrays, pctdone, (tlastrept-tstart)/3600.0);
136 eputs(errmsg);
137 signal(SIGALRM, report);
138 }
139 #endif
140
141
142 openheader() /* save standard output to header file */
143 {
144 hfname = mktemp(HFTEMPLATE);
145 if (freopen(hfname, "w", stdout) == NULL) {
146 sprintf(errmsg, "cannot open header file \"%s\"", hfname);
147 error(SYSTEM, errmsg);
148 }
149 }
150
151
152 closeheader() /* done with header output */
153 {
154 if (hfname == NULL)
155 return;
156 if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL)
157 error(SYSTEM, "error reopening header file");
158 }
159
160
161 dupheader() /* repeat header on standard output */
162 {
163 register int c;
164
165 if (fseek(hfp, 0L, 0) < 0)
166 error(SYSTEM, "seek error on header file");
167 while ((c = getc(hfp)) != EOF)
168 putchar(c);
169 }
170
171
172 rpict(seq, pout, zout, prvr) /* generate image(s) */
173 int seq;
174 char *pout, *zout, *prvr;
175 /*
176 * If seq is greater than zero, then we will render a sequence of
177 * images based on view parameter strings read from the standard input.
178 * If pout is NULL, then all images will be sent to the standard ouput.
179 * If seq is greater than zero and prvr is an integer, then it is the
180 * frame number at which rendering should begin. Preceeding view parameter
181 * strings will be skipped in the input.
182 * If pout and prvr are the same, prvr is renamed to avoid overwriting.
183 * Note that pout and zout should contain %d format specifications for
184 * sequenced file naming.
185 */
186 {
187 extern char *rindex(), *strncpy(), *strcat();
188 char fbuf[128], fbuf2[128];
189 register char *cp;
190 RESOLU rs;
191 double pa;
192 /* finished writing header */
193 closeheader();
194 /* check sampling */
195 if (psample < 1)
196 psample = 1;
197 else if (psample > MAXDIV) {
198 sprintf(errmsg, "pixel sampling reduced from %d to %d",
199 psample, MAXDIV);
200 error(WARNING, errmsg);
201 psample = MAXDIV;
202 }
203 /* get starting frame */
204 if (seq <= 0)
205 seq = 0;
206 else if (prvr != NULL && isint(prvr)) {
207 register int rn; /* skip to specified view */
208 if ((rn = atoi(prvr)) < seq)
209 error(USER, "recover frame less than start frame");
210 if (pout == NULL)
211 error(USER, "missing output file specification");
212 for ( ; seq < rn; seq++)
213 if (nextview(stdin) == EOF)
214 error(USER, "unexpected EOF on view input");
215 prvr = fbuf; /* mark for renaming */
216 }
217 if (pout != NULL) {
218 sprintf(fbuf, pout, seq);
219 if (prvr != NULL && !strcmp(prvr, fbuf)) { /* rename */
220 fbuf2[0] = '\0';
221 if ((cp = rindex(fbuf, '/')) != NULL)
222 strncpy(fbuf2, fbuf, cp-fbuf+1);
223 strcat(fbuf2, RFTEMPLATE);
224 prvr = mktemp(fbuf2);
225 if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
226 sprintf(errmsg,
227 "cannot rename \"%s\" to \"%s\"",
228 fbuf, prvr);
229 error(SYSTEM, errmsg);
230 }
231 }
232 }
233 /* render sequence */
234 do {
235 if (seq && nextview(stdin) == EOF)
236 break;
237 if (pout != NULL) {
238 sprintf(fbuf, pout, seq);
239 if (freopen(fbuf, "w", stdout) == NULL) {
240 sprintf(errmsg,
241 "cannot open output file \"%s\"", fbuf);
242 error(SYSTEM, errmsg);
243 }
244 dupheader();
245 }
246 hres = hresolu; vres = vresolu; pa = pixaspect;
247 if (prvr != NULL)
248 if (viewfile(prvr, &ourview, &rs) <= 0
249 || rs.or != PIXSTANDARD) {
250 sprintf(errmsg,
251 "cannot recover view parameters from \"%s\"", prvr);
252 error(WARNING, errmsg);
253 } else {
254 pa = 0.0;
255 hres = scanlen(&rs);
256 vres = numscans(&rs);
257 }
258 if ((cp = setview(&ourview)) != NULL)
259 error(USER, cp);
260 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
261 if (seq) {
262 if (ralrm > 0) {
263 fprintf(stderr, "FRAME %d:", seq);
264 fprintview(&ourview, stderr);
265 putc('\n', stderr);
266 fflush(stderr);
267 }
268 printf("FRAME=%d\n", seq);
269 }
270 fputs(VIEWSTR, stdout);
271 fprintview(&ourview, stdout);
272 putchar('\n');
273 if (pa < .99 || pa > 1.01)
274 fputaspect(pa, stdout);
275 fputformat(COLRFMT, stdout);
276 putchar('\n');
277 if (zout != NULL)
278 sprintf(cp=fbuf, zout, seq);
279 else
280 cp = NULL;
281 render(cp, prvr);
282 prvr = NULL;
283 } while (seq++);
284 }
285
286
287 nextview(fp) /* get next view from fp */
288 FILE *fp;
289 {
290 char linebuf[256];
291
292 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
293 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
294 return(0);
295 return(EOF);
296 }
297
298
299 render(zfile, oldfile) /* render the scene */
300 char *zfile, *oldfile;
301 {
302 extern long lseek();
303 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
304 float *zbar[MAXDIV+1]; /* z values */
305 char *sampdens; /* previous sample density */
306 int ypos; /* current scanline */
307 int ystep; /* current y step size */
308 int hstep; /* h step size */
309 int zfd;
310 COLOR *colptr;
311 float *zptr;
312 register int i;
313 /* allocate scanlines */
314 for (i = 0; i <= psample; i++) {
315 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
316 if (scanbar[i] == NULL)
317 goto memerr;
318 }
319 hstep = (psample*140+49)/99; /* quincunx sampling */
320 ystep = (psample*99+70)/140;
321 if (hstep > 2) {
322 i = hres/hstep + 2;
323 if ((sampdens = malloc(i)) == NULL)
324 goto memerr;
325 while (i--)
326 sampdens[i] = hstep;
327 } else
328 sampdens = NULL;
329 /* open z file */
330 if (zfile != NULL) {
331 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
332 sprintf(errmsg, "cannot open z file \"%s\"", zfile);
333 error(SYSTEM, errmsg);
334 }
335 for (i = 0; i <= psample; i++) {
336 zbar[i] = (float *)malloc(hres*sizeof(float));
337 if (zbar[i] == NULL)
338 goto memerr;
339 }
340 } else {
341 zfd = -1;
342 for (i = 0; i <= psample; i++)
343 zbar[i] = NULL;
344 }
345 /* write out boundaries */
346 fprtresolu(hres, vres, stdout);
347 /* recover file and compute first */
348 i = salvage(oldfile);
349 if (zfd != -1 && i > 0 &&
350 lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
351 error(SYSTEM, "z file seek error in render");
352 pctdone = 100.0*i/vres;
353 if (ralrm > 0) /* report init stats */
354 report();
355 #ifndef BSD
356 else
357 #endif
358 signal(SIGALRM, report);
359 ypos = vres-1 - i;
360 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
361 /* compute scanlines */
362 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
363 /* bottom adjust? */
364 if (ypos < 0) {
365 ystep += ypos;
366 ypos = 0;
367 }
368 colptr = scanbar[ystep]; /* move base to top */
369 scanbar[ystep] = scanbar[0];
370 scanbar[0] = colptr;
371 zptr = zbar[ystep];
372 zbar[ystep] = zbar[0];
373 zbar[0] = zptr;
374 /* fill base line */
375 fillscanline(scanbar[0], zbar[0], sampdens,
376 hres, ypos, hstep);
377 /* fill bar */
378 fillscanbar(scanbar, zbar, hres, ypos, ystep);
379 /* write it out */
380 #ifndef BSD
381 signal(SIGALRM, SIG_IGN); /* don't interrupt writes */
382 #endif
383 for (i = ystep; i > 0; i--) {
384 if (zfd != -1 && write(zfd, (char *)zbar[i],
385 hres*sizeof(float))
386 < hres*sizeof(float))
387 goto writerr;
388 if (fwritescan(scanbar[i], hres, stdout) < 0)
389 goto writerr;
390 }
391 if (fflush(stdout) == EOF)
392 goto writerr;
393 /* record progress */
394 pctdone = 100.0*(vres-1-ypos)/vres;
395 if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
396 report();
397 #ifndef BSD
398 else
399 signal(SIGALRM, report);
400 #endif
401 }
402 /* clean up */
403 signal(SIGALRM, SIG_IGN);
404 if (zfd != -1) {
405 if (write(zfd, (char *)zbar[0], hres*sizeof(float))
406 < hres*sizeof(float))
407 goto writerr;
408 if (close(zfd) == -1)
409 goto writerr;
410 for (i = 0; i <= psample; i++)
411 free((char *)zbar[i]);
412 }
413 fwritescan(scanbar[0], hres, stdout);
414 if (fflush(stdout) == EOF)
415 goto writerr;
416 for (i = 0; i <= psample; i++)
417 free((char *)scanbar[i]);
418 if (sampdens != NULL)
419 free(sampdens);
420 pctdone = 100.0;
421 if (ralrm > 0)
422 report();
423 return;
424 writerr:
425 error(SYSTEM, "write error in render");
426 memerr:
427 error(SYSTEM, "out of memory in render");
428 }
429
430
431 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
432 register COLOR *scanline;
433 register float *zline;
434 register char *sd;
435 int xres, y, xstep;
436 {
437 static int nc = 0; /* number of calls */
438 int bl = xstep, b = xstep;
439 double z;
440 register int i;
441
442 z = pixvalue(scanline[0], 0, y);
443 if (zline) zline[0] = z;
444 /* zig-zag start for quincunx pattern */
445 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
446 if (i >= xres) {
447 xstep += xres-1-i;
448 i = xres-1;
449 }
450 z = pixvalue(scanline[i], i, y);
451 if (zline) zline[i] = z;
452 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
453 if (i <= xstep)
454 b = fillsample(scanline, zline, 0, y, i, 0, b/2);
455 else
456 b = fillsample(scanline+i-xstep,
457 zline ? zline+i-xstep : NULL,
458 i-xstep, y, xstep, 0, b/2);
459 if (sd) *sd++ = nc & 1 ? bl : b;
460 bl = b;
461 }
462 if (sd && nc & 1) *sd = bl;
463 }
464
465
466 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
467 register COLOR *scanbar[];
468 register float *zbar[];
469 int xres, y, ysize;
470 {
471 COLOR vline[MAXDIV+1];
472 float zline[MAXDIV+1];
473 int b = ysize;
474 register int i, j;
475
476 for (i = 0; i < xres; i++) {
477
478 copycolor(vline[0], scanbar[0][i]);
479 copycolor(vline[ysize], scanbar[ysize][i]);
480 if (zbar[0]) {
481 zline[0] = zbar[0][i];
482 zline[ysize] = zbar[ysize][i];
483 }
484
485 b = fillsample(vline, zbar[0] ? zline : NULL,
486 i, y, 0, ysize, b/2);
487
488 for (j = 1; j < ysize; j++)
489 copycolor(scanbar[j][i], vline[j]);
490 if (zbar[0])
491 for (j = 1; j < ysize; j++)
492 zbar[j][i] = zline[j];
493 }
494 }
495
496
497 int
498 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
499 register COLOR *colline;
500 register float *zline;
501 int x, y;
502 int xlen, ylen;
503 int b;
504 {
505 double ratio;
506 double z;
507 COLOR ctmp;
508 int ncut;
509 register int len;
510
511 if (xlen > 0) /* x or y length is zero */
512 len = xlen;
513 else
514 len = ylen;
515
516 if (len <= 1) /* limit recursion */
517 return(0);
518
519 if (b > 0
520 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
521 || bigdiff(colline[0], colline[len], maxdiff)) {
522
523 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
524 if (zline) zline[len>>1] = z;
525 ncut = 1;
526
527 } else { /* interpolate */
528
529 copycolor(colline[len>>1], colline[len]);
530 ratio = (double)(len>>1) / len;
531 scalecolor(colline[len>>1], ratio);
532 if (zline) zline[len>>1] = zline[len] * ratio;
533 ratio = 1.0 - ratio;
534 copycolor(ctmp, colline[0]);
535 scalecolor(ctmp, ratio);
536 addcolor(colline[len>>1], ctmp);
537 if (zline) zline[len>>1] += zline[0] * ratio;
538 ncut = 0;
539 }
540 /* recurse */
541 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
542
543 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
544 x+(xlen>>1), y+(ylen>>1),
545 xlen-(xlen>>1), ylen-(ylen>>1), b/2);
546
547 return(ncut);
548 }
549
550
551 double
552 pixvalue(col, x, y) /* compute pixel value */
553 COLOR col; /* returned color */
554 int x, y; /* pixel position */
555 {
556 static RAY thisray;
557
558 if (viewray(thisray.rorg, thisray.rdir, &ourview,
559 (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
560 setcolor(col, 0.0, 0.0, 0.0);
561 return(0.0);
562 }
563
564 rayorigin(&thisray, NULL, PRIMARY, 1.0);
565
566 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
567
568 rayvalue(&thisray); /* trace ray */
569
570 copycolor(col, thisray.rcol); /* return color */
571
572 return(thisray.rt); /* return distance */
573 }
574
575
576 int
577 salvage(oldfile) /* salvage scanlines from killed program */
578 char *oldfile;
579 {
580 COLR *scanline;
581 FILE *fp;
582 int x, y;
583
584 if (oldfile == NULL)
585 return(0);
586
587 if ((fp = fopen(oldfile, "r")) == NULL) {
588 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
589 error(WARNING, errmsg);
590 return(0);
591 }
592 /* discard header */
593 getheader(fp, NULL);
594 /* get picture size */
595 if (!fscnresolu(&x, &y, fp)) {
596 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
597 error(WARNING, errmsg);
598 fclose(fp);
599 return(0);
600 }
601
602 if (x != hres || y != vres) {
603 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
604 oldfile);
605 error(USER, errmsg);
606 }
607
608 scanline = (COLR *)malloc(hres*sizeof(COLR));
609 if (scanline == NULL)
610 error(SYSTEM, "out of memory in salvage");
611 for (y = 0; y < vres; y++) {
612 if (freadcolrs(scanline, hres, fp) < 0)
613 break;
614 if (fwritecolrs(scanline, hres, stdout) < 0)
615 goto writerr;
616 }
617 if (fflush(stdout) == EOF)
618 goto writerr;
619 free((char *)scanline);
620 fclose(fp);
621 unlink(oldfile);
622 return(y);
623 writerr:
624 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
625 error(SYSTEM, errmsg);
626 }
627
628
629 int
630 pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
631 register int x, y;
632 int xres, yres;
633 {
634 x -= y;
635 while (x < 0)
636 x += xres;
637 return((((x>>2)*yres + y) << 2) + (x & 3));
638 }