Hi Guys,
I have been struggling for a number of hours trying to make a directional (per fragment) lighting shader.
I have been following this tutorial in the 'per fragment' section of the page - http://www.learnopengles.com/tag/per-vertex-lighting/ along with tutorials from other sites.
This is what I have at this point.
// Vertex shader
varying vec3 v_Normal;
varying vec4 v_Colour;
varying vec3 v_LightPos;
uniform vec3 u_LightPos;
uniform mat4 worldMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
void main()
{
vec4 object_space_pos = vec4(in_Position, 1.0);
gl_Position = worldMatrix * vec4(in_Position, 1.0);
gl_Position = viewMatrix * gl_Position; // WV
gl_Position = projectionMatrix * gl_Position;
mat4 WV = worldMatrix * viewMatrix;
v_Position = vec3(WV * object_space_pos);
v_Normal = vec3(WV * vec4(in_Normal, 0.0));
v_Colour = in_Colour;
v_LightPos = u_LightPos;
}
And
// Fragment
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec4 v_Colour;
varying vec3 v_LightPos;
void main()
{
float dist = length(v_LightPos - v_Position);
vec3 lightVector = normalize(v_LightPos - v_Position);
float diffuse_light = max(dot(v_Normal, lightVector), 0.1);
diffuse_light = diffuse_light * (1.0 / (1.0 + (0.25 * dist * dist)));
gl_FragColor = v_Colour * diffuse_light;
}
If I change the last line of the fragment shader to 'gl_FragColor = v_Colour;' the model (a white sphere) will render to the screen in solid white, as expected.
But if I leave the shader as is above, the object is invisible.
I am suspecting that it is something to do with this line in the vertex shader, but am at a loss as to what is wrong.
v_Position = vec3(WV * object_space_pos);
If I comment the above line out, I get some sort of shading going on which looks like it is trying to light the subject (with the normals calculating etc.)
Any help would be hugely appreciated.
Thanks in advance