Topics in Computer Graphics Spring 2010
Application
Shading
Maps Base texture (RGB) Height map (Grey scale) Normal map (normal encoded RGB)
Normal Map & Height Field
Normal Map Normal vector encoded as rgb [-1,1] 3 [0,1] 3 : rgb = n* RGB decoding in fragment shaders vec3 n = texture2D(NormalMap, texcoord.st).xyz * 2.0 – 1.0 In tangent space, the default (unit) normal points in the +z direction. Hence the RGB color for the straight up normal is (0.5, 0.5, 1.0). This is why normal maps are a blueish color Normals are then used for shading computation Diffuse: nl Specular: (nh) shininess Computations done in tangent space
In order to build this Tangent Space, we need to define an orthonormal (per vertex) basis, which will define our tangent space. Tangent space is composed of 3 orthogonal vectors (T, B, N) Tangent (S Tangent) Bitangent (T Tangent) Normal One has to calculate a tangent space matrix for every single vertex Tangent Space
Suppose a point p i in world coordinate system for whose texture coordinates are (u i, v i ) Writing this equation for the points p1, p2 and p3, defining the triangle : p 1 = u 1.T + v 1.B p 2 = u 2.T + v 2.B p 3 = u 3.T + v 3.B
Tangent Space p 2 - p 1 = (u 2 - u 1 ).T + (v 2 - v 1 ).B p 3 - p 1 = (u 3 - u 1 ).T + (v 3 - v 1 ).B (v 3 - v 1 ).(p 2 - p 1 ) = (v 3 - v 1 ).(u 2 - u 1 ).T + (v 3 - v 1 ).(v 2 - v 1 ).B - (v 2 - v 1 ).(p 3 - p 1 ) - (v 2 - v 1 ).(u 3 - u 1 ).T - (v 2 - v 1 ).(v 3 - v 1 ).B (u 3 - u 1 ).(p 2 - p 1 ) = (u 3 - u 1 ).(u 2 - u 1 ).T + (u 3 - u 1 ).(v 2 - v 1 ).B - (u 2 - u 1 ).(p 3 - p 1 ) - (u 2 - u 1 ).(u 3 - u 1 ).T - (u 2 - u 1 ).(v 3 - v 1 ).B (v 3 - v 1 ).(p 2 - p 1 ) - (v 2 - v 1 ).(p 3 - p 1 ) T = (u 2 - u 1 ).(v 3 - v 1 ) - (v 2 - v 1 ).(u 3 - u 1 ) (u 3 - u 1 ).(p 2 - p 1 ) - (u 2 - u 1 ).(p 3 - p 1 ) B = (v 2 - v 1 ).(u 3 - u 1 ) - (u 2 - u 1 ).(v 3 - v 1 ) 6 eqns, 6 unknowns T,B: (unit) vectors in object space
TBN Matrix Per Vertex Use the averaged face normal as the vertex normal Do the same for tangent and bitangent vectors Note that the T, B vectors might not be orthogonal to the normal vector Use Gram-Schmidt to make sure they are orthonormal
Coordinate Transformation Tangent space to object space Object space to tangent space This reference ( is correcthttp://jerome.jouvie.free.fr/OpenGl/Lessons/Lesson8.php TyphoonLabs is not right.
What is mat3 (v1,v2,v3)?! It turns out to be “blue” This is the matrix that converts object space to tangent space
Reference er_4.pdf er_4.pdf p.html p.html studios.dk/projects/downloads/tangent_matrix_derivation.php studios.dk/projects/downloads/tangent_matrix_derivation.php