Skip to main content
gamedev.net
🔒 Locked 🎯 Unreal

Phong shader problem

Started by biggoron 1 reply 800+ views
Original Post
biggoron
biggoron
I just finished writing a phong shader to handle directional and positional light from a single source but it's not working. The fragments are only receiving the ambient light. I have tried several directions but nothing is working. What I need is for someone to proof read them to make sure they are correct. If they are, it could be a problem with the way I'm setting light and material parameters. Vertex shader
varying vec3 normal, eyeVec, lightDir;

void main(void)
{
	vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
	
	normal = normalize(gl_NormalMatrix * gl_Normal);
	eyeVec = -vVertex;

	lightDir = gl_LightSource[0].position.xyz;

	// Calculate positional direction
	if(gl_LightSource[0].position.w == 1.0) {
		lightDir += eyeVec;
	}

	gl_Position = ftransform();
}



Fragment shader
varying vec3 normal, eyeVec, lightDir;

void main(void)
{
	vec4 colour;
	vec4 amb;
	float lambert;

	amb = (gl_LightSource[0].ambient * gl_FrontMaterial.ambient) + (gl_LightModel.ambient * gl_FrontMaterial.ambient);

	vec3 N, L;
	N = normalize(normal);
	L = normalize(lightDir);
	lambert = dot(N, L);

	colour = amb;

	// In light, calculate diffuse and specular terms
	if(lambert > 0.0) {
		vec4 diffuse, specular;
		vec3 R = reflect(-L, N);

		diffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * lambert;
		specular = gl_LightSource[0].specular * gl_FrontMaterial.specular * pow(max(dot(R, normalize(eyeVec)), 0.0), gl_FrontMaterial.shininess);

		colour += diffuse + specular;
	}

	gl_FragColor = colour;
}



Thanks. [Edited by - biggoron on January 23, 2008 4:43:15 PM]
ma_hty
ma_hty
vec3 R = reflect(-L, eyeVec); <<<<<

You should reflect the light vector with normal (not with eye vector).
biggoron
biggoron
Thanks for pointing that out, you saved me making a future thread ^_^

Topic Locked

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

Sign in to reply to this topic.