1、更新常量缓存
错误的用m_d3dContext->GSSetConstantBuffers(
0,
1,
m_treeConstantBuffer.GetAddressOf()
);
来代替m_d3dContext->VSSetConstantBuffers(
0,
1,
m_treeConstantBuffer.GetAddressOf()
);
主要原因是搞混了常量缓存的更新和shader类型也是有关系的
2、着色器的语义描述
错误的:
struct VertexShaderInput
{
float3 pos : POSITION;
float3 centerpos:POSITION;
};
正确的:
struct VertexShaderInput
{
float3 pos : POSITION0;
float3 centerpos:POSITION1;
};
const D3D11_INPUT_ELEMENT_DESC mytreeVertexDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "POSITION", 1, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
3、几何着色器设为NULL
已经设置了几何着色器,如果不需要了,需设为NULL。
// 设置几何着色器
m_d3dContext->GSSetShader(
NULL,
nullptr,
0
);
4、更新shader中的矩阵需要转置
错误的:
XMStoreFloat4x4(&m_constantBufferData.model, m_matirxRX*m_matirxRZ*m_matrixRY);
正确的:
XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(m_matirxRX*m_matirxRZ*m_matrixRY));
5、世界坐标变换
output.posW = mul(pos, W);
其中pos需要为(x,y,z,1.0)类型
6、左手坐标系与右手坐标系
XMMatrixLookAtRH为右手坐标系
XMMatrixLookAtLH为左手坐标系
在使用中确定使用一个即可。
7、创建常量缓存大小应为4的倍数
CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
DX::ThrowIfFailed(
m_deviceResources->GetD3DDevice()->CreateBuffer(
&constantBufferDesc,
nullptr,
&m_constantBuffer
));
// Constant buffer used to send MVP matrices to the vertex shader.
struct ModelViewProjectionConstantBuffer
{
DirectX::XMFLOAT4 gEyePosW;
DirectX::XMFLOAT4 gEmitPosW;
DirectX::XMFLOAT4 gEmitDirW;
// 游戏时间
float gGameTime;
// 时间步长
float gTimeStep;
float pad;
float pad2;
DirectX::XMFLOAT4X4 model;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
};
其中pad和pad2为补齐字节。其它buffer目前还没发现特殊的要求。