Jump to content
  • Advertisement
Sign in to follow this  
kurlyak

Atribute table is wrong DX9 VC++2005

This topic is 2473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying load game level from level file. Suppose I have a array of verts, array of rectangles, array of triangles. This is whole mesh. At first I am copy verts:


struct FACE_MATMAP
{
UINT face;
UINT mat;
UINT OrigMat;
};



pTR2RoomMesh = new MY_TR2_ROOM[Level->NumRooms];
ZeroMemory(pTR2RoomMesh, sizeof(MY_TR2_ROOM) * Level->NumRooms);

for (int i = 0; i < Level->NumRooms; i++)
{
pTR2RoomMesh.nNumVerts = Level->Rooms.RoomData.NumVertices;
pTR2RoomMesh.pVertsArray = new MY_VERTEX[Level->Rooms.RoomData.NumVertices];
ZeroMemory(pTR2RoomMesh.pVertsArray, sizeof(MY_VERTEX)*Level->Rooms.RoomData.NumVertices);

for(int j = 0; j < Level->Rooms.RoomData.NumVertices; j++)
{
pTR2RoomMesh.pVertsArray[j].p.x = Level->Rooms.RoomData.Vertices[j].Vertex.x;
pTR2RoomMesh.pVertsArray[j].p.y = -Level->Rooms.RoomData.Vertices[j].Vertex.y;
pTR2RoomMesh.pVertsArray[j].p.z = Level->Rooms.RoomData.Vertices[j].Vertex.z;
}
}


Furthemore I am copy rectangles:


DWORD dwNumIndices = Level->Rooms.RoomData.NumRectangles * 2 * 3 + Level->Rooms.RoomData.NumTriangles *3;
pTR2RoomMesh.pIndicesArray = new WORD[dwNumIndices];
DWORD dwCurrIndices = 0;
UINT nNumMatInMapMat = Level->Rooms.RoomData.NumRectangles * 2 + Level->Rooms.RoomData.NumTriangles;
pTR2RoomMesh.pFaceMatArray = new FACE_MATMAP[nNumMatInMapMat];
ZeroMemory(pTR2RoomMesh.pFaceMatArray, sizeof(FACE_MATMAP) * nNumMatInMapMat);
UINT nCurrMat = 0;
set <UINT> texture;
for( int j = 0; j < Level->Rooms.RoomData.NumRectangles; j++)
{
//first triagnle
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Rectangles[j].Vertices[0];
dwCurrIndices++;
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Rectangles[j].Vertices[1];
dwCurrIndices++;
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Rectangles[j].Vertices[2];
dwCurrIndices++;

pTR2RoomMesh.pFaceMatArray[nCurrMat].face = nCurrMat;
pTR2RoomMesh.pFaceMatArray[nCurrMat].mat = Level->Rooms.RoomData.Rectangles[j].Texture;

nCurrMat++;

//second tirangle
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Rectangles[j].Vertices[0];
dwCurrIndices++;
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Rectangles[j].Vertices[2];
dwCurrIndices++;
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Rectangles[j].Vertices[3];
dwCurrIndices++;

pTR2RoomMesh.pFaceMatArray[nCurrMat].face = nCurrMat;
pTR2RoomMesh.pFaceMatArray[nCurrMat].mat = Level->Rooms.RoomData.Rectangles[j].Texture;

nCurrMat++;

texture.insert(Level->Rooms.RoomData.Rectangles[j].Texture);

}


Copy of triangle of mesh:

for( int j = 0; j < Level->Rooms.RoomData.NumTriangles; j++)
{
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Triangles[j].Vertices[0];
dwCurrIndices++;
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Triangles[j].Vertices[1];
dwCurrIndices++;
pTR2RoomMesh.pIndicesArray[dwCurrIndices] = Level->Rooms.RoomData.Triangles[j].Vertices[2];
dwCurrIndices++;

pTR2RoomMesh.pFaceMatArray[nCurrMat].face = nCurrMat;
pTR2RoomMesh.pFaceMatArray[nCurrMat].mat = Level->Rooms.RoomData.Triangles[j].Texture;

nCurrMat++;

texture.insert(Level->Rooms.RoomData.Triangles[j].Texture);
}


Now re-arrange indices and build attribute table:

pTR2RoomMesh.nNumMatFaceMap = nCurrMat;

pTR2RoomMesh.nNumFaces = Level->Rooms.RoomData.NumRectangles * 2 + Level->Rooms.RoomData.NumTriangles;

vector< vector <FACE_MATMAP> > v;
v.resize(texture.size());

nCurrMat = 0;

set<UINT>::iterator it;

