I am experimenting with GLSL shaders. What I wanted to do is just create a simple shader that replicates the openGL lighting model and I can't seem to do it. I'm sure that this is a very noob question with a simple answer but I can't figure it out.
Here is what the opengl render looks like:
http://i713.photobucket.com/albums/ww140/ogre722/nonshader.png?t=1276329120
And this is what the GLSL render looks like:
http://i713.photobucket.com/albums/ww140/ogre722/shader.png?t=1276329172
Based on this code:
[Vertex Shader]
varying vec4 diffuse;
void main()
{
gl_Position = ftransform();
vec3 normal = gl_Normal;
vec3 lightVector = vec3(0,0,5);
lightVector = gl_LightSource[0].position.xyz - gl_Vertex.xyz;
lightVector = normalize(lightVector);
float nxDir = max(0.0, dot(normal, -lightVector));
diffuse = gl_LightSource[0].diffuse * nxDir;
}
[Fragment Shader]
varying vec4 diffuse;
void main()
{
gl_FragColor = gl_LightSource[0].ambient + diffuse;
}
GLSL Lighting Problem
Looks like one vector has wrong direction. U can do sth like
vec3 lightVector = gl_LightSource[0].position.xyz - gl_Vertex.xyz;
instead
vec3 lightVector = gl_Vertex.xyz - gl_LightSource[0].position.xyz;
vec3 lightVector = gl_LightSource[0].position.xyz - gl_Vertex.xyz;
instead
vec3 lightVector = gl_Vertex.xyz - gl_LightSource[0].position.xyz;
Quote:Original post by _OskaR
Looks like one vector has wrong direction. U can do sth like
vec3 lightVector = gl_LightSource[0].position.xyz - gl_Vertex.xyz;
instead
vec3 lightVector = gl_Vertex.xyz - gl_LightSource[0].position.xyz;
I've corrected that and I am still having the same issue. It seems as though the initial position of the light is being taken into consideration, almost as if it were another independent light source as the other circles the model. Its difficult to see in the screenshots, but the back is always in shadow even after the light has changed position.
I don't think you're doing the lighting calculations correctly.
If your model matrix is anything other than identity, this isn't going to work. Keep in mind gl_Vertex is always the exact same value, no matter how you rotate your head. Thats why your light always lights the same side of the object, because you're not supplying any rotation information into the calculation. The object rotates on the screen because you are rotating gl_Vertex with the model matrix. Your light is defined (I'm guessing) in view space, while your gl_Vertex is in model space, and it isn't legal to perform mathematical operations like this between coordinates in different spaces.
You need to pick what space you want to do your calculations in: view space, world space, model space, these are all valid choices, but every value you use in the calculation must be in the same space.
I think the easiest solution for you for this would be to make these modifications:
The ftransform() function hides all of this from you for the vertices. Try to replace it with the real matrix calculations, it may help you understand better.
Quote:
lightVector = gl_LightSource[0].position.xyz - gl_Vertex.xyz;
If your model matrix is anything other than identity, this isn't going to work. Keep in mind gl_Vertex is always the exact same value, no matter how you rotate your head. Thats why your light always lights the same side of the object, because you're not supplying any rotation information into the calculation. The object rotates on the screen because you are rotating gl_Vertex with the model matrix. Your light is defined (I'm guessing) in view space, while your gl_Vertex is in model space, and it isn't legal to perform mathematical operations like this between coordinates in different spaces.
You need to pick what space you want to do your calculations in: view space, world space, model space, these are all valid choices, but every value you use in the calculation must be in the same space.
I think the easiest solution for you for this would be to make these modifications:
vec3 normal = gl_NormalMatrix* gl_Normal; //Normal from model space to view space)lightVector = gl_LightSource[0].position.xyz - vec4(gl_ModelViewMatrix*gl_Vertex).xyz;
The ftransform() function hides all of this from you for the vertices. Try to replace it with the real matrix calculations, it may help you understand better.
Quote:Original post by karwosts
I don't think you're doing the lighting calculations correctly.Quote:
lightVector = gl_LightSource[0].position.xyz - gl_Vertex.xyz;
If your model matrix is anything other than identity, this isn't going to work. Keep in mind gl_Vertex is always the exact same value, no matter how you rotate your head. Thats why your light always lights the same side of the object, because you're not supplying any rotation information into the calculation. The object rotates on the screen because you are rotating gl_Vertex with the model matrix. Your light is defined (I'm guessing) in view space, while your gl_Vertex is in model space, and it isn't legal to perform mathematical operations like this between coordinates in different spaces.
You need to pick what space you want to do your calculations in: view space, world space, model space, these are all valid choices, but every value you use in the calculation must be in the same space.
I think the easiest solution for you for this would be to make these modifications:vec3 normal = gl_NormalMatrix* gl_Normal; //Normal from model space to view space)lightVector = gl_LightSource[0].position.xyz - vec4(gl_ModelViewMatrix*gl_Vertex).xyz;
The ftransform() function hides all of this from you for the vertices. Try to replace it with the real matrix calculations, it may help you understand better.
Thank you for this explanation, you are correct and your solution fixed the problem I was having, it is greatly appreciated.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement