| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: rcmain.c,v 2.42 2025/06/05 18:26:46 greg Exp $"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* rcmain.c - main for rtcontrib ray contribution tracer |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "copyright.h" |
| 9 |
|
| 10 |
#include <signal.h> |
| 11 |
#include <time.h> |
| 12 |
#include "rcontrib.h" |
| 13 |
#include "random.h" |
| 14 |
#include "source.h" |
| 15 |
#include "ambient.h" |
| 16 |
#include "pmapray.h" |
| 17 |
#include "pmapcontrib.h" |
| 18 |
|
| 19 |
int gargc; /* global argc */ |
| 20 |
char **gargv; /* global argv */ |
| 21 |
char *octname; /* global octree name */ |
| 22 |
|
| 23 |
char *sigerr[NSIG]; /* signal error messages */ |
| 24 |
|
| 25 |
int nproc = 1; /* number of processes requested */ |
| 26 |
int nchild = 0; /* number of children (-1 in child) */ |
| 27 |
|
| 28 |
int inpfmt = 'a'; /* input format */ |
| 29 |
int outfmt = 'a'; /* output format */ |
| 30 |
|
| 31 |
int header = 1; /* output header? */ |
| 32 |
int force_open = 0; /* truncate existing output? */ |
| 33 |
int recover = 0; /* recover previous output? */ |
| 34 |
int accumulate = 1; /* input rays per output record */ |
| 35 |
int contrib = 0; /* computing contributions? */ |
| 36 |
|
| 37 |
int xres = 0; /* horizontal (scan) size */ |
| 38 |
int yres = 0; /* vertical resolution */ |
| 39 |
|
| 40 |
int using_stdout = 0; /* are we using stdout? */ |
| 41 |
|
| 42 |
int imm_irrad = 0; /* compute immediate irradiance? */ |
| 43 |
int lim_dist = 0; /* limit distance? */ |
| 44 |
|
| 45 |
int report_intvl = 0; /* reporting interval (seconds) */ |
| 46 |
|
| 47 |
char **modname = NULL; /* ordered modifier name list */ |
| 48 |
int nmods = 0; /* number of modifiers */ |
| 49 |
int modasiz = 0; /* allocated modifier array size */ |
| 50 |
|
| 51 |
void (*addobjnotify[])() = {ambnotify, NULL}; |
| 52 |
|
| 53 |
char RCCONTEXT[] = "RC."; /* our special evaluation context */ |
| 54 |
|
| 55 |
#if defined(_WIN32) || defined(_WIN64) |
| 56 |
#define RCONTRIB_FEATURES "Accumulation\nSummation\nRecovery\n" \ |
| 57 |
"ImmediateIrradiance\n" \ |
| 58 |
"ProgressReporting\nDistanceLimiting\n" \ |
| 59 |
"InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \ |
| 60 |
"Outputs=V,W\n" \ |
| 61 |
"OutputCS=RGB,spec\n" |
| 62 |
#else |
| 63 |
#define RCONTRIB_FEATURES "Multiprocessing\n" \ |
| 64 |
"Accumulation\nSummation\nRecovery\n" \ |
| 65 |
"ImmediateIrradiance\n" \ |
| 66 |
"ProgressReporting\nDistanceLimiting\n" \ |
| 67 |
"InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \ |
| 68 |
"Outputs=V,W\n" \ |
| 69 |
"OutputCS=RGB,spec\n" |
| 70 |
#endif |
| 71 |
|
| 72 |
static void |
| 73 |
printdefaults(void) /* print default values to stdout */ |
| 74 |
{ |
| 75 |
printf("-c %-5d\t\t\t# accumulated rays per record\n", accumulate); |
| 76 |
printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', |
| 77 |
contrib ? "contributions" : "coefficients"); |
| 78 |
if (imm_irrad) |
| 79 |
printf("-I+\t\t\t\t# immediate irradiance on\n"); |
| 80 |
printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc); |
| 81 |
printf("-x %-9d\t\t\t# %s\n", xres, |
| 82 |
yres && xres ? "x resolution" : "flush interval"); |
| 83 |
printf("-y %-9d\t\t\t# y resolution\n", yres); |
| 84 |
printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" : |
| 85 |
"-ld-\t\t\t\t# limit distance off\n"); |
| 86 |
printf(header ? "-h+\t\t\t\t# output header\n" : |
| 87 |
"-h-\t\t\t\t# no header\n"); |
| 88 |
printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n", |
| 89 |
inpfmt, outfmt, formstr(inpfmt), formstr(outfmt)); |
| 90 |
if (report_intvl > 0) |
| 91 |
printf("-t %-9d\t\t\t# time between reports\n", report_intvl); |
| 92 |
printf(erract[WARNING].pf != NULL ? |
| 93 |
"-w+\t\t\t\t# warning messages on\n" : |
| 94 |
"-w-\t\t\t\t# warning messages off\n"); |
| 95 |
print_rdefaults(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
static void |
| 100 |
onsig( /* fatal signal */ |
| 101 |
int signo |
| 102 |
) |
| 103 |
{ |
| 104 |
static int gotsig = 0; |
| 105 |
|
| 106 |
if (gotsig++) /* two signals and we're gone! */ |
| 107 |
_exit(signo); |
| 108 |
|
| 109 |
#ifdef SIGALRM |
| 110 |
alarm(15); /* allow 15 seconds to clean up */ |
| 111 |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
| 112 |
#endif |
| 113 |
eputs("signal - "); |
| 114 |
eputs(sigerr[signo]); |
| 115 |
eputs("\n"); |
| 116 |
quit(3); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
static void |
| 121 |
sigdie( /* set fatal signal */ |
| 122 |
int signo, |
| 123 |
char *msg |
| 124 |
) |
| 125 |
{ |
| 126 |
if (signal(signo, onsig) == SIG_IGN) |
| 127 |
signal(signo, SIG_IGN); |
| 128 |
sigerr[signo] = msg; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/* set input/output format */ |
| 133 |
static void |
| 134 |
setformat(const char *fmt) |
| 135 |
{ |
| 136 |
switch (fmt[0]) { |
| 137 |
case 'f': |
| 138 |
case 'd': |
| 139 |
SET_FILE_BINARY(stdin); |
| 140 |
/* fall through */ |
| 141 |
case 'a': |
| 142 |
inpfmt = fmt[0]; |
| 143 |
break; |
| 144 |
default: |
| 145 |
goto fmterr; |
| 146 |
} |
| 147 |
switch (fmt[1]) { |
| 148 |
case '\0': |
| 149 |
outfmt = inpfmt; |
| 150 |
return; |
| 151 |
case 'a': |
| 152 |
case 'f': |
| 153 |
case 'd': |
| 154 |
case 'c': |
| 155 |
outfmt = fmt[1]; |
| 156 |
break; |
| 157 |
default: |
| 158 |
goto fmterr; |
| 159 |
} |
| 160 |
if (!fmt[2]) |
| 161 |
return; |
| 162 |
fmterr: |
| 163 |
sprintf(errmsg, "Illegal i/o format: -f%s", fmt); |
| 164 |
error(USER, errmsg); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/* Set overriding options */ |
| 169 |
static void |
| 170 |
override_options(void) |
| 171 |
{ |
| 172 |
shadthresh = 0; |
| 173 |
ambssamp = 0; |
| 174 |
ambacc = 0; |
| 175 |
if (accumulate <= 0) /* no output flushing for single record */ |
| 176 |
xres = yres = 0; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
int |
| 181 |
main(int argc, char *argv[]) |
| 182 |
{ |
| 183 |
#define check(ol,al) if (argv[i][ol] || \ |
| 184 |
badarg(argc-i-1,argv+i+1,al)) \ |
| 185 |
goto badopt |
| 186 |
#define check_bool(olen,var) switch (argv[i][olen]) { \ |
| 187 |
case '\0': var = !var; break; \ |
| 188 |
case 'y': case 'Y': case 't': case 'T': \ |
| 189 |
case '+': case '1': var = 1; break; \ |
| 190 |
case 'n': case 'N': case 'f': case 'F': \ |
| 191 |
case '-': case '0': var = 0; break; \ |
| 192 |
default: goto badopt; } |
| 193 |
char *curout = NULL; |
| 194 |
char *prms = NULL; |
| 195 |
char *binval = NULL; |
| 196 |
int bincnt = 0; |
| 197 |
int rval; |
| 198 |
int i; |
| 199 |
/* global program name */ |
| 200 |
argv[0] = fixargv0(argv[0]); |
| 201 |
gargv = argv; |
| 202 |
gargc = argc; |
| 203 |
/* feature check only? */ |
| 204 |
strcat(RFeatureList, RCONTRIB_FEATURES); |
| 205 |
if (argc > 1 && !strcmp(argv[1], "-features")) |
| 206 |
return feature_status(argc-2, argv+2); |
| 207 |
#if defined(_WIN32) || defined(_WIN64) /* increase file limit to maximum */ |
| 208 |
for (i = 8192; i > _IOB_ENTRIES; i >>= 1) |
| 209 |
if (_setmaxstdio(i) == i) |
| 210 |
break; |
| 211 |
#endif |
| 212 |
initfunc(); /* initialize calcomp routines */ |
| 213 |
calcontext(RCCONTEXT); |
| 214 |
/* option city */ |
| 215 |
for (i = 1; i < argc; i++) { |
| 216 |
/* expand arguments */ |
| 217 |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
| 218 |
; |
| 219 |
if (rval < 0) { |
| 220 |
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
| 221 |
error(SYSTEM, errmsg); |
| 222 |
} |
| 223 |
if (argv[i] == NULL || argv[i][0] != '-') |
| 224 |
break; /* break from options */ |
| 225 |
if (!strcmp(argv[i], "-version")) { |
| 226 |
puts(VersionID); |
| 227 |
quit(0); |
| 228 |
} |
| 229 |
if (!strcmp(argv[i], "-defaults") || |
| 230 |
!strcmp(argv[i], "-help")) { |
| 231 |
override_options(); |
| 232 |
printdefaults(); |
| 233 |
quit(0); |
| 234 |
} |
| 235 |
rval = getrenderopt(argc-i, argv+i); |
| 236 |
if (rval >= 0) { |
| 237 |
i += rval; |
| 238 |
continue; |
| 239 |
} |
| 240 |
switch (argv[i][1]) { |
| 241 |
case 'n': /* number of cores */ |
| 242 |
check(2,"i"); |
| 243 |
nproc = atoi(argv[++i]); |
| 244 |
if (nproc <= 0) |
| 245 |
error(USER, "bad number of processes"); |
| 246 |
break; |
| 247 |
case 'V': /* output contributions */ |
| 248 |
check_bool(2,contrib); |
| 249 |
break; |
| 250 |
case 'x': /* x resolution */ |
| 251 |
check(2,"i"); |
| 252 |
xres = atoi(argv[++i]); |
| 253 |
break; |
| 254 |
case 'y': /* y resolution */ |
| 255 |
check(2,"i"); |
| 256 |
yres = atoi(argv[++i]); |
| 257 |
break; |
| 258 |
case 'w': /* warnings */ |
| 259 |
rval = (erract[WARNING].pf != NULL); |
| 260 |
check_bool(2,rval); |
| 261 |
if (rval) erract[WARNING].pf = wputs; |
| 262 |
else erract[WARNING].pf = NULL; |
| 263 |
break; |
| 264 |
case 'l': /* limit distance */ |
| 265 |
if (argv[i][2] != 'd') |
| 266 |
goto badopt; |
| 267 |
check_bool(3,lim_dist); |
| 268 |
break; |
| 269 |
case 'I': /* immed. irradiance */ |
| 270 |
check_bool(2,imm_irrad); |
| 271 |
break; |
| 272 |
case 'f': /* force or format */ |
| 273 |
if (argv[i][2] == 'o') { |
| 274 |
check_bool(3,force_open); |
| 275 |
break; |
| 276 |
} |
| 277 |
setformat(argv[i]+2); |
| 278 |
break; |
| 279 |
case 'o': /* output */ |
| 280 |
check(2,"s"); |
| 281 |
curout = argv[++i]; |
| 282 |
break; |
| 283 |
case 'r': /* recover output */ |
| 284 |
check_bool(2,recover); |
| 285 |
break; |
| 286 |
case 'h': /* header output */ |
| 287 |
check_bool(2,header); |
| 288 |
break; |
| 289 |
case 'p': /* parameter setting(s) */ |
| 290 |
check(2,"s"); |
| 291 |
set_eparams(prms = argv[++i]); |
| 292 |
break; |
| 293 |
case 'c': /* sample count */ |
| 294 |
check(2,"i"); |
| 295 |
accumulate = atoi(argv[++i]); |
| 296 |
break; |
| 297 |
case 'b': /* bin expression/count */ |
| 298 |
if (argv[i][2] == 'n') { |
| 299 |
check(3,"s"); |
| 300 |
bincnt = (int)(eval(argv[++i]) + .5); |
| 301 |
break; |
| 302 |
} |
| 303 |
check(2,"s"); |
| 304 |
binval = argv[++i]; |
| 305 |
break; |
| 306 |
case 'm': /* modifier name */ |
| 307 |
check(2,"s"); |
| 308 |
addmodifier(argv[++i], curout, prms, binval, bincnt); |
| 309 |
break; |
| 310 |
case 'M': /* modifier file */ |
| 311 |
check(2,"s"); |
| 312 |
addmodfile(argv[++i], curout, prms, binval, bincnt); |
| 313 |
break; |
| 314 |
case 't': /* reporting interval */ |
| 315 |
check(2,"i"); |
| 316 |
report_intvl = atoi(argv[++i]); |
| 317 |
break; |
| 318 |
default: |
| 319 |
goto badopt; |
| 320 |
} |
| 321 |
} |
| 322 |
if (nmods <= 0) |
| 323 |
error(USER, "missing required modifier argument"); |
| 324 |
/* override some option settings */ |
| 325 |
override_options(); |
| 326 |
/* set/check spectral sampling */ |
| 327 |
if (setspectrsamp(CNDX, WLPART) < 0) |
| 328 |
error(USER, "unsupported spectral sampling"); |
| 329 |
/* initialize object types */ |
| 330 |
initotypes(); |
| 331 |
/* initialize urand */ |
| 332 |
if (rand_samp) { |
| 333 |
srandom((long)time(0)); |
| 334 |
initurand(0); |
| 335 |
} else { |
| 336 |
srandom(0L); |
| 337 |
initurand(2048); |
| 338 |
} |
| 339 |
/* set up signal handling */ |
| 340 |
sigdie(SIGINT, "Interrupt"); |
| 341 |
#ifdef SIGHUP |
| 342 |
sigdie(SIGHUP, "Hangup"); |
| 343 |
#endif |
| 344 |
sigdie(SIGTERM, "Terminate"); |
| 345 |
#ifdef SIGPIPE |
| 346 |
sigdie(SIGPIPE, "Broken pipe"); |
| 347 |
#endif |
| 348 |
#ifdef SIGALRM |
| 349 |
sigdie(SIGALRM, "Alarm clock"); |
| 350 |
#endif |
| 351 |
#ifdef SIGXCPU |
| 352 |
sigdie(SIGXCPU, "CPU limit exceeded"); |
| 353 |
sigdie(SIGXFSZ, "File size exceeded"); |
| 354 |
#endif |
| 355 |
#ifdef NICE |
| 356 |
nice(NICE); /* lower priority */ |
| 357 |
#endif |
| 358 |
/* get octree */ |
| 359 |
if (i == argc) |
| 360 |
octname = NULL; |
| 361 |
else if (i == argc-1) |
| 362 |
octname = argv[i]; |
| 363 |
else |
| 364 |
goto badopt; |
| 365 |
if (octname == NULL) |
| 366 |
error(USER, "missing octree argument"); |
| 367 |
|
| 368 |
readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL); |
| 369 |
nsceneobjs = nobjects; |
| 370 |
|
| 371 |
/* PMAP: set up & load photon maps */ |
| 372 |
ray_init_pmap(); |
| 373 |
|
| 374 |
marksources(); /* find and mark sources */ |
| 375 |
|
| 376 |
/* PMAP: init photon map for light source contributions */ |
| 377 |
initPmapContrib(&modconttab, nmods); |
| 378 |
|
| 379 |
setambient(); /* initialize ambient calculation */ |
| 380 |
|
| 381 |
rcontrib(); /* trace ray contributions (loop) */ |
| 382 |
|
| 383 |
/* PMAP: free photon maps */ |
| 384 |
ray_done_pmap(); |
| 385 |
|
| 386 |
quit(0); /* exit clean */ |
| 387 |
|
| 388 |
badopt: |
| 389 |
fprintf(stderr, |
| 390 |
"Usage: %s [-n nprocs][-V][-c count][-r][-e expr][-f source][-o ospec][-p p1=V1,p2=V2][-b binv][-bn N] {-m mod | -M file} [rtrace options] octree\n", |
| 391 |
progname); |
| 392 |
sprintf(errmsg, "command line error at '%s'", argv[i]); |
| 393 |
error(USER, errmsg); |
| 394 |
return(1); /* pro forma return */ |
| 395 |
|
| 396 |
#undef check |
| 397 |
#undef check_bool |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
void |
| 402 |
wputs( /* warning output function */ |
| 403 |
const char *s |
| 404 |
) |
| 405 |
{ |
| 406 |
int lasterrno = errno; |
| 407 |
if (erract[WARNING].pf == NULL) |
| 408 |
return; /* called by calcomp or someone */ |
| 409 |
eputs(s); |
| 410 |
errno = lasterrno; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
void |
| 415 |
eputs( /* put string to stderr */ |
| 416 |
const char *s |
| 417 |
) |
| 418 |
{ |
| 419 |
static int midline = 0; |
| 420 |
|
| 421 |
if (!*s) |
| 422 |
return; |
| 423 |
if (!midline++) { |
| 424 |
fputs(progname, stderr); |
| 425 |
fputs(": ", stderr); |
| 426 |
} |
| 427 |
fputs(s, stderr); |
| 428 |
if (s[strlen(s)-1] == '\n') { |
| 429 |
fflush(stderr); |
| 430 |
midline = 0; |
| 431 |
} |
| 432 |
} |