1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.18 |
static const char RCSid[] = "$Id: raycalls.c,v 2.17 2009/12/12 05:20:10 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* raycalls.c - interface for running Radiance rendering as a library |
6 |
|
|
* |
7 |
|
|
* External symbols declared in ray.h |
8 |
|
|
*/ |
9 |
|
|
|
10 |
greg |
2.2 |
#include "copyright.h" |
11 |
greg |
2.1 |
|
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 |
greg |
2.10 |
* 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 |
greg |
2.1 |
* 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 |
greg |
2.3 |
* The call ray_save(rp) fills a parameter structure |
81 |
greg |
2.1 |
* 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 |
schorsch |
2.4 |
#include <string.h> |
93 |
greg |
2.14 |
#include <time.h> |
94 |
schorsch |
2.4 |
|
95 |
greg |
2.1 |
#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 |
greg |
2.6 |
void (*addobjnotify[8])() = {ambnotify, NULL}; |
119 |
greg |
2.1 |
|
120 |
|
|
int do_irrad = 0; /* compute irradiance? */ |
121 |
|
|
|
122 |
greg |
2.13 |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
123 |
|
|
|
124 |
greg |
2.1 |
double dstrsrc = 0.0; /* square source distribution */ |
125 |
greg |
2.8 |
double shadthresh = .03; /* shadow threshold */ |
126 |
|
|
double shadcert = .75; /* shadow certainty */ |
127 |
greg |
2.1 |
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 |
greg |
2.8 |
int maxdepth = 8; /* maximum recursion depth */ |
143 |
|
|
double minweight = 2e-3; /* minimum ray weight */ |
144 |
greg |
2.1 |
|
145 |
|
|
char *ambfile = NULL; /* ambient file name */ |
146 |
|
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
147 |
|
|
int ambvwt = 0; /* initial weight for ambient value */ |
148 |
greg |
2.8 |
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 |
greg |
2.1 |
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 |
greg |
2.17 |
void |
158 |
schorsch |
2.9 |
ray_init( /* initialize ray-tracing calculation */ |
159 |
|
|
char *otnm |
160 |
|
|
) |
161 |
greg |
2.1 |
{ |
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 |
greg |
2.16 |
if (rand_samp) { |
169 |
|
|
srandom((long)time(0)); |
170 |
|
|
initurand(0); |
171 |
|
|
} else { |
172 |
|
|
srandom(0L); |
173 |
|
|
initurand(2048); |
174 |
|
|
} |
175 |
greg |
2.1 |
/* read scene octree */ |
176 |
|
|
readoct(octname = otnm, ~(IO_FILES|IO_INFO), &thescene, NULL); |
177 |
|
|
nsceneobjs = nobjects; |
178 |
|
|
/* find and mark sources */ |
179 |
|
|
marksources(); |
180 |
|
|
/* initialize ambient calculation */ |
181 |
|
|
setambient(); |
182 |
|
|
/* ready to go... */ |
183 |
|
|
} |
184 |
|
|
|
185 |
greg |
2.17 |
void |
186 |
schorsch |
2.9 |
ray_trace( /* trace a primary ray */ |
187 |
|
|
RAY *r |
188 |
|
|
) |
189 |
greg |
2.1 |
{ |
190 |
greg |
2.11 |
rayorigin(r, PRIMARY, NULL, NULL); |
191 |
greg |
2.16 |
samplendx++; |
192 |
greg |
2.1 |
rayvalue(r); /* assumes origin and direction are set */ |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
|
196 |
greg |
2.17 |
void |
197 |
schorsch |
2.9 |
ray_done( /* free ray-tracing data */ |
198 |
|
|
int freall |
199 |
|
|
) |
200 |
greg |
2.1 |
{ |
201 |
|
|
retainfonts = 1; |
202 |
|
|
ambdone(); |
203 |
|
|
ambnotify(OVOID); |
204 |
|
|
freesources(); |
205 |
|
|
freeobjects(0, nobjects); |
206 |
|
|
donesets(); |
207 |
|
|
octdone(); |
208 |
|
|
thescene.cutree = EMPTY; |
209 |
|
|
octname = NULL; |
210 |
|
|
if (freall) { |
211 |
|
|
retainfonts = 0; |
212 |
|
|
freefont(NULL); |
213 |
|
|
freedata(NULL); |
214 |
|
|
initurand(0); |
215 |
|
|
} |
216 |
|
|
if (nobjects > 0) { |
217 |
schorsch |
2.9 |
sprintf(errmsg, "%ld objects left after call to ray_done()", |
218 |
greg |
2.18 |
(long)nobjects); |
219 |
greg |
2.1 |
error(WARNING, errmsg); |
220 |
|
|
} |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
|
224 |
greg |
2.17 |
void |
225 |
schorsch |
2.9 |
ray_save( /* save current parameter settings */ |
226 |
|
|
RAYPARAMS *rp |
227 |
|
|
) |
228 |
greg |
2.1 |
{ |
229 |
|
|
int i, ndx; |
230 |
|
|
|
231 |
|
|
if (rp == NULL) |
232 |
|
|
return; |
233 |
|
|
rp->do_irrad = do_irrad; |
234 |
|
|
rp->dstrsrc = dstrsrc; |
235 |
|
|
rp->shadthresh = shadthresh; |
236 |
|
|
rp->shadcert = shadcert; |
237 |
|
|
rp->directrelay = directrelay; |
238 |
|
|
rp->vspretest = vspretest; |
239 |
|
|
rp->directvis = directvis; |
240 |
|
|
rp->srcsizerat = srcsizerat; |
241 |
|
|
copycolor(rp->cextinction, cextinction); |
242 |
|
|
copycolor(rp->salbedo, salbedo); |
243 |
|
|
rp->seccg = seccg; |
244 |
|
|
rp->ssampdist = ssampdist; |
245 |
|
|
rp->specthresh = specthresh; |
246 |
|
|
rp->specjitter = specjitter; |
247 |
|
|
rp->backvis = backvis; |
248 |
|
|
rp->maxdepth = maxdepth; |
249 |
|
|
rp->minweight = minweight; |
250 |
|
|
copycolor(rp->ambval, ambval); |
251 |
schorsch |
2.4 |
memset(rp->ambfile, '\0', sizeof(rp->ambfile)); |
252 |
greg |
2.1 |
if (ambfile != NULL) |
253 |
|
|
strncpy(rp->ambfile, ambfile, sizeof(rp->ambfile)-1); |
254 |
|
|
rp->ambvwt = ambvwt; |
255 |
|
|
rp->ambacc = ambacc; |
256 |
|
|
rp->ambres = ambres; |
257 |
|
|
rp->ambdiv = ambdiv; |
258 |
|
|
rp->ambssamp = ambssamp; |
259 |
|
|
rp->ambounce = ambounce; |
260 |
|
|
rp->ambincl = ambincl; |
261 |
schorsch |
2.4 |
memset(rp->amblval, '\0', sizeof(rp->amblval)); |
262 |
greg |
2.1 |
ndx = 0; |
263 |
|
|
for (i = 0; i < AMBLLEN && amblist[i] != NULL; i++) { |
264 |
|
|
int len = strlen(amblist[i]); |
265 |
|
|
if (ndx+len >= sizeof(rp->amblval)) |
266 |
|
|
break; |
267 |
|
|
strcpy(rp->amblval+ndx, amblist[i]); |
268 |
|
|
ndx += len+1; |
269 |
|
|
} |
270 |
|
|
while (i <= AMBLLEN) |
271 |
|
|
rp->amblndx[i++] = -1; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
|
275 |
greg |
2.17 |
void |
276 |
schorsch |
2.9 |
ray_restore( /* restore parameter settings */ |
277 |
|
|
RAYPARAMS *rp |
278 |
|
|
) |
279 |
greg |
2.1 |
{ |
280 |
|
|
register int i; |
281 |
|
|
|
282 |
|
|
if (rp == NULL) { /* restore defaults */ |
283 |
|
|
RAYPARAMS dflt; |
284 |
|
|
ray_defaults(&dflt); |
285 |
|
|
ray_restore(&dflt); |
286 |
|
|
return; |
287 |
|
|
} |
288 |
|
|
/* restore saved settings */ |
289 |
|
|
do_irrad = rp->do_irrad; |
290 |
|
|
dstrsrc = rp->dstrsrc; |
291 |
|
|
shadthresh = rp->shadthresh; |
292 |
|
|
shadcert = rp->shadcert; |
293 |
|
|
directrelay = rp->directrelay; |
294 |
|
|
vspretest = rp->vspretest; |
295 |
|
|
directvis = rp->directvis; |
296 |
|
|
srcsizerat = rp->srcsizerat; |
297 |
|
|
copycolor(cextinction, rp->cextinction); |
298 |
|
|
copycolor(salbedo, rp->salbedo); |
299 |
|
|
seccg = rp->seccg; |
300 |
|
|
ssampdist = rp->ssampdist; |
301 |
|
|
specthresh = rp->specthresh; |
302 |
|
|
specjitter = rp->specjitter; |
303 |
|
|
backvis = rp->backvis; |
304 |
|
|
maxdepth = rp->maxdepth; |
305 |
|
|
minweight = rp->minweight; |
306 |
|
|
copycolor(ambval, rp->ambval); |
307 |
|
|
ambvwt = rp->ambvwt; |
308 |
|
|
ambdiv = rp->ambdiv; |
309 |
|
|
ambssamp = rp->ambssamp; |
310 |
|
|
ambounce = rp->ambounce; |
311 |
|
|
for (i = 0; rp->amblndx[i] >= 0; i++) |
312 |
|
|
amblist[i] = rp->amblval + rp->amblndx[i]; |
313 |
|
|
while (i <= AMBLLEN) |
314 |
|
|
amblist[i++] = NULL; |
315 |
|
|
ambincl = rp->ambincl; |
316 |
|
|
/* update ambient calculation */ |
317 |
|
|
ambnotify(OVOID); |
318 |
|
|
if (thescene.cutree != EMPTY) { |
319 |
|
|
int newamb = (ambfile == NULL) ? rp->ambfile[0] : |
320 |
|
|
strcmp(ambfile, rp->ambfile) ; |
321 |
|
|
|
322 |
|
|
if (amblist[0] != NULL) |
323 |
|
|
for (i = 0; i < nobjects; i++) |
324 |
|
|
ambnotify(i); |
325 |
|
|
|
326 |
|
|
ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; |
327 |
|
|
if (newamb) { |
328 |
|
|
ambres = rp->ambres; |
329 |
|
|
ambacc = rp->ambacc; |
330 |
|
|
setambient(); |
331 |
|
|
} else { |
332 |
|
|
setambres(rp->ambres); |
333 |
|
|
setambacc(rp->ambacc); |
334 |
|
|
} |
335 |
|
|
} else { |
336 |
|
|
ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; |
337 |
|
|
ambres = rp->ambres; |
338 |
|
|
ambacc = rp->ambacc; |
339 |
|
|
} |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
|
343 |
greg |
2.17 |
void |
344 |
schorsch |
2.9 |
ray_defaults( /* get default parameter values */ |
345 |
|
|
RAYPARAMS *rp |
346 |
|
|
) |
347 |
greg |
2.1 |
{ |
348 |
|
|
int i; |
349 |
|
|
|
350 |
|
|
if (rp == NULL) |
351 |
|
|
return; |
352 |
|
|
|
353 |
|
|
rp->do_irrad = 0; |
354 |
|
|
rp->dstrsrc = 0.0; |
355 |
greg |
2.8 |
rp->shadthresh = .03; |
356 |
|
|
rp->shadcert = .75; |
357 |
greg |
2.1 |
rp->directrelay = 2; |
358 |
|
|
rp->vspretest = 512; |
359 |
|
|
rp->directvis = 1; |
360 |
|
|
rp->srcsizerat = .2; |
361 |
|
|
setcolor(rp->cextinction, 0., 0., 0.); |
362 |
|
|
setcolor(rp->salbedo, 0., 0., 0.); |
363 |
|
|
rp->seccg = 0.; |
364 |
|
|
rp->ssampdist = 0.; |
365 |
|
|
rp->specthresh = .15; |
366 |
|
|
rp->specjitter = 1.; |
367 |
|
|
rp->backvis = 1; |
368 |
greg |
2.7 |
rp->maxdepth = 8; |
369 |
|
|
rp->minweight = 2e-3; |
370 |
greg |
2.1 |
setcolor(rp->ambval, 0., 0., 0.); |
371 |
schorsch |
2.4 |
memset(rp->ambfile, '\0', sizeof(rp->ambfile)); |
372 |
greg |
2.1 |
rp->ambvwt = 0; |
373 |
greg |
2.7 |
rp->ambres = 256; |
374 |
greg |
2.10 |
rp->ambacc = 0.15; |
375 |
greg |
2.7 |
rp->ambdiv = 1024; |
376 |
|
|
rp->ambssamp = 512; |
377 |
greg |
2.1 |
rp->ambounce = 0; |
378 |
|
|
rp->ambincl = -1; |
379 |
schorsch |
2.4 |
memset(rp->amblval, '\0', sizeof(rp->amblval)); |
380 |
greg |
2.1 |
for (i = AMBLLEN+1; i--; ) |
381 |
|
|
rp->amblndx[i] = -1; |
382 |
|
|
} |