How are you storing and updating your objects?
The object that seems to be causing the issue is setup like this:
A model is loaded form an .x file, then x number of instances of that model is stored in a batch model.
The class basically looks like this:
class Model
{
public:
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
LPDIRECT3DINDEXBUFFER9 indexBuffer;
DWORD numBytesPerVertex;
int numVerts;
int numFaces;
LPDIRECT3DTEXTURE9 diffuseTexture;
LPDIRECT3DTEXTURE9 normalTexture;
//Some functions here
};
The .x model mesh is loaded like this:
LPD3DXBUFFER materialBuffer;
D3DXLoadMeshFromX(meshFileName, D3DXMESH_SYSTEMMEM, d3d.device, NULL, &materialBuffer, NULL, &numSubsets, &mesh);
The vertex and index buffers are intitialized like this:
numBytesPerVertex = mesh->GetNumBytesPerVertex();
int numModelVerts = mesh->GetNumVertices();
int numModelFaces = mesh->GetNumFaces();
numVerts = numModelVerts * numInstances;
numFaces = numModelFaces * numInstances;
//Create vertex buffer
d3d.device->CreateVertexBuffer(numVerts * numBytesPerVertex, D3DUSAGE_WRITEONLY, FVF_MESHVERTEX, D3DPOOL_DEFAULT, &vertexBuffer, NULL));
//Fill vertex buffer
MeshVertex* vertBuf = NULL;
vertexBuffer->Lock(0, 0, (void**)&vertBuf, 0);
MeshVertex* modelVertBuf = NULL;
mesh->LockVertexBuffer(0, (VOID**)&modelVertBuf);
int currentVertex = 0;
for(int i=0; i < numInstances; i++)
{
for(int d=0; d < numModelVerts; d++)
{
vertBuf[currentVertex] = modelVertBuf[d];
currentVertex++;
}
}
mesh->UnlockVertexBuffer();
vertexBuffer->Unlock();
//Create index buffer
d3d.device->CreateIndexBuffer(numFaces * 3 * sizeof(short), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, NULL);
//Fill in index buffer
int currentStartIndex = 0;
int currentIndexIndex = 0;
int numIndices = numModelFaces * 3;
short* indexBuf = NULL;
indexBuffer->Lock(0, 0, (void**)&indexBuf, 0);
short* modelIndeXBuf = NULL;
mesh->LockIndexBuffer(0, (void**)&modelIndeXBuf);
for(int i=0; i < numInstances; i++)
{
for(int d=0; d < numIndices; d++)
{
indexBuf[currentIndexIndex] = modelIndeXBuf[d] + currentStartIndex;
currentIndexIndex++;
}
currentStartIndex += numModelVerts;
}
mesh->UnlockIndexBuffer();
indexBuffer->Unlock();
The vertex declaration looks like this:
#define FVF_MESHVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX3 | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEXCOORDSIZE3(2) | D3DFVF_TEXCOORDSIZE2(3))
struct MeshVertex
{
D3DXVECTOR3 pos;
D3DXVECTOR3 normal;
D3DXVECTOR3 tangent;
D3DXVECTOR3 binormal;
D3DXVECTOR2 tex;
};
const D3DVERTEXELEMENT9 vertexDecl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
{ 0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 },
{ 0, 48, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
The model is rendered like this:
d3d.effectScene->SetTexture( "diffuseMap", diffuseTexture );
d3d.effectScene->SetTexture( "normalMap", normalTexture );
D3DXMATRIX worldMat;
D3DXMatrixIdentity(&worldMat);
d3d.effectScene->SetMatrix( "worldMatrix", &worldMat);
D3DXMATRIX worldViewProjMatrix = worldMat * camera.viewMatrix * camera.projMatrix;
d3d.effectScene->SetMatrix( "worldViewProjMatrix", &worldViewProjMatrix);
d3d.effectScene->CommitChanges();
d3d.device->SetVertexDeclaration(d3d.vertDecl);
d3d.device->SetStreamSource(0, vertexBuffer, 0, numBytesPerVertex);
d3d.device->SetIndices(indexBuffer);
d3d.device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVerts, 0, numFaces);