1 |
/* RCSid $Id: bsdf.h,v 2.27 2020/05/14 19:20:13 greg Exp $ */ |
2 |
/* |
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 |
* 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 |
* BSDF incident & exiting vectors are always oriented away from surface. |
18 |
* |
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 |
#include "platform.h" |
29 |
|
30 |
#ifdef __cplusplus |
31 |
extern "C" { |
32 |
#endif |
33 |
|
34 |
#define SDnameLn 128 /* maximum BSDF name length */ |
35 |
#define SDmaxCh 3 /* maximum # spectral channels */ |
36 |
|
37 |
/* Component flags for SDsampBSDF() and SDdirectHemi() */ |
38 |
#define SDsampR 0x1 /* include reflection */ |
39 |
#define SDsampT 0x2 /* include transmission */ |
40 |
#define SDsampS 0x3 /* include scattering (R+T) */ |
41 |
#define SDsampSp 0x4 /* include non-diffuse portion */ |
42 |
#define SDsampDf 0x8 /* include diffuse portion */ |
43 |
#define SDsampSpR 0x5 /* include non-diffuse reflection */ |
44 |
#define SDsampSpT 0x6 /* include non-diffuse transmission */ |
45 |
#define SDsampSpS 0x7 /* include non-diffuse scattering */ |
46 |
#define SDsampAll 0xF /* include everything */ |
47 |
|
48 |
/* Projected solid angle query flags for SDsizeBSDF() */ |
49 |
#define SDqueryVal 0x0 /* query single value */ |
50 |
#define SDqueryMin 0x1 /* query minimum proj. solid angle */ |
51 |
#define SDqueryMax 0x2 /* query maximum proj. solid angle */ |
52 |
|
53 |
/* Error codes: normal return, out of memory, file i/o, file format, bad argument, |
54 |
bad data, unsupported feature, internal error, unknown error */ |
55 |
typedef enum {SDEnone=0, SDEmemory, SDEfile, SDEformat, SDEargument, |
56 |
SDEdata, SDEsupport, SDEinternal, SDEunknown} SDError; |
57 |
|
58 |
/* English strings corresponding to ennumerated errors */ |
59 |
extern const char *SDerrorEnglish[]; |
60 |
|
61 |
/* Pointer to error list in preferred language */ |
62 |
extern const char **SDerrorList; |
63 |
|
64 |
/* Additional information on last error (generally in English) */ |
65 |
extern char SDerrorDetail[]; |
66 |
|
67 |
/* Holder for BSDF value and spectral color */ |
68 |
typedef struct { |
69 |
double cieY; /* photopic BSDF (Y) value */ |
70 |
C_COLOR spec; /* spectral and (x,y) color */ |
71 |
} SDValue; |
72 |
|
73 |
/* Cached, encoded, cumulative distribution for one incident (solid) angle */ |
74 |
#define SD_CDIST_BASE(styp) double cTotal; \ |
75 |
struct styp *next |
76 |
typedef struct SDCDst_s { |
77 |
SD_CDIST_BASE(SDCDst_s); /* base fields first */ |
78 |
/* ...encoded distribution extends struct */ |
79 |
} SDCDst; |
80 |
|
81 |
extern const SDCDst SDemptyCD; /* empty distribution */ |
82 |
|
83 |
/* Forward declaration of BSDF component */ |
84 |
typedef struct SDComp_s SDComponent; |
85 |
|
86 |
/* Methods needed to handle BSDF components (nothing is optional) */ |
87 |
typedef struct { |
88 |
/* return non-diffuse BSDF */ |
89 |
int (*getBSDFs)(float coef[SDmaxCh], const FVECT outVec, |
90 |
const FVECT inVec, SDComponent *sdc); |
91 |
/* query non-diffuse PSA for vector */ |
92 |
SDError (*queryProjSA)(double *psa, const FVECT v1, |
93 |
const RREAL *v2, int qflags, |
94 |
SDComponent *sdc); |
95 |
/* get cumulative distribution */ |
96 |
const SDCDst *(*getCDist)(const FVECT inVec, SDComponent *sdc); |
97 |
/* sample cumulative distribution */ |
98 |
SDError (*sampCDist)(FVECT ioVec, double randX, |
99 |
const SDCDst *cdp); |
100 |
/* free a spectral BSDF component */ |
101 |
void (*freeSC)(void *dist); |
102 |
} SDFunc; |
103 |
|
104 |
/* Structure to hold a spectral BSDF component (typedef SDComponent above) */ |
105 |
struct SDComp_s { |
106 |
C_COLOR cspec[SDmaxCh]; /* component spectral bases */ |
107 |
const SDFunc *func; /* methods for this component */ |
108 |
void *dist; /* loaded distribution data */ |
109 |
SDCDst *cdList; /* cumulative distribution cache */ |
110 |
}; |
111 |
|
112 |
/* Container for non-diffuse BSDF components */ |
113 |
typedef struct { |
114 |
double minProjSA; /* minimum projected solid angle */ |
115 |
double maxHemi; /* maximum directional hemispherical */ |
116 |
int ncomp; /* number of separate components */ |
117 |
SDComponent comp[1]; /* BSDF components (extends struct) */ |
118 |
} SDSpectralDF; |
119 |
|
120 |
/* Loaded BSDF data */ |
121 |
typedef struct { |
122 |
char name[SDnameLn]; /* BSDF name (usu. derived from file) */ |
123 |
char matn[SDnameLn]; /* material name */ |
124 |
char makr[SDnameLn]; /* manufacturer */ |
125 |
char *mgf; /* geometric description (if any) */ |
126 |
double dim[3]; /* width, height, thickness (meters) */ |
127 |
SDValue rLambFront; /* diffuse front reflectance */ |
128 |
SDValue rLambBack; /* diffuse rear reflectance */ |
129 |
SDValue tLambFront; /* diffuse front transmittance */ |
130 |
SDValue tLambBack; /* diffuse back transmittance */ |
131 |
SDSpectralDF *rf, *rb; /* non-diffuse BRDF components */ |
132 |
SDSpectralDF *tf, *tb; /* non-diffuse BTDF components */ |
133 |
} SDData; |
134 |
|
135 |
/* List of loaded BSDFs */ |
136 |
extern struct SDCache_s { |
137 |
SDData bsdf; /* BSDF data */ |
138 |
unsigned refcnt; /* how many callers are using us? */ |
139 |
struct SDCache_s /* next in cache list */ |
140 |
*next; |
141 |
} *SDcacheList; /* Global BSDF cache */ |
142 |
|
143 |
/* BSDF cache retention policies */ |
144 |
#define SDretainNone 0 /* free unreferenced BSDF data */ |
145 |
#define SDretainBSDFs 1 /* keep loaded BSDFs in memory */ |
146 |
#define SDretainAll 2 /* also keep cumulative cache data */ |
147 |
|
148 |
extern int SDretainSet; /* =SDretainNone by default */ |
149 |
extern unsigned long SDmaxCache; /* =0 (unlimited) by default */ |
150 |
|
151 |
/***************************************************************** |
152 |
* The following routines are less commonly used by applications. |
153 |
*/ |
154 |
|
155 |
#define SDisLoaded(sd) ((sd)->rLambFront.spec.flags != 0) |
156 |
|
157 |
/* Report an error to the indicated stream */ |
158 |
extern SDError SDreportError(SDError ec, FILE *fp); |
159 |
|
160 |
/* Shorten file path to useable BSDF name, removing suffix */ |
161 |
extern void SDclipName(char res[SDnameLn], const char *fname); |
162 |
|
163 |
/* Allocate new spectral distribution function */ |
164 |
extern SDSpectralDF *SDnewSpectralDF(int nc); |
165 |
|
166 |
/* Add component(s) to spectral distribution function */ |
167 |
extern SDSpectralDF *SDaddComponent(SDSpectralDF *odf, int nadd); |
168 |
|
169 |
/* Free a spectral distribution function */ |
170 |
extern void SDfreeSpectralDF(SDSpectralDF *df); |
171 |
|
172 |
/* Initialize an unused BSDF struct and assign name (calls SDclipName) */ |
173 |
extern void SDclearBSDF(SDData *sd, const char *fname); |
174 |
|
175 |
/* Load a BSDF struct from the given file (keeps name unchanged) */ |
176 |
extern SDError SDloadFile(SDData *sd, const char *fname); |
177 |
|
178 |
/* Free data associated with BSDF struct */ |
179 |
extern void SDfreeBSDF(SDData *sd); |
180 |
|
181 |
/* Find writeable BSDF by name, or allocate new cache entry if absent */ |
182 |
extern SDData *SDgetCache(const char *bname); |
183 |
|
184 |
/* Free cached cumulative distributions for BSDF component */ |
185 |
extern void SDfreeCumulativeCache(SDSpectralDF *df); |
186 |
|
187 |
/* Sample an individual BSDF component */ |
188 |
extern SDError SDsampComponent(SDValue *sv, FVECT ioVec, |
189 |
double randX, SDComponent *sdc); |
190 |
|
191 |
/* Convert 1-dimensional random variable to N-dimensional */ |
192 |
extern void SDmultiSamp(double t[], int n, double randX); |
193 |
|
194 |
/* Map a [0,1]^2 square to a unit radius disk */ |
195 |
extern void SDsquare2disk(double ds[2], double seedx, double seedy); |
196 |
|
197 |
/* Map point on unit disk to a unit square in [0,1]^2 range */ |
198 |
extern void SDdisk2square(double sq[2], double diskx, double disky); |
199 |
|
200 |
/***************************************************************** |
201 |
* The calls below are the ones most applications require. |
202 |
* All directions are assumed to be unit vectors. |
203 |
*/ |
204 |
|
205 |
/* Get BSDF from cache (or load and cache it on first call) */ |
206 |
/* Report any problems to stderr, return NULL on failure */ |
207 |
extern const SDData *SDcacheFile(const char *fname); |
208 |
|
209 |
/* Free a BSDF from our cache (clear all if sd==NULL) */ |
210 |
extern void SDfreeCache(const SDData *sd); |
211 |
|
212 |
/* Query projected solid angle resolution for non-diffuse BSDF direction(s) */ |
213 |
extern SDError SDsizeBSDF(double *projSA, const FVECT v1, |
214 |
const RREAL *v2, int qflags, |
215 |
const SDData *sd); |
216 |
|
217 |
/* Return BSDF for the given incident and scattered ray vectors */ |
218 |
extern SDError SDevalBSDF(SDValue *sv, const FVECT outVec, |
219 |
const FVECT inVec, const SDData *sd); |
220 |
|
221 |
/* Compute directional hemispherical scattering at given incident angle */ |
222 |
extern double SDdirectHemi(const FVECT inVec, |
223 |
int sflags, const SDData *sd); |
224 |
|
225 |
/* Sample BSDF direction based on the given random variable */ |
226 |
extern SDError SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, |
227 |
int sflags, const SDData *sd); |
228 |
|
229 |
/***************************************************************** |
230 |
* Vector math for getting between world and local BSDF coordinates. |
231 |
* Directions may be passed unnormalized to these routines. |
232 |
*/ |
233 |
|
234 |
/* Compute World->BSDF transform from surface normal and BSDF up vector */ |
235 |
extern SDError SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, |
236 |
const FVECT uVec); |
237 |
|
238 |
/* Compute inverse transform */ |
239 |
extern SDError SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3]); |
240 |
|
241 |
/* Transform and normalize direction (column) vector */ |
242 |
extern SDError SDmapDir(FVECT resVec, RREAL vMtx[3][3], |
243 |
const FVECT inpVec); |
244 |
|
245 |
/* Application-specific BSDF loading routine (not part of our library) */ |
246 |
extern SDData *loadBSDF(char *name); |
247 |
|
248 |
/* Application-specific BSDF error translator (not part of our library) */ |
249 |
extern char *transSDError(SDError ec); |
250 |
|
251 |
#ifdef __cplusplus |
252 |
} |
253 |
#endif |
254 |
#endif /* ! _BSDF_H_ */ |