PDA

View Full Version : GLSL specular lighting



Jacke
02-24-2008, 02:41 PM
Hi, I've managed to implement the diffuse lighting but I can't get the specular lighting right. I've the following code:

vertex shader

varying vec3 normal, eyeVec;
void main()
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;

normal = normalize(gl_NormalMatrix * gl_Normal);

eyeVec = -vec3(gl_ModelViewMatrix * gl_Vertex);
}

fragment shader

varying vec3 normal, eyeVec;

vec4 light0 ()
{
vec4 color;

vec3 lightDir = normalize(vec3(gl_LightSource[0].position));

vec3 eyeVecNormal = normalize(eyeVec);

vec3 reflectVec = normalize(-reflect(lightDir, normal));

vec4 ambient = gl_LightSource[0].ambient * gl_FrontMaterial.ambient;

vec4 diffuse = gl_LightSource[0].diffuse * max(dot(normal,lightDir),0.0) * gl_FrontMaterial.diffuse;

vec4 specular = gl_FrontMaterial.specular * pow(max(dot(reflectVec, eyeVecNormal), 0.0), gl_FrontMaterial.shininess);

color = ambient + diffuse + specular;

return color;
}

void main()
{
gl_FragColor = light0;
}

When I added the specular light, instead of making a shiny spot in the middle of the object, the spot in the middle is unchanged and the rest is made black. It's a directional lightsource, the lightsource itself doesn't have any position. Anyone know what the problem is?

Jacke
02-25-2008, 08:29 AM
This is the result I get, doesn't seem right..
http://www-und.ida.liu.se/~jacha895/screenshot_specular.png

Komat
02-25-2008, 04:33 PM
To what value is the gl_FrontMaterial.shininess set? The pow operation is undefined for pow( 0.0, <= 0.0 ) situations so in that case the calculation result can be anything.

Zengar
02-25-2008, 05:19 PM
That is one nice cartoon-shading effect ))

Jacke
02-25-2008, 06:08 PM
I tried to replace gl_FrontMaterial.shininess with 0.3*gl_FrontMaterial.shininess before but it didn't make any difference.

Komat
02-26-2008, 01:49 AM
I tried to replace gl_FrontMaterial.shininess with 0.3*gl_FrontMaterial.shininess before but it didn't make any difference.
Well if it is zero or negative, the multiplication will not change that.

Jacke
02-26-2008, 04:24 PM
The weird thing is that I tried pasting code from different tutorials about glsl specular lighting on the web but the result is always as in the picture above or there is no difference from diffuse lighting. Must be something wrong with my .c file but that's written by the examiner of the course I'm taking and we are not supposed to change that one, just the .fs and the .vs.