# (HLSL) Spot light calculations

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

## Recommended Posts

As you can see in the image below, I made a spot light shader
that looks pretty good.

Now, I have an interesting question. In real life when the
atmosphere is foggy, the space inside the red lines in the image
becomes brighter. I want to find a way to calculate (in my pixel
shader) if a pixel is in this area, to make a more realistic spot light.

Of course I have all the necessary vectors that can help:
WorldView
WorldNormal
LightPosition
LightDirection
EyePosition

Any suggestions?

##### Share on other sites
A simple extension to your pixel/fragment shader would be to use a ray/cone intersection. The ray joins the world position of the point being drawn to the world position of the eye.

Finding code to do that should be easy enough to find.

##### Share on other sites
Yes, the ray is obviously the one that you explained, but how am I going to calculate this cone/triangle or whatever in my pixel shader?

##### Share on other sites
Quote:
 Original post by ZosimasYes, the ray is obviously the one that you explained, but how am I going to calculate this cone/triangle or whatever in my pixel shader?

The cone is implicit. Presumably, you have the apex point, apex angle and axis of the cone somewhere? (Or an equivalent representation). After all, you somehow calculated that each shaded point was inside the cone in order to light it...

##### Share on other sites
This is the pixel shader code I use to calculate if a pixel is in the cone:

float3 Ln = normalize(IN.LightVec);float ConeDot;if (LightDirTarg.w == 0) { /*if w==0 : Direction //if w==1 : Target*/	ConeDot = dot(Ln,normalize(LightDirTarg.xyz));} else {	ConeDot = dot(Ln,normalize(LightDirTarg.xyz-LightPosition.xyz));}if (ConeDot <= ((LightConeAngle / 360) * 2) - 1) {/*In light*/} else {/*In shadow*/}

As you said I only have the LightPosition, the LightDirection and the cone angle.
And of course my pixel position and normal, and the eye position.

The problem is that now enters a new factor, the eye direction that confuse me.
I realy can't find a way to calculate it! [totally]

##### Share on other sites
Quote:
 Original post by ZosimasThis is the pixel shader code I use to calculate if a pixel is in the cone:*** Source Snippet Removed ***As you said I only have the LightPosition, the LightDirection and the cone angle.And of course my pixel position and normal, and the eye position.The problem is that now enters a new factor, the eye direction that confuse me.I realy can't find a way to calculate it! [totally]

You have the pixel position. You have the eye position. The eye direction is the vector joining the two i.e.
surface_to_eye = eye_position - pixel_position.

This is assuming eye_position and pixel_position are in the same coordinate space, of course.

##### Share on other sites
Sorry, this is my fault. I didn't mean that.[embarrass]
I can calculate this direction and any other direction, distance or whatever else
I may need. But I can't find the way to combine them to come to a conclusion.
The complex mathematics are my problem!!!
Is this cone between my EyePosition and my pixel? That's the question!

##### Share on other sites
If I were to start talking about dot products, cross products and solutions to quadratic equations, would you understand me?

If not, it might be better to start with one of a number of primers on basic 3D geometry.

##### Share on other sites
I think I know all the basic maths about vectors and matrices.
You can make a start and we'll see how it goes, if you want. :)

Anyway this is not only for me, this can help others that may have this problem, too.

##### Share on other sites
Moving to the Graphics Programming forum.

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 11
• 21
• 12
• 11
• 11
• ### Forum Statistics

• Total Topics
631405
• Total Posts
2999888
×