| 1 |
/* Copyright (c) 1987 Regents of the University of California */
|
| 2 |
|
| 3 |
#ifndef lint
|
| 4 |
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
#endif
|
| 6 |
|
| 7 |
/*
|
| 8 |
* rview.c - routines and variables for interactive view generation.
|
| 9 |
*
|
| 10 |
* 3/24/87
|
| 11 |
*/
|
| 12 |
|
| 13 |
#include "standard.h"
|
| 14 |
|
| 15 |
#include "color.h"
|
| 16 |
|
| 17 |
#include "rpaint.h"
|
| 18 |
|
| 19 |
#include <signal.h>
|
| 20 |
|
| 21 |
#include <ctype.h>
|
| 22 |
|
| 23 |
VIEW ourview = STDVIEW(470); /* viewing parameters */
|
| 24 |
|
| 25 |
int psample = 8; /* pixel sample size */
|
| 26 |
double maxdiff = .15; /* max. sample difference */
|
| 27 |
|
| 28 |
double exposure = 1.0; /* exposure for scene */
|
| 29 |
|
| 30 |
double dstrsrc = 0.0; /* square source distribution */
|
| 31 |
double shadthresh = .1; /* shadow threshold */
|
| 32 |
double shadcert = .25; /* shadow certainty */
|
| 33 |
|
| 34 |
int maxdepth = 4; /* maximum recursion depth */
|
| 35 |
double minweight = 1e-2; /* minimum ray weight */
|
| 36 |
|
| 37 |
COLOR ambval = BLKCOLOR; /* ambient value */
|
| 38 |
double ambacc = 0.2; /* ambient accuracy */
|
| 39 |
int ambres = 8; /* ambient resolution */
|
| 40 |
int ambdiv = 32; /* 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 greyscale = 0; /* map colors to brightness? */
|
| 47 |
char *devname = "X"; /* output device name */
|
| 48 |
|
| 49 |
struct driver *dev = NULL; /* driver functions */
|
| 50 |
|
| 51 |
VIEW oldview; /* previous view parameters */
|
| 52 |
|
| 53 |
PNODE ptrunk; /* the base of our image */
|
| 54 |
RECT pframe; /* current frame boundaries */
|
| 55 |
int pdepth; /* image depth in current frame */
|
| 56 |
|
| 57 |
#define CTRL(c) ('c'-'@')
|
| 58 |
|
| 59 |
|
| 60 |
quit(code) /* quit program */
|
| 61 |
int code;
|
| 62 |
{
|
| 63 |
devclose();
|
| 64 |
exit(code);
|
| 65 |
}
|
| 66 |
|
| 67 |
|
| 68 |
devopen(dname) /* open device driver */
|
| 69 |
char *dname;
|
| 70 |
{
|
| 71 |
extern char *progname, *octname;
|
| 72 |
char *id;
|
| 73 |
register int i;
|
| 74 |
|
| 75 |
id = octname!=NULL ? octname : progname;
|
| 76 |
/* check device table */
|
| 77 |
for (i = 0; devtable[i].name; i++)
|
| 78 |
if (!strcmp(dname, devtable[i].name))
|
| 79 |
if ((dev = (*devtable[i].init)(dname, id)) == NULL) {
|
| 80 |
sprintf(errmsg, "cannot initialize %s", dname);
|
| 81 |
error(USER, errmsg);
|
| 82 |
} else
|
| 83 |
return;
|
| 84 |
/* not there, try exec */
|
| 85 |
if ((dev = comm_init(dname, id)) == NULL) {
|
| 86 |
sprintf(errmsg, "cannot start device \"%s\"", dname);
|
| 87 |
error(USER, errmsg);
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
|
| 92 |
devclose() /* close our device */
|
| 93 |
{
|
| 94 |
if (dev != NULL)
|
| 95 |
(*dev->close)();
|
| 96 |
dev = NULL;
|
| 97 |
}
|
| 98 |
|
| 99 |
|
| 100 |
printdevices() /* print list of output devices */
|
| 101 |
{
|
| 102 |
register int i;
|
| 103 |
|
| 104 |
for (i = 0; devtable[i].name; i++)
|
| 105 |
printf("%-16s # %s\n", devtable[i].name, devtable[i].descrip);
|
| 106 |
}
|
| 107 |
|
| 108 |
|
| 109 |
rview() /* do a view */
|
| 110 |
{
|
| 111 |
char buf[32];
|
| 112 |
|
| 113 |
devopen(devname); /* open device */
|
| 114 |
newimage(); /* set up image */
|
| 115 |
|
| 116 |
for ( ; ; ) { /* quit in command() */
|
| 117 |
while (ourview.hresolu <= 1<<pdepth &&
|
| 118 |
ourview.vresolu <= 1<<pdepth)
|
| 119 |
command("done: ");
|
| 120 |
|
| 121 |
if (ourview.hresolu <= psample<<pdepth &&
|
| 122 |
ourview.vresolu <= psample<<pdepth) {
|
| 123 |
sprintf(buf, "%d sampling...\n", 1<<pdepth);
|
| 124 |
(*dev->comout)(buf);
|
| 125 |
rsample();
|
| 126 |
} else {
|
| 127 |
sprintf(buf, "%d refining...\n", 1<<pdepth);
|
| 128 |
(*dev->comout)(buf);
|
| 129 |
refine(&ptrunk, 0, 0, ourview.hresolu,
|
| 130 |
ourview.vresolu, pdepth+1);
|
| 131 |
}
|
| 132 |
if (dev->inpready)
|
| 133 |
command(": ");
|
| 134 |
else
|
| 135 |
pdepth++;
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
|
| 140 |
command(prompt) /* get/execute command */
|
| 141 |
char *prompt;
|
| 142 |
{
|
| 143 |
#define badcom(s) strncmp(s, inpbuf, args-inpbuf-1)
|
| 144 |
double atof();
|
| 145 |
char inpbuf[256];
|
| 146 |
char *args;
|
| 147 |
again:
|
| 148 |
(*dev->comout)(prompt); /* get command + arguments */
|
| 149 |
(*dev->comin)(inpbuf);
|
| 150 |
for (args = inpbuf; *args && *args != ' '; args++)
|
| 151 |
;
|
| 152 |
if (*args) *args++ = '\0';
|
| 153 |
else *++args = '\0';
|
| 154 |
|
| 155 |
switch (inpbuf[0]) {
|
| 156 |
case 'f': /* new frame */
|
| 157 |
if (badcom("frame"))
|
| 158 |
goto commerr;
|
| 159 |
getframe(args);
|
| 160 |
break;
|
| 161 |
case 'v': /* view */
|
| 162 |
if (badcom("view"))
|
| 163 |
goto commerr;
|
| 164 |
getview(args);
|
| 165 |
break;
|
| 166 |
case 'l': /* last view */
|
| 167 |
if (badcom("last"))
|
| 168 |
goto commerr;
|
| 169 |
lastview(args);
|
| 170 |
break;
|
| 171 |
case 'e': /* exposure */
|
| 172 |
if (badcom("exposure"))
|
| 173 |
goto commerr;
|
| 174 |
getexposure(args);
|
| 175 |
break;
|
| 176 |
case 's': /* set a parameter */
|
| 177 |
if (badcom("set"))
|
| 178 |
goto commerr;
|
| 179 |
setparam(args);
|
| 180 |
break;
|
| 181 |
case 'n': /* new picture */
|
| 182 |
if (badcom("new"))
|
| 183 |
goto commerr;
|
| 184 |
newimage();
|
| 185 |
break;
|
| 186 |
case 't': /* trace a ray */
|
| 187 |
if (badcom("trace"))
|
| 188 |
goto commerr;
|
| 189 |
traceray(args);
|
| 190 |
break;
|
| 191 |
case 'a': /* aim camera */
|
| 192 |
if (badcom("aim"))
|
| 193 |
goto commerr;
|
| 194 |
getaim(args);
|
| 195 |
break;
|
| 196 |
case 'm': /* move camera */
|
| 197 |
if (badcom("move"))
|
| 198 |
goto commerr;
|
| 199 |
getmove(args);
|
| 200 |
break;
|
| 201 |
case 'r': /* rotate camera */
|
| 202 |
if (badcom("rotate"))
|
| 203 |
goto commerr;
|
| 204 |
getrotate(args);
|
| 205 |
break;
|
| 206 |
case 'p': /* pivot view */
|
| 207 |
if (badcom("pivot"))
|
| 208 |
goto commerr;
|
| 209 |
getpivot(args);
|
| 210 |
break;
|
| 211 |
case CTRL(R): /* redraw */
|
| 212 |
redraw();
|
| 213 |
break;
|
| 214 |
case 'w': /* write */
|
| 215 |
if (badcom("write"))
|
| 216 |
goto commerr;
|
| 217 |
writepict(args);
|
| 218 |
break;
|
| 219 |
case 'q': /* quit */
|
| 220 |
if (badcom("quit"))
|
| 221 |
goto commerr;
|
| 222 |
quit(0);
|
| 223 |
case CTRL(C): /* interrupt */
|
| 224 |
goto again;
|
| 225 |
#ifdef SIGTSTP
|
| 226 |
case CTRL(Z): /* stop */
|
| 227 |
devclose();
|
| 228 |
kill(0, SIGTSTP);
|
| 229 |
/* pc stops here */
|
| 230 |
devopen(devname);
|
| 231 |
redraw();
|
| 232 |
break;
|
| 233 |
#endif
|
| 234 |
case '\0': /* continue */
|
| 235 |
break;
|
| 236 |
default:;
|
| 237 |
commerr:
|
| 238 |
if (iscntrl(inpbuf[0]))
|
| 239 |
sprintf(errmsg, "^%c: unknown control",
|
| 240 |
inpbuf[0]|0100);
|
| 241 |
else
|
| 242 |
sprintf(errmsg, "%s: unknown command", inpbuf);
|
| 243 |
error(COMMAND, errmsg);
|
| 244 |
break;
|
| 245 |
}
|
| 246 |
#undef badcom
|
| 247 |
}
|
| 248 |
|
| 249 |
|
| 250 |
rsample() /* sample the image */
|
| 251 |
{
|
| 252 |
int xsiz, ysiz, y;
|
| 253 |
RECT r;
|
| 254 |
PNODE *p;
|
| 255 |
register RECT *rl;
|
| 256 |
register PNODE **pl;
|
| 257 |
register int x;
|
| 258 |
/*
|
| 259 |
* We initialize the bottom row in the image at our current
|
| 260 |
* resolution. During sampling, we check super-pixels to the
|
| 261 |
* right and above by calling bigdiff(). If there is a significant
|
| 262 |
* difference, we subsample the super-pixels. The testing process
|
| 263 |
* includes initialization of the next row.
|
| 264 |
*/
|
| 265 |
xsiz = (((pframe.r-pframe.l)<<pdepth)+ourview.hresolu-1) /
|
| 266 |
ourview.hresolu;
|
| 267 |
ysiz = (((pframe.u-pframe.d)<<pdepth)+ourview.vresolu-1) /
|
| 268 |
ourview.vresolu;
|
| 269 |
rl = (RECT *)malloc(xsiz*sizeof(RECT));
|
| 270 |
pl = (PNODE **)malloc(xsiz*sizeof(PNODE *));
|
| 271 |
if (rl == NULL || pl == NULL)
|
| 272 |
error(SYSTEM, "out of memory in rsample");
|
| 273 |
/*
|
| 274 |
* Initialize the bottom row.
|
| 275 |
*/
|
| 276 |
rl[0].l = rl[0].d = 0;
|
| 277 |
rl[0].r = ourview.hresolu; rl[0].u = ourview.vresolu;
|
| 278 |
pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth);
|
| 279 |
for (x = 1; x < xsiz; x++) {
|
| 280 |
rl[x].l = rl[x].d = 0;
|
| 281 |
rl[x].r = ourview.hresolu; rl[x].u = ourview.vresolu;
|
| 282 |
pl[x] = findrect(pframe.l+((x*ourview.hresolu)>>pdepth),
|
| 283 |
pframe.d, &ptrunk, rl+x, pdepth);
|
| 284 |
}
|
| 285 |
/* sample the image */
|
| 286 |
for (y = 0; /* y < ysiz */ ; y++) {
|
| 287 |
for (x = 0; x < xsiz-1; x++) {
|
| 288 |
if (dev->inpready)
|
| 289 |
goto escape;
|
| 290 |
/*
|
| 291 |
* Test super-pixel to the right.
|
| 292 |
*/
|
| 293 |
if (pl[x] != pl[x+1] && bigdiff(pl[x]->v,
|
| 294 |
pl[x+1]->v, maxdiff)) {
|
| 295 |
refine(pl[x], rl[x].l, rl[x].d,
|
| 296 |
rl[x].r, rl[x].u, 1);
|
| 297 |
refine(pl[x+1], rl[x+1].l, rl[x+1].d,
|
| 298 |
rl[x+1].r, rl[x+1].u, 1);
|
| 299 |
}
|
| 300 |
}
|
| 301 |
if (y >= ysiz-1)
|
| 302 |
break;
|
| 303 |
for (x = 0; x < xsiz; x++) {
|
| 304 |
if (dev->inpready)
|
| 305 |
goto escape;
|
| 306 |
/*
|
| 307 |
* Find super-pixel at this position in next row.
|
| 308 |
*/
|
| 309 |
r.l = r.d = 0;
|
| 310 |
r.r = ourview.hresolu; r.u = ourview.vresolu;
|
| 311 |
p = findrect(pframe.l+((x*ourview.hresolu)>>pdepth),
|
| 312 |
pframe.d+(((y+1)*ourview.vresolu)>>pdepth),
|
| 313 |
&ptrunk, &r, pdepth);
|
| 314 |
/*
|
| 315 |
* Test super-pixel in next row.
|
| 316 |
*/
|
| 317 |
if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) {
|
| 318 |
refine(pl[x], rl[x].l, rl[x].d,
|
| 319 |
rl[x].r, rl[x].u, 1);
|
| 320 |
refine(p, r.l, r.d, r.r, r.u, 1);
|
| 321 |
}
|
| 322 |
/*
|
| 323 |
* Copy into super-pixel array.
|
| 324 |
*/
|
| 325 |
rl[x].l = r.l; rl[x].d = r.d;
|
| 326 |
rl[x].r = r.r; rl[x].u = r.u;
|
| 327 |
pl[x] = p;
|
| 328 |
}
|
| 329 |
}
|
| 330 |
escape:
|
| 331 |
free((char *)rl);
|
| 332 |
free((char *)pl);
|
| 333 |
}
|
| 334 |
|
| 335 |
|
| 336 |
int
|
| 337 |
refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */
|
| 338 |
register PNODE *p;
|
| 339 |
int xmin, ymin, xmax, ymax;
|
| 340 |
int pd;
|
| 341 |
{
|
| 342 |
int growth;
|
| 343 |
int mx, my;
|
| 344 |
int i;
|
| 345 |
|
| 346 |
if (dev->inpready) /* quit for input */
|
| 347 |
return(0);
|
| 348 |
|
| 349 |
if (pd <= 0) /* depth limit */
|
| 350 |
return(0);
|
| 351 |
|
| 352 |
mx = (xmin + xmax) >> 1;
|
| 353 |
my = (ymin + ymax) >> 1;
|
| 354 |
growth = 0;
|
| 355 |
|
| 356 |
if (p->kid == NULL) { /* subdivide */
|
| 357 |
|
| 358 |
if ((p->kid = newptree()) == NULL)
|
| 359 |
error(SYSTEM, "out of memory in refine");
|
| 360 |
/*
|
| 361 |
* The following paint order can leave a black pixel
|
| 362 |
* when redraw() is called in (*dev->paintr)().
|
| 363 |
*/
|
| 364 |
if (p->x >= mx && p->y >= my)
|
| 365 |
pcopy(p, p->kid+UR);
|
| 366 |
else
|
| 367 |
paint(p->kid+UR, mx, my, xmax, ymax);
|
| 368 |
if (p->x < mx && p->y >= my)
|
| 369 |
pcopy(p, p->kid+UL);
|
| 370 |
else
|
| 371 |
paint(p->kid+UL, xmin, my, mx, ymax);
|
| 372 |
if (p->x >= mx && p->y < my)
|
| 373 |
pcopy(p, p->kid+DR);
|
| 374 |
else
|
| 375 |
paint(p->kid+DR, mx, ymin, xmax, my);
|
| 376 |
if (p->x < mx && p->y < my)
|
| 377 |
pcopy(p, p->kid+DL);
|
| 378 |
else
|
| 379 |
paint(p->kid+DL, xmin, ymin, mx, my);
|
| 380 |
|
| 381 |
growth++;
|
| 382 |
}
|
| 383 |
/* do children */
|
| 384 |
if (mx > pframe.l) {
|
| 385 |
if (my > pframe.d)
|
| 386 |
growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1);
|
| 387 |
if (my < pframe.u)
|
| 388 |
growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1);
|
| 389 |
}
|
| 390 |
if (mx < pframe.r) {
|
| 391 |
if (my > pframe.d)
|
| 392 |
growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1);
|
| 393 |
if (my < pframe.u)
|
| 394 |
growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1);
|
| 395 |
}
|
| 396 |
/* recompute sum */
|
| 397 |
if (growth) {
|
| 398 |
setcolor(p->v, 0.0, 0.0, 0.0);
|
| 399 |
for (i = 0; i < 4; i++)
|
| 400 |
addcolor(p->v, p->kid[i].v);
|
| 401 |
scalecolor(p->v, 0.25);
|
| 402 |
}
|
| 403 |
return(growth);
|
| 404 |
}
|