| 1 | greg | 2.1 | #ifndef lint | 
| 2 | greg | 2.13 | static const char       RCSid[] = "$Id: raycalls.c,v 2.12 2005/06/13 20:07:56 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 |  |  |  | 
| 94 | greg | 2.1 | #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 | greg | 2.6 | void    (*addobjnotify[8])() = {ambnotify, NULL}; | 
| 118 | greg | 2.1 |  | 
| 119 |  |  | int     do_irrad = 0;                   /* compute irradiance? */ | 
| 120 |  |  |  | 
| 121 | greg | 2.13 | int     rand_samp = 0;                  /* pure Monte Carlo sampling? */ | 
| 122 |  |  |  | 
| 123 | greg | 2.1 | double  dstrsrc = 0.0;                  /* square source distribution */ | 
| 124 | greg | 2.8 | double  shadthresh = .03;               /* shadow threshold */ | 
| 125 |  |  | double  shadcert = .75;                 /* shadow certainty */ | 
| 126 | greg | 2.1 | 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 | greg | 2.8 | int     maxdepth = 8;                   /* maximum recursion depth */ | 
| 142 |  |  | double  minweight = 2e-3;               /* minimum ray weight */ | 
| 143 | greg | 2.1 |  | 
| 144 |  |  | char    *ambfile = NULL;                /* ambient file name */ | 
| 145 |  |  | COLOR   ambval = BLKCOLOR;              /* ambient value */ | 
| 146 |  |  | int     ambvwt = 0;                     /* initial weight for ambient value */ | 
| 147 | greg | 2.8 | double  ambacc = 0.1;                   /* ambient accuracy */ | 
| 148 |  |  | int     ambres = 256;                   /* ambient resolution */ | 
| 149 |  |  | int     ambdiv = 1024;                  /* ambient divisions */ | 
| 150 |  |  | int     ambssamp = 512;                 /* ambient super-samples */ | 
| 151 | greg | 2.1 | 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 | schorsch | 2.9 | extern void | 
| 157 |  |  | ray_init(                       /* initialize ray-tracing calculation */ | 
| 158 |  |  | char    *otnm | 
| 159 |  |  | ) | 
| 160 | greg | 2.1 | { | 
| 161 |  |  | if (nobjects > 0)               /* free old scene data */ | 
| 162 |  |  | ray_done(0); | 
| 163 |  |  | /* initialize object types */ | 
| 164 |  |  | if (ofun[OBJ_SPHERE].funp == o_default) | 
| 165 |  |  | initotypes(); | 
| 166 |  |  | /* initialize urand */ | 
| 167 | greg | 2.5 | initurand(2048); | 
| 168 | greg | 2.1 | /* read scene octree */ | 
| 169 |  |  | readoct(octname = otnm, ~(IO_FILES|IO_INFO), &thescene, NULL); | 
| 170 |  |  | nsceneobjs = nobjects; | 
| 171 |  |  | /* find and mark sources */ | 
| 172 |  |  | marksources(); | 
| 173 |  |  | /* initialize ambient calculation */ | 
| 174 |  |  | setambient(); | 
| 175 |  |  | /* ready to go... */ | 
| 176 |  |  | } | 
| 177 |  |  |  | 
| 178 | schorsch | 2.9 | extern void | 
| 179 |  |  | ray_trace(                      /* trace a primary ray */ | 
| 180 |  |  | RAY     *r | 
| 181 |  |  | ) | 
| 182 | greg | 2.1 | { | 
| 183 | greg | 2.11 | rayorigin(r, PRIMARY, NULL, NULL); | 
| 184 | greg | 2.12 | samplendx = rand_samp ? random() : samplendx+1; | 
| 185 | greg | 2.1 | rayvalue(r);            /* assumes origin and direction are set */ | 
| 186 |  |  | } | 
| 187 |  |  |  | 
| 188 |  |  |  | 
| 189 | schorsch | 2.9 | extern void | 
| 190 |  |  | ray_done(               /* free ray-tracing data */ | 
| 191 |  |  | int     freall | 
| 192 |  |  | ) | 
| 193 | greg | 2.1 | { | 
| 194 |  |  | retainfonts = 1; | 
| 195 |  |  | ambdone(); | 
| 196 |  |  | ambnotify(OVOID); | 
| 197 |  |  | freesources(); | 
| 198 |  |  | freeobjects(0, nobjects); | 
| 199 |  |  | donesets(); | 
| 200 |  |  | octdone(); | 
| 201 |  |  | thescene.cutree = EMPTY; | 
| 202 |  |  | octname = NULL; | 
| 203 |  |  | if (freall) { | 
| 204 |  |  | retainfonts = 0; | 
| 205 |  |  | freefont(NULL); | 
| 206 |  |  | freedata(NULL); | 
| 207 |  |  | initurand(0); | 
| 208 |  |  | } | 
| 209 |  |  | if (nobjects > 0) { | 
| 210 | schorsch | 2.9 | sprintf(errmsg, "%ld objects left after call to ray_done()", | 
| 211 | greg | 2.1 | nobjects); | 
| 212 |  |  | error(WARNING, errmsg); | 
| 213 |  |  | } | 
| 214 |  |  | } | 
| 215 |  |  |  | 
| 216 |  |  |  | 
| 217 | schorsch | 2.9 | extern void | 
| 218 |  |  | ray_save(                       /* save current parameter settings */ | 
| 219 |  |  | RAYPARAMS       *rp | 
| 220 |  |  | ) | 
| 221 | greg | 2.1 | { | 
| 222 |  |  | int     i, ndx; | 
| 223 |  |  |  | 
| 224 |  |  | if (rp == NULL) | 
| 225 |  |  | return; | 
| 226 |  |  | rp->do_irrad = do_irrad; | 
| 227 |  |  | rp->dstrsrc = dstrsrc; | 
| 228 |  |  | rp->shadthresh = shadthresh; | 
| 229 |  |  | rp->shadcert = shadcert; | 
| 230 |  |  | rp->directrelay = directrelay; | 
| 231 |  |  | rp->vspretest = vspretest; | 
| 232 |  |  | rp->directvis = directvis; | 
| 233 |  |  | rp->srcsizerat = srcsizerat; | 
| 234 |  |  | copycolor(rp->cextinction, cextinction); | 
| 235 |  |  | copycolor(rp->salbedo, salbedo); | 
| 236 |  |  | rp->seccg = seccg; | 
| 237 |  |  | rp->ssampdist = ssampdist; | 
| 238 |  |  | rp->specthresh = specthresh; | 
| 239 |  |  | rp->specjitter = specjitter; | 
| 240 |  |  | rp->backvis = backvis; | 
| 241 |  |  | rp->maxdepth = maxdepth; | 
| 242 |  |  | rp->minweight = minweight; | 
| 243 |  |  | copycolor(rp->ambval, ambval); | 
| 244 | schorsch | 2.4 | memset(rp->ambfile, '\0', sizeof(rp->ambfile)); | 
| 245 | greg | 2.1 | if (ambfile != NULL) | 
| 246 |  |  | strncpy(rp->ambfile, ambfile, sizeof(rp->ambfile)-1); | 
| 247 |  |  | rp->ambvwt = ambvwt; | 
| 248 |  |  | rp->ambacc = ambacc; | 
| 249 |  |  | rp->ambres = ambres; | 
| 250 |  |  | rp->ambdiv = ambdiv; | 
| 251 |  |  | rp->ambssamp = ambssamp; | 
| 252 |  |  | rp->ambounce = ambounce; | 
| 253 |  |  | rp->ambincl = ambincl; | 
| 254 | schorsch | 2.4 | memset(rp->amblval, '\0', sizeof(rp->amblval)); | 
| 255 | greg | 2.1 | ndx = 0; | 
| 256 |  |  | for (i = 0; i < AMBLLEN && amblist[i] != NULL; i++) { | 
| 257 |  |  | int     len = strlen(amblist[i]); | 
| 258 |  |  | if (ndx+len >= sizeof(rp->amblval)) | 
| 259 |  |  | break; | 
| 260 |  |  | strcpy(rp->amblval+ndx, amblist[i]); | 
| 261 |  |  | ndx += len+1; | 
| 262 |  |  | } | 
| 263 |  |  | while (i <= AMBLLEN) | 
| 264 |  |  | rp->amblndx[i++] = -1; | 
| 265 |  |  | } | 
| 266 |  |  |  | 
| 267 |  |  |  | 
| 268 | schorsch | 2.9 | extern void | 
| 269 |  |  | ray_restore(                    /* restore parameter settings */ | 
| 270 |  |  | RAYPARAMS       *rp | 
| 271 |  |  | ) | 
| 272 | greg | 2.1 | { | 
| 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 | schorsch | 2.9 | extern void | 
| 337 |  |  | ray_defaults(           /* get default parameter values */ | 
| 338 |  |  | RAYPARAMS       *rp | 
| 339 |  |  | ) | 
| 340 | greg | 2.1 | { | 
| 341 |  |  | int     i; | 
| 342 |  |  |  | 
| 343 |  |  | if (rp == NULL) | 
| 344 |  |  | return; | 
| 345 |  |  |  | 
| 346 |  |  | rp->do_irrad = 0; | 
| 347 |  |  | rp->dstrsrc = 0.0; | 
| 348 | greg | 2.8 | rp->shadthresh = .03; | 
| 349 |  |  | rp->shadcert = .75; | 
| 350 | greg | 2.1 | rp->directrelay = 2; | 
| 351 |  |  | rp->vspretest = 512; | 
| 352 |  |  | rp->directvis = 1; | 
| 353 |  |  | rp->srcsizerat = .2; | 
| 354 |  |  | setcolor(rp->cextinction, 0., 0., 0.); | 
| 355 |  |  | setcolor(rp->salbedo, 0., 0., 0.); | 
| 356 |  |  | rp->seccg = 0.; | 
| 357 |  |  | rp->ssampdist = 0.; | 
| 358 |  |  | rp->specthresh = .15; | 
| 359 |  |  | rp->specjitter = 1.; | 
| 360 |  |  | rp->backvis = 1; | 
| 361 | greg | 2.7 | rp->maxdepth = 8; | 
| 362 |  |  | rp->minweight = 2e-3; | 
| 363 | greg | 2.1 | setcolor(rp->ambval, 0., 0., 0.); | 
| 364 | schorsch | 2.4 | memset(rp->ambfile, '\0', sizeof(rp->ambfile)); | 
| 365 | greg | 2.1 | rp->ambvwt = 0; | 
| 366 | greg | 2.7 | rp->ambres = 256; | 
| 367 | greg | 2.10 | rp->ambacc = 0.15; | 
| 368 | greg | 2.7 | rp->ambdiv = 1024; | 
| 369 |  |  | rp->ambssamp = 512; | 
| 370 | greg | 2.1 | rp->ambounce = 0; | 
| 371 |  |  | rp->ambincl = -1; | 
| 372 | schorsch | 2.4 | memset(rp->amblval, '\0', sizeof(rp->amblval)); | 
| 373 | greg | 2.1 | for (i = AMBLLEN+1; i--; ) | 
| 374 |  |  | rp->amblndx[i] = -1; | 
| 375 |  |  | } |