| 1 |
greg |
1.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 |
greg |
1.9 |
VIEW ourview = STDVIEW; /* viewing parameters */ |
| 24 |
|
|
int hresolu, vresolu; /* image resolution */ |
| 25 |
greg |
1.1 |
|
| 26 |
|
|
int psample = 8; /* pixel sample size */ |
| 27 |
|
|
double maxdiff = .15; /* max. sample difference */ |
| 28 |
|
|
|
| 29 |
|
|
double exposure = 1.0; /* exposure for scene */ |
| 30 |
|
|
|
| 31 |
|
|
double dstrsrc = 0.0; /* square source distribution */ |
| 32 |
greg |
1.3 |
double shadthresh = .1; /* shadow threshold */ |
| 33 |
greg |
1.4 |
double shadcert = .25; /* shadow certainty */ |
| 34 |
greg |
1.1 |
|
| 35 |
|
|
int maxdepth = 4; /* maximum recursion depth */ |
| 36 |
|
|
double minweight = 1e-2; /* minimum ray weight */ |
| 37 |
|
|
|
| 38 |
|
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
| 39 |
|
|
double ambacc = 0.2; /* ambient accuracy */ |
| 40 |
greg |
1.5 |
int ambres = 8; /* ambient resolution */ |
| 41 |
greg |
1.1 |
int ambdiv = 32; /* ambient divisions */ |
| 42 |
|
|
int ambssamp = 0; /* ambient super-samples */ |
| 43 |
|
|
int ambounce = 0; /* ambient bounces */ |
| 44 |
|
|
char *amblist[128]; /* ambient include/exclude list */ |
| 45 |
|
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
| 46 |
|
|
|
| 47 |
|
|
int greyscale = 0; /* map colors to brightness? */ |
| 48 |
|
|
char *devname = "X"; /* output device name */ |
| 49 |
|
|
|
| 50 |
|
|
struct driver *dev = NULL; /* driver functions */ |
| 51 |
|
|
|
| 52 |
|
|
VIEW oldview; /* previous view parameters */ |
| 53 |
|
|
|
| 54 |
|
|
PNODE ptrunk; /* the base of our image */ |
| 55 |
|
|
RECT pframe; /* current frame boundaries */ |
| 56 |
|
|
int pdepth; /* image depth in current frame */ |
| 57 |
|
|
|
| 58 |
|
|
#define CTRL(c) ('c'-'@') |
| 59 |
|
|
|
| 60 |
|
|
|
| 61 |
|
|
quit(code) /* quit program */ |
| 62 |
|
|
int code; |
| 63 |
|
|
{ |
| 64 |
|
|
devclose(); |
| 65 |
|
|
exit(code); |
| 66 |
|
|
} |
| 67 |
|
|
|
| 68 |
|
|
|
| 69 |
|
|
devopen(dname) /* open device driver */ |
| 70 |
|
|
char *dname; |
| 71 |
|
|
{ |
| 72 |
greg |
1.6 |
extern char *progname, *octname; |
| 73 |
greg |
1.7 |
char *id; |
| 74 |
greg |
1.1 |
register int i; |
| 75 |
greg |
1.6 |
|
| 76 |
greg |
1.7 |
id = octname!=NULL ? octname : progname; |
| 77 |
greg |
1.1 |
/* check device table */ |
| 78 |
|
|
for (i = 0; devtable[i].name; i++) |
| 79 |
|
|
if (!strcmp(dname, devtable[i].name)) |
| 80 |
greg |
1.7 |
if ((dev = (*devtable[i].init)(dname, id)) == NULL) { |
| 81 |
greg |
1.1 |
sprintf(errmsg, "cannot initialize %s", dname); |
| 82 |
|
|
error(USER, errmsg); |
| 83 |
|
|
} else |
| 84 |
|
|
return; |
| 85 |
|
|
/* not there, try exec */ |
| 86 |
greg |
1.7 |
if ((dev = comm_init(dname, id)) == NULL) { |
| 87 |
greg |
1.1 |
sprintf(errmsg, "cannot start device \"%s\"", dname); |
| 88 |
|
|
error(USER, errmsg); |
| 89 |
|
|
} |
| 90 |
|
|
} |
| 91 |
|
|
|
| 92 |
|
|
|
| 93 |
|
|
devclose() /* close our device */ |
| 94 |
|
|
{ |
| 95 |
|
|
if (dev != NULL) |
| 96 |
|
|
(*dev->close)(); |
| 97 |
|
|
dev = NULL; |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
|
| 101 |
|
|
printdevices() /* print list of output devices */ |
| 102 |
|
|
{ |
| 103 |
|
|
register int i; |
| 104 |
|
|
|
| 105 |
|
|
for (i = 0; devtable[i].name; i++) |
| 106 |
|
|
printf("%-16s # %s\n", devtable[i].name, devtable[i].descrip); |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
|
| 110 |
|
|
rview() /* do a view */ |
| 111 |
|
|
{ |
| 112 |
|
|
char buf[32]; |
| 113 |
|
|
|
| 114 |
|
|
devopen(devname); /* open device */ |
| 115 |
|
|
newimage(); /* set up image */ |
| 116 |
|
|
|
| 117 |
|
|
for ( ; ; ) { /* quit in command() */ |
| 118 |
greg |
1.9 |
while (hresolu <= 1<<pdepth && |
| 119 |
|
|
vresolu <= 1<<pdepth) |
| 120 |
greg |
1.1 |
command("done: "); |
| 121 |
|
|
|
| 122 |
greg |
1.9 |
if (hresolu <= psample<<pdepth && |
| 123 |
|
|
vresolu <= psample<<pdepth) { |
| 124 |
greg |
1.1 |
sprintf(buf, "%d sampling...\n", 1<<pdepth); |
| 125 |
|
|
(*dev->comout)(buf); |
| 126 |
|
|
rsample(); |
| 127 |
|
|
} else { |
| 128 |
|
|
sprintf(buf, "%d refining...\n", 1<<pdepth); |
| 129 |
|
|
(*dev->comout)(buf); |
| 130 |
greg |
1.9 |
refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1); |
| 131 |
greg |
1.1 |
} |
| 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 |
greg |
1.8 |
case 'r': /* rotate/repaint */ |
| 202 |
|
|
if (badcom("rotate")) { |
| 203 |
|
|
if (badcom("repaint")) |
| 204 |
|
|
goto commerr; |
| 205 |
|
|
getrepaint(args); |
| 206 |
|
|
break; |
| 207 |
|
|
} |
| 208 |
greg |
1.1 |
getrotate(args); |
| 209 |
|
|
break; |
| 210 |
|
|
case 'p': /* pivot view */ |
| 211 |
|
|
if (badcom("pivot")) |
| 212 |
|
|
goto commerr; |
| 213 |
|
|
getpivot(args); |
| 214 |
|
|
break; |
| 215 |
|
|
case CTRL(R): /* redraw */ |
| 216 |
|
|
redraw(); |
| 217 |
|
|
break; |
| 218 |
|
|
case 'w': /* write */ |
| 219 |
|
|
if (badcom("write")) |
| 220 |
|
|
goto commerr; |
| 221 |
|
|
writepict(args); |
| 222 |
|
|
break; |
| 223 |
|
|
case 'q': /* quit */ |
| 224 |
|
|
if (badcom("quit")) |
| 225 |
|
|
goto commerr; |
| 226 |
|
|
quit(0); |
| 227 |
|
|
case CTRL(C): /* interrupt */ |
| 228 |
|
|
goto again; |
| 229 |
|
|
#ifdef SIGTSTP |
| 230 |
|
|
case CTRL(Z): /* stop */ |
| 231 |
|
|
devclose(); |
| 232 |
|
|
kill(0, SIGTSTP); |
| 233 |
|
|
/* pc stops here */ |
| 234 |
|
|
devopen(devname); |
| 235 |
|
|
redraw(); |
| 236 |
|
|
break; |
| 237 |
|
|
#endif |
| 238 |
|
|
case '\0': /* continue */ |
| 239 |
|
|
break; |
| 240 |
|
|
default:; |
| 241 |
|
|
commerr: |
| 242 |
|
|
if (iscntrl(inpbuf[0])) |
| 243 |
|
|
sprintf(errmsg, "^%c: unknown control", |
| 244 |
|
|
inpbuf[0]|0100); |
| 245 |
|
|
else |
| 246 |
|
|
sprintf(errmsg, "%s: unknown command", inpbuf); |
| 247 |
|
|
error(COMMAND, errmsg); |
| 248 |
|
|
break; |
| 249 |
|
|
} |
| 250 |
|
|
#undef badcom |
| 251 |
|
|
} |
| 252 |
|
|
|
| 253 |
|
|
|
| 254 |
|
|
rsample() /* sample the image */ |
| 255 |
|
|
{ |
| 256 |
|
|
int xsiz, ysiz, y; |
| 257 |
|
|
RECT r; |
| 258 |
|
|
PNODE *p; |
| 259 |
|
|
register RECT *rl; |
| 260 |
|
|
register PNODE **pl; |
| 261 |
|
|
register int x; |
| 262 |
|
|
/* |
| 263 |
|
|
* We initialize the bottom row in the image at our current |
| 264 |
|
|
* resolution. During sampling, we check super-pixels to the |
| 265 |
|
|
* right and above by calling bigdiff(). If there is a significant |
| 266 |
|
|
* difference, we subsample the super-pixels. The testing process |
| 267 |
|
|
* includes initialization of the next row. |
| 268 |
|
|
*/ |
| 269 |
greg |
1.9 |
xsiz = (((pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu; |
| 270 |
|
|
ysiz = (((pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu; |
| 271 |
greg |
1.1 |
rl = (RECT *)malloc(xsiz*sizeof(RECT)); |
| 272 |
|
|
pl = (PNODE **)malloc(xsiz*sizeof(PNODE *)); |
| 273 |
|
|
if (rl == NULL || pl == NULL) |
| 274 |
|
|
error(SYSTEM, "out of memory in rsample"); |
| 275 |
|
|
/* |
| 276 |
|
|
* Initialize the bottom row. |
| 277 |
|
|
*/ |
| 278 |
|
|
rl[0].l = rl[0].d = 0; |
| 279 |
greg |
1.9 |
rl[0].r = hresolu; rl[0].u = vresolu; |
| 280 |
greg |
1.1 |
pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth); |
| 281 |
|
|
for (x = 1; x < xsiz; x++) { |
| 282 |
|
|
rl[x].l = rl[x].d = 0; |
| 283 |
greg |
1.9 |
rl[x].r = hresolu; rl[x].u = vresolu; |
| 284 |
|
|
pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth), |
| 285 |
greg |
1.1 |
pframe.d, &ptrunk, rl+x, pdepth); |
| 286 |
|
|
} |
| 287 |
|
|
/* sample the image */ |
| 288 |
|
|
for (y = 0; /* y < ysiz */ ; y++) { |
| 289 |
|
|
for (x = 0; x < xsiz-1; x++) { |
| 290 |
|
|
if (dev->inpready) |
| 291 |
|
|
goto escape; |
| 292 |
|
|
/* |
| 293 |
|
|
* Test super-pixel to the right. |
| 294 |
|
|
*/ |
| 295 |
|
|
if (pl[x] != pl[x+1] && bigdiff(pl[x]->v, |
| 296 |
|
|
pl[x+1]->v, maxdiff)) { |
| 297 |
|
|
refine(pl[x], rl[x].l, rl[x].d, |
| 298 |
|
|
rl[x].r, rl[x].u, 1); |
| 299 |
|
|
refine(pl[x+1], rl[x+1].l, rl[x+1].d, |
| 300 |
|
|
rl[x+1].r, rl[x+1].u, 1); |
| 301 |
|
|
} |
| 302 |
|
|
} |
| 303 |
|
|
if (y >= ysiz-1) |
| 304 |
|
|
break; |
| 305 |
|
|
for (x = 0; x < xsiz; x++) { |
| 306 |
|
|
if (dev->inpready) |
| 307 |
|
|
goto escape; |
| 308 |
|
|
/* |
| 309 |
|
|
* Find super-pixel at this position in next row. |
| 310 |
|
|
*/ |
| 311 |
|
|
r.l = r.d = 0; |
| 312 |
greg |
1.9 |
r.r = hresolu; r.u = vresolu; |
| 313 |
|
|
p = findrect(pframe.l+((x*hresolu)>>pdepth), |
| 314 |
|
|
pframe.d+(((y+1)*vresolu)>>pdepth), |
| 315 |
greg |
1.1 |
&ptrunk, &r, pdepth); |
| 316 |
|
|
/* |
| 317 |
|
|
* Test super-pixel in next row. |
| 318 |
|
|
*/ |
| 319 |
|
|
if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) { |
| 320 |
|
|
refine(pl[x], rl[x].l, rl[x].d, |
| 321 |
|
|
rl[x].r, rl[x].u, 1); |
| 322 |
|
|
refine(p, r.l, r.d, r.r, r.u, 1); |
| 323 |
|
|
} |
| 324 |
|
|
/* |
| 325 |
|
|
* Copy into super-pixel array. |
| 326 |
|
|
*/ |
| 327 |
|
|
rl[x].l = r.l; rl[x].d = r.d; |
| 328 |
|
|
rl[x].r = r.r; rl[x].u = r.u; |
| 329 |
|
|
pl[x] = p; |
| 330 |
|
|
} |
| 331 |
|
|
} |
| 332 |
|
|
escape: |
| 333 |
|
|
free((char *)rl); |
| 334 |
|
|
free((char *)pl); |
| 335 |
|
|
} |
| 336 |
|
|
|
| 337 |
|
|
|
| 338 |
|
|
int |
| 339 |
|
|
refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */ |
| 340 |
|
|
register PNODE *p; |
| 341 |
|
|
int xmin, ymin, xmax, ymax; |
| 342 |
|
|
int pd; |
| 343 |
|
|
{ |
| 344 |
|
|
int growth; |
| 345 |
|
|
int mx, my; |
| 346 |
|
|
int i; |
| 347 |
|
|
|
| 348 |
|
|
if (dev->inpready) /* quit for input */ |
| 349 |
|
|
return(0); |
| 350 |
|
|
|
| 351 |
|
|
if (pd <= 0) /* depth limit */ |
| 352 |
|
|
return(0); |
| 353 |
|
|
|
| 354 |
|
|
mx = (xmin + xmax) >> 1; |
| 355 |
|
|
my = (ymin + ymax) >> 1; |
| 356 |
|
|
growth = 0; |
| 357 |
|
|
|
| 358 |
|
|
if (p->kid == NULL) { /* subdivide */ |
| 359 |
|
|
|
| 360 |
|
|
if ((p->kid = newptree()) == NULL) |
| 361 |
|
|
error(SYSTEM, "out of memory in refine"); |
| 362 |
|
|
/* |
| 363 |
|
|
* The following paint order can leave a black pixel |
| 364 |
|
|
* when redraw() is called in (*dev->paintr)(). |
| 365 |
|
|
*/ |
| 366 |
|
|
if (p->x >= mx && p->y >= my) |
| 367 |
|
|
pcopy(p, p->kid+UR); |
| 368 |
|
|
else |
| 369 |
|
|
paint(p->kid+UR, mx, my, xmax, ymax); |
| 370 |
|
|
if (p->x < mx && p->y >= my) |
| 371 |
|
|
pcopy(p, p->kid+UL); |
| 372 |
|
|
else |
| 373 |
|
|
paint(p->kid+UL, xmin, my, mx, ymax); |
| 374 |
|
|
if (p->x >= mx && p->y < my) |
| 375 |
|
|
pcopy(p, p->kid+DR); |
| 376 |
|
|
else |
| 377 |
|
|
paint(p->kid+DR, mx, ymin, xmax, my); |
| 378 |
|
|
if (p->x < mx && p->y < my) |
| 379 |
|
|
pcopy(p, p->kid+DL); |
| 380 |
|
|
else |
| 381 |
|
|
paint(p->kid+DL, xmin, ymin, mx, my); |
| 382 |
|
|
|
| 383 |
|
|
growth++; |
| 384 |
|
|
} |
| 385 |
|
|
/* do children */ |
| 386 |
|
|
if (mx > pframe.l) { |
| 387 |
|
|
if (my > pframe.d) |
| 388 |
|
|
growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1); |
| 389 |
|
|
if (my < pframe.u) |
| 390 |
|
|
growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1); |
| 391 |
|
|
} |
| 392 |
|
|
if (mx < pframe.r) { |
| 393 |
|
|
if (my > pframe.d) |
| 394 |
|
|
growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1); |
| 395 |
|
|
if (my < pframe.u) |
| 396 |
|
|
growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1); |
| 397 |
|
|
} |
| 398 |
|
|
/* recompute sum */ |
| 399 |
|
|
if (growth) { |
| 400 |
|
|
setcolor(p->v, 0.0, 0.0, 0.0); |
| 401 |
|
|
for (i = 0; i < 4; i++) |
| 402 |
|
|
addcolor(p->v, p->kid[i].v); |
| 403 |
|
|
scalecolor(p->v, 0.25); |
| 404 |
|
|
} |
| 405 |
|
|
return(growth); |
| 406 |
|
|
} |