Skip to main content
gamedev.net
🔒 Locked

Sorting alpha blended objects

Started by smittix Oct 12, 2005 at 8:25 AM 4 replies 500+ views
Original Post
smittix
smittix
I have a series of objects in my level that all use alpha blending (grass, trees, etc..) and from what I understand, I need to sort them by distance from closest to furthest so they do not occlude each other. I can get the distance to each object from the camera and sort them using: sort(Distances.begin(), Distances.end()); This is fine for sorting the distances, but I need the distances to actually know which object is attatched to them. Would anyone know how to do this, or is there a RenderState I can set that will just render alpha objects front to be for me? Thanks, Chris
smittix
smittix
I thought of one way that might work, but I have not tried it yet. It does look like it would be pretty slow though. Would this technique work for a list that could be up to 500 objects?

vector Distances; //Distances in the same order as AlphaObjects
vector DistancesSorted; //Distances sorted from smallest to greatest
vector AlphaObjects; // List of alpha blendable objects that need to be rendered.

for(int i = 0; i < Distances.size(); i++)
{
for(int j = 0; j < Distances.size(); j++)
{
if(Distances[j] = DistancesSorted)
{
Render( AlphaObjects[j] );
break;
}
}
}

I am fairly certain it will work, the question is just how fast.
eq
eq
Just use:


vector AlphaObjects; // List of alpha blendable objects that need to be rendered.

Make sure that you object has a member called m_distanceToCamera.
For every object pointed to in your vector calculate the distance to the camera and store it in the variable.

Then sort using std::sort, like this:

std::sort(AlphaObjects.begin(), AlphaObjects.end(), &SortObjectsFunctor);

bool SortObjectsFunctor(const Objects* const a, const Objects* const b)
{
return a->m_distanceToCamera < b->m_distanceToCamera;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.