1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pkgBSDF.c,v 2.8 2018/12/01 19:45:43 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Take BSDF XML file and generate a referencing Radiance object |
6 |
*/ |
7 |
|
8 |
#include "rtio.h" |
9 |
#include "paths.h" |
10 |
#include "bsdf.h" |
11 |
|
12 |
int do_stdout = 0; /* send Radiance object to stdout? */ |
13 |
int do_instance = 0; /* produce instance/octree pairs? */ |
14 |
|
15 |
char *progname; /* global argv[0] */ |
16 |
|
17 |
/* Return appropriate suffix for Z offset */ |
18 |
static const char * |
19 |
get_suffix(double zval) |
20 |
{ |
21 |
return((zval > FTINY) ? "_f" : (zval < -FTINY) ? "_b" : ""); |
22 |
} |
23 |
|
24 |
|
25 |
/* Produce a single rectangle corresponding to the given BSDF dimensions */ |
26 |
static void |
27 |
faceBSDF(const SDData *bsp, double zval) |
28 |
{ |
29 |
const char *sfx = get_suffix(zval); |
30 |
/* output material */ |
31 |
printf("void BSDF m_%s%s\n", bsp->name, sfx); |
32 |
printf("6 %g %s.xml 0 1 0 .\n", zval*1.00004, bsp->name); |
33 |
printf("0\n0\n\n"); |
34 |
/* output surface */ |
35 |
zval *= (zval < 0) + 0.00002; |
36 |
printf("m_%s%s polygon %s%s\n", bsp->name, sfx, bsp->name, sfx); |
37 |
printf("0\n0\n12\n"); |
38 |
printf("\t%.6e\t%.6e\t%.6e\n", |
39 |
-.5*bsp->dim[0], -.5*bsp->dim[1], zval); |
40 |
printf("\t%.6e\t%.6e\t%.6e\n", |
41 |
.5*bsp->dim[0], -.5*bsp->dim[1], zval); |
42 |
printf("\t%.6e\t%.6e\t%.6e\n", |
43 |
.5*bsp->dim[0], .5*bsp->dim[1], zval); |
44 |
printf("\t%.6e\t%.6e\t%.6e\n\n", |
45 |
-.5*bsp->dim[0], .5*bsp->dim[1], zval); |
46 |
} |
47 |
|
48 |
|
49 |
/* Convert BSDF (MGF) geometry to Radiance description */ |
50 |
static int |
51 |
geomBSDF(const SDData *bsp) |
52 |
{ |
53 |
char tmpfile[64]; |
54 |
char command[SDnameLn+64]; |
55 |
int fd; |
56 |
/* write MGF to temp file */ |
57 |
fd = open(mktemp(strcpy(tmpfile,TEMPLATE)), O_WRONLY|O_CREAT|O_EXCL, 0600); |
58 |
if (fd < 0) { |
59 |
fprintf(stderr, "Cannot open temp file '%s'\n", tmpfile); |
60 |
return(0); |
61 |
} |
62 |
(void)write(fd, bsp->mgf, strlen(bsp->mgf)); |
63 |
close(fd); |
64 |
/* set up command */ |
65 |
if (do_instance) { |
66 |
sprintf(command, "mgf2rad -s %s | oconv -f - > %s.oct", |
67 |
tmpfile, bsp->name); |
68 |
} else { |
69 |
fflush(stdout); |
70 |
sprintf(command, "mgf2rad -s %s", tmpfile); |
71 |
} |
72 |
if (system(command)) { |
73 |
fprintf(stderr, "Error running: %s\n", command); |
74 |
return(0); |
75 |
} |
76 |
unlink(tmpfile); /* remove temp file */ |
77 |
if (do_instance) { /* need to output instance? */ |
78 |
printf("void instance %s_g\n", bsp->name); |
79 |
printf("1 %s.oct\n0\n0\n\n", bsp->name); |
80 |
} |
81 |
return(1); |
82 |
} |
83 |
|
84 |
|
85 |
/* Load a BSDF XML file and produce a corresponding Radiance object */ |
86 |
static int |
87 |
cvtBSDF(char *fname) |
88 |
{ |
89 |
int retOK; |
90 |
SDData myBSDF; |
91 |
char *pname, *fnbeg; |
92 |
/* find and load the XML file */ |
93 |
retOK = strlen(fname); |
94 |
if (retOK < 5 || strcasecmp(fname+retOK-4, ".xml")) { |
95 |
fprintf(stderr, "%s: input does not end in '.xml'\n", fname); |
96 |
return(0); |
97 |
} |
98 |
pname = getpath(fname, getrlibpath(), R_OK); |
99 |
if (pname == NULL) { |
100 |
fprintf(stderr, "%s: cannot find BSDF file\n", fname); |
101 |
return(0); |
102 |
} |
103 |
fnbeg = strrchr(fname, DIRSEP); |
104 |
if (fnbeg != NULL) /* eliminate directory */ |
105 |
fname = fnbeg+1; |
106 |
SDclearBSDF(&myBSDF, fname); |
107 |
if (SDreportError(SDloadFile(&myBSDF, pname), stderr)) |
108 |
return(0); |
109 |
retOK = (myBSDF.dim[0] > FTINY) & (myBSDF.dim[1] > FTINY); |
110 |
if (!retOK) { |
111 |
fprintf(stderr, "%s: zero width or height\n", fname); |
112 |
} else { |
113 |
if (!do_stdout) { |
114 |
char rname[SDnameLn+4]; |
115 |
strcpy(rname, myBSDF.name); |
116 |
strcat(rname, ".rad"); |
117 |
retOK = (freopen(rname, "w", stdout) != NULL); |
118 |
} |
119 |
if (retOK) { |
120 |
printf("# Produced by: %s %s\n", progname, fname); |
121 |
if (myBSDF.matn[0] && myBSDF.makr[0]) |
122 |
printf("# Material '%s' by '%s'\n\n", |
123 |
myBSDF.matn, myBSDF.makr); |
124 |
if (myBSDF.mgf == NULL) { |
125 |
faceBSDF(&myBSDF, .0); |
126 |
} else { |
127 |
faceBSDF(&myBSDF, myBSDF.dim[2]); |
128 |
if (myBSDF.rb != NULL) |
129 |
faceBSDF(&myBSDF, -myBSDF.dim[2]); |
130 |
retOK = geomBSDF(&myBSDF); |
131 |
} |
132 |
} |
133 |
} |
134 |
SDfreeBSDF(&myBSDF); /* clean up and return */ |
135 |
return(retOK); |
136 |
} |
137 |
|
138 |
|
139 |
/* Generate BSDF-referencing Radiance scenes, one per XML input */ |
140 |
int |
141 |
main(int argc, char *argv[]) |
142 |
{ |
143 |
int status = 0; |
144 |
int i; |
145 |
|
146 |
progname = argv[0]; |
147 |
|
148 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
149 |
switch (argv[i][1]) { |
150 |
case 'i': |
151 |
do_instance = 1; |
152 |
break; |
153 |
case 's': |
154 |
do_stdout = 1; |
155 |
break; |
156 |
default: |
157 |
goto userr; |
158 |
} |
159 |
if (i >= argc) { |
160 |
fprintf(stderr, "%s: missing XML input\n", argv[0]); |
161 |
goto userr; |
162 |
} |
163 |
if (i < argc-1 && do_stdout) { |
164 |
fprintf(stderr, "%s: cannot send multiple BSDFs to stdout\n", |
165 |
argv[0]); |
166 |
return(1); |
167 |
} |
168 |
for ( ; i < argc; i++) |
169 |
if (!cvtBSDF(argv[i])) { |
170 |
fprintf(stderr, "%s: conversion of '%s' failed\n", |
171 |
argv[0], argv[i]); |
172 |
status = 1; |
173 |
} |
174 |
return(status); |
175 |
userr: |
176 |
fprintf(stderr, "Usage: %s [-i][-s] input.xml ..\n", argv[0]); |
177 |
return(1); |
178 |
} |