Advertisement Jump to content
Sign in to follow this  

Wrong Normals in Deferred Renderer

This topic is 1191 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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


    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))) *;

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.


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

Thank you in advance for your help smile.png Edited by Weton

Share this post

Link to post
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 this post

Link to post
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 smile.png

Edited by Weton

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!