int test = 0;
for(it = texture.begin(); it!=texture.end(); it++)
{
for(UINT j=0; j<pTR2RoomMesh.nNumMatFaceMap; j++)
{
UINT mat = pTR2RoomMesh.pFaceMatArray[j].mat;
if(mat == static_cast<UINT> (*it) )
{
FACE_MATMAP fm;
fm.face = j;
fm.OrigMat = mat;
fm.mat = nCurrMat;
v[nCurrMat].push_back(fm);
}
}
nCurrMat++;
}

pTR2RoomMesh.nNumMat = v.size();

vector< vector <FACE_MATMAP> > vn;
vn.resize(pTR2RoomMesh.nNumMat);

pTR2RoomMesh.pIndicesArrayNew = new WORD[dwNumIndices];
ZeroMemory(pTR2RoomMesh.pIndicesArrayNew, sizeof(WORD) * dwNumIndices);

int k=0;
int l=0;
for(UINT r=0; r<pTR2RoomMesh.nNumMat; r++)
{
for(UINT j=0; j<v[r].size(); j++)
{
UINT f = v[r][j].face;

UINT v1 = pTR2RoomMesh.pIndicesArray[f*3];
UINT v2 = pTR2RoomMesh.pIndicesArray[(f*3)+1];
UINT v3 = pTR2RoomMesh.pIndicesArray[(f*3)+2];

pTR2RoomMesh.pIndicesArrayNew[k] = v1;
pTR2RoomMesh.pIndicesArrayNew[k+1] = v2;
pTR2RoomMesh.pIndicesArrayNew[k+2] = v3;
k+=3;


FACE_MATMAP fm;
fm.face = l;
l++;
fm.mat = r;
fm.OrigMat = v[r][j].OrigMat;
vn[r].push_back(fm);
}
}

pTR2RoomMesh.pAttr = new D3DXATTRIBUTERANGE[pTR2RoomMesh.nNumMat];
for(UINT k=0; k<pTR2RoomMesh.nNumMat; k++)
{
pTR2RoomMesh.pAttr[k].AttribId = k;
pTR2RoomMesh.pAttr[k].VertexStart = pTR2RoomMesh.pIndicesArrayNew[vn[k][0].face*3];
pTR2RoomMesh.pAttr[k].VertexCount=vn[k].size() *3;
pTR2RoomMesh.pAttr[k].FaceStart = vn[k][0].face;
pTR2RoomMesh.pAttr[k].FaceCount = (DWORD) vn[k].size();

}

m_pD3DDevice->CreateVertexBuffer( pTR2RoomMesh.nNumVerts * sizeof(LEVEL_VERTEX),
D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1,
D3DPOOL_MANAGED,
&pTR2RoomMesh.pVB, NULL );

LEVEL_VERTEX* pDstV;
pTR2RoomMesh.pVB->Lock( 0, 0, (void**)&pDstV, 0 );

for( UINT j = 0; j < pTR2RoomMesh.nNumVerts; j++)
{
pDstV[j].p.x = pTR2RoomMesh.pVertsArray[j].p.x;
pDstV[j].p.y = pTR2RoomMesh.pVertsArray[j].p.y;
pDstV[j].p.z = pTR2RoomMesh.pVertsArray[j].p.z;
}

pTR2RoomMesh.pVB->Unlock();

WORD *pDstI=0;

m_pD3DDevice->CreateIndexBuffer( pTR2RoomMesh.nNumFaces * 3 * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED,
&pTR2RoomMesh.pIB, NULL );

pTR2RoomMesh.pIB->Lock( 0, 0, (void**)&pDstI, 0 );
for(UINT j = 0; j < pTR2RoomMesh.nNumFaces * 3; j++)
{
pDstI[j] = pTR2RoomMesh.pIndicesArrayNew[j];
}

pTR2RoomMesh.pIB->Unlock();


And draw:

for (int i = 0; i < Level->NumRooms; i++)
{
for(UINT k = 0; k < pTR2RoomMesh.nNumMat; k++)
{
m_pD3DDevice->SetMaterial(&Material);
m_pD3DDevice->SetTexture( 0, pTR2RoomMesh[0].m_TextureArray.g_pRoomTextures );

// Draw the mesh subset

m_pD3DDevice->SetFVF(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1);
m_pD3DDevice->SetStreamSource( 0, pTR2RoomMesh.pVB, 0, sizeof(LEVEL_VERTEX) );
m_pD3DDevice->SetIndices( pTR2RoomMesh.pIB );

m_pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0, //BaseVertexIndex
pTR2RoomMesh.pAttr[k].VertexStart, //MinIndex
pTR2RoomMesh.pAttr[k].VertexCount, //NumVertices
pTR2RoomMesh.pAttr[k].FaceStart*3, //StartIndex
pTR2RoomMesh.pAttr[k].FaceCount ); //NumPrimitives
}
}



When I draw my vert and index buffer DrawIndexedPrimitive() without atribute buffer - its all right. But if I use atribute buffer - draw of mesh is wrong. That is building indices in pIndicesArrayNew is right, but atribute table is wrong. What is the matter? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!