Hi,
I have a problem with my GLSL Per Pixel Shader. I used the following sources for creating it:
Lightouse3D and
Phong for Dummies
Hmm, it is really hard to say what's wrong, so I put a small demo together (with one lightsource [the yellow cube] and a character).
But I'll try to say what's not working: The light completly ignores my character rotation. So, if I stand in front of the light, the frontside of my char is brightened, but if I rotate about 180°, the frontside is still brightened.
Really strange. So, please help.
Here are my shader:
varying vec3 vLightDir;
varying vec3 vNormal;
varying float dist;
varying vec3 vHalf;
uniform vec3 vLightPos;
uniform vec3 vPlayerPos;
uniform vec3 vCameraPos;
vec3 vPos;
vec3 vEye;
void main()
{
vNormal = normalize(gl_Normal /* gl_NormalMatrix*/) * -1.0;
vEye = (gl_Vertex * gl_ModelViewMatrix).xyz ;
//vEye = (gl_Vertex * gl_ModelViewMatrix).xyz;
vPos = (vLightPos - vPlayerPos) - vEye;
vLightDir = normalize(vPos);
vHalf = normalize((vLightPos + vEye) * 0.5);
dist = length(vPos);
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
varying vec3 vLightDir;
varying vec3 vNormal;
varying vec3 vHalf;
varying float dist;
uniform sampler2D materialmap;
uniform vec3 vLightColor;
uniform float invradius;
vec3 vLight;
vec3 vHalfAngle;
vec3 vMaterial;
vec3 n;
float diffuse;
float specular;
float att;
float dist2;
void main()
{
vMaterial = texture2D(materialmap, gl_TexCoord[0].st).xyz;
n = normalize(vNormal);
vLight = normalize(vLightDir);
vHalfAngle = normalize(vHalf);//normalize((vLight + n) * 0.5);
diffuse = max( dot(n, vLight), 0.0);
if(diffuse > 0.0)
{
specular = pow( max( dot(n, vHalfAngle), 0.0), 8.0);
att = max(1.0 - pow(dist * invradius, 2.0), 0.0);
vec3 sMat = vec3(1.0, 1.0, 1.0);
vec3 dMat = vec3(1.0,1.0,1.0);
vMaterial += vLightColor * (diffuse + specular) * att;
}
gl_FragColor.xyz = vMaterial;
}
Here's a small demo to demonstrate the Problem:
~ 6MB
Use W/A/S/D/Q/E for Movement and Cursorkeys for Camera Movement.