I'm trying to implement OpenGL-like lighting on GLSL, but I'm facing problems.
When rendered without the shader via the OpenGL pipeline, everything looks like this
So far so good.
But when I use the shader I've made myself, or any of the shaders at Lighthouse3D everything looks like this
My shader code could be anything from the directional light tutorials at Lighthouse3d.com. All I really want is an per-pixel version of the opengl pipeline code. Everything is rendered via the exact same values, but the problem is in the shader and i have no idea what it is.
I've tried to implement it on my own without copying the sourcecode overthere but the result is the same and I can't make any progress. All pointers would be welcome.
Here is how i setup my lights:
vec4 amb_source = {0.040f, 0.040f, 0.040f, 1.0f};
vec4 dif_source = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat light_pos[] = { 0.0, 100.0, 0.0, 0.0};
normalize_vec3(light_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, amb_source);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dif_source);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
and here are my current shaders
vertex
varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
void main()
{
/* first transform the normal into eye space and
normalize the result */
normal = normalize(gl_NormalMatrix * gl_Normal);
/* now normalize the light's direction. Note that
according to the OpenGL specification, the light
is stored in eye space. Also since we're talking about
a directional light, the position field is actually direction */
lightDir = normalize(vec3(gl_LightSource[0].position));
/* Normalize the halfVector to pass it to the fragment shader */
halfVector = normalize(gl_LightSource[0].halfVector.xyz);
/* Compute the diffuse, ambient and globalAmbient terms */
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;
gl_Position = ftransform();
gl_FrontColor = gl_Color;
}
fragment
varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
void main()
{
vec3 n,halfV,viewV,ldir;
float NdotL,NdotHV;
/* The ambient term will always be present */
vec4 color = ambient;
/* a fragment shader can't write a varying variable, hence we need
a new variable to store the normalized interpolated normal */
n = normalize(normal);
/* compute the dot product between normal and ldir */
NdotL = max(dot(n,lightDir),0.0);
if (NdotL > 0.0) {
color += (diffuse) * NdotL;
halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
color += gl_FrontMaterial.specular *
gl_LightSource[0].specular *
pow(NdotHV, gl_FrontMaterial.shininess);
}
gl_FragColor = color*gl_Color;
}
edit:
Its important to notice that in the second picture the patterns you see there follow the patterns visible in the one rendered via OpenGL pipeline. So the normals are ok, but there is something I don't understand with the code.