| 1 | 
greg | 
1.1 | 
/* Copyright (c) 1986 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  "view.h" | 
| 21 | 
  | 
  | 
 | 
| 22 | 
  | 
  | 
#include  "random.h" | 
| 23 | 
  | 
  | 
 | 
| 24 | 
  | 
  | 
VIEW  ourview = STDVIEW(512);           /* view parameters */ | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
int  psample = 4;                       /* pixel sample size */ | 
| 27 | 
  | 
  | 
double  maxdiff = .05;                  /* max. difference for interpolation */ | 
| 28 | 
greg | 
1.3 | 
double  dstrpix = 0.67;                 /* square pixel distribution */ | 
| 29 | 
greg | 
1.1 | 
 | 
| 30 | 
  | 
  | 
double  dstrsrc = 0.0;                  /* square source distribution */ | 
| 31 | 
greg | 
1.4 | 
double  shadthresh = .05;               /* shadow threshold */ | 
| 32 | 
greg | 
1.5 | 
double  shadcert = .5;                  /* shadow certainty */ | 
| 33 | 
greg | 
1.1 | 
 | 
| 34 | 
  | 
  | 
int  maxdepth = 6;                      /* maximum recursion depth */ | 
| 35 | 
  | 
  | 
double  minweight = 5e-3;               /* minimum ray weight */ | 
| 36 | 
  | 
  | 
 | 
| 37 | 
  | 
  | 
COLOR  ambval = BLKCOLOR;               /* ambient value */ | 
| 38 | 
  | 
  | 
double  ambacc = 0.2;                   /* ambient accuracy */ | 
| 39 | 
greg | 
1.6 | 
int  ambres = 32;                       /* ambient resolution */ | 
| 40 | 
greg | 
1.1 | 
int  ambdiv = 128;                      /* ambient divisions */ | 
| 41 | 
  | 
  | 
int  ambssamp = 0;                      /* ambient super-samples */ | 
| 42 | 
  | 
  | 
int  ambounce = 0;                      /* ambient bounces */ | 
| 43 | 
  | 
  | 
char  *amblist[128];                    /* ambient include/exclude list */ | 
| 44 | 
  | 
  | 
int  ambincl = -1;                      /* include == 1, exclude == 0 */ | 
| 45 | 
  | 
  | 
 | 
| 46 | 
  | 
  | 
int  ralrm = 0;                         /* seconds between reports */ | 
| 47 | 
  | 
  | 
 | 
| 48 | 
  | 
  | 
double  pctdone = 0.0;                  /* percentage done */ | 
| 49 | 
  | 
  | 
 | 
| 50 | 
  | 
  | 
extern long  nrays;                     /* number of rays traced */ | 
| 51 | 
  | 
  | 
 | 
| 52 | 
  | 
  | 
#define  MAXDIV         32              /* maximum sample size */ | 
| 53 | 
  | 
  | 
 | 
| 54 | 
  | 
  | 
#define  pixjitter()    (.5+dstrpix*(.5-frandom())) | 
| 55 | 
  | 
  | 
 | 
| 56 | 
  | 
  | 
 | 
| 57 | 
  | 
  | 
quit(code)                      /* quit program */ | 
| 58 | 
  | 
  | 
int  code; | 
| 59 | 
  | 
  | 
{ | 
| 60 | 
  | 
  | 
        if (code || ralrm > 0)          /* report status */ | 
| 61 | 
  | 
  | 
                report(); | 
| 62 | 
  | 
  | 
 | 
| 63 | 
  | 
  | 
        exit(code); | 
| 64 | 
  | 
  | 
} | 
| 65 | 
  | 
  | 
 | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
