Original Post
What''s the fastest way I can sort the alpha blended objects in my scene in back to front order? I store all my data in one giant linked list which I render from, and objects that need to be sorted are tagged with a boolean.
The code I currently use to do the sort (actually its my entire draw loop for the list) is:
However this seems pretty slow when its running compared to w/o this code, so there must be a better way. I''m wondering though if the issue is the speed of the quicksort, the speed of the memory allocation or both?
##UnknownPlayer##
CBaseObject* TempObject = NULL; // Used for deleting objects
// Sanity Check
if (FirstObject == NULL)
{
return; // Can''t use draw path without an initial object
}
CurrentObject = FirstObject; // Start from beginning of chain
// Set up the delete queue ready for data
GenericList* Queue = new GenericList;
GenericList* Tracking = Queue;
// Alpha sorting setup
uint NumberOfAlphaObjects = 0;
CBaseObject** AlphaObjectList = new CBaseObject*[ObjectsAllocated];
while (CurrentObject != NULL)
{
if (CurrentObject->AlphaBlender && CurrentObject->DarkObjectDerived)
{
AlphaObjectList[NumberOfAlphaObjects] = CurrentObject;
NumberOfAlphaObjects++;
}
else
{
CurrentObject->Draw();
}
if ( (CurrentObject->DeleteNow || CurrentObject->DeleteOnDraw) )
{
Tracking->Payload = CurrentObject;
Tracking->Next = new GenericList;
Tracking = Tracking->Next;
}
CurrentObject = CurrentObject->next;// Move cursor to next object
}
// Sort and render the alpha objects
qsort(AlphaObjectList, NumberOfAlphaObjects, sizeof(CBaseObject*), CompareParticleDistances);
App()->d3ddevice->SetRenderState(D3DRS_ZWRITEENABLE, false);
for (uint i=0; i < NumberOfAlphaObjects; i++)
{
AlphaObjectList[i]->Draw();
}
App()->d3ddevice->SetRenderState(D3DRS_ZWRITEENABLE, true);
// Clean up the list
delete [] AlphaObjectList;
// Render the HUD //
if (HudObject)
{
// The hud is responsible to ensure it doesn''t render twice
HudObject->Draw();
}
// Destroy all objects that have placed requests //
while (Queue != null)
{
DestroyObject((CBaseObject*)Queue->Payload);
Tracking = Queue;
Queue = Queue->Next;
delete Tracking;
}