Commit 6f2417ad authored by Administrator's avatar Administrator
Browse files

add constant buffer for rotation

parent 3417949a
......@@ -127,6 +127,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Timer.cpp" />
<ClCompile Include="WinMain.cpp" />
</ItemGroup>
<ItemGroup>
......@@ -147,6 +148,9 @@
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Timer.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
......
......@@ -21,6 +21,9 @@
<ClCompile Include="WinMain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<FxCompile Include="PixelShader.hlsl">
......@@ -30,4 +33,9 @@
<Filter>Shaders</Filter>
</FxCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Timer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#include "Timer.h"
using namespace std::chrono;
Timer::Timer() noexcept
{
last = steady_clock::now();
}
float Timer::Mark() noexcept
{
const auto old = last;
last = steady_clock::now();
const duration<float> frameTime = last - old;
return frameTime.count();
}
float Timer::Peek() const noexcept
{
return duration<float>(steady_clock::now() - last).count();
}
#pragma once
#include <chrono>
class Timer
{
public:
Timer() noexcept;
float Mark() noexcept;
float Peek() const noexcept;
private:
std::chrono::steady_clock::time_point last;
};
\ No newline at end of file
......@@ -4,11 +4,16 @@ struct VSOut
float4 pos : SV_Position;
};
cbuffer CBuf
{
row_major matrix transform;
};
VSOut main(float2 pos : Position, float3 color : Color)
{
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;
return vso;
}
}
\ No newline at end of file
......@@ -11,6 +11,9 @@
#include <wrl.h>
#include <array> // to usd std::size
#include <cmath> // to use std::sin and std::cos
#include "Timer.h" // to use timer
Timer timer;
// global declarations
Microsoft::WRL::ComPtr<IDXGISwapChain> swapchain; // the pointer to the swap chain interface
......@@ -64,6 +67,29 @@ const Vertex vertices[] =
};
// Define constant buffer
Microsoft::WRL::ComPtr<ID3D11Buffer> pConstantBuffer;
// Create constant buffer struct
struct ConstantBuffer
{
struct
{
float element[4][4];
} transformation;
};
// create constant buffer
const float heightWidthRatio = 0.3f / 0.4f;
float angle = 0.0f;
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,
}
};
// Define input layout
ID3D11InputLayout* pInputLayout;
......@@ -100,6 +126,30 @@ void DefineVertexBuffer()
dev->CreateBuffer(&bd, &sd, &pVertexBuffer);
}
void DefineConstantBuffer(float angle)
{
cb.transformation.element[0][0] = heightWidthRatio * std::cos(angle);
cb.transformation.element[0][1] = -std::sin(angle);
cb.transformation.element[1][0] = heightWidthRatio * std::sin(angle);
cb.transformation.element[1][1] = std::cos(angle);
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;
// Create constant buffer
dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
// Bind constant buffer to vertex buffer
devcon->VSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf());
}
void DefineInputLayout()
{
// Create input elements description, choosing whatever need to send to GPU
......@@ -146,6 +196,7 @@ void drawTriangle()
{
DefineShader();
DefineVertexBuffer();
DefineConstantBuffer(timer.Peek());
DefineInputLayout();
DrawPrimitive();
}
......
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