Skip to content

Commit

Permalink
try fresnel
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleTheFu committed Oct 2, 2024
1 parent 9b49944 commit 1db177e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
3 changes: 2 additions & 1 deletion brdf/glass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Color Glass::sample_f(const Vector3 &wo, Vector3 &wi, float &pdf) const
// Vector3 inputVector = Vector3(wo.x, wo.y, -wo.z);
Vector3 inputVector = -wo;
bool totalReflect;
wi = inputVector._refract(normal, etaInputSide, etaOutputSide, totalReflect);
float fresnel;
wi = inputVector._refract(normal, etaInputSide, etaOutputSide, totalReflect, fresnel);

pdf = 1;

Expand Down
2 changes: 1 addition & 1 deletion camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Camera::Camera()
{
m_factor = 3;
m_factor = 2;

m_Width = Common::default_screen_width * m_factor;
m_Height = Common::default_screen_height * m_factor;
Expand Down
16 changes: 5 additions & 11 deletions material/mixMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ Color MixMaterial::eval(float u, float v, const Vector3 &wo, Vector3 &wi, float

bool totalReflect = false;
Vector3 inputVector = -wo;
Vector3 refractVector = inputVector.refract(normal, etaOutside, etaInside, totalReflect);
float fresnel;
Vector3 refractVector = inputVector._refract(normal, etaOutside, etaInside, totalReflect, fresnel);
float F = 1;
if (!totalReflect)
{
float sinOut = std::sqrt(1 - cosOut * cosOut);
float sinIn = sinOut * etaOutside / etaInside;
float cosIn = std::sqrt(1 - sinIn * sinIn);

float r_pa = (etaInside * cosOut - etaOutside * cosIn) / (etaInside * cosOut + etaOutside * cosIn);
float r_per = (etaOutside * cosOut - etaInside * cosIn) / (etaOutside * cosOut + etaInside * cosIn);

F = 0.5 * (r_pa * r_pa + r_per * r_per);
F = fresnel;
}

float rnd = Common::genRandomDecimal();
Expand All @@ -53,12 +47,12 @@ Color MixMaterial::eval(float u, float v, const Vector3 &wo, Vector3 &wi, float

if (rnd < F)
{
color = m_pMirrorBrdf->sample_f(wo, wi, pdf) * F;
color = m_pMirrorBrdf->sample_f(wo, wi, pdf);
pdf = F;
}
else
{
color = m_pGlassBrdf->sample_f(wo, wi, pdf) * (1 - F);
color = m_pGlassBrdf->sample_f(wo, wi, pdf);
pdf = 1 - F;
}

Expand Down
16 changes: 15 additions & 1 deletion mathmatic/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ Vector3 Vector3::reflect(const Vector3 &normal) const
return 2 * m * n + (*this);
}

Vector3 Vector3::_refract(const Vector3 &normal, float etaInputSide, float etaOutputSide, bool &totalReflect) const
Vector3 Vector3::_refract(const Vector3 &normal,
float etaInputSide,
float etaOutputSide,
bool &totalReflect,
float &fresnel) const
{
assert((normal != Vector3::ZERO && "Vector3::_refract"));
assert((etaInputSide != 0) && "Vector3::_refract");
Expand All @@ -206,6 +210,7 @@ Vector3 Vector3::_refract(const Vector3 &normal, float etaInputSide, float etaOu
if(sin_theta_out_sqr >= 1)
{
totalReflect = true;
fresnel = 1;
return reflect(normal);
}

Expand All @@ -224,6 +229,15 @@ Vector3 Vector3::_refract(const Vector3 &normal, float etaInputSide, float etaOu

Vector3 out = out_p + out_n;

float cos_theta_in = dot;

float r_pa = (etaOutputSide * cos_theta_in - etaInputSide * cos_theta_out) /
(etaOutputSide * cos_theta_in + etaInputSide * cos_theta_out);
float r_per = (etaInputSide * cos_theta_in - etaOutputSide * cos_theta_out) /
(etaInputSide * cos_theta_in + etaOutputSide * cos_theta_out);

fresnel = 0.5 * (r_pa * r_pa + r_per * r_per);

return out;
}

Expand Down
6 changes: 5 additions & 1 deletion mathmatic/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ class Vector3

Vector3 reflect(const Vector3 &normal) const;
Vector3 refract(const Vector3 &normal, float etaInputSide, float etaOutputSide, bool &totalReflect) const;
Vector3 _refract(const Vector3 &normal, float etaOutside, float etaInside, bool &totalReflect) const;
Vector3 _refract(const Vector3 &normal,
float etaOutside,
float etaInside,
bool &totalReflect,
float &fresnel) const;
bool isInSameSide(const Vector3 &that) const;

static Vector3 getRandomVector();
Expand Down
9 changes: 8 additions & 1 deletion scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void Scene::constructScene()
buildRoom();
buildLight();

buildRedBall();
buildMixBall();
}

Expand Down Expand Up @@ -206,8 +207,14 @@ void Scene::buildSceneWithDefaultConfig()
// bunny->addToPool(m_pObjectPool);
}

void Scene::buildRedBall()
{
Ball *redBall = new Ball(Vector3::ZERO, Vector3(25, 70, 250), 20, lambMtrlRed);
m_pObjectPool->add(redBall);
}

void Scene::buildMixBall()
{
Ball *mixBall = new Ball(Vector3::ZERO, Vector3(75, 72, 300), 20, MtrlMix);
Ball *mixBall = new Ball(Vector3::ZERO, Vector3(-28, 72, 250), 20, MtrlMix);
m_pObjectPool->add(mixBall);
}
3 changes: 2 additions & 1 deletion scene/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class Scene
void buildLight();

void buildSceneWithDefaultConfig();


void buildRedBall();
void buildMixBall();

private:
Expand Down

0 comments on commit 1db177e

Please sign in to comment.