| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: raycalls.c,v 2.8 2003/10/04 14:39:53 greg 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 | void    (*addobjnotify[8])() = {ambnotify, NULL}; | 
| 119 |  | 
| 120 | int     do_irrad = 0;                   /* compute irradiance? */ | 
| 121 |  | 
| 122 | double  dstrsrc = 0.0;                  /* square source distribution */ | 
| 123 | double  shadthresh = .03;               /* shadow threshold */ | 
| 124 | double  shadcert = .75;                 /* shadow certainty */ | 
| 125 | int     directrelay = 2;                /* number of source relays */ | 
| 126 | int     vspretest = 512;                /* virtual source pretest density */ | 
| 127 | int     directvis = 1;                  /* sources visible? */ | 
| 128 | double  srcsizerat = .2;                /* maximum ratio source size/dist. */ | 
| 129 |  | 
| 130 | COLOR   cextinction = BLKCOLOR;         /* global extinction coefficient */ | 
| 131 | COLOR   salbedo = BLKCOLOR;             /* global scattering albedo */ | 
| 132 | double  seccg = 0.;                     /* global scattering eccentricity */ | 
| 133 | double  ssampdist = 0.;                 /* scatter sampling distance */ | 
| 134 |  | 
| 135 | double  specthresh = .15;               /* specular sampling threshold */ | 
| 136 | double  specjitter = 1.;                /* specular sampling jitter */ | 
| 137 |  | 
| 138 | int     backvis = 1;                    /* back face visibility */ | 
| 139 |  | 
| 140 | int     maxdepth = 8;                   /* maximum recursion depth */ | 
| 141 | double  minweight = 2e-3;               /* minimum ray weight */ | 
| 142 |  | 
| 143 | char    *ambfile = NULL;                /* ambient file name */ | 
| 144 | COLOR   ambval = BLKCOLOR;              /* ambient value */ | 
| 145 | int     ambvwt = 0;                     /* initial weight for ambient value */ | 
| 146 | double  ambacc = 0.1;                   /* ambient accuracy */ | 
| 147 | int     ambres = 256;                   /* ambient resolution */ | 
| 148 | int     ambdiv = 1024;                  /* ambient divisions */ | 
| 149 | int     ambssamp = 512;                 /* ambient super-samples */ | 
| 150 | int     ambounce = 0;                   /* ambient bounces */ | 
| 151 | char    *amblist[AMBLLEN+1];            /* ambient include/exclude list */ | 
| 152 | int     ambincl = -1;                   /* include == 1, exclude == 0 */ | 
| 153 |  | 
| 154 |  | 
| 155 | extern void | 
| 156 | ray_init(                       /* initialize ray-tracing calculation */ | 
| 157 | char    *otnm | 
| 158 | ) | 
| 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 | extern void | 
| 178 | ray_trace(                      /* trace a primary ray */ | 
| 179 | RAY     *r | 
| 180 | ) | 
| 181 | { | 
| 182 | rayorigin(r, NULL, PRIMARY, 1.0); | 
| 183 | samplendx++; | 
| 184 | rayvalue(r);            /* assumes origin and direction are set */ | 
| 185 | } | 
| 186 |  | 
| 187 |  | 
| 188 | extern void | 
| 189 | ray_done(               /* free ray-tracing data */ | 
| 190 | int     freall | 
| 191 | ) | 
| 192 | { | 
| 193 | retainfonts = 1; | 
| 194 | ambdone(); | 
| 195 | ambnotify(OVOID); | 
| 196 | freesources(); | 
| 197 | freeobjects(0, nobjects); | 
| 198 | donesets(); | 
| 199 | octdone(); | 
| 200 | thescene.cutree = EMPTY; | 
| 201 | octname = NULL; | 
| 202 | if (freall) { | 
| 203 | retainfonts = 0; | 
| 204 | freefont(NULL); | 
| 205 | freedata(NULL); | 
| 206 | initurand(0); | 
| 207 | } | 
| 208 | if (nobjects > 0) { | 
| 209 | sprintf(errmsg, "%ld objects left after call to ray_done()", | 
| 210 | nobjects); | 
| 211 | error(WARNING, errmsg); | 
| 212 | } | 
| 213 | } | 
| 214 |  | 
| 215 |  | 
| 216 | extern void | 
| 217 | ray_save(                       /* save current parameter settings */ | 
| 218 | RAYPARAMS       *rp | 
| 219 | ) | 
| 220 | { | 
| 221 | int     i, ndx; | 
| 222 |  | 
| 223 | if (rp == NULL) | 
| 224 | return; | 
| 225 | rp->do_irrad = do_irrad; | 
| 226 | rp->dstrsrc = dstrsrc; | 
| 227 | rp->shadthresh = shadthresh; | 
| 228 | rp->shadcert = shadcert; | 
| 229 | rp->directrelay = directrelay; | 
| 230 | rp->vspretest = vspretest; | 
| 231 | rp->directvis = directvis; | 
| 232 | rp->srcsizerat = srcsizerat; | 
| 233 | copycolor(rp->cextinction, cextinction); | 
| 234 | copycolor(rp->salbedo, salbedo); | 
| 235 | rp->seccg = seccg; | 
| 236 | rp->ssampdist = ssampdist; | 
| 237 | rp->specthresh = specthresh; | 
| 238 | rp->specjitter = specjitter; | 
| 239 | rp->backvis = backvis; | 
| 240 | rp->maxdepth = maxdepth; | 
| 241 | rp->minweight = minweight; | 
| 242 | copycolor(rp->ambval, ambval); | 
| 243 | memset(rp->ambfile, '\0', sizeof(rp->ambfile)); | 
| 244 | if (ambfile != NULL) | 
| 245 | strncpy(rp->ambfile, ambfile, sizeof(rp->ambfile)-1); | 
| 246 | rp->ambvwt = ambvwt; | 
| 247 | rp->ambacc = ambacc; | 
| 248 | rp->ambres = ambres; | 
| 249 | rp->ambdiv = ambdiv; | 
| 250 | rp->ambssamp = ambssamp; | 
| 251 | rp->ambounce = ambounce; | 
| 252 | rp->ambincl = ambincl; | 
| 253 | memset(rp->amblval, '\0', sizeof(rp->amblval)); | 
| 254 | ndx = 0; | 
| 255 | for (i = 0; i < AMBLLEN && amblist[i] != NULL; i++) { | 
| 256 | int     len = strlen(amblist[i]); | 
| 257 | if (ndx+len >= sizeof(rp->amblval)) | 
| 258 | break; | 
| 259 | strcpy(rp->amblval+ndx, amblist[i]); | 
| 260 | ndx += len+1; | 
| 261 | } | 
| 262 | while (i <= AMBLLEN) | 
| 263 | rp->amblndx[i++] = -1; | 
| 264 | } | 
| 265 |  | 
| 266 |  | 
| 267 | extern void | 
| 268 | ray_restore(                    /* restore parameter settings */ | 
| 269 | RAYPARAMS       *rp | 
| 270 | ) | 
| 271 | { | 
| 272 | register int    i; | 
| 273 |  | 
| 274 | if (rp == NULL) {               /* restore defaults */ | 
| 275 | RAYPARAMS       dflt; | 
| 276 | ray_defaults(&dflt); | 
| 277 | ray_restore(&dflt); | 
| 278 | return; | 
| 279 | } | 
| 280 | /* restore saved settings */ | 
| 281 | do_irrad = rp->do_irrad; | 
| 282 | dstrsrc = rp->dstrsrc; | 
| 283 | shadthresh = rp->shadthresh; | 
| 284 | shadcert = rp->shadcert; | 
| 285 | directrelay = rp->directrelay; | 
| 286 | vspretest = rp->vspretest; | 
| 287 | directvis = rp->directvis; | 
| 288 | srcsizerat = rp->srcsizerat; | 
| 289 | copycolor(cextinction, rp->cextinction); | 
| 290 | copycolor(salbedo, rp->salbedo); | 
| 291 | seccg = rp->seccg; | 
| 292 | ssampdist = rp->ssampdist; | 
| 293 | specthresh = rp->specthresh; | 
| 294 | specjitter = rp->specjitter; | 
| 295 | backvis = rp->backvis; | 
| 296 | maxdepth = rp->maxdepth; | 
| 297 | minweight = rp->minweight; | 
| 298 | copycolor(ambval, rp->ambval); | 
| 299 | ambvwt = rp->ambvwt; | 
| 300 | ambdiv = rp->ambdiv; | 
| 301 | ambssamp = rp->ambssamp; | 
| 302 | ambounce = rp->ambounce; | 
| 303 | for (i = 0; rp->amblndx[i] >= 0; i++) | 
| 304 | amblist[i] = rp->amblval + rp->amblndx[i]; | 
| 305 | while (i <= AMBLLEN) | 
| 306 | amblist[i++] = NULL; | 
| 307 | ambincl = rp->ambincl; | 
| 308 | /* update ambient calculation */ | 
| 309 | ambnotify(OVOID); | 
| 310 | if (thescene.cutree != EMPTY) { | 
| 311 | int     newamb = (ambfile == NULL) ?  rp->ambfile[0] : | 
| 312 | strcmp(ambfile, rp->ambfile) ; | 
| 313 |  | 
| 314 | if (amblist[0] != NULL) | 
| 315 | for (i = 0; i < nobjects; i++) | 
| 316 | ambnotify(i); | 
| 317 |  | 
| 318 | ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; | 
| 319 | if (newamb) { | 
| 320 | ambres = rp->ambres; | 
| 321 | ambacc = rp->ambacc; | 
| 322 | setambient(); | 
| 323 | } else { | 
| 324 | setambres(rp->ambres); | 
| 325 | setambacc(rp->ambacc); | 
| 326 | } | 
| 327 | } else { | 
| 328 | ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL; | 
| 329 | ambres = rp->ambres; | 
| 330 | ambacc = rp->ambacc; | 
| 331 | } | 
| 332 | } | 
| 333 |  | 
| 334 |  | 
| 335 | extern void | 
| 336 | ray_defaults(           /* get default parameter values */ | 
| 337 | RAYPARAMS       *rp | 
| 338 | ) | 
| 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 = .03; | 
| 348 | rp->shadcert = .75; | 
| 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 = 8; | 
| 361 | rp->minweight = 2e-3; | 
| 362 | setcolor(rp->ambval, 0., 0., 0.); | 
| 363 | memset(rp->ambfile, '\0', sizeof(rp->ambfile)); | 
| 364 | rp->ambvwt = 0; | 
| 365 | rp->ambres = 256; | 
| 366 | rp->ambacc = 0.1; | 
| 367 | rp->ambdiv = 1024; | 
| 368 | rp->ambssamp = 512; | 
| 369 | rp->ambounce = 0; | 
| 370 | rp->ambincl = -1; | 
| 371 | memset(rp->amblval, '\0', sizeof(rp->amblval)); | 
| 372 | for (i = AMBLLEN+1; i--; ) | 
| 373 | rp->amblndx[i] = -1; | 
| 374 | } |