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

Comparing ray/src/gen/mkillum.c (file contents):
Revision 1.1 by greg, Tue Jul 23 12:00:29 1991 UTC vs.
Revision 1.7 by greg, Thu Jul 25 12:52:16 1991 UTC

# Line 8 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   * Make illum sources for optimizing rendering process
9   */
10  
11 < #include  "standard.h"
11 > #include  "mkillum.h"
12  
13 < #include  "object.h"
13 > #include  <signal.h>
14  
15 < #include  "otypes.h"
15 > #include  <ctype.h>
16  
17                                  /* default parameters */
18   #define  SAMPDENS       128             /* points per projected steradian */
# Line 25 | Line 25 | static char SCCSid[] = "$SunId$ LBL";
25   #define  S_ALL          3               /* select all */
26  
27                                  /* rtrace command and defaults */
28 < char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-ab", "2",
29 <                "-ad", "256", "-as", "128", "-aa", ".15" };
30 < int  rtargc = 13;
28 > char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-di+",
29 >                "-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", };
30 > int  rtargc = 14;
31                                  /* overriding rtrace options */
32 < char  *myrtopts[] = { "-I-", "-i-", "-di+", "-ov", "-h-", "-fff", NULL };
32 > char  *myrtopts[] = { "-I-", "-i-", "-ov", "-h-", "-fff", NULL };
33  
34 <                                /* illum flags */
35 < #define  IL_FLAT        0x1             /* flat surface */
36 < #define  IL_LIGHT       0x2             /* light rather than illum */
37 < #define  IL_COLDST      0x4             /* colored distribution */
38 < #define  IL_COLAVG      0x8             /* compute average color */
39 < #define  IL_DATCLB      0x10            /* OK to clobber data file */
34 > struct rtproc   rt;             /* our rtrace process */
35  
36 < struct illum_args {
37 <        int     flags;                  /* flags from list above */
38 <        char    matname[MAXSTR];        /* illum material name */
39 <        char    datafile[MAXSTR];       /* distribution data file name */
40 <        int     dfnum;                  /* data file number */
41 <        char    altmatname[MAXSTR];     /* alternate material name */
42 <        int     nsamps;                 /* # of samples in each direction */
43 <        int     nalt, nazi;             /* # of altitude and azimuth angles */
44 <        FVECT   orient;                 /* coordinate system orientation */
50 < } thisillum = {                 /* default values */
51 <        0,
52 <        DFLMAT,
53 <        DFLMAT,
54 <        0,
55 <        VOIDID,
56 <        NSAMPS,
57 < };
36 > struct illum_args  thisillum = {        /* our illum and default values */
37 >                0,
38 >                DFLMAT,
39 >                DFLMAT,
40 >                0,
41 >                VOIDID,
42 >                SAMPDENS,
43 >                NSAMPS,
44 >        };
45  
59 int     sampdens = SAMPDENS;    /* sample point density */
46   char    matcheck[MAXSTR];       /* current material to include or exclude */
47   int     matselect = S_ALL;      /* selection criterion */
48  
49 < int     rt_pd[3];               /* rtrace pipe descriptors */
64 < float   *rt_buf;                /* rtrace i/o buffer */
65 < int     rt_bsiz;                /* maximum rays for rtrace buffer */
66 < int     rt_nrays;               /* current length of rtrace buffer */
49 > FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
50  
51   int     gargc;                  /* global argc */
52   char    **gargv;                /* global argv */
53   #define  progname       gargv[0]
54  
55   int     doneheader = 0;         /* printed header yet? */
56 + #define  checkhead()    if (!doneheader++) printhead(gargc,gargv)
57  
58 + int     warnings = 1;           /* print warnings? */
59 +
60   extern char     *fgetline(), *fgetword(), *sskip(),
61 <                *atos(), *iskip(), *fskip();
61 >                *atos(), *iskip(), *fskip(), *strcpy();
62   extern FILE     *popen();
63  
64  
# Line 82 | Line 68 | char   *argv[];
68   {
69          extern char     *getenv(), *getpath();
70          char    *rtpath;
71 +        FILE    *fp;
72          register int    i;
73                                  /* set global arguments */
74 <        gargc = argc; gargv = argv;
74 >        gargv = argv;
75                                  /* set up rtrace command */
76 <        if (argc < 2)
77 <                error(USER, "too few arguments");
78 <        for (i = 1; i < argc-1; i++)
76 >        for (i = 1; i < argc; i++) {
77 >                if (argv[i][0] == '<' && !argv[i][1])
78 >                        break;
79                  rtargv[rtargc++] = argv[i];
80 +                if (argv[i][0] == '-' && argv[i][1] == 'w')
81 +                        warnings = !warnings;
82 +        }
83 +        if ((gargc = i) < 2)
84 +                error(USER, "too few arguments");
85 +        rtargc--;
86          for (i = 0; myrtopts[i] != NULL; i++)
87                  rtargv[rtargc++] = myrtopts[i];
88 <        rtargv[rtargc++] = argv[argc-1];
88 >        rtargv[rtargc++] = argv[gargc-1];
89          rtargv[rtargc] = NULL;
90                                  /* just asking for defaults? */
91 <        if (!strcmp(argv[argc-1], "-defaults")) {
91 >        if (!strcmp(argv[gargc-1], "-defaults")) {
92                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
93                  if (rtpath == NULL) {
94                          eputs(rtargv[0]);
# Line 108 | Line 101 | char   *argv[];
101          }
102                                  /* else initialize and run our calculation */
103          init();
104 <        mkillum(stdin, "standard input");
105 <        exit(cleanup());        /* cleanup returns rtrace status */
104 >        if (gargc+1 < argc)
105 >                for (i = gargc+1; i < argc; i++) {
106 >                        if ((fp = fopen(argv[i], "r")) == NULL) {
107 >                                sprintf(errmsg,
108 >                                "cannot open scene file \"%s\"", argv[i]);
109 >                                error(SYSTEM, errmsg);
110 >                        }
111 >                        filter(fp, argv[i]);
112 >                        fclose(fp);
113 >                }
114 >        else
115 >                filter(stdin, "standard input");
116 >        quit(0);
117   }
118  
119  
120 + quit(status)                    /* exit with status */
121 + int  status;
122 + {
123 +        int     rtstat;
124 +
125 +        rtstat = close_process(rt.pd);
126 +        if (status == 0)
127 +                if (rtstat < 0)
128 +                        error(WARNING,
129 +                        "unknown return status from rtrace process");
130 +                else
131 +                        status = rtstat;
132 +        exit(status);
133 + }
134 +
135 +
136   init()                          /* start rtrace and set up buffers */
137   {
138 +        extern int  o_face(), o_sphere(), o_ring();
139          int     maxbytes;
140 <
141 <        maxbytes = open_process(rt_pd, rtargv);
140 >                                        /* set up object functions */
141 >        ofun[OBJ_FACE].funp = o_face;
142 >        ofun[OBJ_SPHERE].funp = o_sphere;
143 >        ofun[OBJ_RING].funp = o_ring;
144 >                                        /* set up signal handling */
145 >        signal(SIGPIPE, quit);
146 >                                        /* start rtrace process */
147 >        maxbytes = open_process(rt.pd, rtargv);
148          if (maxbytes == 0) {
149                  eputs(rtargv[0]);
150                  eputs(": command not found\n");
# Line 125 | Line 152 | init()                         /* start rtrace and set up buffers */
152          }
153          if (maxbytes < 0)
154                  error(SYSTEM, "cannot start rtrace process");
155 <        rt_bsiz = maxbytes/(6*sizeof(float));
156 <        rt_buf = (float *)malloc(rt_bsiz*(6*sizeof(float)));
157 <        if (rt_buf == NULL)
155 >        rt.bsiz = maxbytes/(6*sizeof(float));
156 >        rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
157 >        rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
158 >        if (rt.buf == NULL || rt.dest == NULL)
159                  error(SYSTEM, "out of memory in init");
160 <        rt_bsiz--;                      /* allow for flush ray */
161 <        rt_nrays = 0;
160 >        rt.nrays = 0;
161 >                                        /* set up urand */
162 >        initurand(2048);
163   }
164  
165  
137 int
138 cleanup()                       /* close rtrace process and return status */
139 {
140        int     status;
141
142        free((char *)rt_buf);
143        rt_bsiz = 0;
144        status = close_process(rt_pd);
145        if (status < 0)
146                return(0);              /* unknown status */
147        return(status);
148 }
149
150
166   eputs(s)                                /* put string to stderr */
167   register char  *s;
168   {
# Line 163 | Line 178 | register char  *s;
178   }
179  
180  
181 < mkillum(infp, name)             /* process stream */
181 > wputs(s)                        /* print warning if enabled */
182 > char  *s;
183 > {
184 >        if (warnings)
185 >                eputs(s);
186 > }
187 >
188 >
189 > filter(infp, name)              /* process stream */
190   register FILE   *infp;
191   char    *name;
192   {
# Line 177 | Line 200 | char   *name;
200                  if (c == '#') {                         /* comment/options */
201                          buf[0] = c;
202                          fgets(buf+1, sizeof(buf)-1, infp);
203 <                        fputs(buf, stdout);
181 <                        getoptions(buf, name);
203 >                        xoptions(buf, name);
204                  } else if (c == '!') {                  /* command */
205                          buf[0] = c;
206                          fgetline(buf+1, sizeof(buf)-1, infp);
# Line 186 | Line 208 | char   *name;
208                                  sprintf(errmsg, "cannot execute \"%s\"", buf);
209                                  error(SYSTEM, errmsg);
210                          }
211 <                        mkillum(pfp, buf);
211 >                        filter(pfp, buf);
212                          pclose(pfp);
213                  } else {                                /* object */
214                          ungetc(c, infp);
# Line 196 | Line 218 | char   *name;
218   }
219  
220  
221 < getoptions(s, nm)               /* get options from string s */
221 > xoptions(s, nm)                 /* process options in string s */
222   char    *s;
223   char    *nm;
224   {
# Line 205 | Line 227 | char   *nm;
227          int     nerrs = 0;
228          register char   *cp;
229  
230 <        if (strncmp(s, "#@mkillum", 9) || !isspace(s[9]))
230 >        if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
231 >                fputs(s, stdout);               /* not for us */
232                  return;
233 +        }
234          cp = s+10;
235          while (*cp) {
236                  switch (*cp) {
# Line 216 | Line 240 | char   *nm;
240                          cp++;
241                          continue;
242                  case 'm':                       /* material name */
243 <                        cp++;
220 <                        if (*cp != '=')
243 >                        if (*++cp != '=')
244                                  break;
245 <                        cp++;
245 >                        if (!*++cp)
246 >                                break;
247                          atos(thisillum.matname, MAXSTR, cp);
248                          cp = sskip(cp);
249                          if (!(thisillum.flags & IL_DATCLB)) {
# Line 228 | Line 252 | char   *nm;
252                          }
253                          continue;
254                  case 'f':                       /* data file name */
255 <                        cp++;
232 <                        if (*cp != '=')
255 >                        if (*++cp != '=')
256                                  break;
257 <                        cp++;
258 <                        if (*cp == '\0') {
257 >                        if (!*++cp) {
258 >                                strcpy(thisillum.datafile,thisillum.matname);
259                                  thisillum.flags &= ~IL_DATCLB;
260                                  continue;
261                          }
# Line 243 | Line 266 | char   *nm;
266                          continue;
267                  case 'i':                       /* include material */
268                  case 'e':                       /* exclude material */
269 <                        matselect = (*cp++ == 'i') ? S_ELEM : S_COMPL;
247 <                        if (*cp != '=')
269 >                        if (cp[1] != '=')
270                                  break;
271 <                        cp++;
271 >                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
272 >                        cp += 2;
273                          atos(matcheck, MAXSTR, cp);
274                          cp = sskip(cp);
275                          continue;
# Line 259 | Line 282 | char   *nm;
282                          matselect = S_NONE;
283                          continue;
284                  case 'c':                       /* color calculation */
285 <                        cp++;
263 <                        if (*cp != '=')
285 >                        if (*++cp != '=')
286                                  break;
287 <                        cp++;
266 <                        switch (*cp) {
287 >                        switch (*++cp) {
288                          case 'a':                       /* average */
289 <                                thisillum.flags |= IL_COLAVG;
290 <                                thisillum.flags &= ~IL_COLDST;
289 >                                thisillum.flags = (thisillum.flags|IL_COLAVG)
290 >                                                        & ~IL_COLDST;
291                                  break;
292                          case 'd':                       /* distribution */
293 <                                thisillum.flags |= IL_COLDST;
273 <                                thisillum.flags &= ~IL_COLAVG;
293 >                                thisillum.flags |= (IL_COLDST|IL_COLAVG);
294                                  break;
295                          case 'n':                       /* none */
296                                  thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
# Line 281 | Line 301 | char   *nm;
301                          cp = sskip(cp);
302                          continue;
303                  case 'd':                       /* point sample density */
304 <                        cp++;
285 <                        if (*cp != '=')
304 >                        if (*++cp != '=')
305                                  break;
306 <                        cp++;
288 <                        if (!isintd(cp, " \t\n"))
306 >                        if (!isintd(++cp, " \t\n"))
307                                  break;
308 <                        sampdens = atoi(cp);
308 >                        thisillum.sampdens = atoi(cp);
309                          cp = sskip(cp);
310                          continue;
311                  case 's':                       /* point super-samples */
312 <                        cp++;
295 <                        if (*cp != '=')
312 >                        if (*++cp != '=')
313                                  break;
314 <                        cp++;
298 <                        if (!isintd(cp, " \t\n"))
314 >                        if (!isintd(++cp, " \t\n"))
315                                  break;
316                          thisillum.nsamps = atoi(cp);
317                          cp = sskip(cp);
318                          continue;
319 <                case 'o':                       /* output file */
319 >                case 'l':                       /* light sources */
320                          cp++;
321 <                        if (*cp != '=')
321 >                        if (*cp == '+')
322 >                                thisillum.flags |= IL_LIGHT;
323 >                        else if (*cp == '-')
324 >                                thisillum.flags &= ~IL_LIGHT;
325 >                        else
326                                  break;
327                          cp++;
328 +                        continue;
329 +                case 'o':                       /* output file */
330 +                        if (*++cp != '=')
331 +                                break;
332 +                        if (!*++cp)
333 +                                break;
334                          atos(buf, sizeof(buf), cp);
335                          cp = sskip(cp);
336                          if (freopen(buf, "w", stdout) == NULL) {
# Line 314 | Line 340 | char   *nm;
340                          }
341                          doneheader = 0;
342                          continue;
343 +                case '!':                       /* processed file! */
344 +                        sprintf(errmsg, "(%s): already processed!", nm);
345 +                        error(WARNING, errmsg);
346 +                        matselect = S_NONE;
347 +                        return;
348                  }
349          opterr:                                 /* skip faulty option */
350                  cp = sskip(cp);
351                  nerrs++;
352          }
353 +                                                /* print header? */
354 +        checkhead();
355 +                                                /* issue warnings? */
356          if (nerrs) {
357                  sprintf(errmsg, "(%s): %d error(s) in option line:",
358                                  nm, nerrs);
359                  error(WARNING, errmsg);
360 <                eputs(s);
360 >                wputs(s);
361 >                printf("# %s: the following option line has %d error(s):\n",
362 >                                progname, nerrs);
363          }
364 +                                                /* print pure comment */
365 +        printf("# %s", s+2);
366 + }
367 +
368 +
369 + printhead(ac, av)                       /* print out header */
370 + register int  ac;
371 + register char  **av;
372 + {
373 +        putchar('#');
374 +        while (ac-- > 0) {
375 +                putchar(' ');
376 +                fputs(*av++, stdout);
377 +        }
378 +        fputs("\n#@mkillum !\n", stdout);
379 + }
380 +
381 +
382 + xobject(fp, nm)                         /* translate an object from fp */
383 + FILE  *fp;
384 + char  *nm;
385 + {
386 +        OBJREC  thisobj;
387 +        char  str[MAXSTR];
388 +        int  doit;
389 +                                        /* read the object */
390 +        if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
391 +                goto readerr;
392 +        if (fgetword(str, MAXSTR, fp) == NULL)
393 +                goto readerr;
394 +                                        /* is it an alias? */
395 +        if (!strcmp(str, ALIASID)) {
396 +                if (fgetword(str, MAXSTR, fp) == NULL)
397 +                        goto readerr;
398 +                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
399 +                if (fgetword(str, MAXSTR, fp) == NULL)
400 +                        goto readerr;
401 +                printf("\t%s\n", str);
402 +                return;
403 +        }
404 +        thisobj.omod = OVOID;           /* unused field */
405 +        if ((thisobj.otype = otype(str)) < 0) {
406 +                sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
407 +                error(USER, errmsg);
408 +        }
409 +        if (fgetword(str, MAXSTR, fp) == NULL)
410 +                goto readerr;
411 +        thisobj.oname = str;
412 +        if (readfargs(&thisobj.oargs, fp) != 1)
413 +                goto readerr;
414 +        thisobj.os = NULL;
415 +                                        /* check for translation */
416 +        switch (matselect) {
417 +        case S_NONE:
418 +                doit = 0;
419 +                break;
420 +        case S_ALL:
421 +                doit = 1;
422 +                break;
423 +        case S_ELEM:
424 +                doit = !strcmp(thisillum.altmat, matcheck);
425 +                break;
426 +        case S_COMPL:
427 +                doit = strcmp(thisillum.altmat, matcheck);
428 +                break;
429 +        }
430 +        doit = doit && issurface(thisobj.otype);
431 +                                                /* print header? */
432 +        checkhead();
433 +                                                /* process object */
434 +        if (doit)
435 +                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
436 +        else
437 +                printobj(thisillum.altmat, &thisobj);
438 +                                                /* free arguments */
439 +        freefargs(&thisobj.oargs);
440 +        return;
441 + readerr:
442 +        sprintf(errmsg, "(%s): error reading scene", nm);
443 +        error(USER, errmsg);
444   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines