EDIT: I haven't set depth-stencil view, can it be a problem?
I'm trying to follow this tutorial, but when I change camera position in XMMatrixLookAtLH
, color of some vertices disappear. It feels like I'm running into undefined behavior.
Here is the creation of world, view and projection matrices packed in ConstantBuffer
:
ConstantBuffer cb;
cb.world = DirectX::XMMatrixIdentity();
cb.view = XMMatrixLookAtLH({ 4.0f, 0.0f, 0.0f, 1.0f }, { 0.0f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 0.0f });
cb.projection = DirectX::XMMatrixPerspectiveFovLH(XM_PI / 2.0f, (float)windowWidth / (float)windowHeight, 0.1f, 110.0f);
The layout is exactly in the order as they are created and layed out in vertex shader, too. I do transpose them. I also tried to declare the structure with alignas(16)
, but it had no effect.
Here is my creation of the buffer:
ZeroMemory(&bd, sizeof(D3D11_BUFFER_DESC));
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.ByteWidth = sizeof(cb);
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
bd.StructureByteStride = 0;
bd.Usage = D3D11_USAGE_DEFAULT;
ZeroMemory(&initialData, sizeof(D3D11_SUBRESOURCE_DATA));
initialData.pSysMem = &cb;
hr = device->CreateBuffer(&bd, &initialData, &constBuffer);
deviceContext->VSSetConstantBuffers(0, 1, &constBuffer);
Compared to tutorial, I'm initializing it upon creation. I've tried to initialize it after, but it didn't change anything.
Here is my vertex shader:
cbuffer ConstantBuffer : register(b0)
{
matrix world;
matrix view;
matrix projection;
}
struct PixelInputType
{
float4 pos:SV_POSITION;
float4 color:COLOR;
};
PixelInputType main(float4 pos : POSITION, float4 color : COLOR)
{
PixelInputType output = (PixelInputType)0;
output.pos = mul(pos, world);
output.pos = mul(output.pos, view);
output.pos = mul(output.pos, projection);
output.color = color;
return output;
}
It is very different from that in tutorial, because I couldn't make their version compile. I tried to comment view transformation, and it worked as intended.
I've also tried to debug the frame using NSight, it showed that triangles are present, but the color is lost.
I have no idea what could be causing such behavior. I'm compiling on x64. I removed checks for readability, but they are present in every call which returns HRESULT
. On fail, the checker always throws. The cube is centered at the origin, exactly as in tutorial.
EDIT: The viewport MinDepth
and MaxDepth
are set to 0.01 and 1 accordingly.
Here is the screenshot from position (4, 0, 0):
and from position (1, 1, 1):