As MJP said correctly, the way to approach this would be to use deferred rendering.
One way that you could check which lights are "important", is using one of my previous posts, to you actually:
From seeing your comments, I can easily see that you aren't happy about the performance
As other people recommended, you can check whether the object is affected by the light, here's how with a Point Light!
Bounding Spheres
Bounding Spheres is if you were to collect all the vertices in the mesh and make a sphere encapsulate them, like this:
RABBITS!
To get this result, you need two things:
- The center of the mesh
- The radius of the mesh from the center
Calculating the center:
- Declare the variable
- Loop over all your vertices and add them to the center
- Divide the center by the number of vertices.
- THAT'S IT!
D3DXVECTOR3 center = D3DXVECTOR3(0, 0, 0);
for(int v = 0; v < mVertices.size(); v++ )
{
center += mVertices[v];
}
center /= mVertices.size();
Calculating the radius: (With the center)
- Declare the variable
- Loop over all the vertices
- Get the distance between the vertices and the center
- Find the greatest length
float radius = 0.0f;
for (int v = 0; v < mVertices.size(); v ++)
{
D3DXVECTOR3 diff = mVertices[v] - center;
float length = sqrtf(D3DXVec3Dot(&diff, &diff));
if (length > radius)
radius = length;
}
To visualize the result, you just need to translate a sphere (or stuff) to this location: sphere->Translate(mesh->Position + mesh->Center), and also scale it by the meshes radius.
Testing for collision between Sphere-Sphere
As you know the point light has a radius, and so does, hopefully, the mesh with these calculations. To see if they collide, just do the following
- Find the vector between the mesh center and the point light's position.
- Get the length of that vector
- Check whether that length is smaller than both the point light's radius and the mesh's radius.
- If true, they collide!!!
D3DXVECTOR3 mDistance = mesh->center + pointLight.Position;
float d = sqrtf( D3DXVec3Dot( &mDistance, &mDistance) );
if (d < (mesh->BoundingSphere.radius + pointLight.Range) )
// They collide!!!
In other words, if they collide, the light will affect the mesh.
Mini Tutorials!
And "important" lights are those who affect the mesh, and for example: a global light, that lits everything, you can't exclude that.
With the sun (by belfegor ), where you said: "some important lights MIGHT not affect the mesh", are you trying to approach shadows?