(HLSL) Spot light calculations

Started by
9 comments, last by the_edd 13 years, 6 months ago
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?

Spot light shader
Advertisement
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.
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?
Quote:Original post by Zosimas
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?


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...
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]
Quote:Original post by Zosimas
This 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.
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!
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.
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.
Moving to the Graphics Programming forum.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement