-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightingTestGame.cpp
More file actions
219 lines (193 loc) · 6.87 KB
/
LightingTestGame.cpp
File metadata and controls
219 lines (193 loc) · 6.87 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
#include "Game/LightingTest/LightingTestGame.h"
#include "Core/App/AppContext.h"
#include "Core/Assets/AssetCache.h"
#include "Core/Graphics/ModelRenderer.h"
#include "Core/Graphics/Rendering/Lighting/SceneLighting3D.h"
#include "Core/Graphics/Rendering/Renderables/RenderMaterialParameters.h"
#include "Core/Graphics/Rendering/PrimitiveRenderer3D.h"
#include "Core/Input/InputSystem.h"
#include "Core/Input/Keyboard.h"
#include "Game/Common/MiniGameNavigation.h"
#include "Core/Input/RawInputHandler.h"
#include "Core/Platform/Window.h"
#include <cmath>
#include <memory>
#include <stdexcept>
using DirectX::SimpleMath::Matrix;
using DirectX::SimpleMath::Vector3;
void LightingTestGame::Initialize(AppContext &context)
{
if (!context.IsValid())
{
throw std::runtime_error("LightingTestGame::Initialize received invalid AppContext.");
}
SceneCamera.SetPosition(Vector3(0.0f, 3.0f, -10.0f));
SceneCamera.SetRotation(0.0f, 0.1f);
if (AssetCache *const assetCache = context.Assets.Cache; assetCache != nullptr)
{
CubeModel = assetCache->LoadModel("Core/Data/Cube.fbx");
SphereModel = assetCache->LoadModel("Core/Data/Sphere.fbx");
}
Lighting = SceneLighting3DCreateDefaultOutdoor();
if (!Lighting.DirectionalLights.empty())
{
Lighting.DirectionalLights[0].Direction = Vector3(-0.32f, -1.0f, -0.26f);
Lighting.DirectionalLights[0].Intensity = 1.25f;
}
PointLight3D pointLight{};
pointLight.Position = Vector3(0.0f, 3.0f, 0.0f);
pointLight.LightColor = DirectX::SimpleMath::Color(1.0f, 0.92f, 0.84f, 1.0f);
pointLight.Intensity = 3.2f;
pointLight.Range = 10.0f;
Lighting.PointLights.push_back(pointLight);
IsInitialized = true;
}
void LightingTestGame::Update(AppContext &context, const float deltaTime)
{
if (!IsInitialized)
{
return;
}
if (context.Input.System != nullptr
&& WasDebugReturnToMainMenuPressed(*context.Input.System)) {
RequestReturnToEngineMainMenu(context, "LightingTestGame::P");
return;
}
// Escape is semantic Back: return to engine MainMenuGame (no nested panels in this demo).
if (context.Input.System != nullptr
&& context.Input.System->GetKeyboard().WasKeyPressed(Key::Escape))
{
RequestReturnToEngineMainMenu(context, "LightingTestGame::Escape");
return;
}
AccumulatedTimeSeconds += deltaTime;
UpdateCamera(context, deltaTime);
RawInputHandler::Instance().ClearFrameDeltas();
}
void LightingTestGame::Render(AppContext &context)
{
if (!IsInitialized)
{
return;
}
const Window *const mainWindow = context.Platform.MainWindow;
if (mainWindow == nullptr)
{
return;
}
const int width = mainWindow->GetWidth();
const int height = mainWindow->GetHeight();
if (width <= 0 || height <= 0)
{
return;
}
const float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
const Matrix viewMatrix = SceneCamera.GetViewMatrix();
const Matrix projectionMatrix = SceneCamera.GetProjectionMatrix(aspectRatio);
const Vector3 cameraWorldPosition = SceneCamera.GetPosition();
PrimitiveRenderer3D &primitiveRenderer = context.GetPrimitiveRenderer3D();
ModelRenderer &modelRenderer = context.GetModelRenderer();
RenderMaterialParameters floorMaterial{};
floorMaterial.BaseColor = DirectX::SimpleMath::Color(0.22f, 0.24f, 0.28f, 1.0f);
floorMaterial.AmbientFactor = 0.12f;
floorMaterial.SpecularPower = 16.0f;
floorMaterial.SpecularColor = DirectX::SimpleMath::Color(0.14f, 0.14f, 0.14f, 1.0f);
const Matrix floorWorld =
Matrix::CreateScale(18.0f, 0.4f, 18.0f) *
Matrix::CreateTranslation(0.0f, -0.2f, 0.0f);
primitiveRenderer.DrawBoxLit(
floorWorld,
viewMatrix,
projectionMatrix,
cameraWorldPosition,
Lighting,
floorMaterial
);
if (CubeModel != nullptr && CubeModel->IsLoaded())
{
RenderMaterialParameters cubeMaterial{};
cubeMaterial.BaseColor = DirectX::SimpleMath::Color(0.32f, 0.72f, 0.96f, 1.0f);
cubeMaterial.SpecularPower = 52.0f;
cubeMaterial.SpecularColor = DirectX::SimpleMath::Color(0.85f, 0.9f, 1.0f, 1.0f);
const Matrix cubeWorld =
Matrix::CreateScale(0.01f, 0.01f, 0.01f) *
Matrix::CreateRotationY(AccumulatedTimeSeconds * 0.45f) *
Matrix::CreateTranslation(-2.2f, 0.8f, 0.6f);
modelRenderer.DrawModelLit(
*CubeModel,
cubeWorld,
viewMatrix,
projectionMatrix,
cameraWorldPosition,
Lighting,
cubeMaterial
);
}
if (SphereModel != nullptr && SphereModel->IsLoaded())
{
RenderMaterialParameters sphereMaterial{};
sphereMaterial.BaseColor = DirectX::SimpleMath::Color(0.95f, 0.42f, 0.26f, 1.0f);
sphereMaterial.SpecularPower = 96.0f;
sphereMaterial.SpecularColor = DirectX::SimpleMath::Color(1.0f, 0.86f, 0.78f, 1.0f);
const float bobOffset = std::sin(AccumulatedTimeSeconds * 1.5f) * 0.2f;
const Matrix sphereWorld =
Matrix::CreateScale(0.01f, 0.01f, 0.01f) *
Matrix::CreateTranslation(2.0f, 1.35f + bobOffset, -0.4f);
modelRenderer.DrawModelLit(
*SphereModel,
sphereWorld,
viewMatrix,
projectionMatrix,
cameraWorldPosition,
Lighting,
sphereMaterial
);
}
}
void LightingTestGame::UpdateCamera(const AppContext &context, const float deltaTime)
{
const Keyboard &keyboard = context.Input.System->GetKeyboard();
const float moveSpeed = 9.0f;
const float rotateSpeed = 1.7f;
const float mouseSensitivity = 0.0035f;
if (keyboard.IsKeyDown(Key::W))
{
SceneCamera.MoveForward(moveSpeed * deltaTime);
}
if (keyboard.IsKeyDown(Key::S))
{
SceneCamera.MoveForward(-moveSpeed * deltaTime);
}
if (keyboard.IsKeyDown(Key::D))
{
SceneCamera.MoveRight(moveSpeed * deltaTime);
}
if (keyboard.IsKeyDown(Key::A))
{
SceneCamera.MoveRight(-moveSpeed * deltaTime);
}
float yawDelta = 0.0f;
float pitchDelta = 0.0f;
if (context.Input.System->IsRightMouseDown())
{
yawDelta += static_cast<float>(context.Input.System->GetMouseDeltaX()) * mouseSensitivity;
pitchDelta -= static_cast<float>(context.Input.System->GetMouseDeltaY()) * mouseSensitivity;
}
if (keyboard.IsKeyDown(Key::Left))
{
yawDelta -= rotateSpeed * deltaTime;
}
if (keyboard.IsKeyDown(Key::Right))
{
yawDelta += rotateSpeed * deltaTime;
}
if (keyboard.IsKeyDown(Key::Up))
{
pitchDelta += rotateSpeed * deltaTime;
}
if (keyboard.IsKeyDown(Key::Down))
{
pitchDelta -= rotateSpeed * deltaTime;
}
SceneCamera.AddRotation(yawDelta, pitchDelta);
}