Sorry for this potentially nooby question, but I just entered the world of shaders (GLSL), which is completely new to me and introduced me to new things like matrices.
I updated this topic as I stumbled from one problem into another
I now think I got the correct normals
Code: Select all
normal = vec3(mInvWorld * vec4(gl_Normal, 0.0));
normal = normalize(normal);
(However, I do not know, why this is working and gl_NormalMatrix is not...?)
I think I also managed to get the correct transformations for the light and vertex positions. My test:
Code: Select all
Vertex Shader File:
...
position = vec3(gl_Vertex * mTransWorld);
...
Fragment Shader File:
void main (void)
{
vec3 lightPos = vec3(mTransWorld * vec4(mLightPos, 1.0));
float dist = length(position - lightPos);
float range = 150.0;
float diffusePointLight = float(dist < range);
gl_FragColor = vec4(diffusePointLight);
}
However, if I change this to real diffuse lighting:
Code: Select all
Fragment Shader File:
void main (void)
{
vec3 lightPos = vec3(mTransWorld * vec4(mLightPos, 1.0));
float dist = length(position - lightPos);
float range = 150.0;
vec3 surf2light = normalize(lightPos - position);
vec3 norm = normalize(normal);
float diffusePointLight = max(dot(norm, surf2light), 0.0);
gl_FragColor = vec4(diffusePointLight);
}
The lighting seems to come from about the correct position, but it seems to shine as a spot into a specific direction instead of point lighting everything around... if this makes any sense
Thanks for anyone reading this and willing to help
--------
This is the original post:
--------
Now to my problem.
If I understood correctly (if I did not this whole problem is redundant^^) the normals of faces should be independent from camera position.
In my thinking a face that faces upwards should always have the normal vector (0, 1, 0), correct so far?
If these assumptions are correct I stumbled upon a problem:
The normals in my test program are not.
in my vertex shader I have the following lines:and in my pixel shader the following lines, for testing purposes:Code: Select all
//... varying vec3 normal; //... normal = normalize(gl_NormalMatrix * gl_Normal); //...
So, again, if I understood correctly, the faces should have the same color as I fly around with my fps camera.Code: Select all
//... varying vec3 normal; //... vec3 norm = normalize(normal); //... gl_FragColor = vec4(norm, 1.0); //...
as xyz are converted to rgb, faces pointing upwards should always be completely green, right? But they aren't.
Also completely flat faces have different shades of colors ... and change as I fly around...
Is it me misunderstanding something here, or is it me using something in a wrong way? (or something different even^^)
I appreciate any attempts to help me, and I would gladly post more code, if necessary.
Thanks in advance
user-r3
EDIT:
If this topic belongs in the "beginner's help" section, I apologize, I just think this belongs here because the forum states shaders among the topics for advanced help