report()                /* report progress */ | 
| 68 | 
  | 
  | 
{ | 
| 69 | 
  | 
  | 
#ifdef BSD | 
| 70 | 
  | 
  | 
        struct rusage  rubuf; | 
| 71 | 
  | 
  | 
        double  t; | 
| 72 | 
  | 
  | 
 | 
| 73 | 
  | 
  | 
        getrusage(RUSAGE_SELF, &rubuf); | 
| 74 | 
  | 
  | 
        t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; | 
| 75 | 
  | 
  | 
        t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; | 
| 76 | 
  | 
  | 
        getrusage(RUSAGE_CHILDREN, &rubuf); | 
| 77 | 
  | 
  | 
        t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; | 
| 78 | 
  | 
  | 
        t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; | 
| 79 | 
  | 
  | 
 | 
| 80 | 
  | 
  | 
        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", | 
| 81 | 
  | 
  | 
                        nrays, pctdone, t/3600.0); | 
| 82 | 
  | 
  | 
#else | 
| 83 | 
  | 
  | 
        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); | 
| 84 | 
  | 
  | 
#endif | 
| 85 | 
  | 
  | 
        eputs(errmsg); | 
| 86 | 
  | 
  | 
 | 
| 87 | 
  | 
  | 
        if (ralrm > 0) | 
| 88 | 
  | 
  | 
                alarm(ralrm); | 
| 89 | 
  | 
  | 
} | 
| 90 | 
  | 
  | 
 | 
| 91 | 
  | 
  | 
 | 
| 92 | 
  | 
  | 
render(oldfile)                         /* render the scene */ | 
| 93 | 
  | 
  | 
char  *oldfile; | 
| 94 | 
  | 
  | 
{ | 
| 95 | 
  | 
  | 
        COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */ | 
| 96 | 
  | 
  | 
        int  ypos;                      /* current scanline */ | 
| 97 | 
  | 
  | 
        COLOR  *colptr; | 
| 98 | 
  | 
  | 
        register int  i; | 
| 99 | 
  | 
  | 
 | 
| 100 | 
  | 
  | 
        if (psample < 1) | 
| 101 | 
  | 
  | 
                psample = 1; | 
| 102 | 
  | 
  | 
        else if (psample > MAXDIV) | 
| 103 | 
  | 
  | 
                psample = MAXDIV; | 
| 104 | 
  | 
  | 
 | 
| 105 | 
  | 
  | 
        ourview.hresolu -= ourview.hresolu % psample; | 
| 106 | 
  | 
  | 
        ourview.vresolu -= ourview.vresolu % psample; | 
| 107 | 
  | 
  | 
                 | 
| 108 | 
  | 
  | 
        for (i = 0; i <= psample; i++) { | 
| 109 | 
  | 
  | 
                scanbar[i] = (COLOR *)malloc((ourview.hresolu+1)*sizeof(COLOR)); | 
| 110 | 
  | 
  | 
                if (scanbar[i] == NULL) | 
| 111 | 
  | 
  | 
                        error(SYSTEM, "out of memory in render"); | 
| 112 | 
  | 
  | 
        } | 
| 113 | 
  | 
  | 
         | 
| 114 | 
  | 
  | 
                                        /* write out boundaries */ | 
| 115 | 
greg | 
1.7 | 
        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); | 
| 116 | 
greg | 
1.1 | 
 | 
| 117 | 
  | 
  | 
        ypos = ourview.vresolu - salvage(oldfile);      /* find top line */ | 
| 118 | 
  | 
  | 
        fillscanline(scanbar[0], ypos, psample);        /* top scan */ | 
| 119 | 
  | 
  | 
 | 
| 120 | 
  | 
  | 
        for (ypos -= psample; ypos > -psample; ypos -= psample) { | 
| 121 | 
  | 
  | 
         | 
| 122 | 
  | 
  | 
                colptr = scanbar[psample];              /* get last scanline */ | 
| 123 | 
  | 
  | 
                scanbar[psample] = scanbar[0]; | 
| 124 | 
  | 
  | 
                scanbar[0] = colptr; | 
| 125 | 
  | 
  | 
 | 
| 126 | 
  | 
  | 
                fillscanline(scanbar[0], ypos, psample);        /* base scan */ | 
| 127 | 
  | 
  | 
         | 
| 128 | 
  | 
  | 
                fillscanbar(scanbar, ypos, psample); | 
| 129 | 
  | 
  | 
                 | 
| 130 | 
  | 
  | 
                for (i = psample-1; i >= 0; i--) | 
| 131 | 
  | 
  | 
                        if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0) | 
