# Deferred Rendering and Normal Mapping

I'm working on a deferred renderer and need a bit of help getting normal maps working. What I've been doing with them so far is just multiplying the normal texture with the object normals (gl_NormalMatrix * gl_Normal). That appears to be wrong.

I just need someone to explain how I can add the normal map the the normal gbuffer.

The offending line:

    vec3 normal = Normal * texture2D(normalTexture, gl_TexCoord[0]).rgb;


Edit: I managed to get it working :)

Solution:

vec3 Normal = normalize(gl_NormalMatrix * gl_Normal);
vec3 Tangent = normalize(gl_NormalMatrix[0]);
vec3 Binormal = normalize(gl_NormalMatrix[1]);
mat3 tangentToWorld = mat3(Tangent.x, Binormal.x, Normal.x,
Tangent.y, Binormal.y, Normal.y,
Tangent.z, Binormal.z, Normal.z);


and

vec3 normal = (texture2D(normalTexture, gl_TexCoord[0]).rgb * 2.0 - 1.0) * tangentToWorld;


Right, normal mapping isn't done by componentwise multiplying the object normal and the texture normal. They're vectors, and that operation doesn't make geometric sense for vectors.

For the standard way of doing normal mapping - tangent-space normal maps - the idea is to construct a 3x3 tangent-to-world matrix at each fragment and transform the texture normal using this matrix. The matrix is constructed using the geometric vertex normal as well as per-vertex tangent and bitangent vectors constructed from the UV mapping.

There are plenty of articles about how to do this online, and it has nothing to do with deferred shading; normal mapping works the same way whether you're doing deferred or not.

• Sorry to sound impatient, but I did try follow quite a few articles. The main points I'm confused on are which UV coordinates to use and how to multiply my 'normal' vector by a matrix.
– Sine
Aug 18, 2012 at 15:37
• Here is a solid article that I used to successfully implement my normal mapping. ogldev.atspace.co.uk/www/tutorial26/tutorial26.html Aug 18, 2012 at 16:04
• Managed to get it going now :) I found my solution here: pouet.net/topic.php?which=6266#c269765
– Sine
Aug 18, 2012 at 21:03