-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbsdf.cpp
177 lines (164 loc) · 4.99 KB
/
gbsdf.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
#include "gbsdf.h"
#include "gmathutils.h"
#include "gsampler.h"
#include "gutils.h"
using namespace std;
GBxDF::GBxDF(std::shared_ptr<GBSDF> bsdf, BxDFType type) : bsdf(bsdf), type(type) {}
GFColor GBxDF::Sample_f(GSurfaceInteraction &isect, float &pdf) const
{
isect.wi = GSampler::CosineSampleHemisphere();
GMathUtils::SameHemisphere(isect.shadingNormal, isect.wo, isect.wi);
pdf = Pdf(isect);
return f(isect);
}
float GBxDF::Pdf(const GSurfaceInteraction& isect) const
{
float cosThetaI = dot(isect.wi, isect.shadingNormal);
bool isInSameHemisphere = (dot(isect.wo, isect.shadingNormal) * cosThetaI) > 0;
if(isInSameHemisphere)
{
return std::abs(cosThetaI) * M_INVERSE_PI;
}
else
{
return 0;
}
}
GFColor GLambertianReflection::f(const GSurfaceInteraction &isect) const
{
GFColor KdColor(GColor::blackF);
if(GUtils::debugType==GDebugType::kNormalTex)
{
//KdColor = bsdf->normal->sample(isect.uv, isect.p);
}
else if(GUtils::debugType==GDebugType::kWorldNormal)
{
KdColor = GColor::whiteF;
KdColor.SetXYZ((isect.shadingNormal + vec3::one) * 0.5);
}
else if(GUtils::debugType==GDebugType::kWorldTangent)
{
KdColor = GColor::whiteF;
KdColor.SetXYZ((isect.tangent + vec3::one) * 0.5);
}
else
{
KdColor = bsdf->Kd;
}
return KdColor * M_INVERSE_PI;
}
GFColor GSpecularReflection::Sample_f(GSurfaceInteraction &isect, float &pdf) const
{
GFColor KsColor = bsdf->Ks;
if(GColor::IsBlack(KsColor))
{
pdf = 0;
return GColor::blackF;
}
isect.wi = reflect(isect.wo, isect.shadingNormal);
pdf = 1;
auto cosTheta = absDot(isect.wi, isect.shadingNormal);
GFColor F = GMathUtils::SchlickFresnel(KsColor, cosTheta);
return F * KsColor / cosTheta;
}
GFColor GSpecularRefraction::Sample_f(GSurfaceInteraction &isect, float &pdf) const
{
bool entering = dot(isect.wo, isect.normal) > 0;
float etaI = entering ? bsdf->etaA : bsdf->etaB;
float etaT = entering ? bsdf->etaB : bsdf->etaA;
// tips: we trace ray from camera to scene, so wo is exit, wi is incidence
if(!refract(isect.wo, isect.shadingNormal, (double)etaI/etaT, isect.wi))
{
return GColor::blackF;
}
pdf = 1;
auto cosTheta = absDot(isect.wi, isect.shadingNormal);
GFColor F = GMathUtils::SchlickFresnel(bsdf->Ks, cosTheta);
GFColor KtColor = bsdf->Ks;
GFColor ft = KtColor * (GColor::whiteF - F);
// https://www.pbr-book.org/3ed-2018/Light_Transport_III_Bidirectional_Methods/The_Path-Space_Measurement_Equation#fragment-Accountfornon-symmetrywithtransmissiontodifferentmedium-0
//if(false)
{
ft = ft * (etaI * etaI) / (etaT * etaT);
}
return ft / cosTheta;
}
GFColor GBSDF::f(const GSurfaceInteraction& isect) const
{
bool reflect = dot(isect.wi, isect.normal) * dot(isect.wo, isect.normal) > 0;
GFColor f = GColor::blackF;
for(auto& bxdf : bxdfs)
{
if(bxdf->MatchesFlags(isect.bxdfTypes))
{
if((reflect && (bxdf->type & BSDF_REFLECTION)) || (!reflect && (bxdf->type & BSDF_TRANSMISSION)))
{
f = f + bxdf->f(isect);
}
}
}
return f;
}
GFColor GBSDF::Sample_f(GSurfaceInteraction &isect, float &pdf) const
{
int matchingComps = NumComponents(isect.bxdfTypes);
if(matchingComps == 0)
{
pdf = 0;
isect.sampledBxDFType=BxDFType(0);
return GColor::blackF;
}
shared_ptr<GBxDF> bxdf = nullptr;
int count = std::min((int)std::floor(GSampler::Random() * matchingComps), matchingComps-1);
for(auto& tBxdf : bxdfs)
{
if(tBxdf->MatchesFlags(isect.bxdfTypes) && count-- == 0)
{
bxdf = tBxdf;
break;
}
}
pdf = 0;
isect.sampledBxDFType=bxdf->type;
GFColor f = bxdf->Sample_f(isect, pdf);
if(pdf == 0)
{
isect.sampledBxDFType=BxDFType(0);
return GColor::blackF;
}
// Compute overall PDF with all matching _BxDF_s
if(!(bxdf->type & BSDF_SPECULAR) && matchingComps > 1)
{
for(auto& tBxdf : bxdfs)
{
if(tBxdf!=bxdf && tBxdf->MatchesFlags(isect.bxdfTypes))
{
pdf += tBxdf->Pdf(isect);
}
}
}
if(matchingComps > 1) pdf /= matchingComps;
if(!(bxdf->type & BSDF_SPECULAR))
{
bool reflect = dot(isect.wi, isect.normal) * dot(isect.wo, isect.normal) > 0;
f = GColor::blackF;
for(auto& tBxdf : bxdfs)
{
if(tBxdf->MatchesFlags(isect.bxdfTypes))
{
if((reflect && tBxdf->type & BSDF_REFLECTION) || (!reflect && tBxdf->type & BSDF_TRANSMISSION))
{
f = f + tBxdf->f(isect);
}
}
}
}
return f;
}
inline int GBSDF::NumComponents(BxDFType flags) const
{
int num = 0;
for (int i = 0; i < bxdfs.size(); ++i)
if (bxdfs[i]->MatchesFlags(flags)) ++num;
return num;
}