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 f8ff2f9..bf094dd 100644 Binary files a/GDP/D3D11/VertexShader.cso and b/GDP/D3D11/VertexShader.cso differ diff --git a/GDP/D3D11/VertexShader.hlsl b/GDP/D3D11/VertexShader.hlsl index 94efe03..e2b675d 100644 --- a/GDP/D3D11/VertexShader.hlsl +++ b/GDP/D3D11/VertexShader.hlsl @@ -1,4 +1,9 @@ +cbuffer MatrixBuffer +{ + float4x4 WorldViewProjectionMatrix; +}; + float4 main( float4 pos : POSITION ) : SV_POSITION { - return pos; + return mul(pos, WorldViewProjectionMatrix); } \ No newline at end of file diff --git a/GDP/D3D11/main.cpp b/GDP/D3D11/main.cpp index 37faf48..595df54 100644 --- a/GDP/D3D11/main.cpp +++ b/GDP/D3D11/main.cpp @@ -37,10 +37,10 @@ int WINAPI WinMain( error = mesh.init(d3d.getDevice()); CheckError(error); - //// 4. create a camera - //Camera camera = {}; - //error = camera.init(d3d.getDevice(), width, height); - //CheckError(error); + // 4. create a camera + Camera camera = {}; + error = camera.init(width, height); + CheckError(error); // 5. create time Time time = {}; @@ -70,9 +70,12 @@ int WINAPI WinMain( // 8.1. update objects time.update(); mesh.update(time.getDeltaTime()); - //camera.update(time.getDeltaTime()); + camera.update(time.getDeltaTime()); //light.update(time.getDeltaTime()); + material.setMatrices( + d3d.getDeviceContext(), mesh.getWorldMatrix(), camera.getViewMatrix(), camera.getProjectionMatrix()); + // 8.2. render objects d3d.beginScene(); @@ -87,7 +90,7 @@ int WINAPI WinMain( //light.deInit(); material.deInit(); time.deInit(); - //camera.deInit(); + camera.deInit(); mesh.deInit(); d3d.deInit(); window.deInit();