Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rendering Multiple Meshes.

Started by Notthatguy Nov 20, 2011 at 3:06 PM 5 replies 1.9k views
Original Post
Notthatguy
Notthatguy
Hello guys,

Im having trouble with rendering multiple meshes within my DirectX program.

I have one basic mesh that renders all the set up seems fine, then should be rendered with:

renderMesh(pd3dDevice, &g_MeshOne);


void renderMesh(ID3D10Device* pd3dDevice, CDXUTSDKMesh *mesh)
{

pd3dDevice->IASetInputLayout( g_p_VertexLayout );

UINT Strides[1];
UINT Offsets[1];
ID3D10Buffer* pVB[1];
pVB[0] = mesh->GetVB10( 0, 0 );
Strides[0] = ( UINT )mesh->GetVertexStride( 0, 0 );
Offsets[0] = 0;
pd3dDevice->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
pd3dDevice->IASetIndexBuffer( mesh->GetIB10( 0 ), mesh->GetIBFormat10( 0 ), 0 );

D3D10_TECHNIQUE_DESC techDesc;
g_p_Technique->GetDesc( &techDesc );
SDKMESH_SUBSET* pSubset = NULL;
ID3D10ShaderResourceView* pDiffuseRV = NULL;
D3D10_PRIMITIVE_TOPOLOGY PrimType;



for( UINT p = 0; p < techDesc.Passes; ++p )
{
for( UINT subset = 0; subset < mesh->GetNumSubsets( 0 ); ++subset )
{
pSubset = mesh->GetSubset( 0, subset );

PrimType = mesh->GetPrimitiveType10( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType );
pd3dDevice->IASetPrimitiveTopology( PrimType );

pDiffuseRV = mesh->GetMaterial( pSubset->MaterialID )->pDiffuseRV10;
g_p_txDiffuseInShader->SetResource( pDiffuseRV );

g_p_Technique->GetPassByIndex( p )->Apply( 0 );
pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart );
}

}
}


However if I try and render another with renderMesh(pd3dDevice, &g_MeshTwo);
It seems they overwrite each other. Any help will be greatly appreciated, i'm just a beginner so please go easy on me.
Adaline
Adaline
Hello smile.gif
I'm not familiar with [color="#1C2837"]CDXUTSDKMesh[color="#1C2837"] class, but your drawing function seems right.

[color="#1C2837"]This may be a problem in your transform :
[color="#1C2837"]Have you a world matrix for each mesh instance ?
[color="#1C2837"]How do you set up your world matrix ?
[color="#1C2837"]How do you bind it to your shader ?

[color="#1c2837"]PS :
[color="#1c2837"]Usually, you have an entity world transform, and a subset local transform.
[color="#1c2837"]When drawing a subset, the complete world transform is the product of these 2 matrices


[color="#1c2837"]In pseudocode, something like that :

[color="#1c2837"]for each subset :
[color="#1c2837"] worldMatrix=subset.localTransform*entity.worldMatrix
[color="#1c2837"] bind it to shader
[color="#1c2837"] draw subset
[color="#1c2837"]end
Notthatguy
Notthatguy
void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
D3DXMATRIX matWorldViewProjection;
D3DXMATRIX matView;
D3DXMATRIX matProj;


D3DXMATRIX matMesh1World;
D3DXMATRIX matMesh2World;
D3DXMATRIX matMesh1RotY;
D3DXMATRIX matMesh1RotX;
D3DXMATRIX matMesh2RotY;
D3DXMATRIX matMesh2RotX;
float ClearColor[4] = { 0.176f, 0.196f, 0.667f, 0.0f };
ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView();
pd3dDevice->ClearRenderTargetView( pRTV, ClearColor );

// Clear the depth stencil
ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView();
pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 );


{
g_SettingsDlg.OnRender( fElapsedTime );
return;
}




D3DXMatrixRotationX( &matMesh1RotX, g_f_Mesh1RX );
D3DXMatrixRotationY( &matMesh1RotY, g_f_Mesh1RY );
D3DXMatrixRotationX( &matMesh2RotX, g_f_Mesh2RX );
D3DXMatrixRotationY( &matMesh2RotY, g_f_Mesh2RY );


matMesh1World = matMesh1RotX * matMesh1RotY;
matMesh2World = matMesh2RotX * matMesh2RotY;


matProj = *g_Camera.GetProjMatrix();
matView = *g_Camera.GetViewMatrix();
matWorldViewProjection = matMEsh1World * matView * matProj;






g_p_MatWorldViewProjInShader->SetMatrix( ( float* )&matWorldViewProjection );
g_p_MatWorldInShader->SetMatrix( ( float* )&matMesh1World );
g_p_fTimeInShader->SetFloat( ( float )fTime );


g_p_LightDirectionInShader->SetFloatVector( (float *) &g_lightDirection);
g_p_LightDiffuseColourInShader->SetFloatVector( (float *) &g_lightDiffuseColour);
g_p_MaterialDiffuseColourInShader->SetFloatVector((float *) &g_materialDiffuseColour);


renderMesh(pd3dDevice, &g_Mesh1);



renderMesh(pd3dDevice, &g_Mesh2);





DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" );
RenderText();
g_HUD.OnRender( fElapsedTime );
g_SampleUI.OnRender( fElapsedTime );
DXUT_EndPerfEvent();
}

Thanks for the reply btw
grazing
grazing


g_p_MatWorldViewProjInShader->SetMatrix( ( float* )&matWorldViewProjection );


g_p_MatWorldInShader->SetMatrix( ( float* )&matMesh1World ); <--- you set the world matrix for mesh 1 here and render both mesh's using this ..


g_p_fTimeInShader->SetFloat( ( float )fTime );


g_p_LightDirectionInShader->SetFloatVector( (float *) &g_lightDirection);
g_p_LightDiffuseColourInShader->SetFloatVector( (float *) &g_lightDiffuseColour);
g_p_MaterialDiffuseColourInShader->SetFloatVector((float *) &g_materialDiffuseColour);


renderMesh(pd3dDevice, &g_Mesh1);



renderMesh(pd3dDevice, &g_Mesh2);

[/quote]


You need:


renderMesh(pd3dDevice, &g_Mesh1);

g_p_MatWorldInShader->SetMatrix( ( float* )&matMesh2World );

renderMesh(pd3dDevice, &g_Mesh2);
Adaline
Adaline
It seems you just forgot to bind the world matrix and worldViewProjection matrix for the second rendering, that's it smile.gif


renderMesh(pd3dDevice, &g_Mesh1);

matWorldViewProjection = matMesh2World * matView * matProj;

g_p_MatWorldInShader->SetMatrix( ( float* )&matMesh2World );
g_p_MatWorldViewProjInShader->SetMatrix( ( float* )&matWorldViewProjection );

renderMesh(pd3dDevice, &g_Mesh2);
Notthatguy
Notthatguy
Its working!!!! Thanks for all your help guys, much appreciated!
grazing
grazing
EDIT: Just seen it's working now have fun!!




Ok error fixed, ;) But it still seems to only render one object :S Thanks for the help btw guys Much appreciated



If you comment one of the renderMesh function calls out will it draw the other mesh?

If so are they at the same location if you comment one out ?

if so is one mesh bigger than the other?

If so, ensure that the position (transform/translation) of the two matrices are different so you are not rendering one inside the other.




And as NicoLaCrevette pointed out, ensure you are setting the WVP matrix in the shader also (this editor got all messed up while I was formatting my response and I forgot to add it to my MS Word copy of my reply


Many different possibilities!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.