/
bsdf.cpp
243 lines (192 loc) · 8.53 KB
/
bsdf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/***************************************************************************
* Copyright 1998-2018 by authors (see AUTHORS.txt) *
* *
* This file is part of LuxCoreRender. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
#include "slg/bsdf/bsdf.h"
#include "slg/scene/scene.h"
#include "slg/materials/glass.h"
using namespace luxrays;
using namespace slg;
using namespace std;
// Used when hitting a surface
void BSDF::Init(const bool fixedFromLight, const Scene &scene, const Ray &ray,
const RayHit &rayHit, const float passThroughEvent, const PathVolumeInfo *volInfo) {
hitPoint.fromLight = fixedFromLight;
hitPoint.passThroughEvent = passThroughEvent;
hitPoint.p = ray(rayHit.t);
hitPoint.fixedDir = -ray.d;
// Get the scene object
sceneObject = scene.objDefs.GetSceneObject(rayHit.meshIndex);
hitPoint.objectID = sceneObject->GetID();
// Get the triangle
mesh = sceneObject->GetExtMesh();
// Initialized local to world object space transformation
mesh->GetLocal2World(ray.time, hitPoint.localToWorld);
// Get the material
material = sceneObject->GetMaterial();
// Interpolate face normal
hitPoint.geometryN = mesh->GetGeometryNormal(ray.time, rayHit.triangleIndex);
hitPoint.shadeN = mesh->InterpolateTriNormal(ray.time, rayHit.triangleIndex, rayHit.b1, rayHit.b2);
hitPoint.intoObject = (Dot(ray.d, hitPoint.geometryN) < 0.f);
// Set interior and exterior volumes
volInfo->SetHitPointVolumes(hitPoint,
material->GetInteriorVolume(hitPoint, hitPoint.passThroughEvent),
material->GetExteriorVolume(hitPoint, hitPoint.passThroughEvent),
scene.defaultWorldVolume);
// Interpolate color
hitPoint.color = mesh->InterpolateTriColor(rayHit.triangleIndex, rayHit.b1, rayHit.b2);
// Interpolate alpha
hitPoint.alpha = mesh->InterpolateTriAlpha(rayHit.triangleIndex, rayHit.b1, rayHit.b2);
// Check if it is a light source
if (material->IsLightSource())
triangleLightSource = scene.lightDefs.GetLightSourceByMeshAndTriIndex(rayHit.meshIndex, rayHit.triangleIndex);
else
triangleLightSource = NULL;
// Interpolate UV coordinates
hitPoint.uv = mesh->InterpolateTriUV(rayHit.triangleIndex, rayHit.b1, rayHit.b2);
// Compute geometry differentials
mesh->GetDifferentials(hitPoint.localToWorld, rayHit.triangleIndex, hitPoint.shadeN,
&hitPoint.dpdu, &hitPoint.dpdv,
&hitPoint.dndu, &hitPoint.dndv);
// Apply bump or normal mapping
material->Bump(&hitPoint);
// Build the local reference system
frame = hitPoint.GetFrame();
}
// Used when hitting a volume scatter point
void BSDF::Init(const bool fixedFromLight, const Scene &scene, const luxrays::Ray &ray,
const Volume &volume, const float t, const float passThroughEvent) {
hitPoint.fromLight = fixedFromLight;
hitPoint.passThroughEvent = passThroughEvent;
hitPoint.p = ray(t);
hitPoint.fixedDir = -ray.d;
sceneObject = NULL;
mesh = NULL;
material = &volume;
hitPoint.geometryN = Normal(-ray.d);
hitPoint.shadeN = hitPoint.geometryN;
CoordinateSystem(Vector(hitPoint.shadeN), &hitPoint.dpdu, &hitPoint.dpdv);
hitPoint.dndu = hitPoint.dndv = Normal(0.f, 0.f, 0.f);
hitPoint.intoObject = true;
hitPoint.interiorVolume = &volume;
hitPoint.exteriorVolume = &volume;
hitPoint.color = Spectrum(1.f);
hitPoint.alpha = 1.f;
triangleLightSource = NULL;
hitPoint.uv = UV(0.f, 0.f);
hitPoint.objectID = NULL_INDEX;
// Build the local reference system
frame.SetFromZ(hitPoint.shadeN);
}
bool BSDF::IsAlbedoEndPoint() const {
return !IsDelta() ||
// This is a very special case to not have white Albedo AOV if the
// material is mirror. Mirror has no ray split so it can be render
// without any noise.
(material->GetType() != MIRROR);
}
bool BSDF::IsCameraInvisible() const {
return (sceneObject) ? sceneObject->IsCameraInvisible() : false;
}
u_int BSDF::GetObjectID() const {
return (sceneObject) ? sceneObject->GetID() : std::numeric_limits<u_int>::max();
}
Spectrum BSDF::Albedo() const {
return material->Albedo(hitPoint);
}
Spectrum BSDF::EvaluateTotal() const {
return material->EvaluateTotal(hitPoint);
}
Spectrum BSDF::Evaluate(const Vector &generatedDir,
BSDFEvent *event, float *directPdfW, float *reversePdfW) const {
const Vector &eyeDir = hitPoint.fromLight ? generatedDir : hitPoint.fixedDir;
const Vector &lightDir = hitPoint.fromLight ? hitPoint.fixedDir : generatedDir;
const float dotLightDirNG = Dot(lightDir, hitPoint.geometryN);
const float absDotLightDirNG = fabsf(dotLightDirNG);
const float dotEyeDirNG = Dot(eyeDir, hitPoint.geometryN);
const float absDotEyeDirNG = fabsf(dotEyeDirNG);
if (!IsVolume()) {
// These kind of tests make sense only for materials
if ((absDotLightDirNG < DEFAULT_COS_EPSILON_STATIC) ||
(absDotEyeDirNG < DEFAULT_COS_EPSILON_STATIC))
return Spectrum();
const float sideTest = dotEyeDirNG * dotLightDirNG;
if (((sideTest > 0.f) && !(material->GetEventTypes() & REFLECT)) ||
((sideTest < 0.f) && !(material->GetEventTypes() & TRANSMIT)))
return Spectrum();
}
const Vector localLightDir = frame.ToLocal(lightDir);
const Vector localEyeDir = frame.ToLocal(eyeDir);
const Spectrum result = material->Evaluate(hitPoint, localLightDir, localEyeDir,
event, directPdfW, reversePdfW);
assert (!result.IsNaN() && !result.IsInf());
// Adjoint BSDF (not for volumes)
if (hitPoint.fromLight && !IsVolume())
return result * (absDotEyeDirNG / absDotLightDirNG);
else
return result;
}
Spectrum BSDF::ShadowCatcherSample(Vector *sampledDir,
float *pdfW, float *absCosSampledDir, BSDFEvent *event) const {
// Just continue to trace the ray
*sampledDir = -hitPoint.fixedDir;
*absCosSampledDir = AbsDot(*sampledDir, hitPoint.geometryN);
*pdfW = 1.f;
*event = SPECULAR | TRANSMIT;
const Spectrum result(1.f);
// Adjoint BSDF
if (hitPoint.fromLight) {
const float absDotFixedDirNG = AbsDot(hitPoint.fixedDir, hitPoint.geometryN);
const float absDotSampledDirNG = AbsDot(*sampledDir, hitPoint.geometryN);
return result * (absDotSampledDirNG / absDotFixedDirNG);
} else
return result;
}
Spectrum BSDF::Sample(Vector *sampledDir,
const float u0, const float u1,
float *pdfW, float *absCosSampledDir, BSDFEvent *event) const {
Vector localFixedDir = frame.ToLocal(hitPoint.fixedDir);
Vector localSampledDir;
const Spectrum result = material->Sample(hitPoint,
localFixedDir, &localSampledDir, u0, u1, hitPoint.passThroughEvent,
pdfW, absCosSampledDir, event);
if (result.Black())
return result;
*sampledDir = frame.ToWorld(localSampledDir);
// Adjoint BSDF
if (hitPoint.fromLight) {
const float absDotFixedDirNG = AbsDot(hitPoint.fixedDir, hitPoint.geometryN);
const float absDotSampledDirNG = AbsDot(*sampledDir, hitPoint.geometryN);
return result * (absDotSampledDirNG / absDotFixedDirNG);
} else
return result;
}
void BSDF::Pdf(const Vector &sampledDir, float *directPdfW, float *reversePdfW) const {
const Vector &eyeDir = hitPoint.fromLight ? sampledDir : hitPoint.fixedDir;
const Vector &lightDir = hitPoint.fromLight ? hitPoint.fixedDir : sampledDir;
Vector localLightDir = frame.ToLocal(lightDir);
Vector localEyeDir = frame.ToLocal(eyeDir);
material->Pdf(hitPoint, localLightDir, localEyeDir, directPdfW, reversePdfW);
}
Spectrum BSDF::GetPassThroughTransparency() const {
const Vector localFixedDir = frame.ToLocal(hitPoint.fixedDir);
return material->GetPassThroughTransparency(hitPoint, localFixedDir, hitPoint.passThroughEvent);
}
Spectrum BSDF::GetEmittedRadiance(float *directPdfA, float *emissionPdfW) const {
return triangleLightSource ?
triangleLightSource->GetRadiance(hitPoint, directPdfA, emissionPdfW) :
Spectrum();
}