# [HLSL] Reflect function and specular light.

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

## 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 on other sites
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 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 on other sites
That makes sense, thank you.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
14
5. 5

• 12
• 9
• 17
• 18
• 9
• ### Forum Statistics

• Total Topics
632597
• Total Posts
3007329

×