195 lines
6.1 KiB
C++
195 lines
6.1 KiB
C++
#include "Material.h"
|
|
#include "Utilities.h"
|
|
//#include "WICTextureLoader9.h"
|
|
|
|
//using namespace DirectX;
|
|
|
|
UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
|
|
{
|
|
//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 sampler state
|
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
|
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
|
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(200, 0, 200));
|
|
//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.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
|
|
//material.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
|
|
//material.Power = 100.0f;
|
|
//material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
|
|
//material.Emissive = { 0.0f, 0.0f, 0.0f, 1.0f };
|
|
|
|
//pD3DDevice->SetMaterial(&material);
|
|
|
|
// set shaders
|
|
pD3DDeviceContext->VSSetShader(_pVertexShader, nullptr, 0);
|
|
pD3DDeviceContext->PSSetShader(_pPixelShader, nullptr, 0);
|
|
|
|
// set input layoyt
|
|
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)
|
|
{
|
|
HRESULT hr = {};
|
|
ID3DBlob* pCompiledShaderCode = nullptr;
|
|
|
|
// two options of creating a shader object
|
|
// option 1: compile at runtime
|
|
//hr = D3DCompileFromFile(
|
|
// L"VertexShader.hlsl", // shader filename
|
|
// nullptr, nullptr, // additional shared shader code (optional)
|
|
// "main", // shader entry function name
|
|
// "vs_4_0", // shader type & model (version)
|
|
// 0, 0, // optional flags
|
|
// &pCompiledShaderCode,
|
|
// nullptr // optional blob for error messages
|
|
//);
|
|
|
|
// option 2: load compiled shader
|
|
hr = D3DReadFileToBlob(L"VertexShader.cso", &pCompiledShaderCode);
|
|
|
|
CheckFailed(hr, 600);
|
|
|
|
// create shader object
|
|
hr = pD3DDevice->CreateVertexShader(
|
|
pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), // compiled shader code
|
|
nullptr, // again shared shader informations
|
|
&_pVertexShader
|
|
);
|
|
CheckFailed(hr, 605);
|
|
|
|
UINT error = createInputLayout(pD3DDevice, pCompiledShaderCode);
|
|
CheckError(error);
|
|
|
|
safeRelease(pCompiledShaderCode);
|
|
|
|
return 0;
|
|
}
|
|
|
|
UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
|
|
{
|
|
HRESULT hr = {};
|
|
ID3DBlob* pCompiledShaderCode = nullptr;
|
|
|
|
hr = D3DReadFileToBlob(L"PixelShader.cso", &pCompiledShaderCode);
|
|
CheckFailed(hr, 620);
|
|
|
|
// create shader object
|
|
hr = pD3DDevice->CreatePixelShader(
|
|
pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), // compiled shader code
|
|
nullptr, // again shared shader informations
|
|
&_pPixelShader
|
|
);
|
|
CheckFailed(hr, 625);
|
|
|
|
return 0;
|
|
}
|
|
|
|
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pVertexShaderCode)
|
|
{
|
|
// 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), // element description array and array size
|
|
pVertexShaderCode->GetBufferPointer(), pVertexShaderCode->GetBufferSize(), // vertex shader code to check compatability with vertex
|
|
&_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 when D3D11_USAGE_DYNAMIC or D3D11_USAGE_STAGING are used
|
|
|
|
HRESULT hr = pD3DDevice->CreateBuffer(&desc, nullptr, &_pMatrixBuffer);
|
|
CheckFailed(hr, 660);
|
|
|
|
return 0;
|
|
}
|