Wrong depth sorting for two directions (D3D11)

Hello,

I'm trying to play around with direct 3D 11, as I previously only used opengl.
But I have this depth sorting bug, which I am unable to solve atm and I wasted quite some time already trying to figure it out.
Here is a short showcase of what is happening:


It seems the sorting is in reverse order or something. But I cannot find a solution that worked by just searching the web.
Maybe I am doing something basic wrong.

Here is the code:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
internal direct_3d_info
InitializeDirect3D(arena_allocator FixArena, arena_allocator ScratchArena, HWND Window)
{
    direct_3d_info Result = {};
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    D3D_FEATURE_LEVEL FeatureLevels[] = { D3D_FEATURE_LEVEL_11_0 };
    
    ID3D11Device*        BaseDevice;
    ID3D11DeviceContext* BaseDeviceContext;
    
    D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_BGRA_SUPPORT, FeatureLevels,
                      ARRAYSIZE(FeatureLevels), D3D11_SDK_VERSION, &BaseDevice, 0, &BaseDeviceContext);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    BaseDevice->QueryInterface(__uuidof(ID3D11Device1), reinterpret_cast<void**>(&Result.Device));
    
    BaseDeviceContext->QueryInterface(__uuidof(ID3D11DeviceContext1), reinterpret_cast<void**>(&Result.DeviceContext));
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    IDXGIDevice1* DxgiDevice;
    
    Result.Device->QueryInterface(__uuidof(IDXGIDevice1), reinterpret_cast<void**>(&DxgiDevice));
    
    IDXGIAdapter* DxgiAdapter;
    
    DxgiDevice->GetAdapter(&DxgiAdapter);
    
    IDXGIFactory2* DxgiFactory;
    
    DxgiAdapter->GetParent(__uuidof(IDXGIFactory2), reinterpret_cast<void**>(&DxgiFactory));
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    DXGI_SWAP_CHAIN_DESC1 SwapChainDesc;
    SwapChainDesc.Width              = 0; // use window width
    SwapChainDesc.Height             = 0; // use window height
    SwapChainDesc.Format             = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
    SwapChainDesc.Stereo             = FALSE;
    SwapChainDesc.SampleDesc.Count   = 1;
    SwapChainDesc.SampleDesc.Quality = 0;
    SwapChainDesc.BufferUsage        = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    SwapChainDesc.BufferCount        = 2;
    SwapChainDesc.Scaling            = DXGI_SCALING_STRETCH;
    SwapChainDesc.SwapEffect         = DXGI_SWAP_EFFECT_DISCARD;
    SwapChainDesc.AlphaMode          = DXGI_ALPHA_MODE_UNSPECIFIED;
    SwapChainDesc.Flags              = 0;
    
    DxgiFactory->CreateSwapChainForHwnd(Result.Device, Window, &SwapChainDesc, 0, 0, &Result.SwapChain);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    ID3D11Texture2D* FrameBuffer;
    
    Result.SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&FrameBuffer));
    
    Result.Device->CreateRenderTargetView(FrameBuffer, 0, &Result.FrameBufferView);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    D3D11_TEXTURE2D_DESC DepthBufferDesc;
    
    FrameBuffer->GetDesc(&DepthBufferDesc); // base on framebuffer properties
    DepthBufferDesc.Format    = DXGI_FORMAT_D24_UNORM_S8_UINT;
    DepthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    
    ID3D11Texture2D* DepthBuffer = 0;
    Result.Device->CreateTexture2D(&DepthBufferDesc, 0, &DepthBuffer);
    
    D3D11_DEPTH_STENCIL_VIEW_DESC DepthStencilViewDesc;
    DepthStencilViewDesc.Format             = DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
    DepthStencilViewDesc.ViewDimension      = D3D11_DSV_DIMENSION_TEXTURE2D;
    DepthStencilViewDesc.Texture2D.MipSlice = 0;
    
    // Create the depth stencil view
    Result.Device->CreateDepthStencilView(DepthBuffer, // Depth stencil texture
                                          &DepthStencilViewDesc, // Depth stencil desc
                                          &Result.DepthBufferView);  // [out] Depth stencil view
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    ID3DBlob* VsBlob;
    D3DCompileFromFile(L"..\\code\\Shaders\\BaseShaders.hlsl", nullptr, nullptr, "vs_main", "vs_5_0", 0, 0, &VsBlob, nullptr);
    
    Result.Device->CreateVertexShader(VsBlob->GetBufferPointer(), VsBlob->GetBufferSize(), 0, &Result.VertexShader);
    
    D3D11_INPUT_ELEMENT_DESC InputElementDesc[] = // float3 position, float3 normal, float2 texcoord, float3 color
    {
        { "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,                            0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "NOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "TEX", 0, DXGI_FORMAT_R32G32_FLOAT,    0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };
    
    Result.Device->CreateInputLayout(InputElementDesc, ARRAYSIZE(InputElementDesc), VsBlob->GetBufferPointer(), VsBlob->GetBufferSize(), &Result.InputLayout);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    ID3DBlob* PsBlob;
    D3DCompileFromFile(L"..\\code\\Shaders\\BaseShaders.hlsl", 0, 0, "ps_main", "ps_5_0", 0, 0, &PsBlob, 0);
    
    Result.Device->CreatePixelShader(PsBlob->GetBufferPointer(), PsBlob->GetBufferSize(), 0, &Result.PixelShader);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    D3D11_RASTERIZER_DESC1 RasterizerDesc = {};
    RasterizerDesc.FillMode = D3D11_FILL_SOLID;
    RasterizerDesc.CullMode = D3D11_CULL_BACK;
    
    Result.Device->CreateRasterizerState1(&RasterizerDesc, &Result.RasterizerState);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    D3D11_DEPTH_STENCIL_DESC DepthStencilDesc = {};
    DepthStencilDesc.DepthEnable    = TRUE;
    DepthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    DepthStencilDesc.DepthFunc      = D3D11_COMPARISON_LESS;
    
    // Stencil test parameters
    DepthStencilDesc.StencilEnable    = false;
    DepthStencilDesc.StencilReadMask  = 0xFF;
    DepthStencilDesc.StencilWriteMask = 0xFF;
    
    // Stencil operations if pixel is front-facing
    DepthStencilDesc.FrontFace.StencilFailOp      = D3D11_STENCIL_OP_KEEP;
    DepthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
    DepthStencilDesc.FrontFace.StencilPassOp      = D3D11_STENCIL_OP_KEEP;
    DepthStencilDesc.FrontFace.StencilFunc        = D3D11_COMPARISON_ALWAYS;
    
    // Stencil operations if pixel is back-facing
    DepthStencilDesc.BackFace.StencilFailOp      = D3D11_STENCIL_OP_KEEP;
    DepthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
    DepthStencilDesc.BackFace.StencilPassOp      = D3D11_STENCIL_OP_KEEP;
    DepthStencilDesc.BackFace.StencilFunc        = D3D11_COMPARISON_ALWAYS;
    
    Result.Device->CreateDepthStencilState(&DepthStencilDesc, &Result.DepthStencilState);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    D3D11_BUFFER_DESC ConstantBufferDesc = {};
    ConstantBufferDesc.ByteWidth      = sizeof(d3d11_constants) + 0xf & 0xfffffff0;
    ConstantBufferDesc.Usage          = D3D11_USAGE_DYNAMIC;
    ConstantBufferDesc.BindFlags      = D3D11_BIND_CONSTANT_BUFFER;
    ConstantBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    
    Result.Device->CreateBuffer(&ConstantBufferDesc, 0, &Result.ConstantBuffer);
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    Result.DepthBufferWidth  = static_cast<r32>(DepthBufferDesc.Width);  // width
    Result.DepthBufferHeight = static_cast<r32>(DepthBufferDesc.Height); // height
    Result.NearClipping      = 1000.0f;                                    // near
    Result.FarClipping       = 1000000.0f;                                 // far
    
    return Result;
}

