GLSL Shader Help on normals

Started by
5 comments, last by bgoldbeck 12 years, 7 months ago
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;
}

[size="2"]More sprinkles!
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).

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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
[size="2"]More sprinkles!
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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
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.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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.
Thank you all for your help and guidance. I have got it working! Now for my next step : a phong bumpmap shader.cool.gif
[size="2"]More sprinkles!

This topic is closed to new replies.

Advertisement