Skip to content

Commit 13c375e

Browse files
committed
1.[ADD]:add reflect and refract function, and refract normal could be automatically flipped
1 parent cc833d2 commit 13c375e

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

include/Tools.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ struct Tools {
152152
const glm::vec3 &pb,
153153
const glm::vec3 &pc);
154154

155+
static glm::vec3 reflect(const glm::vec3& I, const glm::vec3& N);
156+
157+
static glm::vec3 refract(const glm::vec3& I, const glm::vec3& N, const float& ior);
158+
155159
static float fresnel(const glm::vec3 &rayDirection, const glm::vec3 &normal,
156160
const float &refractiveIndex);
157161

src/Tools.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ glm::vec3 SoftRasterizer::Tools::calculateNormalWithWeight(
247247
return glm::normalize(normal);
248248
}
249249

250+
glm::vec3 SoftRasterizer::Tools::reflect(const glm::vec3& I, const glm::vec3& N) {
251+
return I - 2 * glm::dot(I, N) * N;
252+
}
253+
254+
glm::vec3 SoftRasterizer::Tools::refract(const glm::vec3& I, const glm::vec3& N, const float& ior) {
255+
float cosi = std::clamp(glm::dot(I, N), -1.f, 1.f);
256+
float etai = 1.f, etat = ior;
257+
glm::vec3 n = N;
258+
if (cosi < 0) { cosi = -cosi; }
259+
else { std::swap(etai, etat); n = -N; }
260+
float eta = etai / etat;
261+
float k = 1 - eta * eta * (1 - cosi * cosi);
262+
return k < 0 ? glm::vec3(0.f) : eta * I + (eta * cosi - sqrtf(k)) * n;
263+
}
264+
250265
float SoftRasterizer::Tools::fresnel(const glm::vec3 &rayDirection,
251266
const glm::vec3 &normal,
252267
const float &refractiveIndex) {

0 commit comments

Comments
 (0)