diff --git a/brdf/glass.cpp b/brdf/glass.cpp index 45db1d96..88bf032a 100644 --- a/brdf/glass.cpp +++ b/brdf/glass.cpp @@ -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; diff --git a/camera/camera.cpp b/camera/camera.cpp index e7b087da..dc2b457b 100644 --- a/camera/camera.cpp +++ b/camera/camera.cpp @@ -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; diff --git a/material/mixMaterial.cpp b/material/mixMaterial.cpp index e0cb7df0..e3ee2346 100644 --- a/material/mixMaterial.cpp +++ b/material/mixMaterial.cpp @@ -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(); @@ -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; } diff --git a/mathmatic/vector.cpp b/mathmatic/vector.cpp index b974cc3f..9f0ea74a 100644 --- a/mathmatic/vector.cpp +++ b/mathmatic/vector.cpp @@ -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"); @@ -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); } @@ -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; } diff --git a/mathmatic/vector.h b/mathmatic/vector.h index e011fe79..73f08184 100644 --- a/mathmatic/vector.h +++ b/mathmatic/vector.h @@ -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(); diff --git a/scene/scene.cpp b/scene/scene.cpp index 2f1ae2a8..d1b087c1 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -53,6 +53,7 @@ void Scene::constructScene() buildRoom(); buildLight(); + buildRedBall(); buildMixBall(); } @@ -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); } diff --git a/scene/scene.h b/scene/scene.h index 34c778c5..16061af4 100644 --- a/scene/scene.h +++ b/scene/scene.h @@ -32,7 +32,8 @@ class Scene void buildLight(); void buildSceneWithDefaultConfig(); - + + void buildRedBall(); void buildMixBall(); private: