1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: raycalls.c,v 2.4 2003/06/30 14:59:12 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 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) fills a parameter structure |
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 <string.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 |
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 |
initurand(2048); |
167 |
/* read scene octree */ |
168 |
readoct(octname = otnm, ~(IO_FILES|IO_INFO), &thescene, NULL); |
169 |
nsceneobjs = nobjects; |
170 |
/* find and mark sources */ |
171 |
marksources(); |
172 |
/* initialize ambient calculation */ |
173 |
setambient(); |
174 |
/* ready to go... */ |
175 |
} |
176 |
|
177 |
void |
178 |
ray_trace(r) /* trace a primary ray */ |
179 |
RAY *r; |
180 |
{ |
181 |
rayorigin(r, NULL, PRIMARY, 1.0); |
182 |
samplendx++; |
183 |
rayvalue(r); /* assumes origin and direction are set */ |
184 |
} |
185 |
|
186 |
|
187 |
void |
188 |
ray_done(freall) /* free ray-tracing data */ |
189 |
int freall; |
190 |
{ |
191 |
retainfonts = 1; |
192 |
ambdone(); |
193 |
ambnotify(OVOID); |
194 |
freesources(); |
195 |
freeobjects(0, nobjects); |
196 |
donesets(); |
197 |
octdone(); |
198 |
thescene.cutree = EMPTY; |
199 |
octname = NULL; |
200 |
if (freall) { |
201 |
retainfonts = 0; |
202 |
freefont(NULL); |
203 |
freedata(NULL); |
204 |
initurand(0); |
205 |
} |
206 |
if (nobjects > 0) { |
207 |
sprintf(errmsg, "%d objects left after call to ray_done()", |
208 |
nobjects); |
209 |
error(WARNING, errmsg); |
210 |
} |
211 |
} |
212 |
|
213 |
|
214 |
void |
215 |
ray_save(rp) /* save current parameter settings */ |
216 |
RAYPARAMS *rp; |
217 |
{ |
218 |
int i, ndx; |
219 |
|
220 |
if (rp == NULL) |
221 |
return; |
222 |
rp->do_irrad = do_irrad; |
223 |
rp->dstrsrc = dstrsrc; |
224 |
rp->shadthresh = shadthresh; |
225 |
rp->shadcert = shadcert; |
226 |
rp->directrelay = directrelay; |
227 |
rp->vspretest = vspretest; |
228 |
rp->directvis = directvis; |
229 |
rp->srcsizerat = srcsizerat; |
230 |
copycolor(rp->cextinction, cextinction); |
231 |
copycolor(rp->salbedo, salbedo); |
232 |
rp->seccg = seccg; |
233 |
rp->ssampdist = ssampdist; |
234 |
rp->specthresh = specthresh; |
235 |
rp->specjitter = specjitter; |
236 |
rp->backvis = backvis; |
237 |
rp->maxdepth = maxdepth; |
238 |
rp->minweight = minweight; |
239 |
copycolor(rp->ambval, ambval); |
240 |
memset(rp->ambfile, '\0', sizeof(rp->ambfile)); |
241 |
if (ambfile != NULL) |
242 |
strncpy(rp->ambfile, ambfile, sizeof(rp->ambfile)-1); |
243 |
rp->ambvwt = ambvwt; |
244 |
rp->ambacc = ambacc; |
245 |
rp->ambres = ambres; |
246 |
rp->ambdiv = ambdiv; |
247 |
rp->ambssamp = ambssamp; |
248 |
rp->ambounce = ambounce; |
249 |
rp->ambincl = ambincl; |
250 |
memset(rp->amblval, '\0', sizeof(rp->amblval)); |
251 |
ndx = 0; |
252 |
for (i = 0; i < AMBLLEN && amblist[i] != NULL; i++) { |
253 |
int len = strlen(amblist[i]); |
254 |
if (ndx+len >= sizeof(rp->amblval)) |
255 |
break; |
256 |
strcpy(rp->amblval+ndx, amblist[i]); |
257 |
ndx += len+1; |
258 |
} |
259 |
while (i <= AMBLLEN) |
260 |
rp->amblndx[i++] = -1; |
261 |
} |
262 |
|
263 |
|
264 |
void |
265 |
ray_restore(rp) /* restore parameter settings */ |
266 |
RAYPARAMS *rp; |
267 |
{ |
268 |
register int i; |
269 |
|
270 |
if (rp == NULL) { /* restore defaults */ |
271 |
RAYPARAMS dflt; |
272 |
ray_defaults(&dflt); |
273 |
ray_restore(&dflt); |
274 |
return; |
275 |
} |
276 |
/* restore saved settings */ |
277 |
do_irrad = rp->do_irrad; |
278 |
dstrsrc = rp->dstrsrc; |
279 |
shadthresh = rp->shadthresh; |
280 |
shadcert = rp->shadcert; |
281 |
directrelay = rp->directrelay; |
282 |
vspretest = rp->vspretest; |
283 |
directvis = rp->directvis; |
284 |
srcsizerat = rp->srcsizerat; |
285 |
copycolor(cextinction, rp->cextinction); |
286 |
copycolor(salbedo, rp->salbedo); |
287 |
seccg = rp->seccg; |
288 |
ssampdist = rp->ssampdist; |
289 |
specthresh = rp->specthresh; |
290 |
specjitter = rp->specjitter; |
291 |
backvis = rp->backvis; |
292 |
maxdepth = rp->maxdepth; |
293 |
minweight = rp->minweight; |
294 |
copycolor(ambval, rp->ambval); |
295 |
ambvwt = rp->ambvwt; |
296 |
ambdiv = rp->ambdiv; |
297 |
ambssamp = rp->ambssamp; |
298 |
ambounce = rp->ambounce; |
299 |
for (i = 0; rp->amblndx[i] >= 0; i++) |
300 |
amblist[i] = rp->amblval + rp->amblndx[i]; |
301 |
while (i <= AMBLLEN) |
302 |
amblist[i++] = NULL; |
303 |
ambincl = rp->ambincl; |
304 |
/* update ambient calculation */ |
305 |
ambnotify(OVOID); |
306 |
if (thescene.cutree != EMPTY) { |
307 |
int newamb = (ambfile == NULL) ? rp->ambfile[0] : |
308 |
strcmp(ambfile, rp->ambfile) ; |
309 |
|
310 |
if (amblist[0] != NULL) |
311 |
for (i = 0; i < nobjects; i++) |
312 |
ambnotify(i); |
313 |
|
314 |
ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; |
315 |
if (newamb) { |
316 |
ambres = rp->ambres; |
317 |
ambacc = rp->ambacc; |
318 |
setambient(); |
319 |
} else { |
320 |
setambres(rp->ambres); |
321 |
setambacc(rp->ambacc); |
322 |
} |
323 |
} else { |
324 |
ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; |
325 |
ambres = rp->ambres; |
326 |
ambacc = rp->ambacc; |
327 |
} |
328 |
} |
329 |
|
330 |
|
331 |
void |
332 |
ray_defaults(rp) /* get default parameter values */ |
333 |
RAYPARAMS *rp; |
334 |
{ |
335 |
int i; |
336 |
|
337 |
if (rp == NULL) |
338 |
return; |
339 |
|
340 |
rp->do_irrad = 0; |
341 |
rp->dstrsrc = 0.0; |
342 |
rp->shadthresh = .05; |
343 |
rp->shadcert = .5; |
344 |
rp->directrelay = 2; |
345 |
rp->vspretest = 512; |
346 |
rp->directvis = 1; |
347 |
rp->srcsizerat = .2; |
348 |
setcolor(rp->cextinction, 0., 0., 0.); |
349 |
setcolor(rp->salbedo, 0., 0., 0.); |
350 |
rp->seccg = 0.; |
351 |
rp->ssampdist = 0.; |
352 |
rp->specthresh = .15; |
353 |
rp->specjitter = 1.; |
354 |
rp->backvis = 1; |
355 |
rp->maxdepth = 6; |
356 |
rp->minweight = 4e-3; |
357 |
setcolor(rp->ambval, 0., 0., 0.); |
358 |
memset(rp->ambfile, '\0', sizeof(rp->ambfile)); |
359 |
rp->ambvwt = 0; |
360 |
rp->ambres = 128; |
361 |
rp->ambacc = 0.2; |
362 |
rp->ambdiv = 512; |
363 |
rp->ambssamp = 0; |
364 |
rp->ambounce = 0; |
365 |
rp->ambincl = -1; |
366 |
memset(rp->amblval, '\0', sizeof(rp->amblval)); |
367 |
for (i = AMBLLEN+1; i--; ) |
368 |
rp->amblndx[i] = -1; |
369 |
} |