Jump to content
  • Advertisement
Sign in to follow this  
bgoldbeck

OpenGL GLSL Shader Help on normals

This topic is 2531 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 a beginner in GLSL and a lot of the tutorials are now outdated which are making my life hell. I am attempting to add a simple Lambert light to my textured cube I have in a VBO. Inside this VBO I store the texture coords, vertices, indices, and normals. Everything shows up well enough on the screen, but now I want to put these normals to use with lighting. The old way seemed to use these methods in GLSL (normal = gl_NormalMatrix * gl_Normal;) however, these functions are now deprecated as I understand. Currently the cube I am rendering is too dark in some areas while rotating the cube. Does anyone think they understand the problem going on here is? I can post more code from my OpenGL app if required, here are the pics and the GLSL code.
biggrin.gif
Pictures of Cube Rotating

Vertex Shader.
#version 330

uniform mat4 viewMatrix, projectionMatrix, modelMatrix;

in vec4 in_Position;
in vec3 in_Normal;
in vec2 in_UV;

out vec2 vfTexcood;
out vec3 vfLightDirection;
out vec3 vfNormal;

void main()
{
gl_Position = (projectionMatrix * viewMatrix * modelMatrix) * in_Position;
vfTexcood = in_UV;
vec3 fvLightPosition = vec3(-100.0f, 100.0f, -100.0f);

vec4 objectPosition = modelMatrix * in_Position;
vfLightDirection = ( modelMatrix * vec4(fvLightPosition, 1)).xyz - objectPosition.xyz;

vfNormal = in_Normal;
}


Fragment Shader


#version 330

uniform sampler2D textureMap;
uniform sampler2D bumpMap;

in vec2 vfTexcood;
in vec3 vfLightDirection;
in vec3 vfNormal;

out vec4 out_Color;


void main()
{
float facingRatio = dot(normalize(vfNormal), normalize(vfLightDirection));

vec4 myColor = vec4(0.5f, 0.4f, 0.7f, 1.0f);

out_Color = texture2D( textureMap, vfTexcood) * facingRatio * myColor;
}

Share this post


Link to post
Share on other sites
Advertisement
You multiply the light by the modelview matrix (so the normal is relative to the viewer) but you dont do that with the normal (it is in world space).

Share this post


Link to post
Share on other sites
I understand, but how do you do this in GLSL? I have tried this..


vfNormal = in_Normal * mat3(modelMatrix);

but, I think the problem is that I am casting it down to mat3 and is messing it up. Do my normals need to be vec4 datatypes?

Also, what is the gl_NormalMatrix and gl_Normal anyway? I think it would help if I understood what data are in these.rolleyes.gif

Share this post


Link to post
Share on other sites
Heres an explanation of normal matrix:

http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/

If I remember correctly the normal matrix is the same as the model matrix, except if you do non-uniform scaling, then it has to do some correction or the normal will be incorrect. But you can review the link to be sure.

I think for vfNormal it should be "mat3(modelMatrix) * in_Normal", not the reverse like you have it.

Share this post


Link to post
Share on other sites
vfNormal = mat3(modelMatrix)*in_Normal;

just like your other calls. The vector is always on the right side.


gl_NormalMatrix and gl_Normal anyway? ------gl_Normal is just the input. So it is the raw data untransformed that you sent with your VBO. gl_NormalMatrix is basically the same as the modelview matrix.

Share this post


Link to post
Share on other sites
gl_NormalMatrix is technically the transposed inverse of the 3x3 part of the modelview matrix. So using the CML maths library to calculate it on the cpu before sending it to the shader:

[source lang="cpp"]
// (mv_matrix is the modelview matrix)

n_matrix = cml::matrix33f_c(
mv_matrix(0,0), mv_matrix(0,1), mv_matrix(0,2),
mv_matrix(1,0), mv_matrix(1,1), mv_matrix(1,2),
mv_matrix(2,0), mv_matrix(2,1), mv_matrix(2,2));
n_matrix.inverse();
n_matrix.transpose();

// send n_matrix to shader...
[/source]

IMHO, it's better to do this properly to begin with instead of hacking it with the modelview matrix, so it won't suddenly break when you change something apparently unrelated later.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!