ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raycalls.c
Revision: 2.4
Committed: Mon Jun 30 14:59:12 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.3: +7 -11 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 schorsch 2.4 static const char RCSid[] = "$Id: raycalls.c,v 2.3 2003/05/15 05:13:35 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * raycalls.c - interface for running Radiance rendering as a library
6     *
7     * External symbols declared in ray.h
8     */
9    
10 greg 2.2 #include "copyright.h"
11 greg 2.1
12     /*
13     * These routines are designed to aid the programmer who wishes
14     * to call Radiance as a library. Unfortunately, the system was
15     * not originally intended to be run this way, and there are some
16     * awkward limitations to contend with. The most irritating
17     * perhaps is that the global variables and functions do not have
18     * a prefix, and the symbols are a bit generic. This results in a
19     * serious invasion of the calling application's name-space, and
20     * you may need to rename either some Radiance routines or some
21     * of your routines to avoid conflicts. Another limitation is
22     * that the global variables are not gathered together into any
23     * sort of context, so it is impossible to simultaneously run
24     * this library on multiple scenes or in multiple threads.
25     * You get one scene and one thread, and if you want more, you
26     * will have to go with the process model used by the programs
27     * gen/mkillum, hd/rholo, and px/pinterp. Finally, unrecoverable
28     * errors result in a call to the application-defined function
29     * quit(). The usual thing to do is to call exit().
30     * You might want to do something else instead, like
31     * call setjmp()/longjmp() to bring you back to the calling
32     * function for recovery. You may also wish to define your own
33     * wputs(s) and eputs(s) functions to output warning and error
34     * messages, respectively.
35     *
36     * With those caveats, we have attempted to make the interface
37     * as simple as we can. Global variables and their defaults
38     * are defined below, and including "ray.h" declares these
39     * along with all the routines you are likely to need. First,
40     * assign the global variable progname to your argv[0], then
41     * change the rendering parameters as you like. If you have a set
42     * of option arguments you are working from, the getrenderopt(ac,av)
43     * call should be very useful. Before tracing any rays, you
44     * must read in the octree with a call to ray_init(oct).
45     * Passing NULL for the file name causes ray_init() to read
46     * the octree from the standard input -- rarely a good idea.
47     * However, one may read an octree from a program (such as
48     * oconv) by preceding a shell command by a '!' character.
49     *
50     * To trace a ray, define a RAY object myRay and assign:
51     *
52     * myRay.rorg = ( ray origin point )
53     * myRay.rdir = ( normalized ray direction )
54     * myRay.rmax = ( maximum length, or zero for no limit )
55     *
56     * If you are rendering from a VIEW structure, this can be
57     * accomplished with a single call for the ray at (x,y):
58     *
59     * myRay.rmax = viewray(myRay.rorg, myRay.rdir, &myView, x, y);
60     *
61     * Then, trace the primary ray with:
62     *
63     * ray_trace(&myRay);
64     *
65     * The resulting contents of myRay should provide you with
66     * more than enough information about what the ray hit,
67     * the computed value, etc. For further clues of how to
68     * compute irradiance, how to get callbacks on the evaluated
69     * ray tree, etc., see the contents of rtrace.c. See
70     * also the rpmain.c, rtmain.c, and rvmain.c modules
71     * to learn more how rendering options are processed.
72     *
73     * When you are done, you may call ray_done(1) to clean
74     * up memory used by Radiance. It doesn't free everything,
75     * but it makes a valiant effort. If you call ray_done(0),
76     * it leaves data that is likely to be reused, including
77     * loaded data files and fonts. The library may be
78     * restarted at any point by calling ray_init() on a new
79     * octree.
80     *
81 greg 2.3 * The call ray_save(rp) fills a parameter structure
82 greg 2.1 * with the current global parameter settings, which may be
83     * restored at any time with a call to ray_restore(rp).
84     * This buffer contains no linked information, and thus
85     * may be passed between processes using write() and
86     * read() calls, so long as byte order is maintained.
87     * Calling ray_restore(NULL) restores the original
88     * default parameters, which is also retrievable with
89     * the call ray_defaults(rp). (These should be the
90     * same as the defaults for rtrace.)
91     */
92    
93 schorsch 2.4 #include <string.h>
94    
95 greg 2.1 #include "ray.h"
96     #include "source.h"
97     #include "ambient.h"
98     #include "otypes.h"
99     #include "random.h"
100     #include "data.h"
101     #include "font.h"
102    
103     char *progname = "unknown_app"; /* caller sets to argv[0] */
104    
105     char *octname; /* octree name we are given */
106    
107     char *shm_boundary = NULL; /* boundary of shared memory */
108    
109     CUBE thescene; /* our scene */
110     OBJECT nsceneobjs; /* number of objects in our scene */
111    
112     int dimlist[MAXDIM]; /* sampling dimensions */
113     int ndims = 0; /* number of sampling dimensions */
114     int samplendx = 0; /* index for this sample */
115    
116     void (*trace)() = NULL; /* trace call */
117    
118     extern void ambnotify();
119     void (*addobjnotify[])() = {ambnotify, NULL};
120    
121     int do_irrad = 0; /* compute irradiance? */
122    
123     double dstrsrc = 0.0; /* square source distribution */
124     double shadthresh = .05; /* shadow threshold */
125     double shadcert = .5; /* shadow certainty */
126     int directrelay = 2; /* number of source relays */
127     int vspretest = 512; /* virtual source pretest density */
128     int directvis = 1; /* sources visible? */
129     double srcsizerat = .2; /* maximum ratio source size/dist. */
130    
131     COLOR cextinction = BLKCOLOR; /* global extinction coefficient */
132     COLOR salbedo = BLKCOLOR; /* global scattering albedo */
133     double seccg = 0.; /* global scattering eccentricity */
134     double ssampdist = 0.; /* scatter sampling distance */
135    
136     double specthresh = .15; /* specular sampling threshold */
137     double specjitter = 1.; /* specular sampling jitter */
138    
139     int backvis = 1; /* back face visibility */
140    
141     int maxdepth = 6; /* maximum recursion depth */
142     double minweight = 4e-3; /* minimum ray weight */
143    
144     char *ambfile = NULL; /* ambient file name */
145     COLOR ambval = BLKCOLOR; /* ambient value */
146     int ambvwt = 0; /* initial weight for ambient value */
147     double ambacc = 0.2; /* ambient accuracy */
148     int ambres = 128; /* ambient resolution */
149     int ambdiv = 512; /* ambient divisions */
150     int ambssamp = 0; /* ambient super-samples */
151     int ambounce = 0; /* ambient bounces */
152     char *amblist[AMBLLEN+1]; /* ambient include/exclude list */
153     int ambincl = -1; /* include == 1, exclude == 0 */
154    
155    
156     void
157     ray_init(otnm) /* initialize ray-tracing calculation */
158     char *otnm;
159     {
160     if (nobjects > 0) /* free old scene data */
161     ray_done(0);
162     /* initialize object types */
163     if (ofun[OBJ_SPHERE].funp == o_default)
164     initotypes();
165     /* initialize urand */
166     if (urperm == NULL)
167     initurand(2048);
168     /* read scene octree */
169     readoct(octname = otnm, ~(IO_FILES|IO_INFO), &thescene, NULL);
170     nsceneobjs = nobjects;
171     /* find and mark sources */
172     marksources();
173     /* initialize ambient calculation */
174     setambient();
175     /* ready to go... */
176     }
177    
178     void
179 greg 2.3 ray_trace(r) /* trace a primary ray */
180     RAY *r;
181 greg 2.1 {
182     rayorigin(r, NULL, PRIMARY, 1.0);
183     samplendx++;
184     rayvalue(r); /* assumes origin and direction are set */
185     }
186    
187    
188     void
189     ray_done(freall) /* free ray-tracing data */
190     int freall;
191     {
192     retainfonts = 1;
193     ambdone();
194     ambnotify(OVOID);
195     freesources();
196     freeobjects(0, nobjects);
197     donesets();
198     octdone();
199     thescene.cutree = EMPTY;
200     octname = NULL;
201     if (freall) {
202     retainfonts = 0;
203     freefont(NULL);
204     freedata(NULL);
205     initurand(0);
206     }
207     if (nobjects > 0) {
208     sprintf(errmsg, "%d objects left after call to ray_done()",
209     nobjects);
210     error(WARNING, errmsg);
211     }
212     }
213    
214    
215     void
216     ray_save(rp) /* save current parameter settings */
217     RAYPARAMS *rp;
218     {
219     int i, ndx;
220    
221     if (rp == NULL)
222     return;
223     rp->do_irrad = do_irrad;
224     rp->dstrsrc = dstrsrc;
225     rp->shadthresh = shadthresh;
226     rp->shadcert = shadcert;
227     rp->directrelay = directrelay;
228     rp->vspretest = vspretest;
229     rp->directvis = directvis;
230     rp->srcsizerat = srcsizerat;
231     copycolor(rp->cextinction, cextinction);
232     copycolor(rp->salbedo, salbedo);
233     rp->seccg = seccg;
234     rp->ssampdist = ssampdist;
235     rp->specthresh = specthresh;
236     rp->specjitter = specjitter;
237     rp->backvis = backvis;
238     rp->maxdepth = maxdepth;
239     rp->minweight = minweight;
240     copycolor(rp->ambval, ambval);
241 schorsch 2.4 memset(rp->ambfile, '\0', sizeof(rp->ambfile));
242 greg 2.1 if (ambfile != NULL)
243     strncpy(rp->ambfile, ambfile, sizeof(rp->ambfile)-1);
244     rp->ambvwt = ambvwt;
245     rp->ambacc = ambacc;
246     rp->ambres = ambres;
247     rp->ambdiv = ambdiv;
248     rp->ambssamp = ambssamp;
249     rp->ambounce = ambounce;
250     rp->ambincl = ambincl;
251 schorsch 2.4 memset(rp->amblval, '\0', sizeof(rp->amblval));
252 greg 2.1 ndx = 0;
253     for (i = 0; i < AMBLLEN && amblist[i] != NULL; i++) {
254     int len = strlen(amblist[i]);
255     if (ndx+len >= sizeof(rp->amblval))
256     break;
257     strcpy(rp->amblval+ndx, amblist[i]);
258     ndx += len+1;
259     }
260     while (i <= AMBLLEN)
261     rp->amblndx[i++] = -1;
262     }
263    
264    
265     void
266     ray_restore(rp) /* restore parameter settings */
267     RAYPARAMS *rp;
268     {
269     register int i;
270    
271     if (rp == NULL) { /* restore defaults */
272     RAYPARAMS dflt;
273     ray_defaults(&dflt);
274     ray_restore(&dflt);
275     return;
276     }
277     /* restore saved settings */
278     do_irrad = rp->do_irrad;
279     dstrsrc = rp->dstrsrc;
280     shadthresh = rp->shadthresh;
281     shadcert = rp->shadcert;
282     directrelay = rp->directrelay;
283     vspretest = rp->vspretest;
284     directvis = rp->directvis;
285     srcsizerat = rp->srcsizerat;
286     copycolor(cextinction, rp->cextinction);
287     copycolor(salbedo, rp->salbedo);
288     seccg = rp->seccg;
289     ssampdist = rp->ssampdist;
290     specthresh = rp->specthresh;
291     specjitter = rp->specjitter;
292     backvis = rp->backvis;
293     maxdepth = rp->maxdepth;
294     minweight = rp->minweight;
295     copycolor(ambval, rp->ambval);
296     ambvwt = rp->ambvwt;
297     ambdiv = rp->ambdiv;
298     ambssamp = rp->ambssamp;
299     ambounce = rp->ambounce;
300     for (i = 0; rp->amblndx[i] >= 0; i++)
301     amblist[i] = rp->amblval + rp->amblndx[i];
302     while (i <= AMBLLEN)
303     amblist[i++] = NULL;
304     ambincl = rp->ambincl;
305     /* update ambient calculation */
306     ambnotify(OVOID);
307     if (thescene.cutree != EMPTY) {
308     int newamb = (ambfile == NULL) ? rp->ambfile[0] :
309     strcmp(ambfile, rp->ambfile) ;
310    
311     if (amblist[0] != NULL)
312     for (i = 0; i < nobjects; i++)
313     ambnotify(i);
314    
315     ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL;
316     if (newamb) {
317     ambres = rp->ambres;
318     ambacc = rp->ambacc;
319     setambient();
320     } else {
321     setambres(rp->ambres);
322     setambacc(rp->ambacc);
323     }
324     } else {
325     ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL;
326     ambres = rp->ambres;
327     ambacc = rp->ambacc;
328     }
329     }
330    
331    
332     void
333     ray_defaults(rp) /* get default parameter values */
334     RAYPARAMS *rp;
335     {
336     int i;
337    
338     if (rp == NULL)
339     return;
340    
341     rp->do_irrad = 0;
342     rp->dstrsrc = 0.0;
343     rp->shadthresh = .05;
344     rp->shadcert = .5;
345     rp->directrelay = 2;
346     rp->vspretest = 512;
347     rp->directvis = 1;
348     rp->srcsizerat = .2;
349     setcolor(rp->cextinction, 0., 0., 0.);
350     setcolor(rp->salbedo, 0., 0., 0.);
351     rp->seccg = 0.;
352     rp->ssampdist = 0.;
353     rp->specthresh = .15;
354     rp->specjitter = 1.;
355     rp->backvis = 1;
356     rp->maxdepth = 6;
357     rp->minweight = 4e-3;
358     setcolor(rp->ambval, 0., 0., 0.);
359 schorsch 2.4 memset(rp->ambfile, '\0', sizeof(rp->ambfile));
360 greg 2.1 rp->ambvwt = 0;
361     rp->ambres = 128;
362     rp->ambacc = 0.2;
363     rp->ambdiv = 512;
364     rp->ambssamp = 0;
365     rp->ambounce = 0;
366     rp->ambincl = -1;
367 schorsch 2.4 memset(rp->amblval, '\0', sizeof(rp->amblval));
368 greg 2.1 for (i = AMBLLEN+1; i--; )
369     rp->amblndx[i] = -1;
370     }