Archived

This topic is now archived and is closed to further replies.

psicor

Implementation of ID3DXBaseMesh::DrawSubset

Recommended Posts

psicor    122
Hi , sorry about the topic ,every time i read about somebody ask about ID3DXBaseMesh::DrawSubset implementation in the microsoft.public.win32.programmer.directx.graphics newsgroup i only see people asking why in the word somebody wants to implement by hand the drawsubset method .... then they say that this is an old treat and the implementation are numerus times posted to the newsgroups and you have to search in previus posts... so y search whit google and i can't find anithing ,may be i are dump or something but y can't find any source or something only posts whit the same : "somebody ask about ID3DXBaseMesh::DrawSubset implementation ...." and then : "people asking why in the word somebody wants to implement by hand the drawsubset method ...." so somebody can post some code please? y be tankfull forever please [edited by - psicor on April 30, 2003 6:59:50 PM]

Share this post


Link to post
Share on other sites
Deficite    164
Hello,

I don't wanna spoil the fun of doing it yourself, so I'm gonna give you a hint. This method extract the buffers from a mesh and use them to draw it instead of using DrawSubset. This is not the full implementation, but maybe 90% of it. You'll easily manage the rest.

m_pMesh is a pointer to an initialized and correcly loaded x-file-mesh.
m_pMeshOpt is of course the optimized version of that mesh. This is needed in order to extract information about the Subsets. You will have to figure out the rendering yourself.


    
m_pMesh->Optimize(D3DXMESHOPT_ATTRSORT , NULL, NULL, NULL, NULL, &m_pMeshOpt);

LPDIRECT3DVERTEXBUFFER8 pMeshSourceVB;
LPDIRECT3DINDEXBUFFER8 pMeshSourceIB;
D3DVERTEX* pSrc;
D3DVERTEX* pDst;

setFVF(D3DFVF_D3DVERTEX);
numVert = m_pMeshOpt->GetNumVertices();
numFace = m_pMeshOpt->GetNumFaces();
numSubObj = 0;
getAttributeTable(NULL, &numSubObj);
m_pSubObjs = new D3DXATTRIBUTERANGE[numSubObj];
getAttributeTable(m_pSubObjs, &numSubObj);
m_pD3dDevice->CreateVertexBuffer( numVert * sizeof(D3DVERTEX),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
&m_pMeshVB );

m_pD3dDevice->CreateIndexBuffer( numFace * 3 * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED,
&m_pMeshIB );

m_pMeshOpt->GetVertexBuffer( &pMeshSourceVB );
m_pMeshVB->Lock( 0, 0, (BYTE**)&pDst, 0 );
pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
memcpy( pDst, pSrc, numVert * sizeof(D3DVERTEX) );
m_pMeshVB->Unlock();
pMeshSourceVB->Unlock();
pMeshSourceVB->Release();

m_pMeshOpt->GetIndexBuffer( &pMeshSourceIB );
m_pMeshIB->Lock( 0, 0, (BYTE**)&pDst, 0 );
pMeshSourceIB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
memcpy( pDst, pSrc, 3 * numFace * sizeof(WORD) );
m_pMeshIB->Unlock();
pMeshSourceIB->Unlock();
pMeshSourceIB->Release();



Regards,
Deficte

[edited by - Deficite on April 30, 2003 5:38:52 PM]

[edited by - Deficite on April 30, 2003 5:39:07 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
hi. here''s my simplified version of a DrawSubset replacement:

  
HRESULT geMesh::Render()
{
m_pRender->SetModelMatrix(m_matModel);

HRESULT hr;
LPDIRECT3DINDEXBUFFER8 pIB = NULL;
LPDIRECT3DVERTEXBUFFER8 pVB = NULL;
hr = m_pMesh->GetIndexBuffer (&pIB);
hr = m_pMesh->GetVertexBuffer(&pVB);

if( pVB && pIB )
{
m_pRender->SetIndices(pIB, 0);
m_pRender->SetVertexShader(m_MeshFVF);
m_pRender->SetStreamSource(0, pVB, sizeof(myMeshVB));
for( DWORD i = 0; i < m_nMaterials; i++ )
{
m_pRender->SetMaterial(&m_pMaterial[i]);
m_pRender->SetTexture(m_pTexture[i]);
m_pRender->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
m_pFrame[i].VertexStart, m_pFrame[i].VertexCount,
m_pFrame[i].FaceStart*3, m_pFrame[i].FaceCount);
}
}

if( pIB ) pIB->Release();
if( pVB ) pVB->Release();

return S_OK;
}

hopefully everything is obvious to you, except for maybe m_pFrame, which is an array of D3DXATTRIBUTERANGEs, gotten at mesh load time via the following code:

  
m_pMesh->GetAttributeTable(NULL, &m_nFrames);
m_pFrame = new D3DXATTRIBUTERANGE[m_nFrames];
m_pMesh->GetAttributeTable(m_pFrame, &m_nFrames);

Share this post


Link to post
Share on other sites