• Advertisement
Sign in to follow this  

Camera position in spotlight cone

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using DX9 api for my hobby graphics projects. To cut down number of pixels proessed i need to find if my camera position is inside cone (spotlight volume) to be able to switch face culling to/from CW/CCW. I have used google but didn't find satisfactory answers so i came here in hope that someone will help me.Thanks for your time.

Share this post


Link to post
Share on other sites
Advertisement
First, compute the distance from the camera to the spotlight. If this distance is greater than the range of the spotlight, then you are not in the cone.

Second, take that Vector that you used for the distance, and dot it with the facing vector of the spotlight. If that value is LESS than the cosine of the angle of the cone, you are OUTSIDE the cone. Else you are in.


bool IsCameraInSpotlightCone
{
Vector toCamera = Camera.Position - Spotlight.Position;
float dist = toCamera.Length();
if (dist > Spotlight.Range)
{
return false;
}
toCamera /= dist;
float cosAngle = dot(toCamera, Spotlight.Direction);
if (cosAngle < Spotlight.cosAngle)
{
return false;
}
return true;
}

Share this post


Link to post
Share on other sites
Looks like i am missing spotlight cosAngle to be able to test that. I have radius for cap, position, range and direction. Could you please help me how to calculate cone angle given other info i have.

Thanks for your time.

Share this post


Link to post
Share on other sites
By 'radius for cap' I assume you mean the width of the light at the maximum range?
If so, the angle of the cone is simply given by our old nemesis, Trigonometry.

Tan(angle) = Opposite / Adjacent. In our case, that translates to Tan(angle) = CapRadius/Range

So to get our cosAngle, we do

float angle = atan(Spotlight.CapRadius / Spotlight.Range);
Spotlight.cosAngle = cos(angle);


I recommend just calculating that value once for each spotlight and storing it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement