I'm trying to add bump mapping to my ray tracer, and I'm currently struggling to add bump mapping to it, as I don't seem to get the transformations from texture space to world space correctly.
I have an arbitary Triangle in world-Space defined by it's vertices. Each of the vertices store a normalized UV-Coordinate to interpolate the position on the height map for an Intersection Point. The height map is a gray-Scale Image So when I intersect with the Triangle, I calculate the x and y derivatives of the height map at the corresponding uv-coordinate, transform the x and y unit Vectors (of the Texture) to world-Space, and calculate the changed normal. This currently looks like this:
Point p = lerpbar(uv_vert1, uv_vert2, uv_vert3,
intersection.barycentric.u, intersection.barycentric.v);
float gx = this->_bmap->getColorDX(p);
float gy = this->_bmap->getColorDY(p);
Vector wx = (To_World_Matrix * Vector(1, 0, 0))).normalize();
Vector wy = (To_World_Matrix * Vector(0, 1, 0))).normalize();
Vector n = intersection.normal();
return n + (gx * cross(n, wy) - gy * cross(n, wx));
However, I just can't figure out what Matrix I'd need to use so this produces correct results, as I just don't really understand how to map from unit Vectors to World-Space Vectors when I only know how arbitay Triangle Edges relate to arbitay Vectors in Texture Space.