Hello, I am trying to get improved specular light on a textured sphere with shaders and so far the specular part works great, but I have lost most of my material colors in the process. Here are screenshots to illustrate the problem. The sphere material has blue diffuse and black ambient.
pic 1 is without shader, pic 2 is with shader. Its hard to tell but the specular light looks way better with the shader. But I cant get the material colors to show up better, they are there but very faint. I want sphere to look like pic 1 but with the better specular. here is my shader code any help would be greatly appreciated, Ive been working on this problem for 3 days and I cant seem to get it right. Im pretty sure the problem is in last line of fragment shader “gl_FragColor=…” Im probably computing colors wrong.
shader.vert
varying vec3 normal,lightDir;
varying vec4 pos;
varying vec4 rawpos;
void main() {
normal = normalize(gl_NormalMatrix * gl_Normal);
lightDir = normalize(vec3(gl_LightSource[0].position));
gl_Position = ftransform();
pos = gl_ModelViewMatrix * gl_Vertex;
rawpos = gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
}
shader.frag
varying vec3 normal;
varying vec4 pos;
varying vec3 lightDir;
uniform sampler2D tex;
void main() {
vec4 color = gl_FrontMaterial.diffuse;
vec4 colorAmbient = gl_FrontMaterial.ambient;
vec4 matspec = gl_FrontMaterial.specular;
float shininess = gl_FrontMaterial.shininess;
vec4 lightspec = gl_LightSource[0].specular;
vec4 lpos = gl_LightSource[0].position;
vec4 s = -normalize(pos-lpos);
vec3 light = s.xyz;
vec3 n = normalize(normal);
vec3 r = -reflect(light, n);
r = normalize(r);
vec3 v = -pos.xyz;
v = normalize(v);
vec4 diffuse = color * max(0.0, dot(n, s.xyz)) * gl_LightSource[0].diffuse;
vec4 ambient = colorAmbient * gl_LightSource[0].ambient;
vec4 specular;
if (shininess != 0.0) {
specular = lightspec * matspec * pow(max(0.0, dot(r, v)), shininess);
} else {
specular = vec4(0.0, 0.0, 0.0, 0.0);
}
vec4 texColor = texture2D(tex,gl_TexCoord[0].st);
vec4 endColor = mix(texColor,diffuse,0.5);
vec4 endColor2 = mix(endColor,ambient,0.5);
//color = (color * texColor);
//gl_FragColor = texColor + diffuse;
// gl_FragColor = vec4(ambient + diffuse+ specular + texColor);
gl_FragColor = endColor2 + specular;
}