[HLSL] Reflect function and specular light.

Started by
2 comments, last by Spa8nky 14 years, 3 months ago
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
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


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
That makes sense, thank you.

This topic is closed to new replies.

Advertisement