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