Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[HLSL] Reflect function and specular light.

This topic is 3270 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

Can anyone explain why the reflect funtion in HLSL produces specular light in the opposite direction to what it should be? Here I have my code for producing specular lighting:
	/* ===============================================
	 * --[Specular Light]-----------------------------
	 * ===============================================
	 * I = Si * Sc * (R.V)^n
	 * 
	 * Where:
	 *
	 * Si = Specular Intensity
	 * Sc = Specular Colour
	 * R = Reflection Vector = 2 * (N.L) * N-L  
	 *	   reflect(L, N) = L - 2 * N * dot(L, N) [HLSL Built-in Function]
	 * V = View Vector = Camera.WorldPosition - Pixel.WorldPosition
	 * n = Shininess factor
	 *
	 */
	 
	float Si = 1.0f;
	float4 Sc = float4(1.0f, 0.0f, 0.0f, 1.0f);
	float3 R = 	reflect(-L, N);					// normalize(2 * NdotL * N - L);		
	float RdotV = dot(V, R);
	float n = 15;
	float4 Specular_Light = Si * Sc * pow(saturate(RdotV), n);

If I use reflect(L, N) for my reflection vector then the reflection occurs in the opposite direction to the light direction. However, if I reverse the light direction in the reflect function (reflect(-L, N)) then the specular light is in the correct place. The light direction vector is correct for diffuse lighting but requires reversing for the reflect function. Why is this? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Yep, because you want to reflect the vector from the light to the surface, and typically the light vector is pointing from the surface to the light, so it needs to be negated


Share this post


Link to post
Share on other sites
This image shows what reflect() does

http://media.photobucket.com/image/reflect%20vector/jnosof/reflexion.png

In that image, reflect(R,N) returns L, which is exactly what it should return

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!