diff --git a/include/scene.h b/include/scene.h index f56a935..dd4d728 100644 --- a/include/scene.h +++ b/include/scene.h @@ -332,7 +332,8 @@ class Scene { info.surfaceInfo.barycentric = Vec2f(rayhit.hit.u, rayhit.hit.v); info.surfaceInfo.texcoords = tri.getTexcoords(info.surfaceInfo.barycentric); - info.surfaceInfo.normal = tri.getFaceNormal(info.surfaceInfo.barycentric); + info.surfaceInfo.normal = + tri.computeShadingNormal(info.surfaceInfo.barycentric); orthonormalBasis(info.surfaceInfo.normal, info.surfaceInfo.dpdu, info.surfaceInfo.dpdv); diff --git a/include/triangle.h b/include/triangle.h index be34437..8bc6b55 100644 --- a/include/triangle.h +++ b/include/triangle.h @@ -11,7 +11,7 @@ class Triangle { const float* texcoords; const uint32_t faceID; - + Vec3f geometricNormal; float surfaceArea; public: @@ -22,11 +22,15 @@ class Triangle { normals(normals), texcoords(texcoords), faceID(faceID) { - // compute surface area const Vec3ui vidx = getIndices(); const Vec3f p1 = getVertexPosition(vidx[0]); const Vec3f p2 = getVertexPosition(vidx[1]); const Vec3f p3 = getVertexPosition(vidx[2]); + + // compute geometric normal + geometricNormal = normalize(cross(p2 - p1, p3 - p1)); + + // compute surface area surfaceArea = 0.5f * length(cross(p2 - p1, p3 - p1)); } @@ -53,8 +57,11 @@ class Triangle { indices[3 * faceID + 2]); } - // compute face normal at given position - Vec3f getFaceNormal(const Vec2f& barycentric) const { + // return geometric normal + Vec3f getGeometricNormal() const { return geometricNormal; } + + // compute shading normal at given position + Vec3f computeShadingNormal(const Vec2f& barycentric) const { const Vec3ui vidx = getIndices(); const Vec3f n1 = getVertexNormal(vidx[0]); const Vec3f n2 = getVertexNormal(vidx[1]); @@ -91,7 +98,7 @@ class Triangle { barycentric[0] * p2 + barycentric[1] * p3; // compute normal - ret.normal = getFaceNormal(barycentric); + ret.normal = computeShadingNormal(barycentric); // compute dpdu, dpdv orthonormalBasis(ret.normal, ret.dpdu, ret.dpdv);