1 |
/* RCSid $Id: bsdf.h,v 2.30 2021/12/13 21:05:00 greg Exp $ */ |
2 |
/* |
3 |
* bsdf.h |
4 |
* |
5 |
* Declarations for bidirectional scattering distribution functions. |
6 |
* |
7 |
* A material is oriented in right-hand coordinate system with X-axis |
8 |
* in the surface plane pointed to the right as seen from the front. |
9 |
* This means the Y-axis is "up" and the Z-axis is the surface normal. |
10 |
* |
11 |
* Note that we reverse the identification of "front" and "back" from |
12 |
* the conventions used in WINDOW 6. "Front" in our library points |
13 |
* in the +Z direction, towards the interior of the space rather |
14 |
* than the exterior. |
15 |
* |
16 |
* BSDF incident & exiting vectors are always oriented away from surface. |
17 |
* |
18 |
* Created by Greg Ward on 1/10/11. |
19 |
* |
20 |
*/ |
21 |
|
22 |
#ifndef _BSDF_H_ |
23 |
#define _BSDF_H_ |
24 |
|
25 |
#include "platform.h" |
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 |
/* Component flags for SDsampBSDF() and SDdirectHemi() */ |
37 |
#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 |
/* Projected solid angle query flags for SDsizeBSDF() */ |
48 |
#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 |
|
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 strings corresponding to ennumerated errors */ |
58 |
extern const char *SDerrorEnglish[]; |
59 |
|
60 |
/* Pointer to error list in preferred language */ |
61 |
extern const char **SDerrorList; |
62 |
|
63 |
/* Additional information on last error (generally in English) */ |
64 |
extern char SDerrorDetail[]; |
65 |
|
66 |
/* Holder for BSDF value and spectral color */ |
67 |
typedef struct { |
68 |
double cieY; /* photopic BSDF (Y) value */ |
69 |
C_COLOR spec; /* spectral and (x,y) color */ |
70 |
} SDValue; |
71 |
|
72 |
/* Cached, encoded, cumulative distribution for one incident (solid) angle */ |
73 |
#define SD_CDIST_BASE(styp) double cTotal; \ |
74 |
struct styp *next |
75 |
typedef struct SDCDst_s { |
76 |
SD_CDIST_BASE(SDCDst_s); /* base fields first */ |
77 |
/* ...encoded distribution extends struct */ |
78 |
} SDCDst; |
79 |
|
80 |
extern const SDCDst SDemptyCD; /* empty distribution */ |
81 |
|
82 |
/* Forward declaration of BSDF component */ |
83 |
typedef struct SDComp_s SDComponent; |
84 |
|
85 |
/* Methods needed to handle BSDF components (nothing is optional) */ |
86 |
typedef struct { |
87 |
/* return non-diffuse BSDF */ |
88 |
int (*getBSDFs)(float coef[SDmaxCh], const FVECT inVec, |
89 |
const FVECT outVec, SDComponent *sdc); |
90 |
/* query non-diffuse PSA for vector */ |
91 |
SDError (*queryProjSA)(double *psa, const FVECT v1, |
92 |
const RREAL *v2, int qflags, |
93 |
SDComponent *sdc); |
94 |
/* get cumulative distribution */ |
95 |
const SDCDst *(*getCDist)(const FVECT inVec, SDComponent *sdc); |
96 |
/* sample cumulative distribution */ |
97 |
SDError (*sampCDist)(FVECT ioVec, double randX, |
98 |
const SDCDst *cdp); |
99 |
/* free a spectral BSDF component */ |
100 |
void (*freeSC)(void *dist); |
101 |
} SDFunc; |
102 |
|
103 |
/* Structure to hold a spectral BSDF component (typedef SDComponent above) */ |
104 |
struct SDComp_s { |
105 |
C_COLOR cspec[SDmaxCh]; /* component spectral bases */ |
106 |
const SDFunc *func; /* methods for this component */ |
107 |
void *dist; /* loaded distribution data */ |
108 |
SDCDst *cdList; /* cumulative distribution cache */ |
109 |
}; |
110 |
|
111 |
/* Container for non-diffuse BSDF components */ |
112 |
typedef struct { |
113 |
double minProjSA; /* minimum projected solid angle */ |
114 |
double maxHemi; /* maximum directional hemispherical */ |
115 |
int ncomp; /* number of separate components */ |
116 |
SDComponent comp[1]; /* BSDF components (extends struct) */ |
117 |
} SDSpectralDF; |
118 |
|
119 |
/* Loaded BSDF data */ |
120 |
typedef struct { |
121 |
char name[SDnameLn]; /* BSDF name (usu. derived from file) */ |
122 |
char matn[SDnameLn]; /* material name */ |
123 |
char makr[SDnameLn]; /* manufacturer */ |
124 |
char *mgf; /* geometric description (if any) */ |
125 |
double dim[3]; /* width, height, thickness (meters) */ |
126 |
SDValue rLambFront; /* diffuse front reflectance */ |
127 |
SDValue rLambBack; /* diffuse rear reflectance */ |
128 |
SDValue tLambFront; /* diffuse front transmittance */ |
129 |
SDValue tLambBack; /* diffuse back transmittance */ |
130 |
SDSpectralDF *rf, *rb; /* non-diffuse BRDF components */ |
131 |
SDSpectralDF *tf, *tb; /* non-diffuse BTDF components */ |
132 |
} SDData; |
133 |
|
134 |
/* List of loaded BSDFs */ |
135 |
extern struct SDCache_s { |
136 |
SDData bsdf; /* BSDF data */ |
137 |
unsigned refcnt; /* how many callers are using us? */ |
138 |
struct SDCache_s /* next in cache list */ |
139 |
*next; |
140 |
} *SDcacheList; /* Global BSDF cache */ |
141 |
|
142 |
/* BSDF cache retention policies */ |
143 |
#define SDretainNone 0 /* free unreferenced BSDF data */ |
144 |
#define SDretainBSDFs 1 /* keep loaded BSDFs in memory */ |
145 |
#define SDretainAll 2 /* also keep cumulative cache data */ |
146 |
|
147 |
extern int SDretainSet; /* =SDretainNone by default */ |
148 |
extern unsigned long SDmaxCache; /* =0 (unlimited) by default */ |
149 |
|
150 |
/***************************************************************** |
151 |
* The following routines are less commonly used by applications. |
152 |
*/ |
153 |
|
154 |
#define SDisLoaded(sd) ((sd)->rLambFront.spec.flags != 0) |
155 |
|
156 |
/* Report an error to the indicated stream */ |
157 |
extern SDError SDreportError(SDError ec, FILE *fp); |
158 |
|
159 |
/* Shorten file path to useable BSDF name, removing suffix */ |
160 |
extern void SDclipName(char res[SDnameLn], const char *fname); |
161 |
|
162 |
/* Allocate new spectral distribution function */ |
163 |
extern SDSpectralDF *SDnewSpectralDF(int nc); |
164 |
|
165 |
/* Add component(s) to spectral distribution function */ |
166 |
extern SDSpectralDF *SDaddComponent(SDSpectralDF *odf, int nadd); |
167 |
|
168 |
/* Free a spectral distribution function */ |
169 |
extern void SDfreeSpectralDF(SDSpectralDF *df); |
170 |
|
171 |
/* Initialize an unused BSDF struct and assign name (calls SDclipName) */ |
172 |
extern void SDclearBSDF(SDData *sd, const char *fname); |
173 |
|
174 |
/* Load a BSDF struct from the given file (keeps name unchanged) */ |
175 |
extern SDError SDloadFile(SDData *sd, const char *fname); |
176 |
|
177 |
/* Free data associated with BSDF struct */ |
178 |
extern void SDfreeBSDF(SDData *sd); |
179 |
|
180 |
/* Find writeable BSDF by name, or allocate new cache entry if absent */ |
181 |
extern SDData *SDgetCache(const char *bname); |
182 |
|
183 |
/* Free cached cumulative distributions for BSDF component */ |
184 |
extern void SDfreeCumulativeCache(SDSpectralDF *df); |
185 |
|
186 |
/* Sample an individual BSDF component */ |
187 |
extern SDError SDsampComponent(SDValue *sv, FVECT ioVec, |
188 |
double randX, SDComponent *sdc); |
189 |
|
190 |
/* Convert 1-dimensional random variable to N-dimensional */ |
191 |
extern void SDmultiSamp(double t[], int n, double randX); |
192 |
|
193 |
/***************************************************************** |
194 |
* The calls below are the ones most applications require. |
195 |
* All directions are assumed to be unit vectors. |
196 |
*/ |
197 |
|
198 |
/* Get BSDF from cache (or load and cache it on first call) */ |
199 |
/* Report any problems to stderr, return NULL on failure */ |
200 |
extern const SDData *SDcacheFile(const char *fname); |
201 |
|
202 |
/* Free a BSDF from our cache (clear all if sd==NULL) */ |
203 |
extern void SDfreeCache(const SDData *sd); |
204 |
|
205 |
/* Query projected solid angle resolution for non-diffuse BSDF direction(s) */ |
206 |
extern SDError SDsizeBSDF(double *projSA, const FVECT v1, |
207 |
const RREAL *v2, int qflags, |
208 |
const SDData *sd); |
209 |
|
210 |
/* Return BSDF for the given incident and scattered ray vectors */ |
211 |
extern SDError SDevalBSDF(SDValue *sv, const FVECT inVec, |
212 |
const FVECT outVec, const SDData *sd); |
213 |
|
214 |
/* Compute directional hemispherical scattering at given incident angle */ |
215 |
extern double SDdirectHemi(const FVECT inVec, |
216 |
int sflags, const SDData *sd); |
217 |
|
218 |
/* Sample BSDF direction based on the given random variable */ |
219 |
extern SDError SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, |
220 |
int sflags, const SDData *sd); |
221 |
|
222 |
/***************************************************************** |
223 |
* Vector math for getting between world and local BSDF coordinates. |
224 |
* Directions may be passed unnormalized to these routines. |
225 |
*/ |
226 |
|
227 |
/* Compute World->BSDF transform from surface normal and BSDF up vector */ |
228 |
extern SDError SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, |
229 |
const FVECT uVec); |
230 |
|
231 |
/* Compute inverse transform */ |
232 |
extern SDError SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3]); |
233 |
|
234 |
/* Transform and normalize direction (column) vector */ |
235 |
extern SDError SDmapDir(FVECT resVec, RREAL vMtx[3][3], |
236 |
const FVECT inpVec); |
237 |
|
238 |
/* Application-specific BSDF loading routine (not part of our library) */ |
239 |
extern SDData *loadBSDF(char *name); |
240 |
|
241 |
/* Application-specific BSDF error translator (not part of our library) */ |
242 |
extern char *transSDError(SDError ec); |
243 |
|
244 |
#ifdef __cplusplus |
245 |
} |
246 |
#endif |
247 |
#endif /* ! _BSDF_H_ */ |