1
votes

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):

enter image description here

and from position (1, 1, 1):

enter image description here

1

1 Answers

1
votes

It's caused by the near plane clipping. You need to study near/far planes.

You define the near plane on D3D initialization.

// Initialize the Direct3D object.
result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);

Edit: You can read more detailed information from the link below.

https://msdn.microsoft.com/en-us/library/windows/desktop/bb206341(v=vs.85).aspx