Sign in to follow this  

[HLSL] Reflect function and specular light.

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
This image shows what reflect() does

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this