1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: raycalls.c,v 2.14 2005/06/15 15:36:52 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 "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 |
void (*addobjnotify[8])() = {ambnotify, NULL}; |
119 |
|
120 |
int do_irrad = 0; /* compute irradiance? */ |
121 |
|
122 |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
123 |
|
124 |
double dstrsrc = 0.0; /* square source distribution */ |
125 |
double shadthresh = .03; /* shadow threshold */ |
126 |
double shadcert = .75; /* shadow certainty */ |
127 |
int directrelay = 2; /* number of source relays */ |
128 |
int vspretest = 512; /* virtual source pretest density */ |
129 |
int directvis = 1; /* sources visible? */ |
130 |
double srcsizerat = .2; /* maximum ratio source size/dist. */ |
131 |
|
132 |
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
133 |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
134 |
double seccg = 0.; /* global scattering eccentricity */ |
135 |
double ssampdist = 0.; /* scatter sampling distance */ |
136 |
|
137 |
double specthresh = .15; /* specular sampling threshold */ |
138 |
double specjitter = 1.; /* specular sampling jitter */ |
139 |
|
140 |
int backvis = 1; /* back face visibility */ |
141 |
|
142 |
int maxdepth = 8; /* maximum recursion depth */ |
143 |
double minweight = 2e-3; /* minimum ray weight */ |
144 |
|
145 |
char *ambfile = NULL; /* ambient file name */ |
146 |
COLOR ambval = BLKCOLOR; /* ambient value */ |
147 |
int ambvwt = 0; /* initial weight for ambient value */ |
148 |
double ambacc = 0.1; /* ambient accuracy */ |
149 |
int ambres = 256; /* ambient resolution */ |
150 |
int ambdiv = 1024; /* ambient divisions */ |
151 |
int ambssamp = 512; /* ambient super-samples */ |
152 |
int ambounce = 0; /* ambient bounces */ |
153 |
char *amblist[AMBLLEN+1]; /* ambient include/exclude list */ |
154 |
int ambincl = -1; /* include == 1, exclude == 0 */ |
155 |
|
156 |
|
157 |
extern void |
158 |
ray_init( /* initialize ray-tracing calculation */ |
159 |
char *otnm |
160 |
) |
161 |
{ |
162 |
if (nobjects > 0) /* free old scene data */ |
163 |
ray_done(0); |
164 |
/* initialize object types */ |
165 |
if (ofun[OBJ_SPHERE].funp == o_default) |
166 |
initotypes(); |
167 |
/* initialize urand */ |
168 |
srandom(rand_samp ? (long)time(0) : 0L); |
169 |
initurand(2048); |
170 |
/* read scene octree */ |
171 |
readoct(octname = otnm, ~(IO_FILES|IO_INFO), &thescene, NULL); |
172 |
nsceneobjs = nobjects; |
173 |
/* find and mark sources */ |
174 |
marksources(); |
175 |
/* initialize ambient calculation */ |
176 |
setambient(); |
177 |
/* ready to go... */ |
178 |
} |
179 |
|
180 |
extern void |
181 |
ray_trace( /* trace a primary ray */ |
182 |
RAY *r |
183 |
) |
184 |
{ |
185 |
rayorigin(r, PRIMARY, NULL, NULL); |
186 |
samplendx = rand_samp ? random() : samplendx+1; |
187 |
rayvalue(r); /* assumes origin and direction are set */ |
188 |
} |
189 |
|
190 |
|
191 |
extern void |
192 |
ray_done( /* free ray-tracing data */ |
193 |
int freall |
194 |
) |
195 |
{ |
196 |
retainfonts = 1; |
197 |
ambdone(); |
198 |
ambnotify(OVOID); |
199 |
freesources(); |
200 |
freeobjects(0, nobjects); |
201 |
donesets(); |
202 |
octdone(); |
203 |
thescene.cutree = EMPTY; |
204 |
octname = NULL; |
205 |
if (freall) { |
206 |
retainfonts = 0; |
207 |
freefont(NULL); |
208 |
freedata(NULL); |
209 |
initurand(0); |
210 |
} |
211 |
if (nobjects > 0) { |
212 |
sprintf(errmsg, "%ld objects left after call to ray_done()", |
213 |
nobjects); |
214 |
error(WARNING, errmsg); |
215 |
} |
216 |
} |
217 |
|
218 |
|
219 |
extern void |
220 |
ray_save( /* save current parameter settings */ |
221 |
RAYPARAMS *rp |
222 |
) |
223 |
{ |
224 |
int i, ndx; |
225 |
|
226 |
if (rp == NULL) |
227 |
return; |
228 |
rp->do_irrad = do_irrad; |
229 |
rp->dstrsrc = dstrsrc; |
230 |
rp->shadthresh = shadthresh; |
231 |
rp->shadcert = shadcert; |
232 |
rp->directrelay = directrelay; |
233 |
rp->vspretest = vspretest; |
234 |
rp->directvis = directvis; |
235 |
rp->srcsizerat = srcsizerat; |
236 |
copycolor(rp->cextinction, cextinction); |
237 |
copycolor(rp->salbedo, salbedo); |
238 |
rp->seccg = seccg; |
239 |
rp->ssampdist = ssampdist; |
240 |
rp->specthresh = specthresh; |
241 |
rp->specjitter = specjitter; |
242 |
rp->backvis = backvis; |
243 |
rp->maxdepth = maxdepth; |
244 |
rp->minweight = minweight; |
245 |
copycolor(rp->ambval, ambval); |
246 |
memset(rp->ambfile, '\0', sizeof(rp->ambfile)); |
247 |
if (ambfile != NULL) |
248 |
strncpy(rp->ambfile, ambfile, sizeof(rp->ambfile)-1); |
249 |
rp->ambvwt = ambvwt; |
250 |
rp->ambacc = ambacc; |
251 |
rp->ambres = ambres; |
252 |
rp->ambdiv = ambdiv; |
253 |
rp->ambssamp = ambssamp; |
254 |
rp->ambounce = ambounce; |
255 |
rp->ambincl = ambincl; |
256 |
memset(rp->amblval, '\0', sizeof(rp->amblval)); |
257 |
ndx = 0; |
258 |
for (i = 0; i < AMBLLEN && amblist[i] != NULL; i++) { |
259 |
int len = strlen(amblist[i]); |
260 |
if (ndx+len >= sizeof(rp->amblval)) |
261 |
break; |
262 |
strcpy(rp->amblval+ndx, amblist[i]); |
263 |
ndx += len+1; |
264 |
} |
265 |
while (i <= AMBLLEN) |
266 |
rp->amblndx[i++] = -1; |
267 |
} |
268 |
|
269 |
|
270 |
extern void |
271 |
ray_restore( /* restore parameter settings */ |
272 |
RAYPARAMS *rp |
273 |
) |
274 |
{ |
275 |
register int i; |
276 |
|
277 |
if (rp == NULL) { /* restore defaults */ |
278 |
RAYPARAMS dflt; |
279 |
ray_defaults(&dflt); |
280 |
ray_restore(&dflt); |
281 |
return; |
282 |
} |
283 |
/* restore saved settings */ |
284 |
do_irrad = rp->do_irrad; |
285 |
dstrsrc = rp->dstrsrc; |
286 |
shadthresh = rp->shadthresh; |
287 |
shadcert = rp->shadcert; |
288 |
directrelay = rp->directrelay; |
289 |
vspretest = rp->vspretest; |
290 |
directvis = rp->directvis; |
291 |
srcsizerat = rp->srcsizerat; |
292 |
copycolor(cextinction, rp->cextinction); |
293 |
copycolor(salbedo, rp->salbedo); |
294 |
seccg = rp->seccg; |
295 |
ssampdist = rp->ssampdist; |
296 |
specthresh = rp->specthresh; |
297 |
specjitter = rp->specjitter; |
298 |
backvis = rp->backvis; |
299 |
maxdepth = rp->maxdepth; |
300 |
minweight = rp->minweight; |
301 |
copycolor(ambval, rp->ambval); |
302 |
ambvwt = rp->ambvwt; |
303 |
ambdiv = rp->ambdiv; |
304 |
ambssamp = rp->ambssamp; |
305 |
ambounce = rp->ambounce; |
306 |
for (i = 0; rp->amblndx[i] >= 0; i++) |
307 |
amblist[i] = rp->amblval + rp->amblndx[i]; |
308 |
while (i <= AMBLLEN) |
309 |
amblist[i++] = NULL; |
310 |
ambincl = rp->ambincl; |
311 |
/* update ambient calculation */ |
312 |
ambnotify(OVOID); |
313 |
if (thescene.cutree != EMPTY) { |
314 |
int newamb = (ambfile == NULL) ? rp->ambfile[0] : |
315 |
strcmp(ambfile, rp->ambfile) ; |
316 |
|
317 |
if (amblist[0] != NULL) |
318 |
for (i = 0; i < nobjects; i++) |
319 |
ambnotify(i); |
320 |
|
321 |
ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; |
322 |
if (newamb) { |
323 |
ambres = rp->ambres; |
324 |
ambacc = rp->ambacc; |
325 |
setambient(); |
326 |
} else { |
327 |
setambres(rp->ambres); |
328 |
setambacc(rp->ambacc); |
329 |
} |
330 |
} else { |
331 |
ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; |
332 |
ambres = rp->ambres; |
333 |
ambacc = rp->ambacc; |
334 |
} |
335 |
} |
336 |
|
337 |
|
338 |
extern void |
339 |
ray_defaults( /* get default parameter values */ |
340 |
RAYPARAMS *rp |
341 |
) |
342 |
{ |
343 |
int i; |
344 |
|
345 |
if (rp == NULL) |
346 |
return; |
347 |
|
348 |
rp->do_irrad = 0; |
349 |
rp->dstrsrc = 0.0; |
350 |
rp->shadthresh = .03; |
351 |
rp->shadcert = .75; |
352 |
rp->directrelay = 2; |
353 |
rp->vspretest = 512; |
354 |
rp->directvis = 1; |
355 |
rp->srcsizerat = .2; |
356 |
setcolor(rp->cextinction, 0., 0., 0.); |
357 |
setcolor(rp->salbedo, 0., 0., 0.); |
358 |
rp->seccg = 0.; |
359 |
rp->ssampdist = 0.; |
360 |
rp->specthresh = .15; |
361 |
rp->specjitter = 1.; |
362 |
rp->backvis = 1; |
363 |
rp->maxdepth = 8; |
364 |
rp->minweight = 2e-3; |
365 |
setcolor(rp->ambval, 0., 0., 0.); |
366 |
memset(rp->ambfile, '\0', sizeof(rp->ambfile)); |
367 |
rp->ambvwt = 0; |
368 |
rp->ambres = 256; |
369 |
rp->ambacc = 0.15; |
370 |
rp->ambdiv = 1024; |
371 |
rp->ambssamp = 512; |
372 |
rp->ambounce = 0; |
373 |
rp->ambincl = -1; |
374 |
memset(rp->amblval, '\0', sizeof(rp->amblval)); |
375 |
for (i = AMBLLEN+1; i--; ) |
376 |
rp->amblndx[i] = -1; |
377 |
} |