forked from vixorien/D3D11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
715 lines (633 loc) · 25.3 KB
/
Game.cpp
File metadata and controls
715 lines (633 loc) · 25.3 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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#include "Game.h"
#include "Vertex.h"
#include "Input.h"
#include "Helpers.h"
#include "Mesh.h"
#include "Transform.h"
// Did you know you can press ctrl twice in VS2022 to reveal inline hints? They are pretty useful.
// Needed for a helper function to load pre-compiled shader files
#pragma comment(lib, "d3dcompiler.lib")
#include <d3dcompiler.h>
// For the DirectX Math library
using namespace DirectX;
// --------------------------------------------------------
// Constructor
//
// DXCore (base class) constructor will set up underlying fields.
// Direct3D itself, and our window, are not ready at this point!
//
// hInstance - the application's OS-level handle (unique ID)
// --------------------------------------------------------
Game::Game(HINSTANCE hInstance)
: DXCore(
hInstance, // The application's handle
L"DirectX Game", // Text for the window's title bar (as a wide-character string)
1280, // Width of the window's client area
720, // Height of the window's client area
false, // Sync the framerate to the monitor refresh? (lock framerate)
true) // Show extra stats (fps) in title bar?
{
#if defined(DEBUG) || defined(_DEBUG)
// Do we want a console window? Probably only in debug mode
CreateConsoleWindow(500, 120, 32, 120);
// Initialize all the member variables to appease C++
ambientLight = DirectX::XMFLOAT3(0.0f, 0.0f, 0.0f);
dir1 = {};
dir2 = {};
dir3 = {};
pl1 = {};
pl2 = {};
printf("Console window created successfully. Feel free to printf() here.\n");
#endif
//trf = new Transform();
}
// --------------------------------------------------------
// Destructor - Clean up anything our game has created:
// - Delete all objects manually created within this class
// - Release() all Direct3D objects created within this class
// --------------------------------------------------------
Game::~Game()
{
// Call delete or delete[] on any objects or arrays you've
// created using new or new[] within this class
// - Note: this is unnecessary if using smart pointers
// ImGui clean up (as requested above <3)
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
// Call Release() on any Direct3D objects made within this class
// - Note: this is unnecessary for D3D objects stored in ComPtrs
}
// --------------------------------------------------------
// Called once per program, after Direct3D and the window
// are initialized but before the game loop.
// --------------------------------------------------------
void Game::Init()
{
// Initialize ImGui itself and platform/renderer backends
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplWin32_Init(hWnd);
ImGui_ImplDX11_Init(device.Get(), context.Get());
// Pick a style (uncomment one of these 3)
//ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
ImGui::StyleColorsClassic();
// Create our camera
camera = std::make_shared<Camera>(
0.0f,
2.5f,
-15.0f,
(float)windowWidth / windowHeight, // Turn one into a float so you aren't doing integer division!
XM_PIDIV4, // Pi divided by 4, 45 degrees
2.0f,
1.0f
);
// Loads the shaders, then creates our materials
LoadShaders();
LoadTexturesAndCreateMaterials();
CreateGeometry();
CreateRenderables();
SetupTransforms();
InitLighting();
// Set initial graphics API state
// - These settings persist until we change them
// - Some of these, like the primitive topology & input layout, probably won't change
// - Others, like setting shaders, will need to be moved elsewhere later
{
// Tell the input assembler (IA) stage of the pipeline what kind of
// geometric primitives (points, lines or triangles) we want to draw.
// Essentially: "What kind of shape should the GPU draw with our vertices?"
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
}
}
// --------------------------------------------------------
// Loads shaders from compiled shader object (.cso) files
// --------------------------------------------------------
void Game::LoadShaders()
{
// Loading shaders
// - Visual Studio will compile our shaders at build time
// - They are saved as .cso (Compiled Shader Object) files
// - We need to load them when the application starts
{
vs = std::make_shared<SimpleVertexShader>(device, context, FixPath(L"VertexShader.cso").c_str());
ps = std::make_shared<SimplePixelShader>(device, context, FixPath(L"PixelShader.cso").c_str());
fps = std::make_shared<SimplePixelShader>(device, context, FixPath(L"FancyPixelShader.cso").c_str());
skyVS = std::make_shared<SimpleVertexShader>(device, context, FixPath(L"SkyVertexShader.cso").c_str());
skyPS = std::make_shared<SimplePixelShader>(device, context, FixPath(L"SkyPixelShader.cso").c_str());
shadowVS = std::make_shared<SimpleVertexShader>(device, context, FixPath(L"ShadowVS.cso").c_str());
}
}
// --------------------------------------------------------
// Loads textures with the CreateWICTextureFromFile() function
// --------------------------------------------------------
void Game::LoadTexturesAndCreateMaterials()
{
// Create a sampler state that holds our texture sampling options
Microsoft::WRL::ComPtr<ID3D11SamplerState> sampState;
D3D11_SAMPLER_DESC sampDesc = {};
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.Filter = D3D11_FILTER_ANISOTROPIC;
sampDesc.MaxAnisotropy = 8;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
device->CreateSamplerState(&sampDesc, sampState.GetAddressOf());
// Load textures
#pragma region Cobblestone
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> cobbleAlbedoSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/cobblestone_albedo.png").c_str(),
nullptr,
cobbleAlbedoSRV.GetAddressOf()
);
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> cobbleNormalSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/cobblestone_normals.png").c_str(),
nullptr,
cobbleNormalSRV.GetAddressOf()
);
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> cobbleRoughnessSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/cobblestone_roughness.png").c_str(),
nullptr,
cobbleRoughnessSRV.GetAddressOf()
);
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> cobbleMetalnessSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/cobblestone_metal.png").c_str(),
nullptr,
cobbleMetalnessSRV.GetAddressOf()
);
#pragma endregion
#pragma region Cobblestone
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> scratchAlbedoSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/scratched_albedo.png").c_str(),
nullptr,
scratchAlbedoSRV.GetAddressOf()
);
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> scratchNormalSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/scratched_normals.png").c_str(),
nullptr,
scratchNormalSRV.GetAddressOf()
);
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> scratchRoughnessSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/scratched_roughness.png").c_str(),
nullptr,
scratchRoughnessSRV.GetAddressOf()
);
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> scratchMetalnessSRV;
CreateWICTextureFromFile(
device.Get(),
context.Get(),
FixPath(L"../../Assets/Textures/scratched_metal.png").c_str(),
nullptr,
scratchMetalnessSRV.GetAddressOf()
);
#pragma endregion
// Create materials
// High roughness is a matte surface, low roughness is shiny
mat1 = std::make_shared<Material>(DirectX::XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f), vs, ps);
mat2 = std::make_shared<Material>(DirectX::XMFLOAT4(1.0f, 0.7f, 0.7f, 1.0f), vs, ps);
// Assign textures to materials
mat1->AddTextureSRV("AlbedoMap", cobbleAlbedoSRV);
mat1->AddTextureSRV("NormalMap", cobbleNormalSRV);
mat1->AddTextureSRV("RoughnessMap", cobbleRoughnessSRV);
mat1->AddTextureSRV("MetalnessMap", cobbleMetalnessSRV);
mat1->AddTextureSampler("BasicSampler", sampState);
mat2->AddTextureSRV("AlbedoMap", scratchAlbedoSRV);
mat2->AddTextureSRV("NormalMap", scratchNormalSRV);
mat2->AddTextureSRV("RoughnessMap", scratchRoughnessSRV);
mat2->AddTextureSRV("MetalnessMap", scratchMetalnessSRV);
mat2->AddTextureSampler("BasicSampler", sampState);
}
// --------------------------------------------------------
// Loads the meshes from, then constructs them from, files
// - After this step, the geometry can be reused and drawn over and over by renderables without any more mathematics
// --------------------------------------------------------
void Game::CreateGeometry()
{
// At position 0: the cube
meshes.push_back(std::make_shared<Mesh>(FixPath(L"../../Assets/Models/cube.obj").c_str(), device, context));
// At position 1: the cylinder
meshes.push_back(std::make_shared<Mesh>(FixPath(L"../../Assets/Models/cylinder.obj").c_str(), device, context));
// At position 2: the helix
meshes.push_back(std::make_shared<Mesh>(FixPath(L"../../Assets/Models/helix.obj").c_str(), device, context));
// At position 3: the quad
meshes.push_back(std::make_shared<Mesh>(FixPath(L"../../Assets/Models/quad.obj").c_str(), device, context));
// At position 4: the double sided quad
meshes.push_back(std::make_shared<Mesh>(FixPath(L"../../Assets/Models/quad_double_sided.obj").c_str(), device, context));
// At position 5: the sphere
meshes.push_back(std::make_shared<Mesh>(FixPath(L"../../Assets/Models/sphere.obj").c_str(), device, context));
// At position 6: the torus
meshes.push_back(std::make_shared<Mesh>(FixPath(L"../../Assets/Models/torus.obj").c_str(), device, context));
Microsoft::WRL::ComPtr<ID3D11SamplerState> skySampState;
D3D11_SAMPLER_DESC sampDesc = {};
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.Filter = D3D11_FILTER_ANISOTROPIC;
sampDesc.MaxAnisotropy = 8;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
device->CreateSamplerState(&sampDesc, skySampState.GetAddressOf());
// Now that the meshes are loaded in we can create the sky-
sky = std::make_shared<Sky>(
device.Get(),
context.Get(),
meshes[0], // Skybox is a cube
skyVS,
skyPS,
skySampState.Get(),
FixPath(L"../../Assets/Textures/Sky/right.png").c_str(),
FixPath(L"../../Assets/Textures/Sky/left.png").c_str(),
FixPath(L"../../Assets/Textures/Sky/up.png").c_str(),
FixPath(L"../../Assets/Textures/Sky/down.png").c_str(),
FixPath(L"../../Assets/Textures/Sky/front.png").c_str(),
FixPath(L"../../Assets/Textures/Sky/back.png").c_str()
);
}
// --------------------------------------------------------
// Fills the renderables array
// - Assembles the renderables inside from meshes and materials
// --------------------------------------------------------
void Game::CreateRenderables()
{
// At position 0: the cube
renderables.push_back(std::make_shared<Renderable>(meshes[0], mat1));
// At position 1: the cylinder
renderables.push_back(std::make_shared<Renderable>(meshes[1], mat2));
// At position 2: the helix
renderables.push_back(std::make_shared<Renderable>(meshes[2], mat1));
// At position 3: the quad
renderables.push_back(std::make_shared<Renderable>(meshes[3], mat2));
// At position 4: the double sided quad
renderables.push_back(std::make_shared<Renderable>(meshes[4], mat1));
// At position 5: the sphere
renderables.push_back(std::make_shared<Renderable>(meshes[5], mat2));
// At position 6: the torus
renderables.push_back(std::make_shared<Renderable>(meshes[6], mat1));
}
// --------------------------------------------------------
// Fills the transforms array
// - Also gives the space to adjust transforms before the game starts
// --------------------------------------------------------
void Game::SetupTransforms()
{
for (int i = 0; i < renderables.size(); i++)
{
// Remember that transforms (the array) is all pointers, no real transforms
transforms.push_back(renderables[i]->GetTransform());
}
// Now we can adjust them before the game begins if needed
transforms[0]->SetPosition(-9.0, 0.0, 0.0);
transforms[1]->SetPosition(-6.0, 0.0, 0.0);
transforms[2]->SetPosition(-3.0, 0.0, 0.0);
transforms[3]->SetPosition(0.0, 0.0, 0.0);
transforms[4]->SetPosition(3.0, 0.0, 0.0);
transforms[5]->SetPosition(6.0, 0.0, 0.0);
transforms[6]->SetPosition(9.0, 0.0, 0.0);
}
void Game::CreateShadowMapResources()
{
// Create shadow requirements ------------------------------------------
shadowMapResolution = 1024;
shadowProjectionSize = 10.0f;
// Create the actual texture that will be the shadow map
D3D11_TEXTURE2D_DESC shadowDesc = {};
shadowDesc.Width = shadowMapResolution;
shadowDesc.Height = shadowMapResolution;
shadowDesc.ArraySize = 1;
shadowDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
shadowDesc.CPUAccessFlags = 0;
shadowDesc.Format = DXGI_FORMAT_R32_TYPELESS;
shadowDesc.MipLevels = 1;
shadowDesc.MiscFlags = 0;
shadowDesc.SampleDesc.Count = 1;
shadowDesc.SampleDesc.Quality = 0;
shadowDesc.Usage = D3D11_USAGE_DEFAULT;
Microsoft::WRL::ComPtr<ID3D11Texture2D> shadowTexture;
device->CreateTexture2D(&shadowDesc, 0, shadowTexture.GetAddressOf());
// Create the depth/stencil
D3D11_DEPTH_STENCIL_VIEW_DESC shadowDSDesc = {};
shadowDSDesc.Format = DXGI_FORMAT_D32_FLOAT;
shadowDSDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
shadowDSDesc.Texture2D.MipSlice = 0;
device->CreateDepthStencilView(shadowTexture.Get(), &shadowDSDesc, shadowDSV.GetAddressOf());
// Create the SRV for the shadow map
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = DXGI_FORMAT_R32_FLOAT;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
srvDesc.Texture2D.MostDetailedMip = 0;
device->CreateShaderResourceView(shadowTexture.Get(), &srvDesc, shadowSRV.GetAddressOf());
// Create the special "comparison" sampler state for shadows
D3D11_SAMPLER_DESC shadowSampDesc = {};
shadowSampDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR; // COMPARISON filter!
shadowSampDesc.ComparisonFunc = D3D11_COMPARISON_LESS;
shadowSampDesc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER;
shadowSampDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER;
shadowSampDesc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER;
shadowSampDesc.BorderColor[0] = 1.0f;
shadowSampDesc.BorderColor[1] = 1.0f;
shadowSampDesc.BorderColor[2] = 1.0f;
shadowSampDesc.BorderColor[3] = 1.0f;
device->CreateSamplerState(&shadowSampDesc, &shadowSampler);
// Create a rasterizer state
D3D11_RASTERIZER_DESC shadowRastDesc = {};
shadowRastDesc.FillMode = D3D11_FILL_SOLID;
shadowRastDesc.CullMode = D3D11_CULL_BACK;
shadowRastDesc.DepthClipEnable = true;
shadowRastDesc.DepthBias = 1000; // Multiplied by (smallest possible positive value storable in the depth buffer)
shadowRastDesc.DepthBiasClamp = 0.0f;
shadowRastDesc.SlopeScaledDepthBias = 1.0f;
device->CreateRasterizerState(&shadowRastDesc, &shadowRasterizer);
// Create the "camera" matrices for the shadow map rendering
// View
XMMATRIX shView = XMMatrixLookAtLH(
XMVectorSet(0, 20, -20, 0),
XMVectorSet(0, 0, 0, 0),
XMVectorSet(0, 1, 0, 0));
XMStoreFloat4x4(&shadowViewMatrix, shView);
// Projection - we want ORTHOGRAPHIC for directional light shadows
// NOTE: This particular projection is set up to be SMALLER than
// the overall "scene", to show what happens when objects go
// outside the shadow area. In a game, you'd never want the
// user to see this edge, but I'm specifically making the projection
// small in this demo to show you that it CAN happen.
//
// Ideally, the first two parameters below would be adjusted to
// fit the scene (or however much of the scene the user can see
// at a time). More advanced techniques, like cascaded shadow maps,
// would use multiple (usually 4) shadow maps with increasingly larger
// projections to ensure large open world games have shadows "everywhere"
XMMATRIX shProj = XMMatrixOrthographicLH(shadowProjectionSize, shadowProjectionSize, 0.1f, 100.0f);
XMStoreFloat4x4(&shadowProjectionMatrix, shProj);
}
// --------------------------------------------------------
// Makes the light objects and sets ambient light values
// - Also gives the space to adjust colors/transforms before the game starts
// --------------------------------------------------------
void Game::InitLighting()
{
ambientLight = DirectX::XMFLOAT3(0.0f, 0.0f, 0.24f);
// Directional Light 1
dir1 = {};
dir1.Type = LIGHT_TYPE_DIRECTIONAL;
dir1.Direction = DirectX::XMFLOAT3(1.0f, 0.0f, 0.0f);
dir1.Color = DirectX::XMFLOAT3(0.13f, 0.05f, 0.65f);
dir1.Intensity = 1.0f;
// Directional Light 2
dir2= {};
dir2.Type = LIGHT_TYPE_DIRECTIONAL;
dir2.Direction = DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f);
dir2.Color = DirectX::XMFLOAT3(0.0f, 0.8f, 0.2f);
dir2.Intensity = 1.0f;
// Directional Light 3
dir3= {};
dir3.Type = LIGHT_TYPE_DIRECTIONAL;
dir3.Direction = DirectX::XMFLOAT3(0.0f, -1.0f, 0.0f);
dir3.Color = DirectX::XMFLOAT3(0.8f, 0.02f, 0.13f);
dir3.Intensity = 1.0f;
// Point Light 1
pl1 = {};
pl1.Type = LIGHT_TYPE_POINT;
pl1.Position = DirectX::XMFLOAT3(0.1, -1, 0.2);
pl1.Range = 18.0f;
pl1.Color = DirectX::XMFLOAT3(0.87f, 0.95f, 0.935f);
pl1.Intensity = 1.0f;
// Point Light 2
pl2 = {};
pl2.Type = LIGHT_TYPE_POINT;
pl2.Position = DirectX::XMFLOAT3(0.9, -1.6, 4.0);
pl2.Range = 45.0f;
pl2.Color = DirectX::XMFLOAT3(0.954f, 0.85f, 1.0);
pl2.Intensity = 1.0f;
}
// --------------------------------------------------------
// Renders the shadow map from the light's point of view
// --------------------------------------------------------
void Game::RenderShadowMap()
{
// Initial pipeline setup - No RTV necessary - Clear shadow map
context->OMSetRenderTargets(0, 0, shadowDSV.Get());
context->ClearDepthStencilView(shadowDSV.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0);
context->RSSetState(shadowRasterizer.Get());
// Set the shadow-specific vertex shader
shadowVS->SetShader();
shadowVS->SetMatrix4x4("view", shadowViewMatrix);
shadowVS->SetMatrix4x4("projection", shadowProjectionMatrix);
context->PSSetShader(0, 0, 0); // No pixel shader
// Need to create a viewport that matches the shadow map resolution
D3D11_VIEWPORT viewport = {};
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
viewport.Width = (float)shadowMapResolution;
viewport.Height = (float)shadowMapResolution;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
context->RSSetViewports(1, &viewport);
// Loop and draw all entities
for (auto& e : renderables)
{
shadowVS->SetMatrix4x4("world", e->GetTransform()->GetWorldMatrix());
shadowVS->CopyAllBufferData();
// Draw the mesh
e->GetMesh()->Draw();
}
// Put everything back
context->OMSetRenderTargets(1, backBufferRTV.GetAddressOf(), depthBufferDSV.Get());
}
// -dir3-------------------------------------------------------
// Do this first thing in Update()!
// - Feeds fresh input data to ImGui
// - Determines whether to capture input
// - Resets the gui frame
// - Needs deltaTime to function
// --------------------------------------------------------
ImGuiIO Game::PrepImGui(float deltaTime)
{
// Get a reference to our custom input manager
Input& input = Input::GetInstance();
// Reset input manager's gui state so we dont
// taint our own input (youll uncomment later)
input.SetKeyboardCapture(false);
input.SetMouseCapture(false);
// Feed fresh input data to ImGui
ImGuiIO& io = ImGui::GetIO();
io.DeltaTime = deltaTime;
io.DisplaySize.x = (float)this->windowWidth;
io.DisplaySize.y = (float)this->windowHeight;
io.KeyCtrl = input.KeyDown(VK_CONTROL);
io.KeyShift = input.KeyDown(VK_SHIFT);
io.KeyAlt = input.KeyDown(VK_MENU);
io.MousePos.x = (float)input.GetMouseX();
io.MousePos.y = (float)input.GetMouseY();
io.MouseDown[0] = input.MouseLeftDown();
io.MouseDown[1] = input.MouseRightDown();
io.MouseDown[2] = input.MouseMiddleDown();
io.MouseWheel = input.GetMouseWheel();
input.GetKeyArray(io.KeysDown, 256);
// Reset the frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
// Determine new input capture (youll uncomment later)
input.SetKeyboardCapture(io.WantCaptureKeyboard);
input.SetMouseCapture(io.WantCaptureMouse);
// Show the demo window
//ImGui::ShowDemoWindow();
// Return the io for our Update method to use
return io;
}
// --------------------------------------------------------
// Handles all our ImGui window definition.
// - Takes pointers to values to read/write to them
// --------------------------------------------------------
void Game::UpdateImGui(ImGuiIO frameIO)
{
float framerate = frameIO.Framerate;
ImGui::Begin("Stats"); // Everything after is part of the window
ImGui::Text("ms/frame: %.3f - FPS: %.1f", 1000.0f / framerate, framerate);
ImGui::Text("display size X: %.0f", frameIO.DisplaySize.x);
ImGui::Text("display size Y: %.0f", frameIO.DisplaySize.y);
ImGui::End(); // Ends the current window
//ImGui::Begin("Camera Editor"); // Everything after is part of the window
//ImGui::Text("To be improved... I tried interfacing with the transform but ran out of time.");
//if (ImGui::SliderFloat("Field of View", camera->GetFOV(), XM_PIDIV4, XM_PIDIV2))
//{
// camera->UpdateProjectionMatrix(*camera->GetAspectRatio());
//}
//ImGui::End(); // Ends the current window
ImGui::Begin("Light Editor");
ImGui::Text("Point Light Positions");
ImGui::SliderFloat3("Point Light Position 1", &pl1.Position.x, -10.0f, 10.0f);
ImGui::SliderFloat3("Point Light Position 2", &pl2.Position.x, -10.0f, 10.0f);
ImGui::Text("Point Light Colors");
ImGui::SliderFloat3("Point Light Color 1", &pl1.Color.x, 0.0f, 1.0f);
ImGui::SliderFloat3("Point Light Color 2", &pl2.Color.x, 0.0f, 1.0f);
ImGui::Text("Point Light Ranges");
ImGui::SliderFloat("Point Light 1", &pl1.Range, 0.0f, 100.0f);
ImGui::SliderFloat("Point Light 2", &pl2.Range, 0.0f, 100.0f);
ImGui::Text("Directional Light Colors");
ImGui::SliderFloat3("Directional Light Color 1", &dir1.Color.x, 0.0f, 1.0f);
ImGui::SliderFloat3("Directional Light Color 2", &dir2.Color.x, 0.0f, 1.0f);
ImGui::SliderFloat3("Directional Light Color 3", &dir3.Color.x, 0.0f, 1.0f);
ImGui::End();
}
// --------------------------------------------------------
// Handle resizing to match the new window size.
// - DXCore needs to resize the back buffer
// - Eventually, we'll want to update our 3D camera
// --------------------------------------------------------
void Game::OnResize()
{
// Handle base-level DX resize stuff
DXCore::OnResize();
}
// --------------------------------------------------------
// Update your game here - user input, move objects, AI, etc.
// --------------------------------------------------------
void Game::Update(float deltaTime, float totalTime)
{
// This next line following must be done at the very top of Update(),
// so that the UI has fresh input data and it knows
// a new frame has started!
ImGuiIO frameIO = PrepImGui(deltaTime);
// Actually put the gui on screen
UpdateImGui(frameIO);
// Update the camera :)
camera->Update(deltaTime);
for (int i = 0; i < renderables.size(); i++)
{
renderables[i]->GetTransform()->Rotate(0.0f, deltaTime * 0.1f, 0.0f);
}
// Example input checking: Quit if the escape key is pressed
if (Input::GetInstance().KeyDown(VK_ESCAPE))
{
//delete(trf);
//trf = nullptr;
Quit();
}
}
// --------------------------------------------------------
// Clear the screen, redraw everything, present to the user
// --------------------------------------------------------
void Game::Draw(float deltaTime, float totalTime)
{
// Frame START
// - These things should happen ONCE PER FRAME
// - At the beginning of Game::Draw() before drawing *anything*
{
// Clear the back buffer (erases what's on the screen)
const float bgColor[4] = { 0.4f, 0.6f, 0.75f, 1.0f }; // Cornflower Blue
context->ClearRenderTargetView(backBufferRTV.Get(), bgColor);
// Clear the depth buffer (resets per-pixel occlusion information)
context->ClearDepthStencilView(depthBufferDSV.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0);
}
for (int i = 0; i < renderables.size(); i++)
{
// All the renderables get the ambient light cast onto their pixel shader
renderables[i]->GetMaterial()->GetPS()->SetFloat3("ambientLight", ambientLight);
// They also get the directional light
renderables[i]->GetMaterial()->GetPS()->SetData(
"directionalLight1",
&dir1,
sizeof(Light)
);
renderables[i]->GetMaterial()->GetPS()->SetData(
"directionalLight2",
&dir2,
sizeof(Light)
);
renderables[i]->GetMaterial()->GetPS()->SetData(
"directionalLight3",
&dir3,
sizeof(Light)
);
renderables[i]->GetMaterial()->GetPS()->SetData(
"pointLight1",
&pl1,
sizeof(Light)
);
renderables[i]->GetMaterial()->GetPS()->SetData(
"pointLight2",
&pl2,
sizeof(Light)
);
renderables[i]->Draw(context, camera, totalTime);
}
sky->Draw(context, camera);
// The GUI should be the LAST thing drawn before ending the frame!
// Draw ImGui
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
// Frame END
// - These should happen exactly ONCE PER FRAME
// - At the very end of the frame (after drawing *everything*)
{
// Present the back buffer to the user
// - Puts the results of what we've drawn onto the window
// - Without this, the user never sees anything
swapChain->Present(vsync ? 1 : 0, 0);
// Must re-bind buffers after presenting, as they become unbound
context->OMSetRenderTargets(1, backBufferRTV.GetAddressOf(), depthBufferDSV.Get());
}
}