Skip to main content
gamedev.net
🔒 Locked

per pixel lighting problem

Started by w_poons 4 replies 1.1k views
Original Post
w_poons
w_poons
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]
SimmerD
SimmerD
Your specular is wrong. For one, you seem to be mixing up the H-style (Blinn) specular with the R-style ( Phong ) specular.

phong specular goes like so

float3 eye = normalize( mul( modelviewmatrix, pos ) );float3 r = reflect( eye, n );float3 specular_intensity = pow( dot( eye, r ), shininess );etc.
w_poons
w_poons
thanks! do i understand you correctly, that phong specular is depending on the dot-product betwenn the eye-vector (simple the vertex position transformed by the modelview matrix, because we are in eye-space) and the reflected eye-vector? i miss the light position somewhere.

matches81
matches81
is lightPos in view-space?
If I´m correct, you should also use -P as the vector for the specular calculation, because you need the vector from the point to the eye, not from eye to point. Don´t know if that makes a difference, though...
So your specular intensity should be:

float3 P = (normalize(-ObjectViewPos)).xyz;float3 L = (normalize(LightViewPos - ObjectViewPos)).xyz;float3 R = reflect(L,N);float spec_int = max(0,dot(R,P));


at least that is what it´s supposed to be when all the positions you have are in the same space (view-space in your case). N is the normal vector for that point, also in view-space.
SimmerD
SimmerD
You're right, my mistake. Either reflect the eye through the normal and dot with the light, or reflect the light through the normal and dot with the eye vector.
w_poons
w_poons
Quote:
Original post by matches81
is lightPos in view-space?
If I´m correct, you should also use -P as the vector for the specular calculation, because you need the vector from the point to the eye, not from eye to point. Don´t know if that makes a difference, though...


if I take -P the specular spot is not visible anymore. that could be because of the max function i use.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.