Normal Mapping
To achieve high levels of visual detail without significantly increasing geometric
complexity, we implemented
Normal Mapping. This technique allows us to simulate intricate surface features—such
as scratches,
pores, or embossed patterns—by perturbing the surface normal used in lighting calculations.
By faking the
way light bounces off a surface, we can make a simple flat plane appear as though it has
complex,
three-dimensional depth.
Implementation Details
Normal mapping was integrated directly into the Instance::transformFrame
method, ensuring
that all shapes—regardless of their primitive type—automatically support this feature.
-
Tangent Space Transformation: Normal maps are stored in "tangent space" (the
local coordinate
system of the surface). We use the surface's tangent, bitangent, and shading normal to
build a
coordinate frame that transforms the sampled normal into world space.
-
Data Remapping: Since color textures store values from [0, 1] but surface normals
exist in the
range [-1, 1], we apply a remapping formula: n = 2 ⋅ color - 1.
-
Dynamic Perturbation: The shadingNormal of the
SurfaceEvent
is updated
using the sampled normal, while the geometryNormal remains unchanged. This
ensures that
shading is detailed while ray-object intersections remain computationally efficient.
Challenges
The primary challenge was ensuring the orthonormal consistency of the shading frame
after
transformations were applied.
When an object is scaled or rotated via the m_transform, the tangent and normal
must
be transformed correctly before the normal map is applied. If the transformation or the
subsequent reconstruction of the normal in world space is off by even a small degree, the
lighting
will appear to "flip" or look inverted as the camera moves around the object. Correcting the
vector math to ensure the normal was reconstructed in the correct
Tangent-Bitangent-Normal order.
Modified files
- instance.hpp | Added the m_normal texture property to the Instance class.
- instance.cpp | Implemented the normal remapping and tangent space reconstruction
logic.