Add input layout
Add matrix buffer Convert camera to D3D11
This commit is contained in:
+113
-20
@@ -5,39 +5,86 @@
|
||||
|
||||
UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
|
||||
{
|
||||
HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
|
||||
CheckFailed(hr, 600);
|
||||
//HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
|
||||
//CheckFailed(hr, 600);
|
||||
|
||||
UINT error = 0;
|
||||
|
||||
error = createVertexShader(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
error = createPixelShader(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
error = createMatrixBuffer(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||
{
|
||||
// set texture
|
||||
pD3DDevice->SetTexture(0, _pTexture);
|
||||
//// set texture
|
||||
//pD3DDevice->SetTexture(0, _pTexture);
|
||||
|
||||
// set sampler state
|
||||
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
|
||||
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
|
||||
pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(100, 0, 100));
|
||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
|
||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
|
||||
//// set sampler state
|
||||
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
|
||||
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
|
||||
//pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(100, 0, 100));
|
||||
//pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
||||
//pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
||||
//pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
|
||||
//pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
|
||||
|
||||
// set material
|
||||
D3DMATERIAL9 material = {};
|
||||
material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
material.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
material.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
material.Power = 100.0f;
|
||||
material.Emissive = { 1.0f, 0.0f, 0.0f, 1.0f };
|
||||
//// set material
|
||||
//D3DMATERIAL9 material = {};
|
||||
//material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
//material.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
//material.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
//material.Power = 100.0f;
|
||||
//material.Emissive = { 1.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
pD3DDevice->SetMaterial(&material);
|
||||
//pD3DDevice->SetMaterial(&material);
|
||||
|
||||
// set shader
|
||||
pD3DDeviceContext->VSSetShader(_pVertexShader, nullptr, 0);
|
||||
pD3DDeviceContext->PSSetShader(_pPixelShader, nullptr, 0);
|
||||
|
||||
// set input layout
|
||||
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
|
||||
|
||||
// set matrix buffer
|
||||
pD3DDeviceContext->VSSetConstantBuffers(0, 1, &_pMatrixBuffer);
|
||||
}
|
||||
|
||||
void Material::deInit()
|
||||
{
|
||||
safeRelease(_pInputLayout);
|
||||
safeRelease(_pPixelShader);
|
||||
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)
|
||||
@@ -69,6 +116,9 @@ UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
|
||||
);
|
||||
CheckFailed(hr, 605);
|
||||
|
||||
UINT error = createInputLayout(pD3DDevice, pCompiledShaderCode);
|
||||
CheckError(error);
|
||||
|
||||
safeRelease(pCompiledShaderCode);
|
||||
|
||||
return 0;
|
||||
@@ -94,5 +144,48 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
|
||||
|
||||
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShader)
|
||||
{
|
||||
// https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
|
||||
D3D11_INPUT_ELEMENT_DESC inputs[4] = {};
|
||||
|
||||
// position
|
||||
inputs[0].SemanticName = "POSITION";
|
||||
inputs[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
|
||||
// normal
|
||||
inputs[1].SemanticName = "NORMAL";
|
||||
inputs[1].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
inputs[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
|
||||
// uv
|
||||
inputs[2].SemanticName = "TEXCOORD";
|
||||
inputs[2].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
inputs[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
|
||||
// color
|
||||
inputs[3].SemanticName = "COLOR";
|
||||
inputs[3].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||
inputs[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateInputLayout(
|
||||
inputs, std::size(inputs), // array of element descriptions & size of array
|
||||
pCompiledVertexShader->GetBufferPointer(), pCompiledVertexShader->GetBufferSize(), // check vertex compatability with vertex shader
|
||||
&_pInputLayout
|
||||
);
|
||||
CheckFailed(hr, 640);
|
||||
|
||||
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 if D3D11_USAGE_DYNAMIC or D3D11_USAGE_STAGING is used
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateBuffer(&desc, nullptr, &_pMatrixBuffer);
|
||||
CheckFailed(hr, 660);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user