Original Post
hi, i have a problem with my per pixel lighting shader in cg that drives me crazy. it seems that the specular light is independent from the eye position and i can't figure out why that is the case. i do all calculations in eye space. maybe someone of you has an idea. here is the code to set the parameters.
GLfloat lp[4];
glGetLightfv(light.lightNumber, GL_POSITION, lp);
cgGLSetParameter4fv(cgLightPos, lp);
glPushMatrix();
glTranslatef(object[visibleObjects[j]].position.x, object[visibleObjects[j]].position.y, object[visibleObjects[j]].position.z);
if(extensions.vertexShader)
{
cgGLSetStateMatrixParameter(cgModelView, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetStateMatrixParameter(cgModelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetStateMatrixParameter(cgInverseTranspose, CG_GL_MODELVIEW_MATRIX , CG_GL_MATRIX_INVERSE_TRANSPOSE);
}
renderObject(visibleObjects[j]);
glPopMatrix();
this is my vertex shader
void main(float4 position: POSITION,
float3 normal: NORMAL,
out float4 oPosition: POSITION,
out float4 oObjPosition: TEXCOORD0,
out float3 oNormal: TEXCOORD1,
uniform float4x4 modelViewProj,
uniform float3x3 inverseTranspose,
uniform float4x4 modelView)
{
oPosition = mul(modelViewProj, position);
oObjPosition = mul(modelView, position);
float3 N = mul(inverseTranspose, normal);
oNormal = N;
}
and this my fragment shader
void main(float4 position: POSITION, //clip space coordinates
float4 pos: TEXCOORD0,
float3 normal: TEXCOORD1,
out float4 oColor: COLOR,
uniform float3 lightPos,
uniform float3 lightColor,
uniform float3 diffuseColor,
uniform float3 specularColor,
uniform float shiny)
{
float3 P = pos.xyz;
// diffuse
float3 N = normalize(normal);
float3 lightVec = normalize(lightPos - P);
float dotProduct = max(dot(N, lightVec),0);
float4 diffuse = float4(diffuseColor*lightColor*dotProduct, 1);
// specular
float3 H = normalize(reflect(lightVec, N));
float specLight = pow(max(dot(normalize(P),H),0), shiny);
if (dotProduct <= 0) specLight = 0;
float4 specular = float4(specularColor*lightColor*specLight, 1);
if (dotProduct <= 0)
oColor = float4(0,0,0,1); //amb;
else
oColor = (diffuse + specular);
}
I simple use normalize(P) as the eye-vector because - if i understood it correctly - the eye should be at the origin of the eye-coordinate-system. thanks in advance! [Edited by - w_poons on October 17, 2005 8:25:48 AM]