Jump to content
  • Advertisement
Sign in to follow this  
BlueSpud

OpenGL Normals in World Space from a GLSL Shader

This topic is 2111 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

Hello,

I am implementing deferred lighting and I want to get the normals in world space, as I've been told that they are more efficient way of storing them. After browsing the internet I haven't been able to find anything. I'm fairly inept with opengl's matrixes, but not opengl itself. I'm also using GLSL to render the G-buffer, as it is much easier than regular opengl.  Any help would be appreciated. Thanks.

Edited by BlueSpud

Share this post


Link to post
Share on other sites
Advertisement

Like this?

vec3 world_space_normal = vec3(model_matrix * vec4(vertex_normal, 0.0));

I did some deferred rendering tests about month ago and I did everything in view space. I think view space should be more efficient. Anyway, it doesn't matter that much which space you are using, you can always change them.

vec3 view_space_normal = vec3(view_matrix * model_matrix * vec4(vertex_normal, 0.0));
Edited by Sponji

Share this post


Link to post
Share on other sites

 

Like this?

vec3 world_space_normal = vec3(model_matrix * vec4(vertex_normal, 0.0));

I did some deferred rendering tests about month ago and I did everything in view space. I think view space should be more efficient. Anyway, it doesn't matter that much which space you are using, you can always change them.

vec3 view_space_normal = vec3(view_matrix * model_matrix * vec4(vertex_normal, 0.0));

Because I'm doing this in GLSL, I multiple it by the gl_ModelViewMatrix?

Edited by BlueSpud

Share this post


Link to post
Share on other sites

Be careful. You don't want to translate or scale your normals.

Google for 'normal matrix': normalMat = transpose(inverse(upper33(worldMat))

Share this post


Link to post
Share on other sites

The normal matrix is created via first transposing and then inverting the upper 3×3 part of the world (or world-view) matrix.

normalMat = inverse(transpose(upper33(worldMat))

While it does remove translations, it does not remove scales.  Its purpose is to correct the directions the normals face under non-uniform scaling.  That is, if the model scales up along its X axis by 2 times, without the inverse transpose matrix the normal will also lean further away from 0 along the X axis, when in fact it should actually point more and more towards 0 along the X axis as the model’s X scaling increases.

Using the inverse transpose reverses the scaling to make this happen.

 

In short, the inverse-transpose matrix is meant to correct artifacts under non-uniform scaling conditions.  It does not remove scaling.

 

In order to remove scaling you have to either renormalize the vector after multiplying it by the inverse transpose matrix (most common) or renormalize the 3 rows of the inverse-transpose matrix (faster).

 

 

L. Spiro

Share this post


Link to post
Share on other sites

The normal matrix is created via first transposing and then inverting the upper 3×3 part of the world (or world-view) matrix.

normalMat = inverse(transpose(upper33(worldMat))

While it does remove translations, it does not remove scales.  Its purpose is to correct the directions the normals face under non-uniform scaling.  That is, if the model scales up along its X axis by 2 times, without the inverse transpose matrix the normal will also lean further away from 0 along the X axis, when in fact it should actually point more and more towards 0 along the X axis as the model’s X scaling increases.

Using the inverse transpose reverses the scaling to make this happen.

 

In short, the inverse-transpose matrix is meant to correct artifacts under non-uniform scaling conditions.  It does not remove scaling.

 

In order to remove scaling you have to either renormalize the vector after multiplying it by the inverse transpose matrix (most common) or renormalize the 3 rows of the inverse-transpose matrix (faster).

 

 

L. Spiro

Thanks for the help, but I don't believe that was my problem which is kind of disappointing. Thanks anyways.

Share this post


Link to post
Share on other sites


Because I'm doing this in GLSL, I multiple it by the gl_ModelViewMatrix?

Besides the already mentioned thing about the transpose-inverse: You want the normals in world co-ordinate. Using ModelViewMatrix means the composition of the model (i.e. model local to global) and view (i.e. global to view) matrix. Hence you would go too far in the transformation chain. That said, you just want to use the ModelMatrix.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!