Added: matrix buffer

Added: world, view & projection matrices
Added: delta time calculation
Added: quad mesh
This commit is contained in:
2026-10-12 18:47:53 +02:00
parent daa04be788
commit d2a487a4f6
10 changed files with 139 additions and 12 deletions
+17
View File
@@ -2,6 +2,23 @@
UINT Camera::init(UINT width, UINT height)
{
// view matrix
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f);
XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMMATRIX view = XMMatrixLookToLH(position, forward, up);
XMStoreFloat4x4(&_viewMatrix, view);
// projection matrix
float fov = XMConvertToRadians(60.0f);
float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
float nearClippingPlane = 0.3f;
float farClippingPlane = 10.0f;
XMMATRIX projection = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
XMStoreFloat4x4(&_projectionMatrix, projection);
return 0;
}
Binary file not shown.
@@ -1,4 +1,11 @@
float4 main() : SV_TARGET
struct PixelInput
{
return float4(1.0f, 1.0f, 1.0f, 1.0f);
float4 position : SV_Position;
float4 color : COLOR;
};
float4 main(PixelInput INPUT) : SV_TARGET
{
//return float4(1.0f, 1.0f, 1.0f, 1.0f); // solid color
return INPUT.color; // vertex color
}
Binary file not shown.
@@ -1,6 +1,33 @@
// semantics: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
float4 main( float4 pos : POSITION ) : SV_POSITION
cbuffer MatrixBuffer
{
return pos;
float4x4 WorldViewProjectionMatrix;
float4x4 WorldMatrix;
};
struct VertexInput
{
float4 position : POSITION;
float4 color : COLOR;
};
struct VertexOutput
{
float4 position : SV_Position;
float4 color : COLOR;
};
VertexOutput main(VertexInput INPUT)
{
// mul -> matrix multiplication
// * -> number/scalar calculation
//return mul(INPUT.position, WorldViewProjectionMatrix);
VertexOutput OUTPUT;
OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix);
OUTPUT.color = INPUT.color;
return OUTPUT;
}
+3 -1
View File
@@ -66,13 +66,15 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
// 7. setup render pipeline (device context)
_pD3DDeviceContext->RSSetViewports(1, &viewport);
_pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, nullptr); // TODO: create depth stencil view and set it for ouput merger stage
_pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, nullptr); // TODO: create depth stencil view and set it for output merger stage
return 0;
}
void Direct3D::beginScene()
{
// TODO: implement a custom color via parameters
// TODO: (optional) implement a random color every frame
FLOAT color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
// 8. clear the buffers
+43 -1
View File
@@ -29,6 +29,9 @@ void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
// setup the input layout
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
// setup the matrix buffer
pD3DDeviceContext->VSSetConstantBuffers(0, 1, &_pMatrixBuffer);
}
void Material::deInit()
@@ -43,6 +46,31 @@ void Material::deInit()
void Material::setMatrixBuffer(ID3D11DeviceContext* pD3DDeviceContext, const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix)
{
// 1. prepare the data
XMMATRIX w = XMLoadFloat4x4(&worldMatrix);
XMMATRIX v = XMLoadFloat4x4(&viewMatrix);
XMMATRIX p = XMLoadFloat4x4(&projectionMatrix);
XMMATRIX wvp = w * v * p;
// 2. map the buffer memory
D3D11_MAPPED_SUBRESOURCE data = {};
HRESULT hr = pD3DDeviceContext->Map(
_pMatrixBuffer, // resource to map
0, // resources can have sub resources
D3D11_MAP_WRITE_DISCARD, // resource map type, read & write access, what happen to the old data
0, // optional flags
&data
);
if (FAILED(hr)) return;
// 3. assign the data
MatrixBuffer* pMatrixBuffer = static_cast<MatrixBuffer*>(data.pData);
XMStoreFloat4x4(&pMatrixBuffer->WorldViewProjectionMatrix, XMMatrixTranspose(wvp));
XMStoreFloat4x4(&pMatrixBuffer->WorldMatrix, XMMatrixTranspose(w));
// 4. unmap the buffer memory
pD3DDeviceContext->Unmap(_pMatrixBuffer, 0);
}
UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
@@ -106,12 +134,17 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShaderCode)
{
D3D11_INPUT_ELEMENT_DESC elements[1] = {};
D3D11_INPUT_ELEMENT_DESC elements[2] = {};
// position
elements[0].SemanticName = "POSITION";
elements[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
// color
elements[1].SemanticName = "COLOR";
elements[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
elements[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
HRESULT hr = pD3DDevice->CreateInputLayout(
elements, // array of input layout elements
std::size(elements), // count of elements
@@ -125,6 +158,15 @@ UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVe
UINT Material::createMatrixBuffer(ID3D11Device* pD3DDevice)
{
D3D11_BUFFER_DESC desc = {};
desc.ByteWidth = sizeof(MatrixBuffer);
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
HRESULT hr = pD3DDevice->CreateBuffer(&desc, nullptr, &_pMatrixBuffer);
CheckFailed(hr, 660);
return 0;
}
+20 -3
View File
@@ -12,6 +12,8 @@ UINT Mesh::init(ID3D11Device* pD3DDevice)
error = createIndexBuffer(pD3DDevice);
CheckError(error);
XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity());
return 0;
}
@@ -56,11 +58,21 @@ void Mesh::deInit()
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
{
// TODO: create a quad (square)
// TODO: create a cube
// 1. define the structure (vertices)
Vertex vertices[] = {
Vertex(0.0f, 1.0f, 0.0f), // 0
Vertex(1.0f, -1.0f, 0.0f), // 1
Vertex(-1.0f, -1.0f, 0.0f) // 2
// triangle
Vertex(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f), // 0
Vertex(1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f), // 1
Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f) // 2
// quad
//Vertex(-0.5f, 0.5f, 0.0f),
//Vertex(0.5f, 0.5f, 0.0f),
//Vertex(0.5f, -0.5f, 0.0f),
//Vertex(-0.5f, -0.5f, 0.0f)
};
_vertexCount = std::size(vertices);
@@ -85,7 +97,12 @@ UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice)
{
USHORT indices[] = {
// triangles
0, 1, 2
// quad
//0, 1, 2,
//0, 2, 3
};
_indexCount = std::size(indices);
+13
View File
@@ -1,12 +1,25 @@
#include "Time.h"
#include <string>
UINT Time::init()
{
_lastTime = CLOCK::now();
return 0;
}
void Time::update()
{
TIME_POINT now = CLOCK::now();
SECONDS diff = now - _lastTime;
_deltaTime = diff.count();
_lastTime = now;
#if _DEBUG
wstring output = L"delta time: " + to_wstring(_deltaTime) + L"\n";
output += L"fps: " + to_wstring(1.0f / _deltaTime) + L"\n";
OutputDebugString(output.c_str());
#endif // _DEBUG
}
void Time::deInit()
+5 -3
View File
@@ -6,8 +6,10 @@ using namespace DirectX;
struct Vertex
{
XMFLOAT3 position;
XMFLOAT4 color;
Vertex() : position(0.0f, 0.0f, 0.0f) {}
Vertex(float x, float y, float z) : position(x, y, z) {}
Vertex(XMFLOAT3 p) : position(p) {}
Vertex() : position(0.0f, 0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f) {}
Vertex(float x, float y, float z) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f) {}
Vertex(XMFLOAT3 p) : position(p), color(1.0f, 1.0f, 1.0f, 1.0f) {}
Vertex(float x, float y, float z, float r, float g, float b) : position(x, y, z), color(r, g, b, 1.0f) {}
};