Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
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. –  Daniel van den Houten Aug 18 '12 at 15:37
1  
Here is a solid article that I used to successfully implement my normal mapping. ogldev.atspace.co.uk/www/tutorial26/tutorial26.html –  KlashnikovKid Aug 18 '12 at 16:04
    
Managed to get it going now :) I found my solution here: pouet.net/topic.php?which=6266#c269765 –  Daniel van den Houten Aug 18 '12 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.