internal ID3D11Buffer *
CreateVertexData(direct_3d_info *D3D11Info, vertex_array VertexData)
{
    ID3D11Buffer *Result = 0;
    
    D3D11_BUFFER_DESC VertexBufferDesc = {};
    VertexBufferDesc.ByteWidth = sizeof(r32)*VertexData.Count;
    VertexBufferDesc.Usage     = D3D11_USAGE_IMMUTABLE;
    VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    
    D3D11_SUBRESOURCE_DATA D3D11VertexData = { VertexData.Data };
    
    D3D11Info->Device->CreateBuffer(&VertexBufferDesc, &D3D11VertexData, &Result);
    return Result;
}

internal ID3D11Buffer *
CreateIndexData(direct_3d_info *D3D11Info, index_array IndexData)
{
    ID3D11Buffer *Result = 0;
    
    D3D11_BUFFER_DESC IndexBufferDesc = {};
    IndexBufferDesc.ByteWidth = sizeof(u32)*IndexData.Count;
    IndexBufferDesc.Usage     = D3D11_USAGE_IMMUTABLE;
    IndexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    
    D3D11_SUBRESOURCE_DATA D3D11IndexData = { IndexData.Data };
    
    D3D11Info->Device->CreateBuffer(&IndexBufferDesc, &D3D11IndexData, &Result);
    
    return Result;
}

