Commit afffc70c authored by Administrator's avatar Administrator
Browse files

add constent buffer for rotation

parent 17c163c3
...@@ -25,6 +25,6 @@ void App::DoFrame() ...@@ -25,6 +25,6 @@ void App::DoFrame()
{ {
const float c = sin(timer.Peek()) / 2.0f + 0.5f; const float c = sin(timer.Peek()) / 2.0f + 0.5f;
wnd.Gfx().ClearBuffer(c, c, 1.0f); wnd.Gfx().ClearBuffer(c, c, 1.0f);
wnd.Gfx().DrawTestTriangle(); wnd.Gfx().DrawTestTriangle( timer.Peek() );
wnd.Gfx().EndFrame(); wnd.Gfx().EndFrame();
} }
\ No newline at end of file
...@@ -102,7 +102,7 @@ void Graphics::ClearBuffer(float red, float green, float blue) noexcept ...@@ -102,7 +102,7 @@ void Graphics::ClearBuffer(float red, float green, float blue) noexcept
pContext->ClearRenderTargetView(pTarget.Get(), color); pContext->ClearRenderTargetView(pTarget.Get(), color);
} }
void Graphics::DrawTestTriangle() void Graphics::DrawTestTriangle(float angle)
{ {
namespace wrl = Microsoft::WRL; namespace wrl = Microsoft::WRL;
HRESULT hr; HRESULT hr;
...@@ -176,6 +176,40 @@ void Graphics::DrawTestTriangle() ...@@ -176,6 +176,40 @@ void Graphics::DrawTestTriangle()
// Bind index buffer to pipeline // Bind index buffer to pipeline
pContext->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u); pContext->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u);
// Create constant buffer for transformation matrix
struct ConstantBuffer
{
struct
{
float element[4][4];
} transformation;
};
const float heightWidthRatio = 0.3f / 0.4f;
const ConstantBuffer cb =
{
{
heightWidthRatio * std::cos(angle), -std::sin(angle), 0.0f, 0.0f,
heightWidthRatio * std::sin(angle), std::cos(angle), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
}
};
wrl::ComPtr<ID3D11Buffer> pConstantBuffer;
D3D11_BUFFER_DESC cbd = {};
cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbd.Usage = D3D11_USAGE_DYNAMIC;
cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbd.MiscFlags = 0u;
cbd.ByteWidth = sizeof(cb);
cbd.StructureByteStride = 0u;
D3D11_SUBRESOURCE_DATA csd = {};
csd.pSysMem = &cb;
GFX_THROW_INFO(pDevice->CreateBuffer(&cbd, &csd, &pConstantBuffer));
// Bind constant buffer to vertex
pContext->VSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf());
// create pixel shader // create pixel shader
wrl::ComPtr<ID3D11PixelShader> pPixelShader; wrl::ComPtr<ID3D11PixelShader> pPixelShader;
wrl::ComPtr<ID3DBlob> pBlob; wrl::ComPtr<ID3DBlob> pBlob;
......
...@@ -53,7 +53,7 @@ public: ...@@ -53,7 +53,7 @@ public:
void EndFrame(); void EndFrame();
// 刷新RGB // 刷新RGB
void ClearBuffer(float red, float green, float blue) noexcept; void ClearBuffer(float red, float green, float blue) noexcept;
void DrawTestTriangle(); void DrawTestTriangle(float angle);
private: private:
#ifndef NDEBUG #ifndef NDEBUG
DxgiInfoManager infoManager; DxgiInfoManager infoManager;
......
...@@ -4,10 +4,15 @@ struct VSOut ...@@ -4,10 +4,15 @@ struct VSOut
float4 pos : SV_Position; float4 pos : SV_Position;
}; };
cbuffer CBuf
{
row_major matrix transform;
};
VSOut main( float2 pos : Position, float3 color : Color ) VSOut main( float2 pos : Position, float3 color : Color )
{ {
VSOut vso; VSOut vso;
vso.pos = float4(pos.x, pos.y, 0.0f, 1.0f); vso.pos = mul( float4(pos.x, pos.y, 0.0f, 1.0f), transform );
vso.color = color; vso.color = color;
return vso; return vso;
......
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