This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
681 lines (549 loc) · 20.6 KB
/
Application.cpp
File metadata and controls
681 lines (549 loc) · 20.6 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#include "Application.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Application::Application()
{
_hInst = nullptr;
_hWnd = nullptr;
_driverType = D3D_DRIVER_TYPE_NULL;
_featureLevel = D3D_FEATURE_LEVEL_11_0;
_pd3dDevice = nullptr;
_pImmediateContext = nullptr;
_pSwapChain = nullptr;
_pRenderTargetView = nullptr;
_pVertexShader = nullptr;
_pPixelShader = nullptr;
_pVertexLayout = nullptr;
_pCubeVertexBuffer = nullptr;
_pCubeIndexBuffer = nullptr;
_pPyramidVertexBuffer = nullptr;
_pPyramidIndexBuffer = nullptr;
_pSamplerLinear = nullptr;
_pConstantBuffer = nullptr;
}
Application::~Application()
{
Cleanup();
}
HRESULT Application::Initialise(HINSTANCE hInstance, int nCmdShow)
{
if (FAILED(InitWindow(hInstance, nCmdShow)))
{
return E_FAIL;
}
RECT rc;
GetClientRect(_hWnd, &rc);
_WindowWidth = rc.right - rc.left;
_WindowHeight = rc.bottom - rc.top;
if (FAILED(InitDevice()))
{
Cleanup();
return E_FAIL;
}
// Initialize the world matrix
XMStoreFloat4x4(&_world, XMMatrixIdentity());
//initialize input.
_pInput = new Input();
//populate all of the vectors with xml generated objects
XMLLoader::InitCameras(_pCameras, _WindowWidth, _WindowHeight);
XMLLoader::InitLights(_pLights);
XMLLoader::InitGameObjects(_pObjects, _pd3dDevice);
//load all shader settings now
_fog = XMLLoader::LoadFogSettings("data/shadersettings/fog/fog.xml");
//now initialize hardcoded vectors into the gameobjects array
CreateDDSTextureFromFile(_pd3dDevice, L"data/textures/Crate_COLOR.dds", nullptr, &_pTexture);
GameObject* hardCube = new GameObject();
hardCube->SetMeshData(MeshData(_pCubeVertexBuffer, _pCubeIndexBuffer, sizeof(SimpleVertex), 0, 36));
hardCube->SetShaderResource(_pTexture);
GameObject* hardPyramid = new GameObject();
hardPyramid->SetMeshData(MeshData(_pPyramidVertexBuffer, _pPyramidIndexBuffer, sizeof(SimpleVertex), 0, 18));
hardPyramid->SetShaderResource(_pTexture);
_pObjects.push_back(hardCube);
_pObjects.push_back(hardPyramid);
return S_OK;
}
HRESULT Application::InitShadersAndInputLayout()
{
HRESULT hr;
// Compile the vertex shader
ID3DBlob* pVSBlob = nullptr;
hr = CompileShaderFromFile(L"data/shaders/DX11 Framework.hlsl", "VS", "vs_4_0", &pVSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
L"The HLSL file cannot be compiled. Check VS Outpot for Error Log.", L"Error", MB_OK);
return hr;
}
// Create the vertex shader
hr = _pd3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), nullptr, &_pVertexShader);
if (FAILED(hr))
{
pVSBlob->Release();
return hr;
}
// Compile the pixel shader
ID3DBlob* pPSBlob = nullptr;
hr = CompileShaderFromFile(L"data/shaders/DX11 Framework.hlsl", "PS", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
L"The HLSL file cannot be compiled. Check VS Outpot for Error Log.", L"Error", MB_OK);
return hr;
}
// Create the pixel shader
hr = _pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), nullptr, &_pPixelShader);
pPSBlob->Release();
if (FAILED(hr))
return hr;
// Define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
UINT numElements = ARRAYSIZE(layout);
// Create the input layout
hr = _pd3dDevice->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), &_pVertexLayout);
pVSBlob->Release();
if (FAILED(hr))
return hr;
// Set the input layout
_pImmediateContext->IASetInputLayout(_pVertexLayout);
return hr;
}
HRESULT Application::InitVertexBuffer()
{
HRESULT hr;
// Create vertex buffer for cube
SimpleVertex vertices[] =
{
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 1.0f)},
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
};
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(SimpleVertex) * 8;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = vertices;
hr = _pd3dDevice->CreateBuffer(&bd, &InitData, &_pCubeVertexBuffer);
if (FAILED(hr))
return hr;
// Create vertex buffer for pyramid
SimpleVertex pyramidvertices[] =
{
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 1.0f)},
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(0.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f) },
};
D3D11_BUFFER_DESC pyramidbd;
ZeroMemory(&pyramidbd, sizeof(pyramidbd));
pyramidbd.Usage = D3D11_USAGE_DEFAULT;
pyramidbd.ByteWidth = sizeof(SimpleVertex) * 5;
pyramidbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
pyramidbd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA pyramidInitData;
ZeroMemory(&pyramidInitData, sizeof(pyramidInitData));
pyramidInitData.pSysMem = pyramidvertices;
hr = _pd3dDevice->CreateBuffer(&pyramidbd, &pyramidInitData, &_pPyramidVertexBuffer);
if (FAILED(hr))
return hr;
return S_OK;
}
HRESULT Application::InitIndexBuffer()
{
HRESULT hr;
// Create index buffer for cube
WORD indices[] =
{
0,1,2,
2,1,3,
1,5,3,
3,5,7,
5,4,6,
7,5,6,
4,0,2,
6,4,2,
0,4,1,
4,5,1,
6,2,3,
3,7,6,
};
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(WORD) * 36;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = indices;
hr = _pd3dDevice->CreateBuffer(&bd, &InitData, &_pCubeIndexBuffer);
if (FAILED(hr))
return hr;
// Create index buffer for pyramid
WORD pyramidindices[] =
{
1,3,0,
0,3,2,
3,4,2,
1,4,3,
0,4,1,
2,4,0,
};
D3D11_BUFFER_DESC pyramidbd;
ZeroMemory(&pyramidbd, sizeof(pyramidbd));
pyramidbd.Usage = D3D11_USAGE_DEFAULT;
pyramidbd.ByteWidth = sizeof(WORD) * 18;
pyramidbd.BindFlags = D3D11_BIND_INDEX_BUFFER;
pyramidbd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA pyramidInitData;
ZeroMemory(&pyramidInitData, sizeof(pyramidInitData));
pyramidInitData.pSysMem = pyramidindices;
hr = _pd3dDevice->CreateBuffer(&pyramidbd, &pyramidInitData, &_pPyramidIndexBuffer);
if (FAILED(hr))
return hr;
return S_OK;
}
HRESULT Application::InitWindow(HINSTANCE hInstance, int nCmdShow)
{
// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TUTORIAL1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW );
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = L"TutorialWindowClass";
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_TUTORIAL1);
if (!RegisterClassEx(&wcex))
return E_FAIL;
// Create window
_hInst = hInstance;
RECT rc = {0, 0, 640, 480};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
_hWnd = CreateWindow(L"TutorialWindowClass", L"DX11 Framework", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
nullptr);
if (!_hWnd)
return E_FAIL;
ShowWindow(_hWnd, nCmdShow);
return S_OK;
}
HRESULT Application::CompileShaderFromFile(WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut)
{
HRESULT hr = S_OK;
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined(DEBUG) || defined(_DEBUG)
// Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
// Setting this flag improves the shader debugging experience, but still allows
// the shaders to be optimized and to run exactly the way they will run in
// the release configuration of this program.
dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* pErrorBlob;
hr = D3DCompileFromFile(szFileName, nullptr, nullptr, szEntryPoint, szShaderModel,
dwShaderFlags, 0, ppBlobOut, &pErrorBlob);
if (FAILED(hr))
{
if (pErrorBlob != nullptr)
OutputDebugStringA((char*)pErrorBlob->GetBufferPointer());
if (pErrorBlob) pErrorBlob->Release();
return hr;
}
if (pErrorBlob) pErrorBlob->Release();
return S_OK;
}
HRESULT Application::InitDevice()
{
HRESULT hr = S_OK;
UINT createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT numDriverTypes = ARRAYSIZE(driverTypes);
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = _WindowWidth;
sd.BufferDesc.Height = _WindowHeight;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = _hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
{
_driverType = driverTypes[driverTypeIndex];
hr = D3D11CreateDeviceAndSwapChain(nullptr, _driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &sd, &_pSwapChain, &_pd3dDevice, &_featureLevel, &_pImmediateContext);
if (SUCCEEDED(hr))
break;
}
if (FAILED(hr))
return hr;
D3D11_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = _WindowWidth;
depthStencilDesc.Height = _WindowHeight;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
_pd3dDevice->CreateTexture2D(&depthStencilDesc, nullptr, &_depthStencilBuffer);
_pd3dDevice->CreateDepthStencilView(_depthStencilBuffer, nullptr, &_depthStencilView);
// Create a render target view
ID3D11Texture2D* pBackBuffer = nullptr;
hr = _pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
if (FAILED(hr))
return hr;
hr = _pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &_pRenderTargetView);
pBackBuffer->Release();
if (FAILED(hr))
return hr;
_pImmediateContext->OMSetRenderTargets(1, &_pRenderTargetView, _depthStencilView);
// Setup the viewport
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)_WindowWidth;
vp.Height = (FLOAT)_WindowHeight;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
_pImmediateContext->RSSetViewports(1, &vp);
hr = InitShadersAndInputLayout();
if (FAILED(hr))
{
return S_FALSE;
}
InitVertexBuffer();
InitIndexBuffer();
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = _pd3dDevice->CreateSamplerState(&sampDesc, &_pSamplerLinear);
if (FAILED(hr))
return hr;
// Set primitive topology
_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Create the constant buffer
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(ConstantBuffer);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = 0;
hr = _pd3dDevice->CreateBuffer(&bd, nullptr, &_pConstantBuffer);
// Setup the rasterizer for solid.
D3D11_RASTERIZER_DESC soliddesc;
ZeroMemory(&soliddesc, sizeof(D3D11_RASTERIZER_DESC));
soliddesc.FillMode = D3D11_FILL_SOLID;
soliddesc.CullMode = D3D11_CULL_NONE;
hr = _pd3dDevice->CreateRasterizerState(&soliddesc, &_solid);
// Setup the rasterizer for wireframe.
D3D11_RASTERIZER_DESC wfdesc;
ZeroMemory(&wfdesc, sizeof(D3D11_RASTERIZER_DESC));
wfdesc.FillMode = D3D11_FILL_WIREFRAME;
wfdesc.CullMode = D3D11_CULL_NONE;
hr = _pd3dDevice->CreateRasterizerState(&wfdesc, &_wireFrame);
if (FAILED(hr))
return hr;
return S_OK;
}
void Application::Cleanup()
{
if (_pImmediateContext) _pImmediateContext->ClearState();
if (_pConstantBuffer) _pConstantBuffer->Release();
if (_pCubeVertexBuffer) _pCubeVertexBuffer->Release();
if (_pCubeIndexBuffer) _pCubeIndexBuffer->Release();
if (_pPyramidIndexBuffer) _pPyramidIndexBuffer->Release();
if (_pPyramidVertexBuffer) _pPyramidVertexBuffer->Release();
if (_pVertexLayout) _pVertexLayout->Release();
if (_pVertexShader) _pVertexShader->Release();
if (_pPixelShader) _pPixelShader->Release();
if (_pRenderTargetView) _pRenderTargetView->Release();
if (_pSwapChain) _pSwapChain->Release();
if (_pImmediateContext) _pImmediateContext->Release();
if (_pd3dDevice) _pd3dDevice->Release();
if (_pSamplerLinear) _pSamplerLinear->Release();
if (_solid) _solid->Release();
if (_wireFrame) _wireFrame->Release();
if (_depthStencilView) _depthStencilView->Release();
if (_depthStencilBuffer) _depthStencilBuffer->Release();
if (_pInput) delete(_pInput);
_pCameras.clear();
_pCameras.shrink_to_fit();
_pObjects.clear();
_pObjects.shrink_to_fit();
_pLights.clear();
_pLights.shrink_to_fit();
}
void Application::Update()
{
// Update our time
static float t = 0.0f;
float tDelta = 0.0f;
static float tPrev = 0.0f;
tPrev = t;
if (_driverType == D3D_DRIVER_TYPE_REFERENCE)
{
t += (float)XM_PI * 0.0125f;
}
else
{
static DWORD dwTimeStart = 0;
DWORD dwTimeCur = GetTickCount();
if (dwTimeStart == 0)
dwTimeStart = dwTimeCur;
t = (dwTimeCur - dwTimeStart) / 1000.0f;
}
tDelta = t - tPrev;
//poll input before everything updates.
_pInput->Update();
//update cameras.
//switch cameras first, then update the selected one.
if (_pInput->Key1Down()) _pCurrentCamera = 0;
if (_pInput->Key2Down()) _pCurrentCamera = 1;
if (_pInput->Key3Down()) _pCurrentCamera = 2;
_pCameras[_pCurrentCamera]->Update(_pInput, tDelta);
//TODO: place all this inside GameObjects.
// Update our positions
static float planet_posX = 5.0f;
static float planet_posZ = 10.0f;
static float moon_posX = 7.5f;
static float moon_posZ = 10.0f;
static const float sun_posX = 0.0f;
static const float sun_posZ = 10.0f;
static const float planet_orbit_radius = 7.0f;
static const float moon_orbit_radius = 3.0f;
static const float planet_speed = 0.5f;
static const float moon_speed = 1.5f;
static const float sun_rot_speed = 1.0f;
static const float planet_rot_speed = 5.0f;
static const float moon_rot_speed = -5.0f; //negative is counter clockwise
//calculate next positions based on circle algorithm.
//research reference link: https://funprogramming.org/68-Circular-motion-reviewed.html
planet_posX = sun_posX + cosf(planet_speed * t) * planet_orbit_radius; //uses speed to change rate of orbit.
planet_posZ = sun_posZ + sinf(planet_speed * t) * planet_orbit_radius;
//now we can calculate positions of the moon based on the current planet.
moon_posX = planet_posX + cosf(moon_speed * t) * moon_orbit_radius;
moon_posZ = planet_posZ + sinf(moon_speed * t) * moon_orbit_radius;
//
// Animate a solar system
//
_pObjects[0]->transform.position = XMFLOAT3(sun_posX, 0.0f, sun_posZ);
_pObjects[1]->transform.position = XMFLOAT3(planet_posX, 0.0f, planet_posZ);
_pObjects[2]->transform.position = XMFLOAT3(moon_posX, 0.0f, moon_posZ);
_pObjects[0]->transform.rotation = XMFLOAT3(t * sun_rot_speed / 2, t * sun_rot_speed, 0.0f);
_pObjects[1]->transform.rotation = XMFLOAT3(t * planet_rot_speed / 2, t * planet_rot_speed, 0.0f);
_pObjects[2]->transform.rotation = XMFLOAT3(t * moon_rot_speed / 2, t * moon_rot_speed, 0.0f);
//update lookat camera's target
_pCameras[0]->SetLookAt(XMFLOAT3(planet_posX, 0, planet_posZ));
}
void Application::Draw()
{
//
// Clear the back buffer
//
float ClearColor[4] = {0.0f, 0.125f, 0.3f, 1.0f}; // red,green,blue,alpha
_pImmediateContext->ClearRenderTargetView(_pRenderTargetView, ClearColor);
//change renderer modes depending on input.
if (_pInput->KeyF3Down()) _pImmediateContext->RSSetState(_wireFrame);
else _pImmediateContext->RSSetState(nullptr);
UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
//get current camera projection
XMMATRIX world = XMLoadFloat4x4(&_world); //use core world transform of 0, 0, 0
XMMATRIX view = XMLoadFloat4x4(&_pCameras[_pCurrentCamera]->GetView());
XMMATRIX projection = XMLoadFloat4x4(&_pCameras[_pCurrentCamera]->GetProjection());
//
// Update variables
//
ConstantBuffer cb;
cb.mWorld = XMMatrixTranspose(world);
cb.mView = XMMatrixTranspose(view);
cb.mProjection = XMMatrixTranspose(projection);
cb.DiffLight = _pLights[0].diffuseStr;
cb.DiffMat = _pLights[0].diffuseMat;
cb.DirToLight = _pLights[0].dir;
cb.SpecLight = _pLights[0].specularStr;
cb.SpecMat = _pLights[0].specularMat;
cb.SpecPower = _pLights[0].specularPow;
cb.EyeLoc = _pCameras[_pCurrentCamera]->GetPosition();
cb.AmbLight = _pLights[0].ambientStr;
cb.AmbMat = _pLights[0].ambientMat;
cb.FogStart = _fog.fogStart;
cb.FogColor = _fog.fogColor;
cb.FogRange = _fog.fogRange;
//clear depth stencil and prepare resource
_pImmediateContext->ClearDepthStencilView(_depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
_pImmediateContext->UpdateSubresource(_pConstantBuffer, 0, nullptr, &cb, 0, 0);
//
// Draw all objects
//
for (GameObject* gameObject : _pObjects) gameObject->Draw(_pImmediateContext, _pVertexShader, _pPixelShader, _pConstantBuffer, _pSamplerLinear, cb);
//
// Present our back buffer to our front buffer
//
_pSwapChain->Present(0, 0);
}