Added: matrix buffer
Added: world, view & projection matrices Added: delta time calculation Added: quad mesh
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user