I am working on 2 very basic GLSL shaders. The first is a basic shader that has a diffuse + specular component. My grasp on the mathematics behind specular lighting is a bit shaky and my shader doesn’t seem to work at all right now (for reasons i do not know).
Here’s the code
//VERTEX
uniform mat4 projection, modelview, normalMat;
uniform int mode;
varying vec4 forFragColor;
const vec3 diffuseColor = vec3(0.5, 0.1, 0.2);
const vec3 specColor = vec3(0.9, 0.9, 0.9);
const float shine = 1.9;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec3 lightPos = vec3(0,0,0);
// all following gemetric computations are performed in the
// camera coordinate system (aka eye coordinates)
vec3 normal = vec3(gl_NormalMatrix * gl_Normal);
vec4 vertPos4 = gl_ModelViewMatrix * gl_Vertex;
vec3 vertPos = vec3(vertPos4);
vec3 lightDir = normalize(lightPos - vertPos);
float lambertian = max(dot(lightDir,normal), 0.0);
float specularCoefficient = 0.0;
float diffuseCoefficient = max(0.0, dot(normal, lightDir));
if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(lightDir, reflect(-lightDir, normal))), shine);
vec3 specular = specularCoefficient * specColor
vec3 linearColor = lambertian * (diffuseColor + specular);
forFragColor = vec4(lambertian*diffuseColor , 1.0);
}
//FRAGMENT
precision mediump float;
varying vec4 forFragColor;
void main()
{
gl_FragColor = forFragColor;
}
I’m sure there’s just some silly little mistake somewhere in there that’s breaking it but I can’t figure it out!
The second shader is a basic ‘toon’ shader.
//VERTEX
varying vec3 normal;
void main()
{
normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}
//FRAGMENT
varying vec3 normal;
void main()
{
vec4 color;
vec3 norm = normalize(normal);
vec3 lightPos = vec3(0,0,0);
vec4 vertPos4 = gl_ModelViewMatrix * gl_Vertex;
vec3 vertPos = vec3(vertPos4);
vec3 lightDir = normalize(lightPos - vertPos);
float intensity = dot(lightDir, norm);
if (intensity > 0.8)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else
color = vec4(0.4,0.2,0.2,1.0);
gl_FragColor = color;
}
Both shaders are currently broken. Could anyone shed some light on why this might be?