Commit 17c163c3 authored by Administrator's avatar Administrator
Browse files

add multiple triangles with color

parent 31e225d7
...@@ -108,17 +108,33 @@ void Graphics::DrawTestTriangle() ...@@ -108,17 +108,33 @@ void Graphics::DrawTestTriangle()
HRESULT hr; HRESULT hr;
struct Vertex struct Vertex
{
struct
{ {
float x; float x;
float y; float y;
} pos;
struct
{
float r;
float g;
float b;
} color;
}; };
// create vertex buffer (1 2d triangle at center of screen) // create vertex buffer (1 2d triangle at center of screen)
const Vertex vertices[] = const Vertex vertices[] =
{ {
{ 0.0f,0.5f }, // center
{ 0.5f,-0.5f }, { 0.0f, 0.5f, 1.0f, 0.0f, 0.0f },
{ -0.5f,-0.5f }, { 0.5f, -0.5f, 0.0f, 1.0f, 0.0f },
{ -0.5f, -0.5f, 0.0f, 0.0f, 1.0f },
// left top
{ -0.3f, 0.3f, 0.0f, 1.0f, 0.0f },
// right top
{ 0.3f, 0.3f, 0.0f, 0.0f, 1.0f },
// center bottom
{ 0.0f, -0.8f, 1.0f, 0.0f, 0.0f },
}; };
wrl::ComPtr<ID3D11Buffer> pVertexBuffer; wrl::ComPtr<ID3D11Buffer> pVertexBuffer;
D3D11_BUFFER_DESC bd = {}; D3D11_BUFFER_DESC bd = {};
...@@ -137,6 +153,29 @@ void Graphics::DrawTestTriangle() ...@@ -137,6 +153,29 @@ void Graphics::DrawTestTriangle()
const UINT offset = 0u; const UINT offset = 0u;
pContext->IASetVertexBuffers(0u, 1u, pVertexBuffer.GetAddressOf(), &stride, &offset); pContext->IASetVertexBuffers(0u, 1u, pVertexBuffer.GetAddressOf(), &stride, &offset);
// Create index buffer
const unsigned short indices[] =
{
0, 1, 2,
0, 2, 3,
0, 4, 1,
2, 1, 5,
};
wrl::ComPtr<ID3D11Buffer> pIndexBuffer;
D3D11_BUFFER_DESC ibd = {};
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.Usage = D3D11_USAGE_DEFAULT;
ibd.CPUAccessFlags = 0u;
ibd.MiscFlags = 0u;
ibd.ByteWidth = sizeof(indices);
ibd.StructureByteStride = sizeof(unsigned short);
D3D11_SUBRESOURCE_DATA isd = {};
isd.pSysMem = indices;
GFX_THROW_INFO(pDevice->CreateBuffer(&ibd, &isd, &pIndexBuffer));
// Bind index buffer to pipeline
pContext->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u);
// create pixel shader // create pixel shader
wrl::ComPtr<ID3D11PixelShader> pPixelShader; wrl::ComPtr<ID3D11PixelShader> pPixelShader;
wrl::ComPtr<ID3DBlob> pBlob; wrl::ComPtr<ID3DBlob> pBlob;
...@@ -160,6 +199,7 @@ void Graphics::DrawTestTriangle() ...@@ -160,6 +199,7 @@ void Graphics::DrawTestTriangle()
const D3D11_INPUT_ELEMENT_DESC ied[] = const D3D11_INPUT_ELEMENT_DESC ied[] =
{ {
{ "Position",0,DXGI_FORMAT_R32G32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 }, { "Position",0,DXGI_FORMAT_R32G32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "Color",0,DXGI_FORMAT_R32G32B32_FLOAT,0,8,D3D11_INPUT_PER_VERTEX_DATA,0 },
}; };
GFX_THROW_INFO(pDevice->CreateInputLayout( GFX_THROW_INFO(pDevice->CreateInputLayout(
ied, (UINT)std::size(ied), ied, (UINT)std::size(ied),
...@@ -188,7 +228,8 @@ void Graphics::DrawTestTriangle() ...@@ -188,7 +228,8 @@ void Graphics::DrawTestTriangle()
pContext->RSSetViewports(1u, &vp); pContext->RSSetViewports(1u, &vp);
GFX_THROW_INFO_ONLY(pContext->Draw((UINT)std::size(vertices), 0u)); //GFX_THROW_INFO_ONLY(pContext->Draw((UINT)std::size(vertices), 0u));
GFX_THROW_INFO_ONLY(pContext->DrawIndexed((UINT)std::size(indices), 0u, 0u));
} }
// Graphics exception stuff // Graphics exception stuff
......
float4 main() : SV_Target float4 main( float3 color : Color) : SV_Target
{ {
return float4(1.0f,1.0f,1.0f,1.0f); return float4(color, 1.0f);
} }
\ No newline at end of file
float4 main( float2 pos : Position ) : SV_Position struct VSOut
{ {
return float4(pos.x,pos.y,0.0f,1.0f); float3 color : Color;
float4 pos : SV_Position;
};
VSOut main( float2 pos : Position, float3 color : Color )
{
VSOut vso;
vso.pos = float4(pos.x, pos.y, 0.0f, 1.0f);
vso.color = color;
return vso;
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment