Add matrix buffer
Convert camera to D3D11 Add world, view & projection matrix exposing
This commit is contained in:
+3
-13
@@ -1,9 +1,6 @@
|
|||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
#include <DirectXMath.h>
|
|
||||||
|
|
||||||
using namespace DirectX;
|
UINT Camera::init(UINT width, UINT height)
|
||||||
|
|
||||||
UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
|
|
||||||
{
|
{
|
||||||
// view matrix
|
// view matrix
|
||||||
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
|
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
|
||||||
@@ -11,9 +8,7 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
|
|||||||
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
|
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up);
|
XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up);
|
||||||
XMFLOAT4X4 v;
|
XMStoreFloat4x4(&_viewMatrix, lookTo);
|
||||||
XMStoreFloat4x4(&v, lookTo);
|
|
||||||
_viewMatrix = *reinterpret_cast<D3DMATRIX*>(&v);
|
|
||||||
|
|
||||||
// projection matrix
|
// projection matrix
|
||||||
float fov = XMConvertToRadians(60.0f);
|
float fov = XMConvertToRadians(60.0f);
|
||||||
@@ -22,12 +17,7 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
|
|||||||
float farClippingPlane = 100.0f;
|
float farClippingPlane = 100.0f;
|
||||||
|
|
||||||
XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
|
XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
|
||||||
XMFLOAT4X4 p;
|
XMStoreFloat4x4(&_projectionMatrix, perspective);
|
||||||
XMStoreFloat4x4(&p, perspective);
|
|
||||||
_projectionMatrix = *reinterpret_cast<D3DMATRIX*>(&p);
|
|
||||||
|
|
||||||
pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix);
|
|
||||||
pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-4
@@ -1,15 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d9.h>
|
#include <DirectXMath.h>
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UINT init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height);
|
UINT init(UINT width, UINT height);
|
||||||
void update(FLOAT dt);
|
void update(FLOAT dt);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
const XMFLOAT4X4& getViewMatrix() const { return _viewMatrix; }
|
||||||
|
const XMFLOAT4X4& getProjectionMatrix() const { return _projectionMatrix; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
D3DMATRIX _viewMatrix = {};
|
XMFLOAT4X4 _viewMatrix = {};
|
||||||
D3DMATRIX _projectionMatrix = {};
|
XMFLOAT4X4 _projectionMatrix = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
|
|||||||
error = createPixelShader(pD3DDevice);
|
error = createPixelShader(pD3DDevice);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
|
error = createMatrixBuffer(pD3DDevice);
|
||||||
|
CheckError(error);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +53,9 @@ void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
|
|||||||
|
|
||||||
// set input layoyt
|
// set input layoyt
|
||||||
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
|
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
|
||||||
|
|
||||||
|
// set matrix buffer
|
||||||
|
pD3DDeviceContext->VSSetConstantBuffers(0, 1, &_pMatrixBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Material::deInit()
|
void Material::deInit()
|
||||||
@@ -59,6 +65,29 @@ void Material::deInit()
|
|||||||
safeRelease(_pVertexShader);
|
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<MatrixBuffer*>(data.pData);
|
||||||
|
XMStoreFloat4x4(&pMatrixBuffer->WorldViewProjectionMatrix, XMMatrixTranspose(w * v * p));
|
||||||
|
|
||||||
|
// 4. unmap matrix buffer
|
||||||
|
pD3DDeviceContext->Unmap(_pMatrixBuffer, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
|
UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
|
||||||
{
|
{
|
||||||
HRESULT hr = {};
|
HRESULT hr = {};
|
||||||
@@ -149,3 +178,17 @@ UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pVertexShad
|
|||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d11.h>
|
#include <d3d11.h>
|
||||||
#include <d3dcompiler.h>
|
#include <d3dcompiler.h>
|
||||||
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
#pragma comment(lib, "d3dcompiler.lib")
|
#pragma comment(lib, "d3dcompiler.lib")
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
class Material
|
class Material
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -11,15 +14,27 @@ public:
|
|||||||
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
UINT setMatrices(ID3D11DeviceContext* pD3DDeviceContext,
|
||||||
|
const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UINT createVertexShader(ID3D11Device* pD3DDevice);
|
UINT createVertexShader(ID3D11Device* pD3DDevice);
|
||||||
UINT createPixelShader(ID3D11Device* pD3DDevice);
|
UINT createPixelShader(ID3D11Device* pD3DDevice);
|
||||||
UINT createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pVertexShaderCode);
|
UINT createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pVertexShaderCode);
|
||||||
|
|
||||||
|
UINT createMatrixBuffer(ID3D11Device* pD3DDevice);
|
||||||
|
|
||||||
ID3D11VertexShader* _pVertexShader = nullptr;
|
ID3D11VertexShader* _pVertexShader = nullptr;
|
||||||
ID3D11PixelShader* _pPixelShader = nullptr;
|
ID3D11PixelShader* _pPixelShader = nullptr;
|
||||||
ID3D11InputLayout* _pInputLayout = nullptr;
|
ID3D11InputLayout* _pInputLayout = nullptr;
|
||||||
|
|
||||||
//IDirect3DTexture9* _pTexture = {};
|
//IDirect3DTexture9* _pTexture = {};
|
||||||
|
|
||||||
|
struct MatrixBuffer
|
||||||
|
{
|
||||||
|
XMFLOAT4X4 WorldViewProjectionMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
ID3D11Buffer* _pMatrixBuffer = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ public:
|
|||||||
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UINT createVertexBuffer(ID3D11Device* pD3DDevice);
|
UINT createVertexBuffer(ID3D11Device* pD3DDevice);
|
||||||
UINT createIndexBuffer(ID3D11Device* pD3DDevice);
|
UINT createIndexBuffer(ID3D11Device* pD3DDevice);
|
||||||
|
|||||||
Binary file not shown.
@@ -1,4 +1,9 @@
|
|||||||
|
cbuffer MatrixBuffer
|
||||||
|
{
|
||||||
|
float4x4 WorldViewProjectionMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
float4 main( float4 pos : POSITION ) : SV_POSITION
|
float4 main( float4 pos : POSITION ) : SV_POSITION
|
||||||
{
|
{
|
||||||
return pos;
|
return mul(pos, WorldViewProjectionMatrix);
|
||||||
}
|
}
|
||||||
+9
-6
@@ -37,10 +37,10 @@ int WINAPI WinMain(
|
|||||||
error = mesh.init(d3d.getDevice());
|
error = mesh.init(d3d.getDevice());
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
//// 4. create a camera
|
// 4. create a camera
|
||||||
//Camera camera = {};
|
Camera camera = {};
|
||||||
//error = camera.init(d3d.getDevice(), width, height);
|
error = camera.init(width, height);
|
||||||
//CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
// 5. create time
|
// 5. create time
|
||||||
Time time = {};
|
Time time = {};
|
||||||
@@ -70,9 +70,12 @@ int WINAPI WinMain(
|
|||||||
// 8.1. update objects
|
// 8.1. update objects
|
||||||
time.update();
|
time.update();
|
||||||
mesh.update(time.getDeltaTime());
|
mesh.update(time.getDeltaTime());
|
||||||
//camera.update(time.getDeltaTime());
|
camera.update(time.getDeltaTime());
|
||||||
//light.update(time.getDeltaTime());
|
//light.update(time.getDeltaTime());
|
||||||
|
|
||||||
|
material.setMatrices(
|
||||||
|
d3d.getDeviceContext(), mesh.getWorldMatrix(), camera.getViewMatrix(), camera.getProjectionMatrix());
|
||||||
|
|
||||||
// 8.2. render objects
|
// 8.2. render objects
|
||||||
d3d.beginScene();
|
d3d.beginScene();
|
||||||
|
|
||||||
@@ -87,7 +90,7 @@ int WINAPI WinMain(
|
|||||||
//light.deInit();
|
//light.deInit();
|
||||||
material.deInit();
|
material.deInit();
|
||||||
time.deInit();
|
time.deInit();
|
||||||
//camera.deInit();
|
camera.deInit();
|
||||||
mesh.deInit();
|
mesh.deInit();
|
||||||
d3d.deInit();
|
d3d.deInit();
|
||||||
window.deInit();
|
window.deInit();
|
||||||
|
|||||||
Reference in New Issue
Block a user