| 1 |
/* RCSid $Id: bsdf.h,v 2.10 2011/04/11 03:47:46 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 vectors always oriented away from surface, even when "incident."
|
| 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 |
|
| 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 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 |
const FVECT inVec, const void *dist);
|
| 85 |
/* query non-diffuse PSA for vector */
|
| 86 |
SDError (*queryProjSA)(double *psa, const FVECT v1,
|
| 87 |
const RREAL *v2, int qflags,
|
| 88 |
const void *dist);
|
| 89 |
/* get cumulative distribution */
|
| 90 |
const SDCDst *(*getCDist)(const FVECT inVec, SDComponent *sdc);
|
| 91 |
/* sample cumulative distribution */
|
| 92 |
SDError (*sampCDist)(FVECT outVec, double randX,
|
| 93 |
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 |
char name[SDnameLn]; /* BSDF name (derived from file) */
|
| 117 |
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 |
/* BSDF cache retention preference */
|
| 134 |
#define SDretainNone 0 /* free unreferenced data (default) */
|
| 135 |
#define SDretainBSDFs 1 /* keep loaded BSDFs in cache */
|
| 136 |
#define SDretainAll 2 /* also keep cumulative cache data */
|
| 137 |
|
| 138 |
extern int SDretainSet; /* set to SDretainNone by default */
|
| 139 |
|
| 140 |
/*****************************************************************
|
| 141 |
* The following routines are less commonly used by applications.
|
| 142 |
*/
|
| 143 |
|
| 144 |
#define SDisLoaded(sd) ((sd)->rLambFront.spec.flags != 0)
|
| 145 |
|
| 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 |
/* Initialize an unused BSDF struct and assign name (calls SDclipName) */
|
| 159 |
extern void SDclearBSDF(SDData *sd, const char *fname);
|
| 160 |
|
| 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 |
extern SDError SDsampComponent(SDValue *sv, FVECT outVec,
|
| 175 |
const FVECT inVec, double randX,
|
| 176 |
SDComponent *sdc);
|
| 177 |
|
| 178 |
/* Convert 1-dimensional random variable to N-dimensional */
|
| 179 |
extern void SDmultiSamp(double t[], int n, double randX);
|
| 180 |
|
| 181 |
/* Map a [0,1]^2 square to a unit radius disk */
|
| 182 |
void SDsquare2disk(double ds[2], double seedx, double seedy);
|
| 183 |
|
| 184 |
/* Map point on unit disk to a unit square in [0,1]^2 range */
|
| 185 |
void SDdisk2square(double sq[2], double diskx, double disky);
|
| 186 |
|
| 187 |
/*****************************************************************
|
| 188 |
* The calls below are the ones most applications require.
|
| 189 |
* All directions are assumed to be unit vectors.
|
| 190 |
*/
|
| 191 |
|
| 192 |
/* Get BSDF from cache (or load and cache it on first call) */
|
| 193 |
/* Report any problems to stderr and return NULL on failure */
|
| 194 |
extern const SDData *SDcacheFile(const char *fname);
|
| 195 |
|
| 196 |
/* Free a BSDF from our cache (clear all if NULL) */
|
| 197 |
extern void SDfreeCache(const SDData *sd);
|
| 198 |
|
| 199 |
/* Query projected solid angle resolution for non-diffuse BSDF direction(s) */
|
| 200 |
extern SDError SDsizeBSDF(double *projSA, const FVECT v1,
|
| 201 |
const RREAL *v2, int qflags,
|
| 202 |
const SDData *sd);
|
| 203 |
|
| 204 |
/* Return BSDF for the given incident and scattered ray vectors */
|
| 205 |
extern SDError SDevalBSDF(SDValue *sv, const FVECT outVec,
|
| 206 |
const FVECT inVec, const SDData *sd);
|
| 207 |
|
| 208 |
/* Compute directional hemispherical scattering at given incident angle */
|
| 209 |
extern double SDdirectHemi(const FVECT inVec,
|
| 210 |
int sflags, const SDData *sd);
|
| 211 |
|
| 212 |
/* Sample BSDF direction based on the given random variable */
|
| 213 |
extern SDError SDsampBSDF(SDValue *sv, FVECT outVec,
|
| 214 |
const FVECT inVec, double randX,
|
| 215 |
int sflags, const SDData *sd);
|
| 216 |
|
| 217 |
/*****************************************************************
|
| 218 |
* Vector math for getting between world and local BSDF coordinates.
|
| 219 |
* Directions may be passed unnormalized to these routines.
|
| 220 |
*/
|
| 221 |
|
| 222 |
/* Compute World->BSDF transform from surface normal and up (Y) vector */
|
| 223 |
extern SDError SDcompXform(RREAL vMtx[3][3], const FVECT sNrm,
|
| 224 |
const FVECT uVec);
|
| 225 |
|
| 226 |
/* Compute inverse transform */
|
| 227 |
extern SDError SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3]);
|
| 228 |
|
| 229 |
/* Transform and normalize direction (column) vector */
|
| 230 |
extern SDError SDmapDir(FVECT resVec, RREAL vMtx[3][3],
|
| 231 |
const FVECT inpVec);
|
| 232 |
|
| 233 |
/* System-specific BSDF loading routine (not part of our library) */
|
| 234 |
extern SDData *loadBSDF(char *name);
|
| 235 |
|
| 236 |
/* System-specific BSDF error translator (not part of our library) */
|
| 237 |
extern char *transSDError(SDError ec);
|
| 238 |
|
| 239 |
/*################################################################*/
|
| 240 |
/*######### DEPRECATED DEFINITIONS AWAITING PERMANENT REMOVAL #######*/
|
| 241 |
/*
|
| 242 |
* Header for BSDF i/o and access routines
|
| 243 |
*/
|
| 244 |
|
| 245 |
#include "mat4.h"
|
| 246 |
/* up directions */
|
| 247 |
typedef enum {
|
| 248 |
UDzneg=-3,
|
| 249 |
UDyneg=-2,
|
| 250 |
UDxneg=-1,
|
| 251 |
UDunknown=0,
|
| 252 |
UDxpos=1,
|
| 253 |
UDypos=2,
|
| 254 |
UDzpos=3
|
| 255 |
} UpDir;
|
| 256 |
/* BSDF coordinate calculation routines */
|
| 257 |
/* vectors always point away from surface */
|
| 258 |
|
| 259 |
typedef int b_vecf2(FVECT v, int n, void *cd);
|
| 260 |
typedef int b_ndxf2(FVECT v, void *cd);
|
| 261 |
typedef double b_radf2(int n, void *cd);
|
| 262 |
|
| 263 |
/* Bidirectional Scattering Distribution Function */
|
| 264 |
struct BSDF_data {
|
| 265 |
int ninc; /* number of incoming directions */
|
| 266 |
int nout; /* number of outgoing directions */
|
| 267 |
float dim[3]; /* width, height, thickness (meters) */
|
| 268 |
char *mgf; /* geometric description (if any) */
|
| 269 |
void *ib_priv; /* input basis private data */
|
| 270 |
b_vecf2 *ib_vec; /* get input vector from index */
|
| 271 |
b_ndxf2 *ib_ndx; /* get input index from vector */
|
| 272 |
b_radf2 *ib_ohm; /* get input radius for index */
|
| 273 |
void *ob_priv; /* output basis private data */
|
| 274 |
b_vecf2 *ob_vec; /* get output vector from index */
|
| 275 |
b_ndxf2 *ob_ndx; /* get output index from vector */
|
| 276 |
b_radf2 *ob_ohm; /* get output radius for index */
|
| 277 |
float *bsdf; /* scattering distribution data */
|
| 278 |
}; /* bidirectional scattering distrib. func. */
|
| 279 |
|
| 280 |
#define getBSDF_incvec(v,b,i) (*(b)->ib_vec)(v,i,(b)->ib_priv)
|
| 281 |
#define getBSDF_incndx(b,v) (*(b)->ib_ndx)(v,(b)->ib_priv)
|
| 282 |
#define getBSDF_incohm(b,i) (*(b)->ib_ohm)(i,(b)->ib_priv)
|
| 283 |
#define getBSDF_outvec(v,b,o) (*(b)->ob_vec)(v,o,(b)->ob_priv)
|
| 284 |
#define getBSDF_outndx(b,v) (*(b)->ob_ndx)(v,(b)->ob_priv)
|
| 285 |
#define getBSDF_outohm(b,o) (*(b)->ob_ohm)(o,(b)->ob_priv)
|
| 286 |
#define BSDF_value(b,i,o) (b)->bsdf[(o)*(b)->ninc + (i)]
|
| 287 |
|
| 288 |
extern struct BSDF_data *load_BSDF(char *fname);
|
| 289 |
extern void free_BSDF(struct BSDF_data *b);
|
| 290 |
extern int r_BSDF_incvec(FVECT v, struct BSDF_data *b, int i,
|
| 291 |
double rv, MAT4 xm);
|
| 292 |
extern int r_BSDF_outvec(FVECT v, struct BSDF_data *b, int o,
|
| 293 |
double rv, MAT4 xm);
|
| 294 |
extern int getBSDF_xfm(MAT4 xm, FVECT nrm, UpDir ud, char *xfbuf);
|
| 295 |
|
| 296 |
/*######### END DEPRECATED DEFINITIONS #######*/
|
| 297 |
/*################################################################*/
|
| 298 |
|
| 299 |
#ifdef __cplusplus
|
| 300 |
}
|
| 301 |
#endif
|
| 302 |
#endif /* ! _BSDF_H_ */
|