internal d3d11_texture
CreateTexture(direct_3d_info *D3D11Info, texture_info TextureInfo)
{
    d3d11_texture Result = {};
    
    D3D11_TEXTURE2D_DESC TextureDesc = {};
    TextureDesc.Width              = TextureInfo.Width;  // width in texels
    TextureDesc.Height             = TextureInfo.Height; // height in texels
    TextureDesc.MipLevels          = 1;
    TextureDesc.ArraySize          = 1;
    TextureDesc.Format             = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
    TextureDesc.SampleDesc.Count   = 1;
    TextureDesc.Usage              = D3D11_USAGE_IMMUTABLE;
    TextureDesc.BindFlags          = D3D11_BIND_SHADER_RESOURCE;
    
    D3D11_SUBRESOURCE_DATA D3D11TextureData = {};
    D3D11TextureData.pSysMem            = TextureInfo.Data;
    D3D11TextureData.SysMemPitch        = TextureInfo.Width * sizeof(u32); // 4 bytes per pixel
    
    ID3D11Texture2D* Texture;
    
    D3D11Info->Device->CreateTexture2D(&TextureDesc, &D3D11TextureData, &Texture);
    
    D3D11Info->Device->CreateShaderResourceView(Texture, 0, &Result.View);
    
    D3D11_SAMPLER_DESC SamplerDesc = {};
    SamplerDesc.Filter         = D3D11_FILTER_MIN_MAG_MIP_POINT;
    SamplerDesc.AddressU       = D3D11_TEXTURE_ADDRESS_WRAP;
    SamplerDesc.AddressV       = D3D11_TEXTURE_ADDRESS_WRAP;
    SamplerDesc.AddressW       = D3D11_TEXTURE_ADDRESS_WRAP;
    SamplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    
    D3D11Info->Device->CreateSamplerState(&SamplerDesc, &Result.SamplerState);
    
    return Result;
}

