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