Hi,
I've stumbled across a problem, I have a bunch of particles that are rendered in 3d space, but transparency problems occur, during each frame the particle buffer ( a chunk of data sent to the gpu ), is updated, so I thought why not do the range based sorting ( from camera position to particle ) there for a nice and easy fix, but obstacles occur.
How I do it: ( Keep in mind that the following code is not done, its just a quick prototype, making it nicer is done later on )
D3DXVECTOR3 __g_vCam; // Yeah yeah, globals are bad, it's 1:40 at night here!
bool ceparticle_sort(CE_NAMESPACE::Physics::CEParticle &p0, CE_NAMESPACE::Physics::CEParticle &p1)
{
float dist0 = D3DXVec3Length(&(p0.vPosition - __g_vCam));
float dist1 = D3DXVec3Length(&(p1.vPosition - __g_vCam));
if (dist1 < dist0) return true;
}
void CE_NAMESPACE::CEParticleSet::BuffersUpdate(DX11 *pDX11, D3DXVECTOR3 *pCam)
{
std::vector<Physics::CEParticle> m_vParticles_Sort;
m_vParticles_Sort.resize(m_iMax); // m_iMax -> Max Nr of particles
__g_vCam = *pCam; // store camera/eye position
UNTIL(m_iMax)
{
// m_pParticles is a pointer to an array created at runtime
m_vParticles_Sort[i] = m_pParticles[i]; // copy
}
// Sort Array
std::sort(m_vParticles_Sort.begin(), m_vParticles_Sort.end(), ceparticle_sort);
// Map
pDX11->BufferMap(m_pBufferPoints, &m_vParticles_Sort[0], sizeof(Physics::CEParticle), m_iMax);
}
A way of speeding up the reaction would be to store pointers of particles and then processing that for faster memory management, but, the demands of the graphic apis are strict, it must have a pointer to an array that leads directly to the memory, not another point, so that's not an option.
In this sole operation the frame time increased by 2/3 as the cpu is slowing the operation down.
EDIT. pDX11 is a custom wrapper for DX11, by the graphics API I mean Direct3D 11.
So here I am, and my actual question is, how would I go about to approach this problem? What have you done?
Thank you for your time.
-MIGI0027