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