# Wrong Normals in Deferred Renderer

## Recommended Posts

Hi there,

I try to make a deferred renderer, but it seems like my normals don't face the direction I expcted.

I successfully created a framebuffer with 3 targets color, normal and depth/stencil to wich I can draw and read from in my shaders.
I calculate the view (looking at negative z, up is positive y) and projection matrices wich results in these values:
v:{ m11: -1, m21: 0, m31: 0, m41: 0, m12: 0, m22: 1, m32: 0, m42: 0, m13: 0, m23: 0, m33: -1, m43: 0, m14: 0, m24: 0, m34: 0, m44: 1 }
p:{ m11: -1.2990379, m21: 0, m31: 0, m41: 0, m12: 0, m22: 1.7320507, m32: 0, m42: 0, m13: 0, m23: 0, m33: 1.020202, m43: 1, m14: 0, m24: 0, m34: -2.020202, m44: 0 }
stored in column major order: m11,m21,m31,m41,m12,...

In the fist pass I render all my ojects to the framebuffer using the following shader:
    #version 420
#extension GL_ARB_explicit_attrib_location : require
#extension GL_ARB_explicit_uniform_location : enable

//CAMERA
layout(binding=1) uniform CameraBlock {
mat4 v;
mat4 p;
mat4 p_v; //P*V
//...
};

//...

//MESH
layout(binding=3, std140) uniform MeshBlock {
mat4 m;
};

layout(location=0) in vec3 inPos;
layout(location=1) in vec2 inTexCoord;
layout(location=2) in vec3 inNormal;

out vec2 texCoord;
out vec3 normal;

void main() {
gl_Position =  p_v*m * vec4(inPos, 1.0);
texCoord = inTexCoord;
normal = normalize(transpose(inverse(mat3(v*m))) * inNormal.xyz);
}


Using the view and perspective matrix from above the normals point in the wrong (?) direction. But when I only use 'm' instead of 'v*m' I get the result I expected.

[attachment=29360:normals.png]

Are my matrices wrong or didn't I calculate the normals correctly?
I am surprised, that the view matrix isn't an identity matrix.

##### Share on other sites
Could you post the code you are using to produce your view and model matrix? I don't see anything wrong with the math in the shader right away. The only I would change there is to not calculate the transpose inverse in the shader, do that once outside the shader and pass it in as uniform. The inverse is expensive to be calculating for every vertex. Edited by HappyCoder

##### Share on other sites

Yes, I'd calculate the matrices in advance, but I couldn't find my mistake for so long, so I just left it in there ;)

I took the functions from a library. So when you asked me for the code I looked it up and noticed that it didn't work as expected.

So I used this perspective matrix and everything works :

let p = Mat4::new(
near/right, 0.       , 0.                    , 0.                     ,
0.        , near/left, 0.                    , 0.                     ,
0.        , 0.       , -(far+near)/(far-near), -2.*far*near/(far-near),
0.        , 0.       , -1.                   , 0.                     ,
);


Thank you a lot for asking the right question

Edited by Weton

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982139

• 9
• 24
• 9
• 9
• 13