Original Post
Hello world, :) I'm currently developing a game with Managed DirectX and C# 2.0. In my custom engine I organize renderable Entity in one or more Scene classes. During the render process these Entities need to be sorted in multiple ways: - Opaque entities from front-to-back for better performance with the ZBuffer - Alpha blended entities from back-to-front - All entities from front-to-back for Mouse Picking And when I render the same scene from a different angle (e.g. for a reflection) I need to resort everything again. Currently I store the entities like this: Whenever I need to sort the entities, I use the List's .Sort() method, which allows me to pass a custom Comparison delegate: So far this method works just fine, however I suppose once my scene starts containing more objects (currently it's just about 10) resorting the list multiple times every single frame might become a serious bottleneck in the render loop. Now here are my questions: - Is there some more efficient way to handle all this sorting? - Would it make sense to create a copy for each required order of the list, recreating and resorting these copies whenever the base list changes? - Is a List<> a good storage method anyway? - Might it be worth the hassle turning my single-threaded application into a multi-threaded one, to better handle the sorting on Dual Core systems? Eagerly awaiting your opinions, Bastian
private List<Entity> Entities;
public void Render()
{
...
Entities.Sort(new Comparison<Entity>(CameraDistDiff));
...
}
private int CameraDistDiff(Entity entity1, Entity entity2)
{
// Compares the distance of two entities to the camera
}