From c6fdf567b7e65dc0d681d38e0630ce552679bb23 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Fri, 27 Nov 2026 12:17:17 +0100 Subject: [PATCH] Add matrix buffer Convert camera to D3D11 Add world, view & projection matrix exposing --- GDP/D3D11/Camera.cpp | 18 ++++----------- GDP/D3D11/Camera.h | 14 ++++++++---- GDP/D3D11/Material.cpp | 43 ++++++++++++++++++++++++++++++++++++ GDP/D3D11/Material.h | 15 +++++++++++++ GDP/D3D11/Mesh.h | 2 ++ GDP/D3D11/VertexShader.cso | Bin 12672 -> 13060 bytes GDP/D3D11/VertexShader.hlsl | 7 +++++- GDP/D3D11/main.cpp | 15 ++++++++----- 8 files changed, 89 insertions(+), 25 deletions(-) diff --git a/GDP/D3D11/Camera.cpp b/GDP/D3D11/Camera.cpp index 40a2d32..f882c86 100644 --- a/GDP/D3D11/Camera.cpp +++ b/GDP/D3D11/Camera.cpp @@ -1,9 +1,6 @@ #include "Camera.h" -#include -using namespace DirectX; - -UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height) +UINT Camera::init(UINT width, UINT height) { // view matrix XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f); @@ -11,10 +8,8 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height) XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up); - XMFLOAT4X4 v; - XMStoreFloat4x4(&v, lookTo); - _viewMatrix = *reinterpret_cast(&v); - + XMStoreFloat4x4(&_viewMatrix, lookTo); + // projection matrix float fov = XMConvertToRadians(60.0f); float aspectRatio = static_cast(width) / static_cast(height); @@ -22,12 +17,7 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height) float farClippingPlane = 100.0f; XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane); - XMFLOAT4X4 p; - XMStoreFloat4x4(&p, perspective); - _projectionMatrix = *reinterpret_cast(&p); - - pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix); - pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix); + XMStoreFloat4x4(&_projectionMatrix, perspective); return 0; } diff --git a/GDP/D3D11/Camera.h b/GDP/D3D11/Camera.h index 2922d26..8745026 100644 --- a/GDP/D3D11/Camera.h +++ b/GDP/D3D11/Camera.h @@ -1,15 +1,21 @@ #pragma once -#include +#include +#include + +using namespace DirectX; class Camera { public: - UINT init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height); + UINT init(UINT width, UINT height); void update(FLOAT dt); void deInit(); + const XMFLOAT4X4& getViewMatrix() const { return _viewMatrix; } + const XMFLOAT4X4& getProjectionMatrix() const { return _projectionMatrix; } + private: - D3DMATRIX _viewMatrix = {}; - D3DMATRIX _projectionMatrix = {}; + XMFLOAT4X4 _viewMatrix = {}; + XMFLOAT4X4 _projectionMatrix = {}; }; diff --git a/GDP/D3D11/Material.cpp b/GDP/D3D11/Material.cpp index 647450a..1d54056 100644 --- a/GDP/D3D11/Material.cpp +++ b/GDP/D3D11/Material.cpp @@ -17,6 +17,9 @@ UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName) error = createPixelShader(pD3DDevice); CheckError(error); + error = createMatrixBuffer(pD3DDevice); + CheckError(error); + return 0; } @@ -50,6 +53,9 @@ void Material::render(ID3D11DeviceContext* pD3DDeviceContext) // set input layoyt pD3DDeviceContext->IASetInputLayout(_pInputLayout); + + // set matrix buffer + pD3DDeviceContext->VSSetConstantBuffers(0, 1, &_pMatrixBuffer); } void Material::deInit() @@ -59,6 +65,29 @@ void Material::deInit() safeRelease(_pVertexShader); } +UINT Material::setMatrices(ID3D11DeviceContext* pD3DDeviceContext, + const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix) +{ + // 1. load matrix data + XMMATRIX w = XMLoadFloat4x4(&worldMatrix); + XMMATRIX v = XMLoadFloat4x4(&viewMatrix); + XMMATRIX p = XMLoadFloat4x4(&projectionMatrix); + + // 2. map matrix buffer + D3D11_MAPPED_SUBRESOURCE data = {}; + HRESULT hr = pD3DDeviceContext->Map(_pMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data); + CheckFailed(hr, 665); + + // 3. set data + MatrixBuffer* pMatrixBuffer = static_cast(data.pData); + XMStoreFloat4x4(&pMatrixBuffer->WorldViewProjectionMatrix, XMMatrixTranspose(w * v * p)); + + // 4. unmap matrix buffer + pD3DDeviceContext->Unmap(_pMatrixBuffer, 0); + + return 0; +} + UINT Material::createVertexShader(ID3D11Device* pD3DDevice) { HRESULT hr = {}; @@ -149,3 +178,17 @@ UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pVertexShad return 0; } + +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; // has to be set when D3D11_USAGE_DYNAMIC or D3D11_USAGE_STAGING are used + + HRESULT hr = pD3DDevice->CreateBuffer(&desc, nullptr, &_pMatrixBuffer); + CheckFailed(hr, 660); + + return 0; +} diff --git a/GDP/D3D11/Material.h b/GDP/D3D11/Material.h index 02fb0ef..ad13285 100644 --- a/GDP/D3D11/Material.h +++ b/GDP/D3D11/Material.h @@ -1,9 +1,12 @@ #pragma once #include #include +#include #pragma comment(lib, "d3dcompiler.lib") +using namespace DirectX; + class Material { public: @@ -11,15 +14,27 @@ public: void render(ID3D11DeviceContext* pD3DDeviceContext); void deInit(); + UINT setMatrices(ID3D11DeviceContext* pD3DDeviceContext, + const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix); + private: UINT createVertexShader(ID3D11Device* pD3DDevice); UINT createPixelShader(ID3D11Device* pD3DDevice); UINT createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pVertexShaderCode); + UINT createMatrixBuffer(ID3D11Device* pD3DDevice); + ID3D11VertexShader* _pVertexShader = nullptr; ID3D11PixelShader* _pPixelShader = nullptr; ID3D11InputLayout* _pInputLayout = nullptr; //IDirect3DTexture9* _pTexture = {}; + + struct MatrixBuffer + { + XMFLOAT4X4 WorldViewProjectionMatrix; + }; + + ID3D11Buffer* _pMatrixBuffer = nullptr; }; diff --git a/GDP/D3D11/Mesh.h b/GDP/D3D11/Mesh.h index 7601877..5f30f23 100644 --- a/GDP/D3D11/Mesh.h +++ b/GDP/D3D11/Mesh.h @@ -12,6 +12,8 @@ public: void render(ID3D11DeviceContext* pD3DDeviceContext); void deInit(); + const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; } + private: UINT createVertexBuffer(ID3D11Device* pD3DDevice); UINT createIndexBuffer(ID3D11Device* pD3DDevice); diff --git a/GDP/D3D11/VertexShader.cso b/GDP/D3D11/VertexShader.cso index f8ff2f929f0525e1a00e05cd1023829e4ad85ef5..bf094dd6b05a029a246b329e41b31a9563c456e8 100644 GIT binary patch literal 13060 zcmeHOT}%{L6h5=NR#*Xn;=i#IOA}BFi_rLQ5LSc)6@JF}ZHP6k z4~?OzF+MaYk0!ie(k8~fnA9|;zL=&DO(SX3n#MFu4AeADADDjM%pKesmTI6@a1J?h z?m73~bI(0@_WqnZ1P|5Je*VGFI|uWA_;%#3cj)Sik=Hd5@l}e<2i*$n_lQ(`Md~X= zt^+?=FLDdGBOcqd6?sJQjGI}YRY+=5jAuY$F?gn-6DbEYlpi-E60F(3s|3PBDvKlv z%mN+*Qgr~rVbI#m;r&S?kqmc5+Imiglkwg}n-NYJ$Knv3NURU6uWF5icLkfm!OCDo zMfiY`NEv;hu1LF)DC>$QqcSwawnR+xL+E517Ztsv=oLk8fIf%(?-aeO=%1iRBi%8m z8q&mz9zEz4vH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