1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rv2.c,v 2.42 2003/07/21 22:30:19 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* rv2.c - command routines used in tracing a view. |
6 |
* |
7 |
* External symbols declared in rpaint.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
#include <ctype.h> |
13 |
#include <string.h> |
14 |
|
15 |
#include "platform.h" |
16 |
#include "ray.h" |
17 |
#include "ambient.h" |
18 |
#include "otypes.h" |
19 |
#include "rpaint.h" |
20 |
|
21 |
extern int psample; /* pixel sample size */ |
22 |
extern double maxdiff; /* max. sample difference */ |
23 |
|
24 |
#define CTRL(c) ((c)-'@') |
25 |
|
26 |
#ifdef SMLFLT |
27 |
#define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3) |
28 |
#else |
29 |
#define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3) |
30 |
#endif |
31 |
|
32 |
extern char rifname[128]; /* rad input file name */ |
33 |
|
34 |
extern char *progname; |
35 |
extern char *octname; |
36 |
|
37 |
|
38 |
extern void |
39 |
getframe( /* get a new frame */ |
40 |
char *s |
41 |
) |
42 |
{ |
43 |
if (getrect(s, &pframe) < 0) |
44 |
return; |
45 |
pdepth = 0; |
46 |
} |
47 |
|
48 |
|
49 |
extern void |
50 |
getrepaint( /* get area and repaint */ |
51 |
char *s |
52 |
) |
53 |
{ |
54 |
RECT box; |
55 |
|
56 |
if (getrect(s, &box) < 0) |
57 |
return; |
58 |
paintrect(&ptrunk, 0, 0, hresolu, vresolu, &box); |
59 |
} |
60 |
|
61 |
|
62 |
extern void |
63 |
getview( /* get/show view parameters */ |
64 |
char *s |
65 |
) |
66 |
{ |
67 |
FILE *fp; |
68 |
char buf[128]; |
69 |
char *fname; |
70 |
int change = 0; |
71 |
VIEW nv; |
72 |
|
73 |
while (isspace(*s)) |
74 |
s++; |
75 |
if (*s == '-') { /* command line parameters */ |
76 |
nv = ourview; |
77 |
if (sscanview(&nv, s)) |
78 |
newview(&nv); |
79 |
else |
80 |
error(COMMAND, "bad view option(s)"); |
81 |
return; |
82 |
} |
83 |
if (sscanf(s, "%s", buf) == 1) { /* write parameters to a file */ |
84 |
if ((fname = getpath(buf, NULL, 0)) == NULL || |
85 |
(fp = fopen(fname, "a")) == NULL) { |
86 |
sprintf(errmsg, "cannot open \"%s\"", buf); |
87 |
error(COMMAND, errmsg); |
88 |
return; |
89 |
} |
90 |
fputs(progname, fp); |
91 |
fprintview(&ourview, fp); |
92 |
fputs(sskip(s), fp); |
93 |
putc('\n', fp); |
94 |
fclose(fp); |
95 |
return; |
96 |
} |
97 |
sprintf(buf, "view type (%c): ", ourview.type); |
98 |
(*dev->comout)(buf); |
99 |
(*dev->comin)(buf, NULL); |
100 |
if (buf[0] == CTRL('C')) return; |
101 |
if (buf[0] && buf[0] != ourview.type) { |
102 |
nv.type = buf[0]; |
103 |
change++; |
104 |
} else |
105 |
nv.type = ourview.type; |
106 |
sprintf(buf, "view point (%.6g %.6g %.6g): ", |
107 |
ourview.vp[0], ourview.vp[1], ourview.vp[2]); |
108 |
(*dev->comout)(buf); |
109 |
(*dev->comin)(buf, NULL); |
110 |
if (buf[0] == CTRL('C')) return; |
111 |
if (sscanvec(buf, nv.vp)) |
112 |
change++; |
113 |
else |
114 |
VCOPY(nv.vp, ourview.vp); |
115 |
sprintf(buf, "view direction (%.6g %.6g %.6g): ", |
116 |
ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); |
117 |
(*dev->comout)(buf); |
118 |
(*dev->comin)(buf, NULL); |
119 |
if (buf[0] == CTRL('C')) return; |
120 |
if (sscanvec(buf, nv.vdir)) |
121 |
change++; |
122 |
else |
123 |
VCOPY(nv.vdir, ourview.vdir); |
124 |
sprintf(buf, "view up (%.6g %.6g %.6g): ", |
125 |
ourview.vup[0], ourview.vup[1], ourview.vup[2]); |
126 |
(*dev->comout)(buf); |
127 |
(*dev->comin)(buf, NULL); |
128 |
if (buf[0] == CTRL('C')) return; |
129 |
if (sscanvec(buf, nv.vup)) |
130 |
change++; |
131 |
else |
132 |
VCOPY(nv.vup, ourview.vup); |
133 |
sprintf(buf, "view horiz and vert size (%.6g %.6g): ", |
134 |
ourview.horiz, ourview.vert); |
135 |
(*dev->comout)(buf); |
136 |
(*dev->comin)(buf, NULL); |
137 |
if (buf[0] == CTRL('C')) return; |
138 |
if (sscanf(buf, "%lf %lf", &nv.horiz, &nv.vert) == 2) |
139 |
change++; |
140 |
else { |
141 |
nv.horiz = ourview.horiz; nv.vert = ourview.vert; |
142 |
} |
143 |
sprintf(buf, "fore and aft clipping plane (%.6g %.6g): ", |
144 |
ourview.vfore, ourview.vaft); |
145 |
(*dev->comout)(buf); |
146 |
(*dev->comin)(buf, NULL); |
147 |
if (buf[0] == CTRL('C')) return; |
148 |
if (sscanf(buf, "%lf %lf", &nv.vfore, &nv.vaft) == 2) |
149 |
change++; |
150 |
else { |
151 |
nv.vfore = ourview.vfore; nv.vaft = ourview.vaft; |
152 |
} |
153 |
sprintf(buf, "view shift and lift (%.6g %.6g): ", |
154 |
ourview.hoff, ourview.voff); |
155 |
(*dev->comout)(buf); |
156 |
(*dev->comin)(buf, NULL); |
157 |
if (buf[0] == CTRL('C')) return; |
158 |
if (sscanf(buf, "%lf %lf", &nv.hoff, &nv.voff) == 2) |
159 |
change++; |
160 |
else { |
161 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
162 |
} |
163 |
if (change) |
164 |
newview(&nv); |
165 |
} |
166 |
|
167 |
|
168 |
extern void |
169 |
lastview( /* return to a previous view */ |
170 |
char *s |
171 |
) |
172 |
{ |
173 |
char buf[128]; |
174 |
char *fname; |
175 |
int success; |
176 |
VIEW nv; |
177 |
|
178 |
if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */ |
179 |
nv = stdview; |
180 |
if ((fname = getpath(buf, "", R_OK)) == NULL || |
181 |
(success = viewfile(fname, &nv, NULL)) == -1) { |
182 |
sprintf(errmsg, "cannot open \"%s\"", buf); |
183 |
error(COMMAND, errmsg); |
184 |
return; |
185 |
} |
186 |
if (!success) |
187 |
error(COMMAND, "wrong file format"); |
188 |
else |
189 |
newview(&nv); |
190 |
return; |
191 |
} |
192 |
if (oldview.type == 0) { /* no old view! */ |
193 |
error(COMMAND, "no previous view"); |
194 |
return; |
195 |
} |
196 |
nv = ourview; |
197 |
ourview = oldview; |
198 |
oldview = nv; |
199 |
newimage(); |
200 |
} |
201 |
|
202 |
|
203 |
extern void |
204 |
saveview( /* save view to rad file */ |
205 |
char *s |
206 |
) |
207 |
{ |
208 |
char view[64]; |
209 |
char *fname; |
210 |
FILE *fp; |
211 |
|
212 |
if (*atos(view, sizeof(view), s)) { |
213 |
if (isint(view)) { |
214 |
error(COMMAND, "cannot write view by number"); |
215 |
return; |
216 |
} |
217 |
s = sskip(s); |
218 |
} |
219 |
while (isspace(*s)) |
220 |
s++; |
221 |
if (*s) |
222 |
atos(rifname, sizeof(rifname), s); |
223 |
else if (rifname[0] == '\0') { |
224 |
error(COMMAND, "no previous rad file"); |
225 |
return; |
226 |
} |
227 |
if ((fname = getpath(rifname, NULL, 0)) == NULL || |
228 |
(fp = fopen(fname, "a")) == NULL) { |
229 |
sprintf(errmsg, "cannot open \"%s\"", rifname); |
230 |
error(COMMAND, errmsg); |
231 |
return; |
232 |
} |
233 |
fputs("view= ", fp); |
234 |
fputs(view, fp); |
235 |
fprintview(&ourview, fp); |
236 |
putc('\n', fp); |
237 |
fclose(fp); |
238 |
} |
239 |
|
240 |
|
241 |
extern void |
242 |
loadview( /* load view from rad file */ |
243 |
char *s |
244 |
) |
245 |
{ |
246 |
char buf[512]; |
247 |
char *fname; |
248 |
FILE *fp; |
249 |
VIEW nv; |
250 |
|
251 |
strcpy(buf, "rad -n -s -V -v "); |
252 |
if (sscanf(s, "%s", buf+strlen(buf)) == 1) |
253 |
s = sskip(s); |
254 |
else |
255 |
strcat(buf, "1"); |
256 |
if (*s) |
257 |
atos(rifname, sizeof(rifname), s); |
258 |
else if (rifname[0] == '\0') { |
259 |
error(COMMAND, "no previous rad file"); |
260 |
return; |
261 |
} |
262 |
if ((fname = getpath(rifname, "", R_OK)) == NULL) { |
263 |
sprintf(errmsg, "cannot access \"%s\"", rifname); |
264 |
error(COMMAND, errmsg); |
265 |
return; |
266 |
} |
267 |
sprintf(buf+strlen(buf), " %s", fname); |
268 |
if ((fp = popen(buf, "r")) == NULL) { |
269 |
error(COMMAND, "cannot run rad"); |
270 |
return; |
271 |
} |
272 |
buf[0] = '\0'; |
273 |
fgets(buf, sizeof(buf), fp); |
274 |
pclose(fp); |
275 |
nv = stdview; |
276 |
if (!sscanview(&nv, buf)) { |
277 |
error(COMMAND, "rad error -- no such view?"); |
278 |
return; |
279 |
} |
280 |
newview(&nv); |
281 |
} |
282 |
|
283 |
|
284 |
extern void |
285 |
getaim( /* aim camera */ |
286 |
char *s |
287 |
) |
288 |
{ |
289 |
double zfact; |
290 |
VIEW nv; |
291 |
|
292 |
if (getinterest(s, 1, nv.vdir, &zfact) < 0) |
293 |
return; |
294 |
nv.type = ourview.type; |
295 |
VCOPY(nv.vp, ourview.vp); |
296 |
VCOPY(nv.vup, ourview.vup); |
297 |
nv.horiz = ourview.horiz; nv.vert = ourview.vert; |
298 |
nv.vfore = ourview.vfore; nv.vaft = ourview.vaft; |
299 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
300 |
zoomview(&nv, zfact); |
301 |
newview(&nv); |
302 |
} |
303 |
|
304 |
|
305 |
extern void |
306 |
getmove( /* move camera */ |
307 |
char *s |
308 |
) |
309 |
{ |
310 |
FVECT vc; |
311 |
double mag; |
312 |
|
313 |
if (getinterest(s, 0, vc, &mag) < 0) |
314 |
return; |
315 |
moveview(0.0, 0.0, mag, vc); |
316 |
} |
317 |
|
318 |
|
319 |
extern void |
320 |
getrotate( /* rotate camera */ |
321 |
char *s |
322 |
) |
323 |
{ |
324 |
VIEW nv; |
325 |
FVECT v1; |
326 |
double angle, elev, zfact; |
327 |
|
328 |
elev = 0.0; zfact = 1.0; |
329 |
if (sscanf(s, "%lf %lf %lf", &angle, &elev, &zfact) < 1) { |
330 |
error(COMMAND, "missing angle"); |
331 |
return; |
332 |
} |
333 |
nv.type = ourview.type; |
334 |
VCOPY(nv.vp, ourview.vp); |
335 |
VCOPY(nv.vup, ourview.vup); |
336 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
337 |
nv.vfore = ourview.vfore; nv.vaft = ourview.vaft; |
338 |
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
339 |
if (elev != 0.0) { |
340 |
fcross(v1, nv.vdir, ourview.vup); |
341 |
normalize(v1); |
342 |
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
343 |
} |
344 |
nv.horiz = ourview.horiz; nv.vert = ourview.vert; |
345 |
zoomview(&nv, zfact); |
346 |
newview(&nv); |
347 |
} |
348 |
|
349 |
|
350 |
extern void |
351 |
getpivot( /* pivot viewpoint */ |
352 |
register char *s |
353 |
) |
354 |
{ |
355 |
FVECT vc; |
356 |
double angle, elev, mag; |
357 |
|
358 |
elev = 0.0; |
359 |
if (sscanf(s, "%lf %lf", &angle, &elev) < 1) { |
360 |
error(COMMAND, "missing angle"); |
361 |
return; |
362 |
} |
363 |
if (getinterest(sskip2(s,2), 0, vc, &mag) < 0) |
364 |
return; |
365 |
moveview(angle, elev, mag, vc); |
366 |
} |
367 |
|
368 |
|
369 |
extern void |
370 |
getexposure( /* get new exposure */ |
371 |
char *s |
372 |
) |
373 |
{ |
374 |
char buf[128]; |
375 |
register char *cp; |
376 |
RECT r; |
377 |
int x, y; |
378 |
register PNODE *p = &ptrunk; |
379 |
int adapt = 0; |
380 |
double e = 1.0; |
381 |
|
382 |
for (cp = s; isspace(*cp); cp++) |
383 |
; |
384 |
if (*cp == '@') { |
385 |
adapt++; |
386 |
while (isspace(*++cp)) |
387 |
; |
388 |
} |
389 |
if (*cp == '\0') { /* normalize to point */ |
390 |
if (dev->getcur == NULL) |
391 |
return; |
392 |
(*dev->comout)("Pick point for exposure\n"); |
393 |
if ((*dev->getcur)(&x, &y) == ABORT) |
394 |
return; |
395 |
r.l = r.d = 0; |
396 |
r.r = hresolu; r.u = vresolu; |
397 |
p = findrect(x, y, &ptrunk, &r, -1); |
398 |
} else { |
399 |
if (*cp == '=') { /* absolute setting */ |
400 |
p = NULL; |
401 |
e = 1.0/exposure; |
402 |
for (cp++; isspace(*cp); cp++) |
403 |
; |
404 |
if (*cp == '\0') { /* interactive */ |
405 |
sprintf(buf, "exposure (%f): ", exposure); |
406 |
(*dev->comout)(buf); |
407 |
(*dev->comin)(buf, NULL); |
408 |
for (cp = buf; isspace(*cp); cp++) |
409 |
; |
410 |
if (*cp == '\0') |
411 |
return; |
412 |
} |
413 |
} |
414 |
if (*cp == '+' || *cp == '-') /* f-stops */ |
415 |
e *= pow(2.0, atof(cp)); |
416 |
else /* multiplier */ |
417 |
e *= atof(cp); |
418 |
} |
419 |
if (p != NULL) { /* relative setting */ |
420 |
if (bright(p->v) < 1e-15) { |
421 |
error(COMMAND, "cannot normalize to zero"); |
422 |
return; |
423 |
} |
424 |
if (adapt) |
425 |
e *= 106./pow(1.219+pow(luminance(p->v)/exposure,.4),2.5)/exposure; |
426 |
else |
427 |
e *= 0.5 / bright(p->v); |
428 |
} |
429 |
if (e <= FTINY || fabs(1.0 - e) <= FTINY) |
430 |
return; |
431 |
scalepict(&ptrunk, e); |
432 |
exposure *= e; |
433 |
redraw(); |
434 |
} |
435 |
|
436 |
typedef union {int i; double d; COLOR C;} *MyUptr; |
437 |
|
438 |
extern int |
439 |
getparam( /* get variable from user */ |
440 |
char *str, |
441 |
char *dsc, |
442 |
int typ, |
443 |
void *p |
444 |
) |
445 |
{ |
446 |
register MyUptr ptr = (MyUptr)p; |
447 |
int i0; |
448 |
double d0, d1, d2; |
449 |
char buf[48]; |
450 |
|
451 |
switch (typ) { |
452 |
case 'i': /* integer */ |
453 |
if (sscanf(str, "%d", &i0) != 1) { |
454 |
(*dev->comout)(dsc); |
455 |
sprintf(buf, " (%d): ", ptr->i); |
456 |
(*dev->comout)(buf); |
457 |
(*dev->comin)(buf, NULL); |
458 |
if (sscanf(buf, "%d", &i0) != 1) |
459 |
return(0); |
460 |
} |
461 |
ptr->i = i0; |
462 |
return(1); |
463 |
case 'r': /* real */ |
464 |
if (sscanf(str, "%lf", &d0) != 1) { |
465 |
(*dev->comout)(dsc); |
466 |
sprintf(buf, " (%.6g): ", ptr->d); |
467 |
(*dev->comout)(buf); |
468 |
(*dev->comin)(buf, NULL); |
469 |
if (sscanf(buf, "%lf", &d0) != 1) |
470 |
return(0); |
471 |
} |
472 |
ptr->d = d0; |
473 |
return(1); |
474 |
case 'b': /* boolean */ |
475 |
if (sscanf(str, "%1s", buf) != 1) { |
476 |
(*dev->comout)(dsc); |
477 |
sprintf(buf, "? (%c): ", ptr->i ? 'y' : 'n'); |
478 |
(*dev->comout)(buf); |
479 |
(*dev->comin)(buf, NULL); |
480 |
if (buf[0] == '\0' || |
481 |
strchr("yY+1tTnN-0fF", buf[0]) == NULL) |
482 |
return(0); |
483 |
} |
484 |
ptr->i = strchr("yY+1tT", buf[0]) != NULL; |
485 |
return(1); |
486 |
case 'C': /* color */ |
487 |
if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) { |
488 |
(*dev->comout)(dsc); |
489 |
sprintf(buf, " (%.6g %.6g %.6g): ", |
490 |
colval(ptr->C,RED), |
491 |
colval(ptr->C,GRN), |
492 |
colval(ptr->C,BLU)); |
493 |
(*dev->comout)(buf); |
494 |
(*dev->comin)(buf, NULL); |
495 |
if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3) |
496 |
return(0); |
497 |
} |
498 |
setcolor(ptr->C, d0, d1, d2); |
499 |
return(1); |
500 |
} |
501 |
return 0; /* nothing matched */ |
502 |
} |
503 |
|
504 |
|
505 |
extern void |
506 |
setparam( /* get/set program parameter */ |
507 |
register char *s |
508 |
) |
509 |
{ |
510 |
char buf[128]; |
511 |
|
512 |
if (s[0] == '\0') { |
513 |
(*dev->comout)( |
514 |
"aa ab ad ar as av aw b dc dv dj ds dt i lr lw me ma mg ms ps pt sj st bv: "); |
515 |
(*dev->comin)(buf, NULL); |
516 |
s = buf; |
517 |
} |
518 |
switch (s[0]) { |
519 |
case 'l': /* limit */ |
520 |
switch (s[1]) { |
521 |
case 'w': /* weight */ |
522 |
getparam(s+2, "limit weight", 'r', |
523 |
(void *)&minweight); |
524 |
break; |
525 |
case 'r': /* reflection */ |
526 |
getparam(s+2, "limit reflection", 'i', |
527 |
(void *)&maxdepth); |
528 |
break; |
529 |
default: |
530 |
goto badparam; |
531 |
} |
532 |
break; |
533 |
case 'd': /* direct */ |
534 |
switch (s[1]) { |
535 |
case 'j': /* jitter */ |
536 |
getparam(s+2, "direct jitter", 'r', |
537 |
(void *)&dstrsrc); |
538 |
break; |
539 |
case 'c': /* certainty */ |
540 |
getparam(s+2, "direct certainty", 'r', |
541 |
(void *)&shadcert); |
542 |
break; |
543 |
case 't': /* threshold */ |
544 |
getparam(s+2, "direct threshold", 'r', |
545 |
(void *)&shadthresh); |
546 |
break; |
547 |
case 'v': /* visibility */ |
548 |
getparam(s+2, "direct visibility", 'b', |
549 |
(void *)&directvis); |
550 |
break; |
551 |
case 's': /* sampling */ |
552 |
getparam(s+2, "direct sampling", 'r', |
553 |
(void *)&srcsizerat); |
554 |
break; |
555 |
default: |
556 |
goto badparam; |
557 |
} |
558 |
break; |
559 |
case 'b': /* back faces or black and white */ |
560 |
switch (s[1]) { |
561 |
case 'v': /* back face visibility */ |
562 |
getparam(s+2, "back face visibility", 'b', |
563 |
(void *)&backvis); |
564 |
break; |
565 |
case '\0': /* black and white */ |
566 |
case ' ': |
567 |
case 'y': case 'Y': case 't': case 'T': case '1': case '+': |
568 |
case 'n': case 'N': case 'f': case 'F': case '0': case '-': |
569 |
getparam(s+1, "black and white", 'b', |
570 |
(void *)&greyscale); |
571 |
break; |
572 |
default: |
573 |
goto badparam; |
574 |
} |
575 |
break; |
576 |
case 'i': /* irradiance */ |
577 |
getparam(s+1, "irradiance", 'b', |
578 |
(void *)&do_irrad); |
579 |
break; |
580 |
case 'a': /* ambient */ |
581 |
switch (s[1]) { |
582 |
case 'v': /* value */ |
583 |
getparam(s+2, "ambient value", 'C', |
584 |
(void *)ambval); |
585 |
break; |
586 |
case 'w': /* weight */ |
587 |
getparam(s+2, "ambient value weight", 'i', |
588 |
(void *)&ambvwt); |
589 |
break; |
590 |
case 'a': /* accuracy */ |
591 |
if (getparam(s+2, "ambient accuracy", 'r', |
592 |
(void *)&ambacc)) |
593 |
setambacc(ambacc); |
594 |
break; |
595 |
case 'd': /* divisions */ |
596 |
getparam(s+2, "ambient divisions", 'i', |
597 |
(void *)&ambdiv); |
598 |
break; |
599 |
case 's': /* samples */ |
600 |
getparam(s+2, "ambient super-samples", 'i', |
601 |
(void *)&ambssamp); |
602 |
break; |
603 |
case 'b': /* bounces */ |
604 |
getparam(s+2, "ambient bounces", 'i', |
605 |
(void *)&ambounce); |
606 |
break; |
607 |
case 'r': |
608 |
if (getparam(s+2, "ambient resolution", 'i', |
609 |
(void *)&ambres)) |
610 |
setambres(ambres); |
611 |
break; |
612 |
default: |
613 |
goto badparam; |
614 |
} |
615 |
break; |
616 |
case 'm': /* medium */ |
617 |
switch (s[1]) { |
618 |
case 'e': /* extinction coefficient */ |
619 |
getparam(s+2, "extinction coefficient", 'C', |
620 |
(void *)cextinction); |
621 |
break; |
622 |
case 'a': /* scattering albedo */ |
623 |
getparam(s+2, "scattering albedo", 'C', |
624 |
(void *)salbedo); |
625 |
break; |
626 |
case 'g': /* scattering eccentricity */ |
627 |
getparam(s+2, "scattering eccentricity", 'r', |
628 |
(void *)&seccg); |
629 |
break; |
630 |
case 's': /* sampling distance */ |
631 |
getparam(s+2, "mist sampling distance", 'r', |
632 |
(void *)&ssampdist); |
633 |
break; |
634 |
default: |
635 |
goto badparam; |
636 |
} |
637 |
break; |
638 |
case 'p': /* pixel */ |
639 |
switch (s[1]) { |
640 |
case 's': /* sample */ |
641 |
if (getparam(s+2, "pixel sample", 'i', |
642 |
(void *)&psample)) |
643 |
pdepth = 0; |
644 |
break; |
645 |
case 't': /* threshold */ |
646 |
if (getparam(s+2, "pixel threshold", 'r', |
647 |
(void *)&maxdiff)) |
648 |
pdepth = 0; |
649 |
break; |
650 |
default: |
651 |
goto badparam; |
652 |
} |
653 |
break; |
654 |
case 's': /* specular */ |
655 |
switch (s[1]) { |
656 |
case 'j': /* jitter */ |
657 |
getparam(s+2, "specular jitter", 'r', |
658 |
(void *)&specjitter); |
659 |
break; |
660 |
case 't': /* threshold */ |
661 |
getparam(s+2, "specular threshold", 'r', |
662 |
(void *)&specthresh); |
663 |
break; |
664 |
default: |
665 |
goto badparam; |
666 |
} |
667 |
break; |
668 |
case '\0': /* nothing */ |
669 |
break; |
670 |
default:; |
671 |
badparam: |
672 |
*sskip(s) = '\0'; |
673 |
sprintf(errmsg, "%s: unknown variable", s); |
674 |
error(COMMAND, errmsg); |
675 |
break; |
676 |
} |
677 |
} |
678 |
|
679 |
|
680 |
extern void |
681 |
traceray(s) /* trace a single ray */ |
682 |
char *s; |
683 |
{ |
684 |
char buf[128]; |
685 |
int x, y; |
686 |
OBJREC *ino; |
687 |
RAY thisray; |
688 |
|
689 |
thisray.rmax = 0.0; |
690 |
|
691 |
if (!sscanvec(s, thisray.rorg) || |
692 |
!sscanvec(sskip2(s,3), thisray.rdir)) { |
693 |
|
694 |
if (dev->getcur == NULL) |
695 |
return; |
696 |
(*dev->comout)("Pick ray\n"); |
697 |
if ((*dev->getcur)(&x, &y) == ABORT) |
698 |
return; |
699 |
|
700 |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
701 |
&ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) { |
702 |
error(COMMAND, "not on image"); |
703 |
return; |
704 |
} |
705 |
|
706 |
} else if (normalize(thisray.rdir) == 0.0) { |
707 |
error(COMMAND, "zero ray direction"); |
708 |
return; |
709 |
} |
710 |
|
711 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
712 |
|
713 |
rayvalue(&thisray); |
714 |
|
715 |
if (thisray.ro == NULL) |
716 |
(*dev->comout)("ray hit nothing"); |
717 |
else { |
718 |
sprintf(buf, "ray hit %s%s %s \"%s\"", |
719 |
thisray.rod < 0.0 ? "back of " : "", |
720 |
thisray.ro->omod == OVOID ? VOIDID : |
721 |
objptr(thisray.ro->omod)->oname, |
722 |
ofun[thisray.ro->otype].funame, |
723 |
thisray.ro->oname); |
724 |
if ((ino = objptr(thisray.robj)) != thisray.ro) |
725 |
sprintf(buf+strlen(buf), " in %s \"%s\"", |
726 |
ofun[ino->otype].funame, ino->oname); |
727 |
(*dev->comout)(buf); |
728 |
(*dev->comin)(buf, NULL); |
729 |
if (thisray.rot >= FHUGE) |
730 |
(*dev->comout)("at infinity"); |
731 |
else { |
732 |
sprintf(buf, "at (%.6g %.6g %.6g) (%.6g)", |
733 |
thisray.rop[0], thisray.rop[1], |
734 |
thisray.rop[2], thisray.rt); |
735 |
(*dev->comout)(buf); |
736 |
} |
737 |
(*dev->comin)(buf, NULL); |
738 |
sprintf(buf, "value (%.5g %.5g %.5g) (%.3gL)", |
739 |
colval(thisray.rcol,RED), |
740 |
colval(thisray.rcol,GRN), |
741 |
colval(thisray.rcol,BLU), |
742 |
luminance(thisray.rcol)); |
743 |
(*dev->comout)(buf); |
744 |
} |
745 |
(*dev->comin)(buf, NULL); |
746 |
} |
747 |
|
748 |
|
749 |
extern void |
750 |
writepict(s) /* write the picture to a file */ |
751 |
char *s; |
752 |
{ |
753 |
static char buf[128]; |
754 |
char *fname; |
755 |
FILE *fp; |
756 |
COLR *scanline; |
757 |
int y; |
758 |
|
759 |
while (isspace(*s)) |
760 |
s++; |
761 |
if (*s) |
762 |
atos(buf, sizeof(buf), s); |
763 |
else if (buf[0] == '\0') { |
764 |
error(COMMAND, "no file"); |
765 |
return; |
766 |
} |
767 |
if ((fname = getpath(buf, NULL, 0)) == NULL || |
768 |
(fp = fopen(fname, "w")) == NULL) { |
769 |
sprintf(errmsg, "cannot open \"%s\"", buf); |
770 |
error(COMMAND, errmsg); |
771 |
return; |
772 |
} |
773 |
SET_FILE_BINARY(fp); |
774 |
(*dev->comout)("writing \""); |
775 |
(*dev->comout)(fname); |
776 |
(*dev->comout)("\"...\n"); |
777 |
/* write header */ |
778 |
newheader("RADIANCE", fp); |
779 |
fputs(progname, fp); |
780 |
fprintview(&ourview, fp); |
781 |
if (octname != NULL) |
782 |
fprintf(fp, " %s\n", octname); |
783 |
else |
784 |
putc('\n', fp); |
785 |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
786 |
fputnow(fp); |
787 |
if (exposure != 1.0) |
788 |
fputexpos(exposure, fp); |
789 |
if (dev->pixaspect != 1.0) |
790 |
fputaspect(dev->pixaspect, fp); |
791 |
fputformat(COLRFMT, fp); |
792 |
putc('\n', fp); |
793 |
fprtresolu(hresolu, vresolu, fp); |
794 |
|
795 |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
796 |
if (scanline == NULL) { |
797 |
error(COMMAND, "not enough memory!"); |
798 |
fclose(fp); |
799 |
unlink(fname); |
800 |
return; |
801 |
} |
802 |
for (y = vresolu-1; y >= 0; y--) { |
803 |
getpictcolrs(y, scanline, &ptrunk, hresolu, vresolu); |
804 |
if (fwritecolrs(scanline, hresolu, fp) < 0) |
805 |
break; |
806 |
} |
807 |
free((void *)scanline); |
808 |
if (fclose(fp) < 0) |
809 |
error(COMMAND, "write error"); |
810 |
} |