I guess option 1 then. So what i got from this is that we put all vertices(square,triangle) in a single vertex buffer, we update every vertex so that the object that we want to draw(square) sits in its designated position and then just draw only the vertices of that object using
context->Draw and ignore all other vertices. We do the same for the triangle except we update it differently.
Is this how it's done and if so doesn't updating every vertex in the buffer just for drawing one object impact performance?
I know this is not the most technical explanation so here is some code. i managed to update the square and the triangle separatelly :
The D3DGraphics.cpp
void D3DGraphics::InitGraphics()
{ VERTEX OurVertices[] =
{
{-0.5f, -0.5f, 0.5f, DirectX::PackedVector::XMCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
{-0.5f, 0.5f, 0.5f, DirectX::PackedVector::XMCOLOR(0.0f, 1.0f, 0.0f, 1.0f)},
{ 0.5f, 0.5f, 0.5f, DirectX::PackedVector::XMCOLOR(0.0f, 0.0f, 1.0f, 1.0f)},
{ 0.8f, -0.5f, 0.5f, DirectX::PackedVector::XMCOLOR(0.0f, 0.0f, 1.0f, 1.0f)},
{-0.4f, -0.3f, 0.4f, DirectX::PackedVector::XMCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
{-0.4f, 0.3f, 0.4f, DirectX::PackedVector::XMCOLOR(0.0f, 1.0f, 0.0f, 1.0f)},
{ 0.3f, 0.3f, 0.4f, DirectX::PackedVector::XMCOLOR(0.0f, 0.0f, 1.0f, 1.0f)}
};
DWORD indices[] =
{0,2,3,
0,1,2,
4,5,6};
//create and fill the index buffer
D3D11_BUFFER_DESC indexBufferDesc;
//...
//create and fill the vertex buffer
D3D11_BUFFER_DESC bd;
//...
//Create the buffer to send to the cbuffer in effect file
D3D11_BUFFER_DESC cbbd;
//...
//Camera information
//Set the View matrix
//Set the Projection matrix
void D3DGraphics::DrawVertices()
{
// select which vertex buffer to display
UINT stride = sizeof(VERTEX);
UINT offset = 0;
m_pImmediateContext->IASetIndexBuffer( squareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
m_pImmediateContext->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
// select which primtive type we are using
m_pImmediateContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//Refresh the Depth/Stencil view
m_pImmediateContext->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
//we update and draw the square
WVP = squareWorld * camView * camProjection;
cbPerObj.WVP = XMMatrixTranspose(WVP);
m_pImmediateContext->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
m_pImmediateContext->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
m_pImmediateContext->DrawIndexed(6, 0, 0 );
//we update and draw the triangle
WVP = triangleWorld * camView * camProjection;
cbPerObj.WVP = XMMatrixTranspose(WVP);
m_pImmediateContext->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
m_pImmediateContext->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
m_pImmediateContext->DrawIndexed( 3, 4, 4 );
}
void D3DGraphics::UpdateScene()
{
//Keep the cubes rotating
rot += .0001f;
if(rot > 6.2831f)
rot = 0.0f;
//Reset squareWorld
squareWorld = DirectX::XMMatrixIdentity();
//Define square's world space matrix
DirectX::XMVECTOR rotaxis = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
Rotation = DirectX::XMMatrixRotationAxis( rotaxis, rot);
Translation = DirectX::XMMatrixTranslation( 0.0f, 0.0f, 0.0f );
//Set square's world space using the transformations
squareWorld = Translation * Rotation;
//Reset triangleWorld
triangleWorld = DirectX::XMMatrixIdentity();
//Define triangle's world space matrix
Rotation = DirectX::XMMatrixRotationAxis( rotaxis, -rot);
Scale = DirectX::XMMatrixScaling( 1.3f, 1.3f, 1.3f );
//Set triangle's world space matrix
triangleWorld = Rotation * Scale;
}
The shaders.fx code:
cbuffer cbPerObject
{
float4x4 WVP;
};
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
VOut output;
output.position = mul(position, WVP);
output.color = color;
return output;
}
float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}
Sorry if there's some unnecessary stuff here it's my first time posting on gamedev and Thanks