internal void
Direct3DDraw(direct_3d_info *D3D11Info, d3d11_render_object *Camera)
{
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    
    FLOAT BackgroundColor[4] = { 0.025f, 0.025f, 0.025f, 1.0f};
    
    UINT Stride = 11 * 4; // vertex size (11 floats: float3 position, float3 normal, float2 texcoord, float3 color)
    UINT Offset = 0;
    
    D3D11_VIEWPORT Viewport = { 0.0f, 0.0f, D3D11Info->DepthBufferWidth, D3D11Info->DepthBufferHeight, 0.0f, 1.0f };
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    D3D11Info->DeviceContext->ClearRenderTargetView(D3D11Info->FrameBufferView, BackgroundColor);
    D3D11Info->DeviceContext->ClearDepthStencilView(D3D11Info->DepthBufferView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 
                                                    1.0f, 0);
    
    D3D11Info->DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    D3D11Info->DeviceContext->IASetInputLayout(D3D11Info->InputLayout);
    
    D3D11Info->DeviceContext->OMSetRenderTargets(1, &D3D11Info->FrameBufferView, D3D11Info->DepthBufferView);
    D3D11Info->DeviceContext->OMSetDepthStencilState(D3D11Info->DepthStencilState, 1);
    D3D11Info->DeviceContext->OMSetBlendState(0, 0, 0xffffffff); // use default blend mode (i.e. disable)
    
    D3D11Info->DeviceContext->VSSetShader(D3D11Info->VertexShader, 0, 0);
    D3D11Info->DeviceContext->VSSetConstantBuffers(0, 1, &D3D11Info->ConstantBuffer);
    
    D3D11Info->DeviceContext->RSSetViewports(1, &Viewport);
    D3D11Info->DeviceContext->RSSetState(D3D11Info->RasterizerState);
    
    D3D11Info->DeviceContext->PSSetShader(D3D11Info->PixelShader, 0, 0);
    
    m4 Projection = 
    {
        2 * D3D11Info->NearClipping / D3D11Info->DepthBufferWidth, 0, 0, 0, 
        0, 2 * D3D11Info->NearClipping / D3D11Info->DepthBufferHeight, 0, 0, 
        0, 0, D3D11Info->FarClipping / (D3D11Info->FarClipping - D3D11Info->NearClipping), 1, 
        0, 0, D3D11Info->NearClipping * D3D11Info->FarClipping / (D3D11Info->NearClipping - D3D11Info->FarClipping), 0 
    };
    m4 CamMatrix = Invert(&Camera->T._Transform);
    For(D3D11Info->ObjectList.Count)
    {
        d3d11_render_object *Object = D3D11Info->ObjectList.Objects+It;
        if(!Object->Render) continue;
        
        D3D11_MAPPED_SUBRESOURCE MappedSubresource;
        D3D11Info->DeviceContext->Map(D3D11Info->ConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedSubresource);
        
        d3d11_constants *Constants = reinterpret_cast<d3d11_constants *>(MappedSubresource.pData);
        Constants->Transform   = Object->T._Transform*CamMatrix;
        Constants->Projection  = Projection;
        Constants->LightVector = { 1.0f, -1.0f, 1.0f };
        
        D3D11Info->DeviceContext->Unmap(D3D11Info->ConstantBuffer, 0);
        
        ///////////////////////////////////////////////////////////////////////////////////////////
        
        D3D11Info->DeviceContext->IASetVertexBuffers(0, 1, &Object->VertexBuffer, &Stride, &Offset);
        D3D11Info->DeviceContext->IASetIndexBuffer(Object->IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
        
        D3D11Info->DeviceContext->PSSetShaderResources(0, 1, &Object->Texture.View);
        D3D11Info->DeviceContext->PSSetSamplers(0, 1, &Object->Texture.SamplerState);
        
        ///////////////////////////////////////////////////////////////////////////////////////////
        
        D3D11Info->DeviceContext->DrawIndexed(Object->IndexCount, 0, 0);
    }
    ///////////////////////////////////////////////////////////////////////////////////////////
    
    D3D11Info->SwapChain->Present(1, 0);
}


I assume I'm just missing something basic.

Ah, almost forgot. The code is based/used from here: https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052

Thanks in advance for the help!

Edited by Tim Dierks on
I don't think that has anything to do with depth sorting. It looks like polygons are culled because they are classified as back facing. Try disabling back face culling in RasterizerDesc.CullMode. Or fix the vertex order in polygon for it to not be back face.
Thanks for the reply.

I tried your suggestions but it didn't seem to work.
When I use D3D11_CULL_NONE it looks exactly the same, with D3D11_CULL_FRONT it looks like the problems are similar, but different.


I also tried switching the triangle direction, but the result was exactly the same except inverted for the cull modes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
internal index_array
SwizzleIndices(index_array Indices)
{
    For(Indices.Count/3)
    {
        i32 I = It*3;
        u32 Tmp = Indices.Data[I+1];
        Indices.Data[I+1] = Indices.Data[I+2];
        Indices.Data[I+2] = Tmp;
    }
    
    return Indices;
}


Edited by Tim Dierks on
I don't know anything about DirectX but you seem to initialize the depth stencil buffer differently than in the gist code. I suppose it's because you want to do something with that, but does it work if you remove the change ?

Your code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    D3D11_TEXTURE2D_DESC DepthBufferDesc;
    
    FrameBuffer->GetDesc(&DepthBufferDesc); // base on framebuffer properties
    DepthBufferDesc.Format    = DXGI_FORMAT_D24_UNORM_S8_UINT;
    DepthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    
    ID3D11Texture2D* DepthBuffer = 0;
    Result.Device->CreateTexture2D(&DepthBufferDesc, 0, &DepthBuffer);
    
    D3D11_DEPTH_STENCIL_VIEW_DESC DepthStencilViewDesc;
    DepthStencilViewDesc.Format             = DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
    DepthStencilViewDesc.ViewDimension      = D3D11_DSV_DIMENSION_TEXTURE2D;
    DepthStencilViewDesc.Texture2D.MipSlice = 0;
    
    // Create the depth stencil view
    Result.Device->CreateDepthStencilView(DepthBuffer, // Depth stencil texture
                                          &DepthStencilViewDesc, // Depth stencil desc
                                          &Result.DepthBufferView);  // [out] Depth stencil view


From github:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    D3D11_TEXTURE2D_DESC depthBufferDesc;

    frameBuffer->GetDesc(&depthBufferDesc); // base on framebuffer properties

    depthBufferDesc.Format    = DXGI_FORMAT_D24_UNORM_S8_UINT;
    depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;

    ID3D11Texture2D* depthBuffer;

    device->CreateTexture2D(&depthBufferDesc, nullptr, &depthBuffer);

    ID3D11DepthStencilView* depthBufferView;

    device->CreateDepthStencilView(depthBuffer, nullptr, &depthBufferView);
Oh, what mrmixer noticed is interesting - your depth/stencil view has DXGI_FORMAT_D32_FLOAT_S8X24_UINT format, but your actual texture DXGI_FORMAT_D24_UNORM_S8_UINT. Not sure exactly what that means for runtime... In this situation it is better either match formats, or just pass NULL when creating view, that will make it automatically use same format as texture.
@mrmixer, Yes, adding that was part of me trying to find the bug. As I thought it has something to do with the depth buffer, or something like that. But in the end, it really only switches the two axes in which the rendering buggy.

@mmozeiko
Interestingly, changing the Format to DXGI_FORMAT_D24_UNORM_S8_UINT results in the same result as if I would not use the D3D11_DEPTH_STENCIL_VIEW_DESC at all.
It reverses the two axis in which you have to look to produce the visual error (from +x/+z to -x/-z, or something like that).


Man, you guys are awesome. Even if we don't find the bug, thank you for the help either way!

I put the project on github here: https://github.com/CaptainTimberTim/DungeonDirect3D
Maybe it helps to have the whole thing (Direct3D_11_TD.c/.h are the important files).

Edited by Tim Dierks on
You're missing data/Models/Room_001.obj file in repository.
Oh damn, somehow I had them in the .gitignore.

Now everything is there.
I don't know if it helps, but when I take a capture with RenderDoc, the depth buffer isn't correct. Each draw call set's the affected pixels to 0 (the clears sets them to 1) which means nothing will be written after the pixel as been drawn once. There is no "gradient" depending on the depth.
Your projection matrix is super strange. No idea why near plane is multiplied into first two diagonal components.
It looks like everything is inverted inside out.
If I change projection matrix to following:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    float fov = 60.f;
    float t = 1.f / tanf(fov);
    float aspect = (float)D3D11Info->DepthBufferWidth / (float)D3D11Info->DepthBufferHeight;

    m4 Projection =
    {
        t / aspect, 0, 0, 0,
        0, t, 0, 0,
        0, 0, D3D11Info->FarClipping / (D3D11Info->FarClipping - D3D11Info->NearClipping), 1,
        0, 0, D3D11Info->NearClipping * D3D11Info->FarClipping / (D3D11Info->NearClipping - D3D11Info->FarClipping), 0 
    };

And your near/far clip planes to following:
1
2
    Result.NearClipping      = 0.1f;                                    // near 
    Result.FarClipping       = 1000.0f;                                 // far

then everything renders as expected (aside some z-fighting).

Edited by Mārtiņš Možeiko on
Ah! Thank you!
That is something I would never have found... I am not very firm in projection matrix stuff.
Especially, because it kinda looked correct most of the time, except the weird culling thing.

I got the matrix from the same sample code as the rest of the Direct3D stuff, here:
https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052
And it looks the same.


Now I can finally actually start doing stuff, yay!

Thank you very much.