1 |
greg |
2.14 |
/* RCSid $Id: bsdf.h,v 2.13 2011/06/05 20:27:14 greg Exp $ */ |
2 |
greg |
2.4 |
/* |
3 |
|
|
* bsdf.h |
4 |
|
|
* |
5 |
|
|
* Declarations for bidirectional scattering distribution functions. |
6 |
|
|
* Assumes <stdio.h> already included. |
7 |
|
|
* |
8 |
|
|
* A material is oriented in right-hand coordinate system with X-axis |
9 |
|
|
* in the surface plane pointed to the right as seen from the front. |
10 |
|
|
* This means the Y-axis is "up" and the Z-axis is the surface normal. |
11 |
|
|
* |
12 |
greg |
2.8 |
* Note that we reverse the identification of "front" and "back" from |
13 |
|
|
* the conventions used in WINDOW 6. "Front" in our library points |
14 |
|
|
* in the +Z direction, towards the interior of the space rather |
15 |
|
|
* than the exterior. |
16 |
|
|
* |
17 |
greg |
2.13 |
* BSDF incident & exiting vectors are always oriented away from surface. |
18 |
greg |
2.4 |
* |
19 |
|
|
* Created by Greg Ward on 1/10/11. |
20 |
|
|
* |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#ifndef _BSDF_H_ |
24 |
|
|
#define _BSDF_H_ |
25 |
|
|
|
26 |
|
|
#include "fvect.h" |
27 |
|
|
#include "ccolor.h" |
28 |
|
|
|
29 |
|
|
#ifdef __cplusplus |
30 |
|
|
extern "C" { |
31 |
|
|
#endif |
32 |
|
|
|
33 |
|
|
#define SDnameLn 128 /* maximum BSDF name length */ |
34 |
|
|
#define SDmaxCh 3 /* maximum # spectral channels */ |
35 |
|
|
|
36 |
greg |
2.9 |
/* Component flags for SDsampBSDF() and SDdirectHemi() */ |
37 |
greg |
2.4 |
#define SDsampR 0x1 /* include reflection */ |
38 |
|
|
#define SDsampT 0x2 /* include transmission */ |
39 |
|
|
#define SDsampS 0x3 /* include scattering (R+T) */ |
40 |
|
|
#define SDsampSp 0x4 /* include non-diffuse portion */ |
41 |
|
|
#define SDsampDf 0x8 /* include diffuse portion */ |
42 |
|
|
#define SDsampSpR 0x5 /* include non-diffuse reflection */ |
43 |
|
|
#define SDsampSpT 0x6 /* include non-diffuse transmission */ |
44 |
|
|
#define SDsampSpS 0x7 /* include non-diffuse scattering */ |
45 |
|
|
#define SDsampAll 0xF /* include everything */ |
46 |
|
|
|
47 |
greg |
2.9 |
/* Projected solid angle query flags for SDsizeBSDF() */ |
48 |
greg |
2.7 |
#define SDqueryVal 0x0 /* query single value */ |
49 |
|
|
#define SDqueryMin 0x1 /* query minimum proj. solid angle */ |
50 |
|
|
#define SDqueryMax 0x2 /* query maximum proj. solid angle */ |
51 |
greg |
2.4 |
|
52 |
|
|
/* Error codes: normal return, out of memory, file i/o, file format, bad argument, |
53 |
|
|
bad data, unsupported feature, internal error, unknown error */ |
54 |
|
|
typedef enum {SDEnone=0, SDEmemory, SDEfile, SDEformat, SDEargument, |
55 |
|
|
SDEdata, SDEsupport, SDEinternal, SDEunknown} SDError; |
56 |
|
|
|
57 |
|
|
/* English ASCII strings corresponding to ennumerated errors */ |
58 |
|
|
extern const char *SDerrorEnglish[]; |
59 |
|
|
|
60 |
|
|
/* Additional information on last error (ASCII English) */ |
61 |
|
|
extern char SDerrorDetail[]; |
62 |
|
|
|
63 |
|
|
/* Holder for BSDF value and spectral color */ |
64 |
|
|
typedef struct { |
65 |
|
|
double cieY; /* photopic BSDF (Y) value */ |
66 |
|
|
C_COLOR spec; /* spectral and (x,y) color */ |
67 |
|
|
} SDValue; |
68 |
|
|
|
69 |
|
|
/* Cached, encoded, cumulative distribution for one incident (solid) angle */ |
70 |
|
|
#define SD_CDIST_BASE double cTotal; \ |
71 |
|
|
struct SDCDst_s *next |
72 |
|
|
typedef struct SDCDst_s { |
73 |
|
|
SD_CDIST_BASE; /* base fields first */ |
74 |
|
|
/* ...encoded distribution extends struct */ |
75 |
|
|
} SDCDst; |
76 |
|
|
|
77 |
|
|
/* Forward declaration of BSDF component */ |
78 |
|
|
typedef struct SDComp_s SDComponent; |
79 |
|
|
|
80 |
|
|
/* Methods needed to handle BSDF components (nothing is optional) */ |
81 |
|
|
typedef const struct { |
82 |
|
|
/* return non-diffuse BSDF */ |
83 |
|
|
int (*getBSDFs)(float coef[SDmaxCh], const FVECT outVec, |
84 |
greg |
2.12 |
const FVECT inVec, SDComponent *sdc); |
85 |
greg |
2.4 |
/* query non-diffuse PSA for vector */ |
86 |
greg |
2.11 |
SDError (*queryProjSA)(double *psa, const FVECT v1, |
87 |
|
|
const RREAL *v2, int qflags, |
88 |
greg |
2.12 |
SDComponent *sdc); |
89 |
greg |
2.4 |
/* get cumulative distribution */ |
90 |
|
|
const SDCDst *(*getCDist)(const FVECT inVec, SDComponent *sdc); |
91 |
|
|
/* sample cumulative distribution */ |
92 |
greg |
2.12 |
SDError (*sampCDist)(FVECT ioVec, double randX, |
93 |
greg |
2.4 |
const SDCDst *cdp); |
94 |
|
|
/* free a spectral BSDF component */ |
95 |
|
|
void (*freeSC)(void *dist); |
96 |
|
|
} SDFunc; |
97 |
|
|
|
98 |
|
|
/* Structure to hold a spectral BSDF component (typedef SDComponent above) */ |
99 |
|
|
struct SDComp_s { |
100 |
|
|
C_COLOR cspec[SDmaxCh]; /* component spectral bases */ |
101 |
|
|
SDFunc *func; /* methods for this component */ |
102 |
|
|
void *dist; /* loaded distribution data */ |
103 |
|
|
SDCDst *cdList; /* cumulative distribution cache */ |
104 |
|
|
}; |
105 |
|
|
|
106 |
|
|
/* Container for non-diffuse BSDF components */ |
107 |
|
|
typedef struct { |
108 |
|
|
double minProjSA; /* minimum projected solid angle */ |
109 |
|
|
double maxHemi; /* maximum directional hemispherical */ |
110 |
|
|
int ncomp; /* number of separate components */ |
111 |
|
|
SDComponent comp[1]; /* BSDF components (extends struct) */ |
112 |
|
|
} SDSpectralDF; |
113 |
|
|
|
114 |
|
|
/* Loaded BSDF data */ |
115 |
|
|
typedef struct { |
116 |
greg |
2.13 |
char name[SDnameLn]; /* BSDF name (usu. derived from file) */ |
117 |
greg |
2.4 |
char *mgf; /* geometric description (if any) */ |
118 |
|
|
float dim[3]; /* width, height, thickness (meters) */ |
119 |
|
|
SDValue rLambFront; /* diffuse front reflectance */ |
120 |
|
|
SDValue rLambBack; /* diffuse rear reflectance */ |
121 |
|
|
SDValue tLamb; /* diffuse transmission */ |
122 |
|
|
SDSpectralDF *rf, *rb, *tf; /* non-diffuse BSDF components */ |
123 |
|
|
} SDData; |
124 |
|
|
|
125 |
|
|
/* List of loaded BSDFs */ |
126 |
|
|
extern struct SDCache_s { |
127 |
|
|
SDData bsdf; /* BSDF data */ |
128 |
|
|
unsigned refcnt; /* how many callers are using us? */ |
129 |
|
|
struct SDCache_s /* next in cache list */ |
130 |
|
|
*next; |
131 |
|
|
} *SDcacheList; /* Global BSDF cache */ |
132 |
|
|
|
133 |
greg |
2.14 |
/* BSDF cache retention policies */ |
134 |
|
|
#define SDretainNone 0 /* free unreferenced BSDF data */ |
135 |
|
|
#define SDretainBSDFs 1 /* keep loaded BSDFs in memory */ |
136 |
greg |
2.4 |
#define SDretainAll 2 /* also keep cumulative cache data */ |
137 |
|
|
|
138 |
greg |
2.13 |
extern int SDretainSet; /* =SDretainNone by default */ |
139 |
greg |
2.4 |
|
140 |
|
|
/***************************************************************** |
141 |
|
|
* The following routines are less commonly used by applications. |
142 |
|
|
*/ |
143 |
|
|
|
144 |
greg |
2.6 |
#define SDisLoaded(sd) ((sd)->rLambFront.spec.flags != 0) |
145 |
greg |
2.4 |
|
146 |
|
|
/* Report an error to the indicated stream (in English) */ |
147 |
|
|
extern SDError SDreportEnglish(SDError ec, FILE *fp); |
148 |
|
|
|
149 |
|
|
/* Shorten file path to useable BSDF name, removing suffix */ |
150 |
|
|
extern void SDclipName(char res[SDnameLn], const char *fname); |
151 |
|
|
|
152 |
|
|
/* Allocate new spectral distribution function */ |
153 |
|
|
extern SDSpectralDF *SDnewSpectralDF(int nc); |
154 |
|
|
|
155 |
|
|
/* Free a spectral distribution function */ |
156 |
|
|
extern void SDfreeSpectralDF(SDSpectralDF *df); |
157 |
|
|
|
158 |
greg |
2.10 |
/* Initialize an unused BSDF struct and assign name (calls SDclipName) */ |
159 |
|
|
extern void SDclearBSDF(SDData *sd, const char *fname); |
160 |
greg |
2.4 |
|
161 |
|
|
/* Load a BSDF struct from the given file (keeps name unchanged) */ |
162 |
|
|
extern SDError SDloadFile(SDData *sd, const char *fname); |
163 |
|
|
|
164 |
|
|
/* Free data associated with BSDF struct */ |
165 |
|
|
extern void SDfreeBSDF(SDData *sd); |
166 |
|
|
|
167 |
|
|
/* Find writeable BSDF by name, or allocate new cache entry if absent */ |
168 |
|
|
extern SDData *SDgetCache(const char *bname); |
169 |
|
|
|
170 |
|
|
/* Free cached cumulative distributions for BSDF component */ |
171 |
|
|
extern void SDfreeCumulativeCache(SDSpectralDF *df); |
172 |
|
|
|
173 |
|
|
/* Sample an individual BSDF component */ |
174 |
greg |
2.12 |
extern SDError SDsampComponent(SDValue *sv, FVECT ioVec, |
175 |
|
|
double randX, SDComponent *sdc); |
176 |
greg |
2.4 |
|
177 |
|
|
/* Convert 1-dimensional random variable to N-dimensional */ |
178 |
|
|
extern void SDmultiSamp(double t[], int n, double randX); |
179 |
|
|
|
180 |
|
|
/* Map a [0,1]^2 square to a unit radius disk */ |
181 |
|
|
void SDsquare2disk(double ds[2], double seedx, double seedy); |
182 |
|
|
|
183 |
|
|
/* Map point on unit disk to a unit square in [0,1]^2 range */ |
184 |
|
|
void SDdisk2square(double sq[2], double diskx, double disky); |
185 |
|
|
|
186 |
|
|
/***************************************************************** |
187 |
|
|
* The calls below are the ones most applications require. |
188 |
|
|
* All directions are assumed to be unit vectors. |
189 |
|
|
*/ |
190 |
|
|
|
191 |
|
|
/* Get BSDF from cache (or load and cache it on first call) */ |
192 |
greg |
2.13 |
/* Report any problems to stderr (in English), return NULL on failure */ |
193 |
greg |
2.4 |
extern const SDData *SDcacheFile(const char *fname); |
194 |
|
|
|
195 |
greg |
2.13 |
/* Free a BSDF from our cache (clear all if sd==NULL) */ |
196 |
greg |
2.4 |
extern void SDfreeCache(const SDData *sd); |
197 |
|
|
|
198 |
greg |
2.11 |
/* Query projected solid angle resolution for non-diffuse BSDF direction(s) */ |
199 |
|
|
extern SDError SDsizeBSDF(double *projSA, const FVECT v1, |
200 |
|
|
const RREAL *v2, int qflags, |
201 |
|
|
const SDData *sd); |
202 |
greg |
2.4 |
|
203 |
|
|
/* Return BSDF for the given incident and scattered ray vectors */ |
204 |
|
|
extern SDError SDevalBSDF(SDValue *sv, const FVECT outVec, |
205 |
|
|
const FVECT inVec, const SDData *sd); |
206 |
|
|
|
207 |
|
|
/* Compute directional hemispherical scattering at given incident angle */ |
208 |
|
|
extern double SDdirectHemi(const FVECT inVec, |
209 |
|
|
int sflags, const SDData *sd); |
210 |
|
|
|
211 |
|
|
/* Sample BSDF direction based on the given random variable */ |
212 |
greg |
2.12 |
extern SDError SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, |
213 |
greg |
2.4 |
int sflags, const SDData *sd); |
214 |
|
|
|
215 |
|
|
/***************************************************************** |
216 |
|
|
* Vector math for getting between world and local BSDF coordinates. |
217 |
|
|
* Directions may be passed unnormalized to these routines. |
218 |
|
|
*/ |
219 |
|
|
|
220 |
greg |
2.13 |
/* Compute World->BSDF transform from surface normal and BSDF up vector */ |
221 |
greg |
2.4 |
extern SDError SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, |
222 |
|
|
const FVECT uVec); |
223 |
|
|
|
224 |
|
|
/* Compute inverse transform */ |
225 |
|
|
extern SDError SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3]); |
226 |
|
|
|
227 |
|
|
/* Transform and normalize direction (column) vector */ |
228 |
|
|
extern SDError SDmapDir(FVECT resVec, RREAL vMtx[3][3], |
229 |
|
|
const FVECT inpVec); |
230 |
|
|
|
231 |
greg |
2.13 |
/* Application-specific BSDF loading routine (not part of our library) */ |
232 |
greg |
2.5 |
extern SDData *loadBSDF(char *name); |
233 |
|
|
|
234 |
greg |
2.13 |
/* Application-specific BSDF error translator (not part of our library) */ |
235 |
greg |
2.5 |
extern char *transSDError(SDError ec); |
236 |
|
|
|
237 |
greg |
2.4 |
/*################################################################*/ |
238 |
|
|
/*######### DEPRECATED DEFINITIONS AWAITING PERMANENT REMOVAL #######*/ |
239 |
greg |
2.1 |
/* |
240 |
|
|
* Header for BSDF i/o and access routines |
241 |
|
|
*/ |
242 |
greg |
2.2 |
|
243 |
greg |
2.4 |
#include "mat4.h" |
244 |
greg |
2.1 |
/* up directions */ |
245 |
|
|
typedef enum { |
246 |
|
|
UDzneg=-3, |
247 |
|
|
UDyneg=-2, |
248 |
|
|
UDxneg=-1, |
249 |
|
|
UDunknown=0, |
250 |
|
|
UDxpos=1, |
251 |
|
|
UDypos=2, |
252 |
|
|
UDzpos=3 |
253 |
|
|
} UpDir; |
254 |
|
|
/* BSDF coordinate calculation routines */ |
255 |
|
|
/* vectors always point away from surface */ |
256 |
greg |
2.4 |
|
257 |
|
|
typedef int b_vecf2(FVECT v, int n, void *cd); |
258 |
|
|
typedef int b_ndxf2(FVECT v, void *cd); |
259 |
|
|
typedef double b_radf2(int n, void *cd); |
260 |
greg |
2.1 |
|
261 |
|
|
/* Bidirectional Scattering Distribution Function */ |
262 |
|
|
struct BSDF_data { |
263 |
|
|
int ninc; /* number of incoming directions */ |
264 |
|
|
int nout; /* number of outgoing directions */ |
265 |
greg |
2.3 |
float dim[3]; /* width, height, thickness (meters) */ |
266 |
|
|
char *mgf; /* geometric description (if any) */ |
267 |
greg |
2.1 |
void *ib_priv; /* input basis private data */ |
268 |
greg |
2.4 |
b_vecf2 *ib_vec; /* get input vector from index */ |
269 |
|
|
b_ndxf2 *ib_ndx; /* get input index from vector */ |
270 |
|
|
b_radf2 *ib_ohm; /* get input radius for index */ |
271 |
greg |
2.1 |
void *ob_priv; /* output basis private data */ |
272 |
greg |
2.4 |
b_vecf2 *ob_vec; /* get output vector from index */ |
273 |
|
|
b_ndxf2 *ob_ndx; /* get output index from vector */ |
274 |
|
|
b_radf2 *ob_ohm; /* get output radius for index */ |
275 |
greg |
2.1 |
float *bsdf; /* scattering distribution data */ |
276 |
|
|
}; /* bidirectional scattering distrib. func. */ |
277 |
|
|
|
278 |
|
|
#define getBSDF_incvec(v,b,i) (*(b)->ib_vec)(v,i,(b)->ib_priv) |
279 |
|
|
#define getBSDF_incndx(b,v) (*(b)->ib_ndx)(v,(b)->ib_priv) |
280 |
|
|
#define getBSDF_incohm(b,i) (*(b)->ib_ohm)(i,(b)->ib_priv) |
281 |
|
|
#define getBSDF_outvec(v,b,o) (*(b)->ob_vec)(v,o,(b)->ob_priv) |
282 |
|
|
#define getBSDF_outndx(b,v) (*(b)->ob_ndx)(v,(b)->ob_priv) |
283 |
|
|
#define getBSDF_outohm(b,o) (*(b)->ob_ohm)(o,(b)->ob_priv) |
284 |
|
|
#define BSDF_value(b,i,o) (b)->bsdf[(o)*(b)->ninc + (i)] |
285 |
|
|
|
286 |
|
|
extern struct BSDF_data *load_BSDF(char *fname); |
287 |
|
|
extern void free_BSDF(struct BSDF_data *b); |
288 |
|
|
extern int r_BSDF_incvec(FVECT v, struct BSDF_data *b, int i, |
289 |
|
|
double rv, MAT4 xm); |
290 |
|
|
extern int r_BSDF_outvec(FVECT v, struct BSDF_data *b, int o, |
291 |
|
|
double rv, MAT4 xm); |
292 |
greg |
2.3 |
extern int getBSDF_xfm(MAT4 xm, FVECT nrm, UpDir ud, char *xfbuf); |
293 |
greg |
2.2 |
|
294 |
greg |
2.4 |
/*######### END DEPRECATED DEFINITIONS #######*/ |
295 |
|
|
/*################################################################*/ |
296 |
|
|
|
297 |
|
|
#ifdef __cplusplus |
298 |
|
|
} |
299 |
|
|
#endif |
300 |
|
|
#endif /* ! _BSDF_H_ */ |