| 132 | 
  | 
  | 
                                goto writerr; | 
| 133 | 
  | 
  | 
                if (fflush(stdout) == EOF) | 
| 134 | 
  | 
  | 
                        goto writerr; | 
| 135 | 
  | 
  | 
                pctdone = 100.0*(ourview.vresolu-ypos)/ourview.vresolu; | 
| 136 | 
  | 
  | 
        } | 
| 137 | 
  | 
  | 
                 | 
| 138 | 
  | 
  | 
        for (i = 0; i <= psample; i++) | 
| 139 | 
  | 
  | 
                free((char *)scanbar[i]); | 
| 140 | 
  | 
  | 
        return; | 
| 141 | 
  | 
  | 
writerr: | 
| 142 | 
  | 
  | 
        error(SYSTEM, "write error in render"); | 
| 143 | 
  | 
  | 
} | 
| 144 | 
  | 
  | 
 | 
| 145 | 
  | 
  | 
 | 
| 146 | 
  | 
  | 
fillscanline(scanline, y, xstep)                /* fill scan line at y */ | 
| 147 | 
  | 
  | 
register COLOR  *scanline; | 
| 148 | 
  | 
  | 
int  y, xstep; | 
| 149 | 
  | 
  | 
{ | 
| 150 | 
  | 
  | 
        int  b = xstep; | 
| 151 | 
  | 
  | 
        register int  i; | 
| 152 | 
  | 
  | 
         | 
| 153 | 
  | 
  | 
        pixvalue(scanline[0], 0, y); | 
| 154 | 
  | 
  | 
 | 
| 155 | 
  | 
  | 
        for (i = xstep; i <= ourview.hresolu; i += xstep) { | 
| 156 | 
  | 
  | 
         | 
| 157 | 
  | 
  | 
                pixvalue(scanline[i], i, y); | 
| 158 | 
  | 
  | 
                 | 
| 159 | 
  | 
  | 
                b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2); | 
| 160 | 
  | 
  | 
        } | 
| 161 | 
  | 
  | 
} | 
| 162 | 
  | 
  | 
 | 
| 163 | 
  | 
  | 
 | 
| 164 | 
  | 
  | 
fillscanbar(scanbar, y, ysize)          /* fill interior */ | 
| 165 | 
  | 
  | 
register COLOR  *scanbar[]; | 
| 166 | 
  | 
  | 
int  y, ysize; | 
| 167 | 
  | 
  | 
{ | 
| 168 | 
  | 
  | 
        COLOR  vline[MAXDIV+1]; | 
| 169 | 
  | 
  | 
        int  b = ysize; | 
| 170 | 
  | 
  | 
        register int  i, j; | 
| 171 | 
  | 
  | 
         | 
| 172 | 
  | 
  | 
        for (i = 0; i < ourview.hresolu; i++) { | 
| 173 | 
  | 
  | 
                 | 
| 174 | 
  | 
  | 
                copycolor(vline[0], scanbar[0][i]); | 
| 175 | 
  | 
  | 
                copycolor(vline[ysize], scanbar[ysize][i]); | 
| 176 | 
  | 
  | 
                 | 
| 177 | 
  | 
  | 
                b = fillsample(vline, i, y, 0, ysize, b/2); | 
| 178 | 
  | 
  | 
                 | 
| 179 | 
  | 
  | 
                for (j = 1; j < ysize; j++) | 
| 180 | 
  | 
  | 
                        copycolor(scanbar[j][i], vline[j]); | 
| 181 | 
  | 
  | 
        } | 
| 182 | 
  | 
  | 
} | 
| 183 | 
  | 
  | 
 | 
