-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
173 lines (136 loc) · 4.54 KB
/
Copy pathGraphics.cpp
File metadata and controls
173 lines (136 loc) · 4.54 KB
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
#include "Graphics.h"
#include <cassert>
#include <vector>
extern int iHeight;
extern int iWidth;
float FoV = DirectX::XM_PIDIV4;
float aspectRatio = (float)iHeight / (float)iWidth;
Graphics::Graphics(HWND hwnd, int width, int height, bool windowed)
{
DXGI_MODE_DESC swapChainBufferDesc;
ZeroMemory(&swapChainBufferDesc, sizeof(DXGI_MODE_DESC));
swapChainBufferDesc.Width = 0;
swapChainBufferDesc.Height = 0;
swapChainBufferDesc.RefreshRate.Numerator = 60;
swapChainBufferDesc.RefreshRate.Denominator = 1;
swapChainBufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swapChainBufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainBufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainDesc.BufferDesc = swapChainBufferDesc;
swapChainDesc.SampleDesc.Count = 8;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.Windowed = windowed;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
//Create our SwapChain
D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, NULL, 0,
D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice, NULL, &pContext);
//Create our BackBuffer
ID3D11Texture2D* pBackBuffer = nullptr;
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)& pBackBuffer);
//Create our Render Target
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
pBackBuffer->Release();
//Set our Render Target
pContext->OMSetRenderTargets(1, &pRenderTargetView, NULL);
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = (float)width;
viewport.Height = (float)height;
viewport.MinDepth = 0;
viewport.MaxDepth = 1;
//Set the Viewport
pContext->RSSetViewports(1, &viewport);
ID3D11Texture2D* depthStencilBuffer = nullptr;
//Describe our Depth/Stencil Buffer
D3D11_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = width;
depthStencilDesc.Height = height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 8;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
pDevice->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
pDevice->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
depthStencilBuffer->Release();
pContext->OMSetRenderTargets(1, &pRenderTargetView, depthStencilView);
//pDevice->QueryInterface(IID_PPV_ARGS(&debug));
}
Graphics::~Graphics()
{
pSwapChain->Release();
pSwapChain = nullptr;
pDevice->Release();
pDevice->Release();
pDevice = nullptr;
pRenderTargetView->Release();
pRenderTargetView = nullptr;
pContext->Release();
pContext = nullptr;
depthStencilView->Release();
depthStencilView = nullptr;
/*if (debug)
{
debug->ReportLiveDeviceObjects(D3D11_RLDO_DETAIL);
debug->Release();
debug = nullptr;
}*/
}
void Graphics::startDraw()
{
pContext->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
pContext->ClearRenderTargetView(pRenderTargetView, clearColor);
}
void Graphics::endDraw()
{
pSwapChain->Present(0, 0);
}
void Graphics::moveLight(float x, float y, float z)
{
lightPos[0] = x;
lightPos[1] = y;
lightPos[2] = z;
struct constantBufferLight
{
alignas(16) float element[3];
};
constantBufferLight cbLPos =
{
{
lightPos[0], lightPos[1], lightPos[2]
}
};
ID3D11Buffer* cbL;
D3D11_SUBRESOURCE_DATA cbData;
ZeroMemory(&cbData, sizeof(D3D11_SUBRESOURCE_DATA));
cbData.pSysMem = &cbLPos;
D3D11_BUFFER_DESC cbDesc;
ZeroMemory(&cbDesc, sizeof(D3D11_BUFFER_DESC));
cbDesc.Usage = D3D11_USAGE_DEFAULT;
cbDesc.ByteWidth = sizeof(cbLPos);
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = 0;
cbDesc.MiscFlags = 0;
pDevice->CreateBuffer(&cbDesc, &cbData, &cbL);
pContext->PSSetConstantBuffers(0, 1, &cbL);
cbL->Release();
}
ID3D11Device* Graphics::getDevice()
{
return pDevice;
}
ID3D11DeviceContext* Graphics::getContext()
{
return pContext;
}