Jump to content
  • Advertisement
Sign in to follow this  
IvanMilles

Specular of GLSL point light

This topic is 4329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been banging my head on the keyboard for a couple of days now, perhaps one of you could point me in the right direction here. I have a specific light setup (one positional fill light and one directional kicker light) that works fine in fixed functionality, but that I can't seem to mimic on the shader path. Here's the shader (no fragment part)
void main()
{	
	const int kFillLight = 0, kKickLight = 1;
	
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

	vec3 normal = gl_NormalMatrix * gl_Normal;
	vec3 vertex = (gl_ModelViewMatrix * gl_Vertex).xyz;

	vec3 fillLightDir = normalize (gl_LightSource[kFillLight].position.xyz - vertex);
	vec3 kickLightDir = normalize (gl_LightSource[kKickLight].position.xyz);

	float fillNDotL = max (0.0, dot (normal, fillLightDir));
	float kickNDotL = max (0.0, dot (normal, kickLightDir));
	
	vec3 globalAmbient = gl_LightModel.ambient.rgb * gl_FrontMaterial.ambient.rgb;

	vec4 fillDiffuse = gl_FrontMaterial.diffuse * gl_LightSource[kFillLight].diffuse;
	vec4 kickDiffuse = gl_FrontMaterial.diffuse * gl_LightSource[kKickLight].diffuse;

	vec3 fillSpecular = vec3(0), kickSpecular = vec3(0);

	if (kickNDotL > 0.0)
	{
		vec3 halfVector = normalize (gl_LightSource[kKickLight].halfVector.xyz);
		float nDotHV = max (0.0, dot (normal, halfVector));
		kickSpecular = gl_FrontMaterial.specular.rgb * gl_LightSource[kKickLight].specular.rgb * pow (nDotHV, gl_FrontMaterial.shininess);
	}
	if (fillNDotL > 0.0)
	{
		vec3 halfVector = normalize (fillLightDir - normalize(vertex));
		float nDotHV = max (0.0, dot (normal, halfVector));
		fillSpecular = gl_FrontMaterial.specular.rgb * gl_LightSource[kFillLight].specular.rgb * pow (nDotHV, gl_FrontMaterial.shininess);
	}

	vec3 ambient =	globalAmbient;
	vec3 diffuse =	kickDiffuse.rgb * kickNDotL + fillDiffuse.rgb * fillNDotL;
	vec3 specular = kickSpecular.rgb + fillSpecular.rgb;
	
	gl_FrontColor = vec4 (ambient + diffuse + specular, gl_FrontMaterial.diffuse.a);
} 
The problem is that the specular reflection from the point light (fill light, calculated in the fillNDotL > 0.0 clause) seems to come from the wrong direction. I've pretty much confirmed this: if I turn the specular reflection of the point light down to zero, the rendering is identical. If I make the point light a directional light, the rendering is identical. Hence, it is the point/specular combination that is at fault. I'm unsure if the eye vector is correctly calculated - is it just -vertex even if I have translated the entire scene? Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try to compute your own half vector. From my experience gl states arent supported very well. You can check it by mapping gl_LightSource[kKickLight].halfVector.xyz to a color range and displaying it directly.

Share this post


Link to post
Share on other sites
Thanks, but that's what I'm doing with the problematic light. The kick light is fine, but the fill light is definitely off by a lot. I can't use the GL computed half-vector because it assumes that the light is directional... So if you could help me with finding the proper eye vector to build the half-vector..?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!