Home:ALL Converter>Raytracing normal mapping

Raytracing normal mapping

Ask Time:2016-12-07T18:55:02         Author:Tab

Json Formatter

I am currently working on a raytracer and I just "bumped" in an issue. I implemented texture mapping for planes, cylinders and spheres and it's working pretty well... Except for the normal map part. Here is what I have, the in-world position and the in-world normals of each pixel : world-space normals. And some tangent-space normal map (the usual normal map).

I can't seem to figure out how to convert the tangent-space normals to world-space. I have tried using a "TBN" matrix but the normals are off : normal map projected normals.

And here is my code to compute the new normal :

VEC3    t = vec3_cross(worldnormal, new_vec3(0.0, 1.0, 0.0)); 
VEC3    b;
if (!vec3_length(t))
    t = vec3_cross(worldnormal, new_vec3(0.0, 0.0, 1.0));
t = vec3_normalize(t);
b = vec3_normalize((vec3_cross(worldnormal, t)));
VEC3    map_n = vec3_normalize(get_texture_color(normal_map, texcoords));
MAT3    tbn = new_mat3(t, b, worldnormal);
worldnormal = vec3_normalize(mat3_mult_vec3(tbn, map_n));

get_texture_color() returns the normal map's texture color divided by 255.f

Author:Tab,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41015574/raytracing-normal-mapping
yy