ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raycalls.c
Revision: 2.32
Committed: Wed Apr 23 02:35:26 2025 UTC (9 days, 9 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.31: +4 -1 lines
Log Message:
fix: Added missing calls to initfunc()

File Contents

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