| 184 | 
  | 
  | 
 | 
| 185 | 
  | 
  | 
int | 
| 186 | 
  | 
  | 
fillsample(colline, x, y, xlen, ylen, b)        /* fill interior points */ | 
| 187 | 
  | 
  | 
register COLOR  *colline; | 
| 188 | 
  | 
  | 
int  x, y; | 
| 189 | 
  | 
  | 
int  xlen, ylen; | 
| 190 | 
  | 
  | 
int  b; | 
| 191 | 
  | 
  | 
{ | 
| 192 | 
  | 
  | 
        double  ratio; | 
| 193 | 
  | 
  | 
        COLOR  ctmp; | 
| 194 | 
  | 
  | 
        int  ncut; | 
| 195 | 
  | 
  | 
        register int  len; | 
| 196 | 
  | 
  | 
         | 
| 197 | 
  | 
  | 
        if (xlen > 0)                   /* x or y length is zero */ | 
| 198 | 
  | 
  | 
                len = xlen; | 
| 199 | 
  | 
  | 
        else | 
| 200 | 
  | 
  | 
                len = ylen; | 
| 201 | 
  | 
  | 
                 | 
| 202 | 
  | 
  | 
        if (len <= 1)                   /* limit recursion */ | 
| 203 | 
  | 
  | 
                return(0); | 
| 204 | 
  | 
  | 
         | 
| 205 | 
  | 
  | 
        if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) { | 
| 206 | 
  | 
  | 
         | 
| 207 | 
  | 
  | 
                pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); | 
| 208 | 
  | 
  | 
                ncut = 1; | 
| 209 | 
  | 
  | 
                 | 
| 210 | 
  | 
  | 
        } else {                                        /* interpolate */ | 
| 211 | 
  | 
  | 
         | 
| 212 | 
  | 
  | 
                copycolor(colline[len>>1], colline[len]); | 
| 213 | 
  | 
  | 
                ratio = (double)(len>>1) / len; | 
| 214 | 
  | 
  | 
                scalecolor(colline[len>>1], ratio); | 
| 215 | 
  | 
  | 
                copycolor(ctmp, colline[0]); | 
| 216 | 
  | 
  | 
                ratio = 1.0 - ratio; | 
| 217 | 
  | 
  | 
                scalecolor(ctmp, ratio); | 
| 218 | 
  | 
  | 
                addcolor(colline[len>>1], ctmp); | 
| 219 | 
  | 
  | 
                ncut = 0; | 
| 220 | 
  | 
  | 
        } | 
| 221 | 
  | 
  | 
                                                        /* recurse */ | 
| 222 | 
  | 
  | 
        ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2); | 
| 223 | 
  | 
  | 
         | 
| 224 | 
  | 
  | 
        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1), | 
| 225 | 
  | 
  | 
                                xlen - (xlen>>1), ylen - (ylen>>1), b/2); | 
| 226 | 
  | 
  | 
 | 
| 227 | 
  | 
  | 
        return(ncut); | 
| 228 | 
  | 
  | 
} | 
| 229 | 
  | 
  | 
 | 
| 230 | 
  | 
  | 
 | 
| 231 | 
  | 
  | 
pixvalue(col, x, y)             /* compute pixel value */ | 
| 232 | 
  | 
  | 
COLOR  col;                     /* returned color */ | 
| 233 | 
  | 
  | 
int  x, y;                      /* pixel position */ | 
| 234 | 
  | 
  | 
{ | 
| 235 | 
  | 
  | 
        static RAY  thisray;    /* our ray for this pixel */ | 
| 236 | 
  | 
  | 
 | 
| 237 | 
  | 
  | 
        rayview(thisray.rorg, thisray.rdir, &ourview, | 
| 238 | 
  | 
  | 
                        x + pixjitter(), y + pixjitter()); | 
| 239 | 
  | 
  | 
 | 
| 240 | 
  | 
  | 
        rayorigin(&thisray, NULL, PRIMARY, 1.0); | 
| 241 | 
  | 
  | 
         | 
| 242 | 
  | 
  | 
        rayvalue(&thisray);                     /* trace ray */ | 
| 243 | 
  | 
  | 
 | 
| 244 | 
  | 
  | 
        copycolor(col, thisray.rcol);           /* return color */ | 
| 245 | 
  | 
  | 
} | 
| 246 | 
  | 
  | 
 | 
