DirectX 11 - Depth Buffer

I'm working on a game in DirectX for university.

I have been advised to add a depth buffer into the project, which I have done.
For some reason, when the program is run no objects appear on the screen which rendered before adding the depth buffer in.

Creation of Depth Buffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	//Describe the Depth/Stencil Buffer
	D3D11_TEXTURE2D_DESC db;
	db.Width = Width;
	db.Height = Height;
	db.MipLevels = 1;
	db.ArraySize = 1;
	db.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	db.SampleDesc.Count = 1;
	db.SampleDesc.Quality = 0;
	db.Usage = D3D11_USAGE_DEFAULT;
	db.BindFlags = D3D11_BIND_DEPTH_STENCIL;
	db.CPUAccessFlags = 0;
	db.MiscFlags = 0;

	//Create Depth View
	hr = g_pd3dDevice->CreateTexture2D(&db, NULL, &g_depthBuffer);
	if (FAILED(hr)) {
		MessageBox(NULL, L"Error.", L"Error", MB_OK);
	}
	g_pd3dDevice->CreateDepthStencilView(g_depthBuffer, NULL, &g_depthView);
	//g_pd3dDevice->CreateDepthStencilState(&db, &g_depthStencil);

	//Set Render Target
    g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_depthView ); 



Update & Render Functions
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
void Render()
{
	//Update our time
	static float t = 0.0f;
	if ( g_driverType == D3D_DRIVER_TYPE_REFERENCE )
	{
		t += ( float )XM_PI * 0.0125f;
	}
	else
	{
		static ULONGLONG timeStart = 0;
		ULONGLONG timeCur = GetTickCount64();
		if (timeStart == 0)
			timeStart = timeCur;
		t = (timeCur - timeStart) / 1000.0f;
	}

	//
	// Animate the cube
	//
	//g_World = XMMatrixRotationY( t );

	//
	// Clear the back buffer
	//
	g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, Colors::MidnightBlue );
	//g_pImmediateContext->ClearDepthStencilView( g_depthView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0 );

	//
	// Update variables
	//
	ConstantBuffer cb;
		//cb.mWorld = cube1;
	cb.mWorld = XMMatrixTranspose( cube1 );

	cb.mView = XMMatrixTranspose( g_View );
	cb.mProjection = XMMatrixTranspose( g_Projection );
	g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, nullptr, &cb, 0, 0 );

	//
	// Draws first square
	//

	g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
	g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer);
	g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0);
	g_pImmediateContext->DrawIndexed(36, 0, 0); //36 vertices needed for 12 triangles in a triangle list


	ConstantBuffer cb2;
		//cb2.mWorld = cube2;
	cb2.mWorld = XMMatrixTranspose(cube2);

	cb2.mView = XMMatrixTranspose(g_View);
	cb2.mProjection = XMMatrixTranspose(g_Projection);
	g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb2, 0, 0);

//	g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
//	g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer);
//	g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0);
	g_pImmediateContext->DrawIndexed(36, 0, 0);

	//cube1 = XMMatrixIdentity();

	//
	// Present our back buffer to our front buffer
	//
	g_pSwapChain->Present( 0, 0 );
}

void Update()
{
	rot += 0.0005f;
	if (rot > 6.26f)
		rot = 0.0f;

	cube1 = XMMatrixIdentity();
	
	XMVECTOR rotaxis = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
	Rotation = XMMatrixRotationAxis(rotaxis, rot);
	Translation = XMMatrixTranslation(1.0f, 1.0f, 0.0f);

	cube1 = Translation * Rotation;


	cube2 = XMMatrixIdentity();


	Rotation = XMMatrixRotationAxis(rotaxis, -rot);
	Scale = XMMatrixScaling(0.5f, 0.5f, 0.5f);

	cube2 = Scale;
}
Last edited on
Well, you're neither creating a D3D11DepthStencilState nor clearing your depth stencil view. That's likely why.
Topic archived. No new replies allowed.