1 |
– |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* rtrace.c - program and variables for individual ray tracing. |
9 |
– |
* |
10 |
– |
* 6/11/86 |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
|
/* |
11 |
|
* Input is in the form: |
12 |
|
* |
21 |
|
* irradiance values are desired. |
22 |
|
*/ |
23 |
|
|
24 |
< |
#include "ray.h" |
24 |
> |
#include <time.h> |
25 |
|
|
26 |
< |
#include "octree.h" |
27 |
< |
|
26 |
> |
#include "platform.h" |
27 |
> |
#include "ray.h" |
28 |
|
#include "otypes.h" |
32 |
– |
|
29 |
|
#include "resolu.h" |
30 |
|
|
31 |
+ |
CUBE thescene; /* our scene */ |
32 |
+ |
OBJECT nsceneobjs; /* number of objects in our scene */ |
33 |
+ |
|
34 |
|
int dimlist[MAXDIM]; /* sampling dimensions */ |
35 |
|
int ndims = 0; /* number of sampling dimensions */ |
36 |
|
int samplendx = 0; /* index for this sample */ |
37 |
|
|
38 |
|
int imm_irrad = 0; /* compute immediate irradiance? */ |
39 |
+ |
int lim_dist = 0; /* limit distance? */ |
40 |
|
|
41 |
|
int inform = 'a'; /* input format */ |
42 |
|
int outform = 'a'; /* output format */ |
43 |
|
char *outvals = "v"; /* output specification */ |
44 |
|
|
45 |
+ |
int do_irrad = 0; /* compute irradiance? */ |
46 |
+ |
|
47 |
+ |
void (*trace)() = NULL; /* trace call */ |
48 |
+ |
|
49 |
+ |
extern void ambnotify(), tranotify(); |
50 |
+ |
void (*addobjnotify[])() = {ambnotify, tranotify, NULL}; |
51 |
|
char *tralist[128]; /* list of modifers to trace (or no) */ |
52 |
|
int traincl = -1; /* include == 1, exclude == 0 */ |
53 |
|
#define MAXTSET 511 /* maximum number in trace set */ |
65 |
|
double srcsizerat = .2; /* maximum ratio source size/dist. */ |
66 |
|
|
67 |
|
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
68 |
< |
double salbedo = 0.; /* global scattering albedo */ |
68 |
> |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
69 |
|
double seccg = 0.; /* global scattering eccentricity */ |
70 |
|
double ssampdist = 0.; /* scatter sampling distance */ |
71 |
|
|
77 |
|
int maxdepth = 6; /* maximum recursion depth */ |
78 |
|
double minweight = 4e-3; /* minimum ray weight */ |
79 |
|
|
80 |
+ |
char *ambfile = NULL; /* ambient file name */ |
81 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
82 |
+ |
int ambvwt = 0; /* initial weight for ambient value */ |
83 |
|
double ambacc = 0.2; /* ambient accuracy */ |
84 |
|
int ambres = 128; /* ambient resolution */ |
85 |
|
int ambdiv = 512; /* ambient divisions */ |
88 |
|
char *amblist[128]; /* ambient include/exclude list */ |
89 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
90 |
|
|
83 |
– |
extern OBJREC Lamb; /* a Lambertian surface */ |
91 |
|
|
92 |
|
static RAY thisray; /* for our convenience */ |
93 |
|
|
94 |
< |
static int oputo(), oputd(), oputv(), oputl(), oputL(), |
94 |
> |
static void oputo(), oputd(), oputv(), oputl(), oputL(), oputc(), |
95 |
|
oputp(), oputn(), oputN(), oputs(), oputw(), oputm(); |
96 |
|
|
97 |
< |
static int ourtrace(), tabin(); |
98 |
< |
static int (*ray_out[16])(), (*every_out[16])(); |
97 |
> |
static void ourtrace(), tabin(); |
98 |
> |
static void (*ray_out[16])(), (*every_out[16])(); |
99 |
|
static int castonly = 0; |
100 |
|
|
101 |
< |
static int puta(), putf(), putd(); |
101 |
> |
static void puta(), putf(), putd(); |
102 |
|
|
103 |
< |
static int (*putreal)(); |
103 |
> |
static void (*putreal)(); |
104 |
|
|
105 |
+ |
void bogusray(), rad(), irrad(), printvals(); |
106 |
|
|
107 |
+ |
|
108 |
+ |
void |
109 |
|
quit(code) /* quit program */ |
110 |
|
int code; |
111 |
|
{ |
112 |
< |
#ifndef NIX |
112 |
> |
#ifndef NON_POSIX /* XXX we don't clean up elsewhere? */ |
113 |
|
headclean(); /* delete header file */ |
114 |
|
pfclean(); /* clean up persist files */ |
115 |
|
#endif |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
+ |
void |
135 |
|
rtrace(fname) /* trace rays from file */ |
136 |
|
char *fname; |
137 |
|
{ |
138 |
|
long vcount = hresolu>1 ? hresolu*vresolu : vresolu; |
139 |
|
long nextflush = hresolu; |
140 |
|
FILE *fp; |
141 |
+ |
double d; |
142 |
|
FVECT orig, direc; |
143 |
|
/* set up input */ |
144 |
|
if (fname == NULL) |
147 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
148 |
|
error(SYSTEM, errmsg); |
149 |
|
} |
150 |
< |
#ifdef MSDOS |
150 |
> |
#ifdef _WIN32 |
151 |
|
if (inform != 'a') |
152 |
< |
setmode(fileno(fp), O_BINARY); |
152 |
> |
SET_FILE_BINARY(fp); |
153 |
|
#endif |
154 |
|
/* set up output */ |
155 |
|
setoutput(outvals); |
173 |
|
while (getvec(orig, inform, fp) == 0 && |
174 |
|
getvec(direc, inform, fp) == 0) { |
175 |
|
|
176 |
< |
if (normalize(direc) == 0.0) { /* zero ==> flush */ |
177 |
< |
fflush(stdout); |
178 |
< |
continue; |
179 |
< |
} |
180 |
< |
samplendx++; |
176 |
> |
d = normalize(direc); |
177 |
> |
if (d == 0.0) { /* zero ==> flush */ |
178 |
> |
bogusray(); |
179 |
> |
if (--nextflush <= 0 || vcount <= 0) { |
180 |
> |
fflush(stdout); |
181 |
> |
nextflush = hresolu; |
182 |
> |
} |
183 |
> |
} else { |
184 |
> |
samplendx++; |
185 |
|
/* compute and print */ |
186 |
< |
if (imm_irrad) |
187 |
< |
irrad(orig, direc); |
188 |
< |
else |
189 |
< |
rad(orig, direc); |
186 |
> |
if (imm_irrad) |
187 |
> |
irrad(orig, direc); |
188 |
> |
else |
189 |
> |
rad(orig, direc, lim_dist ? d : 0.0); |
190 |
|
/* flush if time */ |
191 |
< |
if (--nextflush == 0) { |
192 |
< |
fflush(stdout); |
193 |
< |
nextflush = hresolu; |
191 |
> |
if (--nextflush == 0) { |
192 |
> |
fflush(stdout); |
193 |
> |
nextflush = hresolu; |
194 |
> |
} |
195 |
|
} |
196 |
|
if (ferror(stdout)) |
197 |
|
error(SYSTEM, "write error"); |
209 |
|
setoutput(vs) /* set up output tables */ |
210 |
|
register char *vs; |
211 |
|
{ |
212 |
< |
extern int (*trace)(); |
213 |
< |
register int (**table)() = ray_out; |
212 |
> |
extern void (*trace)(); |
213 |
> |
register void (**table)() = ray_out; |
214 |
|
|
215 |
|
castonly = 1; |
216 |
|
while (*vs) |
235 |
|
*table++ = oputl; |
236 |
|
castonly = 0; |
237 |
|
break; |
238 |
+ |
case 'c': /* local coordinates */ |
239 |
+ |
*table++ = oputc; |
240 |
+ |
break; |
241 |
|
case 'L': /* single ray length */ |
242 |
|
*table++ = oputL; |
243 |
|
break; |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
< |
rad(org, dir) /* compute and print ray value(s) */ |
268 |
> |
void |
269 |
> |
bogusray() /* print out empty record */ |
270 |
> |
{ |
271 |
> |
thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] = |
272 |
> |
thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0; |
273 |
> |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
274 |
> |
printvals(&thisray); |
275 |
> |
} |
276 |
> |
|
277 |
> |
|
278 |
> |
void |
279 |
> |
rad(org, dir, dmax) /* compute and print ray value(s) */ |
280 |
|
FVECT org, dir; |
281 |
+ |
double dmax; |
282 |
|
{ |
283 |
|
VCOPY(thisray.rorg, org); |
284 |
|
VCOPY(thisray.rdir, dir); |
285 |
< |
thisray.rmax = 0.0; |
285 |
> |
thisray.rmax = dmax; |
286 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
287 |
< |
if (castonly) |
288 |
< |
localhit(&thisray, &thescene) || sourcehit(&thisray); |
289 |
< |
else |
287 |
> |
if (castonly) { |
288 |
> |
if (!localhit(&thisray, &thescene)) |
289 |
> |
if (thisray.ro == &Aftplane) { /* clipped */ |
290 |
> |
thisray.ro = NULL; |
291 |
> |
thisray.rot = FHUGE; |
292 |
> |
} else |
293 |
> |
sourcehit(&thisray); |
294 |
> |
} else |
295 |
|
rayvalue(&thisray); |
296 |
|
printvals(&thisray); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
+ |
void |
301 |
|
irrad(org, dir) /* compute immediate irradiance value */ |
302 |
|
FVECT org, dir; |
303 |
|
{ |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
+ |
void |
324 |
|
printvals(r) /* print requested ray values */ |
325 |
|
RAY *r; |
326 |
|
{ |
327 |
< |
register int (**tp)(); |
327 |
> |
register void (**tp)(); |
328 |
|
|
329 |
|
if (ray_out[0] == NULL) |
330 |
|
return; |
335 |
|
} |
336 |
|
|
337 |
|
|
338 |
+ |
int |
339 |
|
getvec(vec, fmt, fp) /* get a vector from fp */ |
340 |
|
register FVECT vec; |
341 |
|
int fmt; |
342 |
|
FILE *fp; |
343 |
|
{ |
304 |
– |
extern char *fgetword(); |
344 |
|
static float vf[3]; |
345 |
|
static double vd[3]; |
346 |
|
char buf[32]; |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
+ |
void |
376 |
|
tranotify(obj) /* record new modifier */ |
377 |
|
OBJECT obj; |
378 |
|
{ |
380 |
|
register OBJREC *o = objptr(obj); |
381 |
|
register char **tralp; |
382 |
|
|
383 |
+ |
if (obj == OVOID) { /* starting over */ |
384 |
+ |
traset[0] = 0; |
385 |
+ |
hitlimit = 0; |
386 |
+ |
return; |
387 |
+ |
} |
388 |
|
if (hitlimit || !ismodifier(o->otype)) |
389 |
|
return; |
390 |
|
for (tralp = tralist; *tralp != NULL; tralp++) |
400 |
|
} |
401 |
|
|
402 |
|
|
403 |
< |
static |
403 |
> |
static void |
404 |
|
ourtrace(r) /* print ray values */ |
405 |
|
RAY *r; |
406 |
|
{ |
407 |
< |
register int (**tp)(); |
407 |
> |
register void (**tp)(); |
408 |
|
|
409 |
|
if (every_out[0] == NULL) |
410 |
|
return; |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
< |
static |
423 |
> |
static void |
424 |
|
tabin(r) /* tab in appropriate amount */ |
425 |
|
RAY *r; |
426 |
|
{ |
431 |
|
} |
432 |
|
|
433 |
|
|
434 |
< |
static |
434 |
> |
static void |
435 |
|
oputo(r) /* print origin */ |
436 |
< |
register RAY *r; |
436 |
> |
RAY *r; |
437 |
|
{ |
438 |
|
(*putreal)(r->rorg[0]); |
439 |
|
(*putreal)(r->rorg[1]); |
441 |
|
} |
442 |
|
|
443 |
|
|
444 |
< |
static |
444 |
> |
static void |
445 |
|
oputd(r) /* print direction */ |
446 |
< |
register RAY *r; |
446 |
> |
RAY *r; |
447 |
|
{ |
448 |
|
(*putreal)(r->rdir[0]); |
449 |
|
(*putreal)(r->rdir[1]); |
451 |
|
} |
452 |
|
|
453 |
|
|
454 |
< |
static |
454 |
> |
static void |
455 |
|
oputv(r) /* print value */ |
456 |
< |
register RAY *r; |
456 |
> |
RAY *r; |
457 |
|
{ |
458 |
|
COLR cout; |
459 |
|
|
470 |
|
} |
471 |
|
|
472 |
|
|
473 |
< |
static |
473 |
> |
static void |
474 |
|
oputl(r) /* print effective distance */ |
475 |
< |
register RAY *r; |
475 |
> |
RAY *r; |
476 |
|
{ |
477 |
|
(*putreal)(r->rt); |
478 |
|
} |
479 |
|
|
480 |
|
|
481 |
< |
static |
481 |
> |
static void |
482 |
|
oputL(r) /* print single ray length */ |
483 |
< |
register RAY *r; |
483 |
> |
RAY *r; |
484 |
|
{ |
485 |
|
(*putreal)(r->rot); |
486 |
|
} |
487 |
|
|
488 |
|
|
489 |
< |
static |
489 |
> |
static void |
490 |
> |
oputc(r) /* print local coordinates */ |
491 |
> |
RAY *r; |
492 |
> |
{ |
493 |
> |
(*putreal)(r->uv[0]); |
494 |
> |
(*putreal)(r->uv[1]); |
495 |
> |
} |
496 |
> |
|
497 |
> |
|
498 |
> |
static void |
499 |
|
oputp(r) /* print point */ |
500 |
< |
register RAY *r; |
500 |
> |
RAY *r; |
501 |
|
{ |
502 |
|
if (r->rot < FHUGE) { |
503 |
|
(*putreal)(r->rop[0]); |
511 |
|
} |
512 |
|
|
513 |
|
|
514 |
< |
static |
514 |
> |
static void |
515 |
|
oputN(r) /* print unperturbed normal */ |
516 |
< |
register RAY *r; |
516 |
> |
RAY *r; |
517 |
|
{ |
518 |
|
if (r->rot < FHUGE) { |
519 |
|
(*putreal)(r->ron[0]); |
527 |
|
} |
528 |
|
|
529 |
|
|
530 |
< |
static |
530 |
> |
static void |
531 |
|
oputn(r) /* print perturbed normal */ |
532 |
|
RAY *r; |
533 |
|
{ |
546 |
|
} |
547 |
|
|
548 |
|
|
549 |
< |
static |
549 |
> |
static void |
550 |
|
oputs(r) /* print name */ |
551 |
< |
register RAY *r; |
551 |
> |
RAY *r; |
552 |
|
{ |
553 |
|
if (r->ro != NULL) |
554 |
|
fputs(r->ro->oname, stdout); |
558 |
|
} |
559 |
|
|
560 |
|
|
561 |
< |
static |
561 |
> |
static void |
562 |
|
oputw(r) /* print weight */ |
563 |
< |
register RAY *r; |
563 |
> |
RAY *r; |
564 |
|
{ |
565 |
|
(*putreal)(r->rweight); |
566 |
|
} |
567 |
|
|
568 |
|
|
569 |
< |
static |
569 |
> |
static void |
570 |
|
oputm(r) /* print modifier */ |
571 |
< |
register RAY *r; |
571 |
> |
RAY *r; |
572 |
|
{ |
573 |
|
if (r->ro != NULL) |
574 |
< |
fputs(objptr(r->ro->omod)->oname, stdout); |
574 |
> |
if (r->ro->omod != OVOID) |
575 |
> |
fputs(objptr(r->ro->omod)->oname, stdout); |
576 |
> |
else |
577 |
> |
fputs(VOIDID, stdout); |
578 |
|
else |
579 |
|
putchar('*'); |
580 |
|
putchar('\t'); |
581 |
|
} |
582 |
|
|
583 |
|
|
584 |
< |
static |
584 |
> |
static void |
585 |
|
puta(v) /* print ascii value */ |
586 |
|
double v; |
587 |
|
{ |
589 |
|
} |
590 |
|
|
591 |
|
|
592 |
< |
static |
592 |
> |
static void |
593 |
|
putd(v) /* print binary double */ |
594 |
|
double v; |
595 |
|
{ |
597 |
|
} |
598 |
|
|
599 |
|
|
600 |
< |
static |
600 |
> |
static void |
601 |
|
putf(v) /* print binary float */ |
602 |
|
double v; |
603 |
|
{ |