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