我想補充一個正常的地圖信息爲我着色器。我使用DirectX 11 C++ SDK中的教程5作爲基礎。在DirectX 11中爲着色器添加正常信息 - 在着色器中不可見的法線?
着色器創建的代碼(我把它只是要確定我已經發布了每一個重要的信息,但我認爲只有D3D11_INPUT_ELEMENT_DESC佈局[]事項):
// Compile the vertex shader
ID3DBlob* pVSBlob = NULL;
HRESULT hr = CompileShaderFromFile(strToWchart(vertexShaderFileName), "VS", "vs_4_0", &pVSBlob);
// Create the vertex shader
hr = device->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &vertexShader);
if(FAILED(hr))
{
pVSBlob->Release();
}
// Define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 12, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = ARRAYSIZE(layout);
// Create the input layout
hr = device->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), &*vertexLayout);
pVSBlob->Release();
// Set the input layout
context->IASetInputLayout(*vertexLayout);
// Compile the pixel shader
ID3DBlob* pPSBlob = NULL;
hr = CompileShaderFromFile(strToWchart(pixelShaderFileName), "PS", "ps_4_0", &pPSBlob);
// Create the pixel shader
hr = device->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &pixelShader);
pPSBlob->Release();
}
所以乾脆我已經改變D3D11_INPUT_ELEMENT_DESC佈局[]至(加入 「NORMAL」 行):
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 12, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
比我已經改變在箱子幾行創建方法:
// Create vertex buffer
SimpleVertex vertices[] =
{
{ XMFLOAT3( - halfSize.getX(), + halfSize.getY(), - halfSize.getZ()), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
{ XMFLOAT3( + halfSize.getX(), + halfSize.getY(), - halfSize.getZ()), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f), XMFLOAT4(0.0f, 1.0f, 1.0f, 1.0f) },
{ XMFLOAT3( + halfSize.getX(), + halfSize.getY(), + halfSize.getZ()), XMFLOAT4(0.0f, 1.0f, 1.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
{ XMFLOAT3( - halfSize.getX(), + halfSize.getY(), + halfSize.getZ()), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
{ XMFLOAT3( - halfSize.getX(), - halfSize.getY(), - halfSize.getZ()), XMFLOAT4(1.0f, 0.0f, 1.0f, 1.0f), XMFLOAT4(0.0f, 1.0f, 1.0f, 1.0f) },
{ XMFLOAT3( + halfSize.getX(), - halfSize.getY(), - halfSize.getZ()), XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
{ XMFLOAT3( + halfSize.getX(), - halfSize.getY(), + halfSize.getZ()), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f), XMFLOAT4(1.0f, 0.0f, 1.0f, 0.0f) },
{ XMFLOAT3( - halfSize.getX(), - halfSize.getY(), + halfSize.getZ()), XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
};
因此,在所有行的最後XMFLOAT4是正常的(不是重要的是,這些都是錯誤的法線,現在我只想讓它在頂點着色器中可見)。
我也改變了SimpleVertex:
struct SimpleVertex
{
XMFLOAT3 Pos;
XMFLOAT4 Color;
XMFLOAT4 Normal;
};
而我的着色器,以檢查是否存在法線:
cbuffer ConstantBuffer : register(b0)
{
matrix World;
matrix View;
matrix Projection;
}
struct VS_INPUT
{
float4 Pos : POSITION;
float4 Color : COLOR;
float4 Normal : NORMAL;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
};
PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT)0;
output.Pos = mul(input.Pos, World);
output.Pos = mul(output.Pos, View);
output.Pos = mul(output.Pos, Projection);
output.Color = input.Normal; //just to know if I have a normals
return output;
}
float4 PS(PS_INPUT input) : SV_Target
{
return input.Color;
}
但效果只是一個黑盒子(甚至output.Color =輸入。正常)。
那麼,有什麼問題嗎?我必須改變其他東西才能在着色器中獲得法線信息?
就是這樣。謝謝。 – PolGraphic