如何使用translate从一个顶点数组创建两个对象?

时间:2014-05-11 19:09:45

标签: directx directx-11

我有一个对象,我可以渲染它,但我想使用它的顶点两次,但我不知道如何。 编辑:我希望他们在比赛期间独立翻译。

这是我从txt的代码读取对象:

fin.open("piyon.txt");
fin >> vertexCountpiyon;
verticespiyon = new SimpleVertex[vertexCountpiyon];

for(int i=0; i<vertexCountpiyon; i++)
{
    fin >> verticespiyon[i].Pos.x >> verticespiyon[i].Pos.y >> verticespiyon[i].Pos.z;
    fin >> verticespiyon[i].Tex.x >> verticespiyon[i].Tex.y;
    fin >> verticespiyon[i].Normal.x >> verticespiyon[i].Normal.y >> verticespiyon[i].Normal.z;
}   
fin.close();

bd.ByteWidth = sizeof( SimpleVertex ) * vertexCountpiyon;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = verticespiyon;
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer_piyon );
if( FAILED( hr ) )     return hr;    

和我的渲染代码:

    g_pImmediateContext->IASetVertexBuffers( 0, 1, &g_pVertexBuffer_piyon, &stride, &offset );
    cBuffer.vMeshColor = XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ); 
    XMMATRIX mTranslateBeyazPiyon = XMMatrixTranslation( -17.5F, 0, -12.5F );
    cBuffer.mWorld = XMMatrixTranspose( mTranslateBeyazPiyon );
    g_World_Piyon = mTranslateBeyazPiyon;
    g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cBuffer, 0, 0 );
    g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
    g_pImmediateContext->VSSetConstantBuffers( 2, 1, &g_pConstantBuffer );
    g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );
    g_pImmediateContext->PSSetConstantBuffers( 2, 1, &g_pConstantBuffer );
    g_pImmediateContext->Draw( 7050, 0 );

1 个答案:

答案 0 :(得分:1)

如果你想在不同的地方绘制同一个物体,你只需要改变你的世界矩阵,然后再画画。因此,使用代码的一部分作为参考,如下所示:

// set world matrix for first object ...
XMMATRIX mTranslateBeyazPiyon = XMMatrixTranslation( -17.5F, 0, -12.5F );
cBuffer.mWorld = XMMatrixTranspose( mTranslateBeyazPiyon );
g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cBuffer, 0, 0 );

// ... set any other common state

// draw first object
g_pImmediateContext->Draw( 7050, 0 );

// set world matrix for second object ... for example, translated somewhere else
mTranslateBeyazPiyon = XMMatrixTranslation( -34.5F, 0, -24.5F );
cBuffer.mWorld = XMMatrixTranspose( mTranslateBeyazPiyon );
g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cBuffer, 0, 0 );

// draw second object
g_pImmediateContext->Draw( 7050, 0 );