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 |
* rv2.c - command routines used in tracing a view. |
9 |
* |
10 |
* 3/24/87 |
11 |
*/ |
12 |
|
13 |
#include "ray.h" |
14 |
|
15 |
#include "octree.h" |
16 |
|
17 |
#include "otypes.h" |
18 |
|
19 |
#include "rpaint.h" |
20 |
|
21 |
#include <ctype.h> |
22 |
|
23 |
#define CTRL(c) ('c'-'@') |
24 |
|
25 |
extern char *progname; |
26 |
|
27 |
|
28 |
getframe(s) /* get a new frame */ |
29 |
char *s; |
30 |
{ |
31 |
if (getrect(s, &pframe) < 0) |
32 |
return; |
33 |
pdepth = 0; |
34 |
} |
35 |
|
36 |
|
37 |
getrepaint(s) /* get area and repaint */ |
38 |
char *s; |
39 |
{ |
40 |
RECT box; |
41 |
|
42 |
if (getrect(s, &box) < 0) |
43 |
return; |
44 |
paintrect(&ptrunk, 0, 0, hresolu, vresolu, &box); |
45 |
} |
46 |
|
47 |
|
48 |
getview(s) /* get/show view parameters */ |
49 |
char *s; |
50 |
{ |
51 |
FILE *fp; |
52 |
char buf[128]; |
53 |
char *fname; |
54 |
int change = 0; |
55 |
VIEW nv; |
56 |
|
57 |
if (sscanf(s, "%s", buf) == 1) { /* write parameters to a file */ |
58 |
if ((fname = getpath(buf, NULL, 0)) == NULL || |
59 |
(fp = fopen(fname, "a")) == NULL) { |
60 |
sprintf(errmsg, "cannot open \"%s\"", buf); |
61 |
error(COMMAND, errmsg); |
62 |
return; |
63 |
} |
64 |
fputs(progname, fp); |
65 |
fprintview(&ourview, fp); |
66 |
fputs(sskip(s), fp); |
67 |
fputs("\n", fp); |
68 |
fclose(fp); |
69 |
return; |
70 |
} |
71 |
sprintf(buf, "view type (%c): ", ourview.type); |
72 |
(*dev->comout)(buf); |
73 |
(*dev->comin)(buf, NULL); |
74 |
if (buf[0] == CTRL(C)) return; |
75 |
if (buf[0] && buf[0] != ourview.type) { |
76 |
nv.type = buf[0]; |
77 |
change++; |
78 |
} else |
79 |
nv.type = ourview.type; |
80 |
sprintf(buf, "view point (%.6g %.6g %.6g): ", |
81 |
ourview.vp[0], ourview.vp[1], ourview.vp[2]); |
82 |
(*dev->comout)(buf); |
83 |
(*dev->comin)(buf, NULL); |
84 |
if (buf[0] == CTRL(C)) return; |
85 |
if (sscanf(buf, "%lf %lf %lf", &nv.vp[0], &nv.vp[1], &nv.vp[2]) == 3) |
86 |
change++; |
87 |
else |
88 |
VCOPY(nv.vp, ourview.vp); |
89 |
sprintf(buf, "view direction (%.6g %.6g %.6g): ", |
90 |
ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); |
91 |
(*dev->comout)(buf); |
92 |
(*dev->comin)(buf, NULL); |
93 |
if (buf[0] == CTRL(C)) return; |
94 |
if (sscanf(buf,"%lf %lf %lf",&nv.vdir[0],&nv.vdir[1],&nv.vdir[2]) == 3) |
95 |
change++; |
96 |
else |
97 |
VCOPY(nv.vdir, ourview.vdir); |
98 |
sprintf(buf, "view up (%.6g %.6g %.6g): ", |
99 |
ourview.vup[0], ourview.vup[1], ourview.vup[2]); |
100 |
(*dev->comout)(buf); |
101 |
(*dev->comin)(buf, NULL); |
102 |
if (buf[0] == CTRL(C)) return; |
103 |
if (sscanf(buf,"%lf %lf %lf",&nv.vup[0],&nv.vup[1],&nv.vup[2]) == 3) |
104 |
change++; |
105 |
else |
106 |
VCOPY(nv.vup, ourview.vup); |
107 |
sprintf(buf, "view horiz and vert size (%.6g %.6g): ", |
108 |
ourview.horiz, ourview.vert); |
109 |
(*dev->comout)(buf); |
110 |
(*dev->comin)(buf, NULL); |
111 |
if (buf[0] == CTRL(C)) return; |
112 |
if (sscanf(buf, "%lf %lf", &nv.horiz, &nv.vert) == 2) |
113 |
change++; |
114 |
else { |
115 |
nv.horiz = ourview.horiz; nv.vert = ourview.vert; |
116 |
} |
117 |
sprintf(buf, "view shift and lift (%.6g %.6g): ", |
118 |
ourview.hoff, ourview.voff); |
119 |
(*dev->comout)(buf); |
120 |
(*dev->comin)(buf, NULL); |
121 |
if (buf[0] == CTRL(C)) return; |
122 |
if (sscanf(buf, "%lf %lf", &nv.hoff, &nv.voff) == 2) |
123 |
change++; |
124 |
else { |
125 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
126 |
} |
127 |
if (change) |
128 |
newview(&nv); |
129 |
} |
130 |
|
131 |
|
132 |
lastview(s) /* return to a previous view */ |
133 |
char *s; |
134 |
{ |
135 |
char buf[128]; |
136 |
char *fname; |
137 |
int success; |
138 |
VIEW nv; |
139 |
|
140 |
if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */ |
141 |
copystruct(&nv, &stdview); |
142 |
if ((fname = getpath(buf, NULL, 0)) == NULL || |
143 |
(success = viewfile(fname, &nv, 0, 0)) == -1) { |
144 |
sprintf(errmsg, "cannot open \"%s\"", buf); |
145 |
error(COMMAND, errmsg); |
146 |
return; |
147 |
} |
148 |
if (!success) |
149 |
error(COMMAND, "wrong file format"); |
150 |
else |
151 |
newview(&nv); |
152 |
return; |
153 |
} |
154 |
if (oldview.type == 0) { /* no old view! */ |
155 |
error(COMMAND, "no previous view"); |
156 |
return; |
157 |
} |
158 |
copystruct(&nv, &ourview); |
159 |
copystruct(&ourview, &oldview); |
160 |
copystruct(&oldview, &nv); |
161 |
newimage(); |
162 |
} |
163 |
|
164 |
|
165 |
getaim(s) /* aim camera */ |
166 |
char *s; |
167 |
{ |
168 |
extern double tan(), atan(); |
169 |
double zfact; |
170 |
VIEW nv; |
171 |
|
172 |
if (getinterest(s, 1, nv.vdir, &zfact) < 0) |
173 |
return; |
174 |
nv.type = ourview.type; |
175 |
VCOPY(nv.vp, ourview.vp); |
176 |
VCOPY(nv.vup, ourview.vup); |
177 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
178 |
nv.horiz = ourview.horiz; nv.vert = ourview.vert; |
179 |
zoomview(&nv, zfact); |
180 |
newview(&nv); |
181 |
} |
182 |
|
183 |
|
184 |
getmove(s) /* move camera */ |
185 |
char *s; |
186 |
{ |
187 |
FVECT vc; |
188 |
double mag; |
189 |
|
190 |
if (getinterest(s, 0, vc, &mag) < 0) |
191 |
return; |
192 |
moveview(0.0, 0.0, mag, vc); |
193 |
} |
194 |
|
195 |
|
196 |
getrotate(s) /* rotate camera */ |
197 |
char *s; |
198 |
{ |
199 |
extern double normalize(), tan(), atan(); |
200 |
VIEW nv; |
201 |
FVECT v1; |
202 |
double angle, elev, zfact; |
203 |
|
204 |
elev = 0.0; zfact = 1.0; |
205 |
if (sscanf(s, "%lf %lf %lf", &angle, &elev, &zfact) < 1) { |
206 |
error(COMMAND, "missing angle"); |
207 |
return; |
208 |
} |
209 |
nv.type = ourview.type; |
210 |
VCOPY(nv.vp, ourview.vp); |
211 |
VCOPY(nv.vup, ourview.vup); |
212 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
213 |
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
214 |
if (elev != 0.0) { |
215 |
fcross(v1, nv.vdir, ourview.vup); |
216 |
normalize(v1); |
217 |
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
218 |
} |
219 |
nv.horiz = ourview.horiz; nv.vert = ourview.vert; |
220 |
zoomview(&nv, zfact); |
221 |
newview(&nv); |
222 |
} |
223 |
|
224 |
|
225 |
getpivot(s) /* pivot viewpoint */ |
226 |
register char *s; |
227 |
{ |
228 |
FVECT vc; |
229 |
double angle, elev, mag; |
230 |
|
231 |
elev = 0.0; |
232 |
if (sscanf(s, "%lf %lf", &angle, &elev) < 1) { |
233 |
error(COMMAND, "missing angle"); |
234 |
return; |
235 |
} |
236 |
if (getinterest(sskip(sskip(s)), 0, vc, &mag) < 0) |
237 |
return; |
238 |
moveview(angle, elev, mag, vc); |
239 |
} |
240 |
|
241 |
|
242 |
getexposure(s) /* get new exposure */ |
243 |
char *s; |
244 |
{ |
245 |
double atof(), pow(), fabs(); |
246 |
char buf[128]; |
247 |
register char *cp; |
248 |
register PNODE *p; |
249 |
RECT r; |
250 |
int x, y; |
251 |
double e; |
252 |
|
253 |
for (cp = s; isspace(*cp); cp++) |
254 |
; |
255 |
if (*cp == '\0') { /* normalize to point */ |
256 |
if (dev->getcur == NULL) |
257 |
return; |
258 |
(*dev->comout)("Pick point for exposure\n"); |
259 |
if ((*dev->getcur)(&x, &y) == ABORT) |
260 |
return; |
261 |
r.l = r.d = 0; |
262 |
r.r = hresolu; r.u = vresolu; |
263 |
p = findrect(x, y, &ptrunk, &r, -1); |
264 |
e = 1.0; |
265 |
} else { |
266 |
if (*cp == '=') { /* absolute setting */ |
267 |
p = NULL; |
268 |
e = 1.0/exposure; |
269 |
for (cp++; isspace(*cp); cp++) |
270 |
; |
271 |
if (*cp == '\0') { /* interactive */ |
272 |
sprintf(buf, "exposure (%lf): ", exposure); |
273 |
(*dev->comout)(buf); |
274 |
(*dev->comin)(buf, NULL); |
275 |
for (cp = buf; isspace(*cp); cp++) |
276 |
; |
277 |
if (*cp == '\0') |
278 |
return; |
279 |
} |
280 |
} else { /* normalize to average */ |
281 |
p = &ptrunk; |
282 |
e = 1.0; |
283 |
} |
284 |
if (*cp == '+' || *cp == '-') /* f-stops */ |
285 |
e *= pow(2.0, atof(cp)); |
286 |
else /* multiplier */ |
287 |
e *= atof(cp); |
288 |
} |
289 |
if (p != NULL) { /* relative setting */ |
290 |
if (bright(p->v) <= FTINY) { |
291 |
error(COMMAND, "cannot normalize to zero"); |
292 |
return; |
293 |
} |
294 |
e *= 0.5 / bright(p->v); |
295 |
} |
296 |
if (e <= FTINY || fabs(1.0 - e) <= FTINY) |
297 |
return; |
298 |
scalepict(&ptrunk, e); |
299 |
exposure *= e; |
300 |
redraw(); |
301 |
} |
302 |
|
303 |
|
304 |
getparam(str, dsc, typ, ptr) /* get variable from user */ |
305 |
char *str, *dsc; |
306 |
int typ; |
307 |
register union {int i; double d; COLOR C;} *ptr; |
308 |
{ |
309 |
int i0; |
310 |
double d0, d1, d2; |
311 |
char buf[48]; |
312 |
|
313 |
switch (typ) { |
314 |
case 'i': /* integer */ |
315 |
if (sscanf(str, "%d", &i0) != 1) { |
316 |
(*dev->comout)(dsc); |
317 |
sprintf(buf, " (%d): ", ptr->i); |
318 |
(*dev->comout)(buf); |
319 |
(*dev->comin)(buf, NULL); |
320 |
if (sscanf(buf, "%d", &i0) != 1) |
321 |
break; |
322 |
} |
323 |
ptr->i = i0; |
324 |
break; |
325 |
case 'r': /* real */ |
326 |
if (sscanf(str, "%lf", &d0) != 1) { |
327 |
(*dev->comout)(dsc); |
328 |
sprintf(buf, " (%.6g): ", ptr->d); |
329 |
(*dev->comout)(buf); |
330 |
(*dev->comin)(buf, NULL); |
331 |
if (sscanf(buf, "%lf", &d0) != 1) |
332 |
break; |
333 |
} |
334 |
ptr->d = d0; |
335 |
break; |
336 |
case 'b': /* boolean */ |
337 |
if (sscanf(str, "%1s", buf) != 1) { |
338 |
(*dev->comout)(dsc); |
339 |
sprintf(buf, " (%c): ", ptr->i ? 'y' : 'n'); |
340 |
(*dev->comout)(buf); |
341 |
(*dev->comin)(buf, NULL); |
342 |
} |
343 |
ptr->i = tolower(buf[0]) == 'y'; |
344 |
break; |
345 |
case 'C': /* color */ |
346 |
if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) { |
347 |
(*dev->comout)(dsc); |
348 |
sprintf(buf, " (%.6g %.6g %.6g): ", |
349 |
colval(ptr->C,RED), |
350 |
colval(ptr->C,GRN), |
351 |
colval(ptr->C,BLU)); |
352 |
(*dev->comout)(buf); |
353 |
(*dev->comin)(buf, NULL); |
354 |
if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3) |
355 |
break; |
356 |
} |
357 |
setcolor(ptr->C, d0, d1, d2); |
358 |
break; |
359 |
} |
360 |
} |
361 |
|
362 |
|
363 |
setparam(s) /* get/set program parameter */ |
364 |
register char *s; |
365 |
{ |
366 |
extern int psample; |
367 |
extern double maxdiff; |
368 |
extern double minweight; |
369 |
extern int maxdepth; |
370 |
extern double dstrsrc; |
371 |
extern double shadthresh; |
372 |
extern double shadcert; |
373 |
extern COLOR ambval; |
374 |
extern double ambacc; |
375 |
extern double minarad; |
376 |
extern int ambres; |
377 |
extern int ambdiv; |
378 |
extern int ambssamp; |
379 |
extern int ambounce; |
380 |
extern int directinvis; |
381 |
extern int do_irrad; |
382 |
char buf[128]; |
383 |
|
384 |
if (s[0] == '\0') { |
385 |
(*dev->comout)( |
386 |
"aa ab ad ar as av b dc di dj dt i lr lw sp st: "); |
387 |
(*dev->comin)(buf, NULL); |
388 |
s = buf; |
389 |
} |
390 |
switch (s[0]) { |
391 |
case 'l': /* limit */ |
392 |
switch (s[1]) { |
393 |
case 'w': /* weight */ |
394 |
getparam(s+2, "limit weight", 'r', &minweight); |
395 |
break; |
396 |
case 'r': /* reflection */ |
397 |
getparam(s+2, "limit reflection", 'i', &maxdepth); |
398 |
break; |
399 |
default: |
400 |
goto badparam; |
401 |
} |
402 |
break; |
403 |
case 'd': /* direct */ |
404 |
switch (s[1]) { |
405 |
case 'j': /* jitter */ |
406 |
getparam(s+2, "direct jitter", 'r', &dstrsrc); |
407 |
break; |
408 |
case 'c': /* certainty */ |
409 |
getparam(s+2, "direct certainty", 'r', &shadcert); |
410 |
break; |
411 |
case 't': /* threshold */ |
412 |
getparam(s+2, "direct threshold", 'r', &shadthresh); |
413 |
break; |
414 |
case 'i': /* invisibility */ |
415 |
getparam(s+2, "direct invisibility", |
416 |
'b', &directinvis); |
417 |
break; |
418 |
default: |
419 |
goto badparam; |
420 |
} |
421 |
break; |
422 |
case 'b': /* black and white */ |
423 |
getparam(s+1, "black and white", 'b', &greyscale); |
424 |
break; |
425 |
case 'i': /* irradiance */ |
426 |
getparam(s+1, "irradiance", 'b', &do_irrad); |
427 |
break; |
428 |
case 'a': /* ambient */ |
429 |
switch (s[1]) { |
430 |
case 'v': /* value */ |
431 |
getparam(s+2, "ambient value", 'C', ambval); |
432 |
break; |
433 |
case 'a': /* accuracy */ |
434 |
getparam(s+2, "ambient accuracy", 'r', &ambacc); |
435 |
break; |
436 |
case 'd': /* divisions */ |
437 |
getparam(s+2, "ambient divisions", 'i', &ambdiv); |
438 |
break; |
439 |
case 's': /* samples */ |
440 |
getparam(s+2, "ambient super-samples", 'i', &ambssamp); |
441 |
break; |
442 |
case 'b': /* bounces */ |
443 |
getparam(s+2, "ambient bounces", 'i', &ambounce); |
444 |
break; |
445 |
case 'r': |
446 |
getparam(s+2, "ambient resolution", 'i', &ambres); |
447 |
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
448 |
break; |
449 |
default: |
450 |
goto badparam; |
451 |
} |
452 |
break; |
453 |
case 's': /* sample */ |
454 |
switch (s[1]) { |
455 |
case 'p': /* pixel */ |
456 |
getparam(s+2, "sample pixel", 'i', &psample); |
457 |
pdepth = 0; |
458 |
break; |
459 |
case 't': /* threshold */ |
460 |
getparam(s+2, "sample threshold", 'r', &maxdiff); |
461 |
pdepth = 0; |
462 |
break; |
463 |
default: |
464 |
goto badparam; |
465 |
} |
466 |
break; |
467 |
case '\0': /* nothing */ |
468 |
break; |
469 |
default:; |
470 |
badparam: |
471 |
sprintf(errmsg, "%s: unknown variable", s); |
472 |
error(COMMAND, errmsg); |
473 |
break; |
474 |
} |
475 |
} |
476 |
|
477 |
|
478 |
traceray(s) /* trace a single ray */ |
479 |
char *s; |
480 |
{ |
481 |
char buf[128]; |
482 |
int x, y; |
483 |
RAY thisray; |
484 |
|
485 |
if (sscanf(s, "%lf %lf %lf %lf %lf %lf", |
486 |
&thisray.rorg[0], &thisray.rorg[1], &thisray.rorg[2], |
487 |
&thisray.rdir[0], &thisray.rdir[1], &thisray.rdir[2]) != 6) { |
488 |
|
489 |
if (dev->getcur == NULL) |
490 |
return; |
491 |
(*dev->comout)("Pick ray\n"); |
492 |
if ((*dev->getcur)(&x, &y) == ABORT) |
493 |
return; |
494 |
|
495 |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
496 |
(x+.5)/hresolu, (y+.5)/vresolu) < 0) { |
497 |
error(COMMAND, "not on image"); |
498 |
return; |
499 |
} |
500 |
|
501 |
} else if (normalize(thisray.rdir) == 0.0) { |
502 |
error(COMMAND, "zero ray direction"); |
503 |
return; |
504 |
} |
505 |
|
506 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
507 |
|
508 |
rayvalue(&thisray); |
509 |
|
510 |
if (thisray.ro == NULL) |
511 |
(*dev->comout)("ray hit nothing"); |
512 |
else { |
513 |
sprintf(buf, "ray hit %s %s \"%s\"", |
514 |
objptr(thisray.ro->omod)->oname, |
515 |
ofun[thisray.ro->otype].funame, |
516 |
thisray.ro->oname); |
517 |
(*dev->comout)(buf); |
518 |
(*dev->comin)(buf, NULL); |
519 |
if (thisray.rot >= FHUGE) |
520 |
(*dev->comout)("at infinity"); |
521 |
else { |
522 |
sprintf(buf, "at (%.6g %.6g %.6g)", thisray.rop[0], |
523 |
thisray.rop[1], thisray.rop[2]); |
524 |
(*dev->comout)(buf); |
525 |
} |
526 |
(*dev->comin)(buf, NULL); |
527 |
sprintf(buf, "with value (%.6g %.6g %.6g)", |
528 |
colval(thisray.rcol,RED), |
529 |
colval(thisray.rcol,GRN), |
530 |
colval(thisray.rcol,BLU)); |
531 |
(*dev->comout)(buf); |
532 |
} |
533 |
(*dev->comin)(buf, NULL); |
534 |
} |
535 |
|
536 |
|
537 |
writepict(s) /* write the picture to a file */ |
538 |
char *s; |
539 |
{ |
540 |
static char buf[128]; |
541 |
char *fname; |
542 |
FILE *fp; |
543 |
COLR *scanline; |
544 |
int y; |
545 |
|
546 |
if (sscanf(s, "%s", buf) != 1 && buf[0] == '\0') { |
547 |
error(COMMAND, "no file"); |
548 |
return; |
549 |
} |
550 |
if ((fname = getpath(buf, NULL, 0)) == NULL || |
551 |
(fp = fopen(fname, "w")) == NULL) { |
552 |
sprintf(errmsg, "cannot open \"%s\"", buf); |
553 |
error(COMMAND, errmsg); |
554 |
return; |
555 |
} |
556 |
(*dev->comout)("writing \""); |
557 |
(*dev->comout)(fname); |
558 |
(*dev->comout)("\"...\n"); |
559 |
/* write header */ |
560 |
fputs(progname, fp); |
561 |
fprintview(&ourview, fp); |
562 |
putc('\n', fp); |
563 |
if (exposure != 1.0) |
564 |
fputexpos(exposure, fp); |
565 |
if (dev->pixaspect != 1.0) |
566 |
fputaspect(dev->pixaspect, fp); |
567 |
fputformat(COLRFMT, fp); |
568 |
putc('\n', fp); |
569 |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, fp); |
570 |
|
571 |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
572 |
if (scanline == NULL) |
573 |
error(SYSTEM, "out of memory in writepict"); |
574 |
for (y = vresolu-1; y >= 0; y--) { |
575 |
getpictcolrs(y, scanline, &ptrunk, hresolu, vresolu); |
576 |
if (fwritecolrs(scanline, hresolu, fp) < 0) |
577 |
break; |
578 |
} |
579 |
if (fclose(fp) < 0) |
580 |
error(COMMAND, "write error"); |
581 |
free((char *)scanline); |
582 |
} |