| 247 | 
  | 
  | 
 | 
| 248 | 
  | 
  | 
int | 
| 249 | 
  | 
  | 
salvage(oldfile)                /* salvage scanlines from killed program */ | 
| 250 | 
  | 
  | 
char  *oldfile; | 
| 251 | 
  | 
  | 
{ | 
| 252 | 
  | 
  | 
        COLR  *scanline; | 
| 253 | 
  | 
  | 
        FILE  *fp; | 
| 254 | 
  | 
  | 
        int  x, y; | 
| 255 | 
  | 
  | 
 | 
| 256 | 
  | 
  | 
        if (oldfile == NULL) | 
| 257 | 
  | 
  | 
                return(0); | 
| 258 | 
  | 
  | 
        else if ((fp = fopen(oldfile, "r")) == NULL) { | 
| 259 | 
  | 
  | 
                sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); | 
| 260 | 
  | 
  | 
                error(WARNING, errmsg); | 
| 261 | 
  | 
  | 
                return(0); | 
| 262 | 
  | 
  | 
        } | 
| 263 | 
  | 
  | 
                                /* discard header */ | 
| 264 | 
  | 
  | 
        getheader(fp, NULL); | 
| 265 | 
  | 
  | 
                                /* get picture size */ | 
| 266 | 
greg | 
1.7 | 
        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { | 
| 267 | 
greg | 
1.2 | 
                sprintf(errmsg, "bad recover file \"%s\"", oldfile); | 
| 268 | 
  | 
  | 
                error(WARNING, errmsg); | 
| 269 | 
greg | 
1.1 | 
                fclose(fp); | 
| 270 | 
  | 
  | 
                return(0); | 
| 271 | 
  | 
  | 
        } | 
| 272 | 
  | 
  | 
 | 
| 273 | 
  | 
  | 
        if (x != ourview.hresolu || y != ourview.vresolu) { | 
| 274 | 
  | 
  | 
                sprintf(errmsg, "resolution mismatch in recover file \"%s\"", | 
| 275 | 
  | 
  | 
                                oldfile); | 
| 276 | 
  | 
  | 
                error(USER, errmsg); | 
| 277 | 
  | 
  | 
                return(0); | 
| 278 | 
  | 
  | 
        } | 
| 279 | 
  | 
  | 
 | 
| 280 | 
  | 
  | 
        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR)); | 
| 281 | 
  | 
  | 
        if (scanline == NULL) | 
| 282 | 
  | 
  | 
                error(SYSTEM, "out of memory in salvage"); | 
| 283 | 
  | 
  | 
        for (y = 0; y < ourview.vresolu; y++) { | 
| 284 | 
  | 
  | 
                if (freadcolrs(scanline, ourview.hresolu, fp) < 0) | 
| 285 | 
  | 
  | 
                        break; | 
| 286 | 
  | 
  | 
                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0) | 
| 287 | 
  | 
  | 
                        goto writerr; | 
| 288 | 
  | 
  | 
        } | 
| 289 | 
  | 
  | 
        if (fflush(stdout) == EOF) | 
| 290 | 
  | 
  | 
                goto writerr; | 
| 291 | 
  | 
  | 
        free((char *)scanline); | 
| 292 | 
  | 
  | 
        fclose(fp); | 
| 293 | 
  | 
  | 
        unlink(oldfile); | 
| 294 | 
  | 
  | 
        return(y); | 
| 295 | 
  | 
  | 
writerr: | 
| 296 | 
  | 
  | 
        error(SYSTEM, "write error in salvage"); | 
| 297 | 
  | 
  | 
} |