ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv2.c
(Generate patch)

Comparing ray/src/rt/rv2.c (file contents):
Revision 1.6 by greg, Wed Jun 7 08:38:35 1989 UTC vs.
Revision 2.40 by schorsch, Thu Jun 5 19:29:34 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines