Hello,
I have a problem with PPL - when I change my camera position (I use gluLookAt), lighting gets wrong.
Download
this and use N/M, U/I or J/K to move the camera and to see what I mean.
Any ideas what am I doing wrong? I'm doing lighting in eye-space.
Shaders:
//
// Vertex Program
//
void vp_main (uniform float4x4 modelviewprojmatrix,
uniform float4x4 modelviewmatrix,
uniform float4x4 inv_transpose,
float4 pos : POSITION,
float3 normal : NORMAL,
out float3 out_norm : TEXCOORD0,
out float3 out_vpos : TEXCOORD1,
out float4 out_pos : POSITION)
{
out_pos = mul (modelviewprojmatrix, pos); // Object Space to Clip Space
out_vpos = mul (modelviewmatrix, pos).xyz; // Object Space to Eye Space
out_norm = mul ((float3x3) inv_transpose, normal); // Object Space to Eye Space
}
//
// Fragment Program
//
void fp_main (uniform float3 material,
uniform float3 light_ambient,
uniform float3 light_diffuse,
uniform float3 light_specular,
uniform float shine,
uniform float3 light_pos,
uniform float3 eye_pos,
float3 normal : TEXCOORD0,
float3 vertex_pos : TEXCOORD1,
out float4 out_color : COLOR)
{
// Ambient Component
float3 ambient_l = material * light_ambient;
// Diffuse Component
float3 N = normalize (normal);
float3 L = normalize (light_pos - vertex_pos);
float3 diffuse_l = material * light_diffuse * max (dot (N, L), 0.0);
// Specular Component
float3 V = normalize (eye_pos - vertex_pos);
float3 H = normalize (V + L);
float3 specular_l = material * light_specular * pow ( max (dot (N, H), 0.0), shine);
if (dot (N, L) <= 0.0) { specular_l = 0.0; }
out_color.rgb = ambient_l + diffuse_l + specular_l;
out_color.a = 1.0;
}
Render function:
gluLookAt (cp[0], cp[1], cp[2], cl[0], cl[1], cl[2], 0, 1, 0);
// Update light/eye position
cgGLSetParameter3fv (light_pos, l_p);
cgGLSetParameter3fv (eye_pos, cp);
// Enable Shaders
cgGLEnableProfile (cgProfileVertex); cgGLBindProgram (cgProgramVertex);
cgGLEnableProfile (cgProfileFragment); cgGLBindProgram (cgProgramFragment);
RenderSurface (...);
I pass matrices in RenderSurface() after all transformations:
cgGLSetStateMatrixParameter (modelviewprojmatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetStateMatrixParameter (modelviewmatrix, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetStateMatrixParameter (invtranspose, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);