Появился вопрос про правильную организацию рендеринга. Алгоритм описывать долго, поэтому код( unordered_map<IDirect3DTexture9*,vector<Mesh*>> g_meshes )

for( auto groupIterator = g_meshes.begin(); groupIterator != g_meshes.end(); ++groupIterator )
{
IDirect3DTexture9 * texture = groupIterator->first;
auto & meshes = groupIterator->second;
g_device->SetTexture( 0, texture );
g_textureChanges++;
for( auto meshIterator = meshes.begin(); meshIterator != meshes.end(); ++meshIterator )
{
Mesh * mesh = *meshIterator;
SceneNode * node = mesh->parent;
if( !IsMeshVisible( mesh )) // frustum cull
continue;
if( !node->visible )
continue;
if( node->parent )
if( !node->parent->visible )
continue;
if( !mesh->ib )
continue;
if( !mesh->vb )
continue;
D3DXMATRIX world; GetD3DMatrixFromBulletTransform( node->globalTransform, world );
g_device->SetTransform( D3DTS_WORLD, &world );
g_lightShader->vsc->SetMatrix( g_device, g_lightShader->vWorld, &world );
g_device->SetStreamSource( 0, mesh->vb, 0, sizeof( Vertex ));
g_device->SetIndices( mesh->ib );
g_device->SetRenderState ( D3DRS_ALPHABLENDENABLE, FALSE );
// draw mesh with each light
for( size_t j = 0; j < g_lights.size(); j++ )
{
Light * light = g_lights.at( j );
SetLightShaderFor( light );
if( !LightIntersectsMesh( mesh, light ))
continue;
g_device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, mesh->vertexCount, 0, mesh->faceCount );
g_dips++;
if( j == 0 )
{
g_device->SetRenderState ( D3DRS_ALPHABLENDENABLE, TRUE );
g_device->SetRenderState ( D3DRS_SRCBLEND, D3DBLEND_ONE );
g_device->SetRenderState ( D3DRS_DESTBLEND, D3DBLEND_ONE );
}
}
}
Немного про алгоритм всё таки напишу. При добавлении меша в рендерер, он помещается в соответствующую группу с определенной текстурой - таким образом можно избежать подобной отрисовки
g_device->SetTexture( 0, A );
g_device->DrawPrimitive( ... );
g_device->SetTexture( 0, B );
g_device->DrawPrimitive( ... );
То есть сортируем меши по текстуре.
Каждый меш отрисовываем всеми источниками света с аддитивным блендингом.
Есть какие либо замечания\поправки\баги в алгоритме?