-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostProcessingDemo.cpp
More file actions
447 lines (359 loc) · 13 KB
/
PostProcessingDemo.cpp
File metadata and controls
447 lines (359 loc) · 13 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
#include "PostProcessingDemo.h"
// NOTE(luca) - By rendering the game not on the screen but on a quad that covers the screen leaves room to apply any kind of effects (ex: distortion)
PostProcessingDemo::PostProcessingDemo() = default;
PostProcessingDemo::~PostProcessingDemo() = default;
// NOTE(luca) - To send data from the constant buffer to the pixel shader where the effects are created we must use: mPostProcessMat.setData(<new_c_buffer>)
// Structure created to be passed through the constant buffer
// DirectX handles the constant data in video memory in chunks of 16B
// so if we have a structure with 24B this size must be modified to be multiple of 16 (+ 8B)
__declspec(align(16)) // This line of code aligns into chuncks of 16B
struct constant
{
Matrix4x4 m_world;
Matrix4x4 m_view;
Matrix4x4 m_proj;
Vec4 m_light_direction;
Vec4 m_camera_position;
Vec4 m_light_position = Vec4(0, 1, 0, 0);
// Light radius is for point light
float m_light_radius = 4.0f;
ULONGLONG m_time = 0;
};
void PostProcessingDemo::onCreate()
{
Window::onCreate();
// Create the swap chain
RECT rc = this->getClientWindowRect();
mSwapChain = GraphicsEngine::get()->getRenderSystem()->createSwapChain(
this->mHwnd, rc.right - rc.left, rc.bottom - rc.top
);
InputSystem::get()->addListener(this);
InputSystem::get()->showCursor(false);
// Base material - to hold the directional light property for most meshes
mBaseMat = GraphicsEngine::get()->createMaterial(
L"DirectionalLightVS.hlsl", L"DirectionalLightPS.hlsl"
);
mBaseMat->setCullMode(CULL_MODE::CULL_MODE_BACK);
// Sky mesh
mSkyMesh = GraphicsEngine::get()->getMeshManager()->createMeshFromFile(L"Assets\\Meshes\\sphere.obj");
mSkyMat = GraphicsEngine::get()->createMaterial(L"SkyBoxVS.hlsl", L"SkyBoxPS.hlsl");
mSkyMat->addTexture(GraphicsEngine::get()->getTextureManager()->createTextureFromFile(L"Assets\\Textures\\stars_map.jpg"));
mSkyMat->setCullMode(CULL_MODE::CULL_MODE_FRONT);
// Player mesh
//mpm1 = GraphicsEngine::get()->getMeshManager()->createMeshFromFile(L"Assets\\Meshes\\BoomBox.obj");
mpm1 = GraphicsEngine::get()->getMeshManager()->createMeshFromFile(L"Assets\\Meshes\\gltfTest\\BoomBox.gltf");
mpmat1 = GraphicsEngine::get()->createMaterial(mBaseMat);
mpmat1->addTexture(GraphicsEngine::get()->getTextureManager()->createTextureFromFile(L"Assets\\Meshes\\gltfTest\\BoomBox_baseColor.png"));
mpmat1->addTexture(GraphicsEngine::get()->getTextureManager()->createTextureFromFile(L"Assets\\Meshes\\gltfTest\\BoomBox_occlusionRoughnessMetallic.png"));
mpmat1->addTexture(GraphicsEngine::get()->getTextureManager()->createTextureFromFile(L"Assets\\Meshes\\gltfTest\\BoomBox_normal.png"));
mpmat1->addTexture(GraphicsEngine::get()->getTextureManager()->createTextureFromFile(L"Assets\\Meshes\\gltfTest\\BoomBox_emissive.png"));
mpmat1->setCullMode(CULL_MODE::CULL_MODE_FRONT);
// BumpMapping test
mSphereMesh = GraphicsEngine::get()->getMeshManager()->createMeshFromFile(L"Assets\\Meshes\\sphere.obj");
mSphereMat = GraphicsEngine::get()->createMaterial(L"BumpMappingVS.hlsl", L"BumpMappingPS.hlsl");
mSphereMat->addTexture(GraphicsEngine::get()->getTextureManager()->createTextureFromFile(L"Assets\\Textures\\brick_d.jpg"));
mSphereMat->addTexture(GraphicsEngine::get()->getTextureManager()->createTextureFromFile(L"Assets\\Textures\\brick_n.jpg"));
mSphereMat->setCullMode(CULL_MODE::CULL_MODE_BACK);
// Base material - to hold the directional light property for most meshes
mPostProcessMat = GraphicsEngine::get()->createMaterial(
L"PostProcessVS.hlsl", L"DistortionEffectPS.hlsl"
);
mPostProcessMat->setCullMode(CULL_MODE::CULL_MODE_BACK);
VertexMesh quad_vertex_list[] = {
VertexMesh(
Vec3(-1, -1, 0), Vec2(0, 1), Vec3(), Vec3(), Vec3()
),
VertexMesh(
Vec3(-1, 1, 0), Vec2(0, 0), Vec3(), Vec3(), Vec3()
),
VertexMesh(
Vec3( 1, 1, 0), Vec2(1, 0), Vec3(), Vec3(), Vec3()
),
VertexMesh(
Vec3( 1, -1, 0), Vec2(1, 1), Vec3(), Vec3(), Vec3()
),
};
UINT quad_index_list[] = {
0, 1, 2,
2, 3, 0,
};
MaterialSlot quad_mat_slots[] = {
{0, 6, 0}
};
// Create the quad in which we render the game
this->mQuadMesh = GraphicsEngine::get()->getMeshManager()->createMesh(
quad_vertex_list, 4,
quad_index_list, 6,
quad_mat_slots, 1
);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
this->mProjCam.setPerspectiveFovLH(
1.57f,
(float)((float)width / (float)height),
0.1f,
5000.0f
);
m_list_materials.reserve(32);
mRenderTarget = GraphicsEngine::get()->getTextureManager()->createTexture(
Rect(rc.right - rc.left, rc.bottom - rc.top), Texture::Type::RenderTarget
);
mDepthStencil = GraphicsEngine::get()->getTextureManager()->createTexture(
Rect(rc.right - rc.left, rc.bottom - rc.top), Texture::Type::DepthStencil
);
mPostProcessMat->addTexture(mRenderTarget);
}
void PostProcessingDemo::onUpdate()
{
Window::onUpdate();
InputSystem::get()->update();
// ------------------------------------------------
// SCENE RENDERED TO RENDER TARGET
// ------------------------------------------------
// Clear the whole window and show a solid color
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->clearRenderTargetColor(
this->mRenderTarget, 0, 0.3f, 0.4f, 1
);
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->clearDepthStencil(
this->mDepthStencil
);
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->setRenderTarget(
this->mRenderTarget, this->mDepthStencil
);
// Set viewport of render target in which we have to draw (the whole window)
Rect viewport_size = mRenderTarget->getSize();
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->setViewportSize(
viewport_size.width, viewport_size.height
);
// Render skybox
m_list_materials.clear();
m_list_materials.push_back(mSkyMat);
this->drawMesh(mSkyMesh, m_list_materials);
this->updateSkyBox();
// Render player mesh
m_list_materials.clear();
m_list_materials.push_back(mpmat1);
this->drawMesh(mpm1, m_list_materials);
this->updateModel(Vec3(0, 0, 2), Vec3(0, 0, 0), Vec3(111, 111, 111), m_list_materials);
// Normal Mapping Render technique test
m_list_materials.clear();
m_list_materials.push_back(mSphereMat);
this->drawMesh(mSphereMesh, m_list_materials);
this->updateModel(Vec3(3, 1, 0), Vec3(0, 0, 0), Vec3(1, 1, 1), m_list_materials);
// ------------------------------------------------
// Update camera & light
this->updateThirdPersonCamera();
this->updateLight();
m_delta_mouse_x = 0;
m_delta_mouse_y = 0;
m_forward = 0;
m_rightward = 0;
m_time += m_delta_time;
// Clear the whole window and show a solid color
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->clearRenderTargetColor(
this->mSwapChain, 0, 0.3f, 0.4f, 1
);
// Set viewport of render target in which we have to draw (the whole window)
RECT rc = this->getClientWindowRect();
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->setViewportSize(
rc.right - rc.left, rc.bottom - rc.top
);
m_list_materials.clear();
m_list_materials.push_back(mPostProcessMat);
this->drawMesh(mQuadMesh, m_list_materials);
// Start swapping between the back and front buffer and present the rendered images on screen
this->mSwapChain->present(true);
}
void PostProcessingDemo::onDestroy()
{
Window::onDestroy();
}
void PostProcessingDemo::onFocus()
{
Window::onFocus();
InputSystem::get()->addListener(this);
}
void PostProcessingDemo::onKillFocus()
{
Window::onKillFocus();
InputSystem::get()->removeListener(this);
}
void PostProcessingDemo::onResize()
{
Window::onResize();
RECT rc = this->getClientWindowRect();
this->mSwapChain->resize(rc.right - rc.left, rc.bottom - rc.top);
mRenderTarget = GraphicsEngine::get()->getTextureManager()->createTexture(
Rect(rc.right - rc.left, rc.bottom - rc.top), Texture::Type::RenderTarget
);
mDepthStencil = GraphicsEngine::get()->getTextureManager()->createTexture(
Rect(rc.right - rc.left, rc.bottom - rc.top), Texture::Type::DepthStencil
);
mPostProcessMat->removeTexture(0);
mPostProcessMat->addTexture(mRenderTarget);
}
void PostProcessingDemo::onKeyDown(int key)
{
switch (key)
{
case 'W':
{
m_forward = 1.0f;
break;
}
case 'S':
{
m_forward = -1.0f;
break;
}
case 'A':
{
m_rightward = -1.0f;
break;
}
case 'D':
{
m_rightward = 1.0f;
break;
}
default:
break;
}
}
void PostProcessingDemo::onKeyUp(int key)
{
m_forward = 0.0f;
m_rightward = 0.0f;
if (key == 'F') {
// Toggle fullscreen
mFullScreen = (mFullScreen) ? false : true;
RECT size_screen = this->getSizeScreen();
this->mSwapChain->setFullScreen(mFullScreen, size_screen.right, size_screen.bottom);
}
}
void PostProcessingDemo::onMouseMove(const Point & mouse_pos)
{
RECT win_size = this->getClientWindowRect();
int width = win_size.right - win_size.left;
int height = win_size.bottom - win_size.top;
m_delta_mouse_x = (int)(mouse_pos.m_x - (int)(win_size.left + (width / 2.0f)));
m_delta_mouse_y = (int)(mouse_pos.m_y - (int)(win_size.top + (height / 2.0f)));
// Keep the mouse in the center of the screen
InputSystem::get()->setCursorPosition(Point(win_size.left + width / 2.0f, win_size.top + height / 2.0f));
}
void PostProcessingDemo::onLeftMouseDown(const Point & mouse_pos)
{
}
void PostProcessingDemo::onLeftMouseUp(const Point & mouse_pos)
{
}
void PostProcessingDemo::onRightMouseDown(const Point & mouse_pos)
{
}
void PostProcessingDemo::onRightMouseUp(const Point & mouse_pos)
{
}
void PostProcessingDemo::updateModel(Vec3 position, Vec3 rotation, Vec3 scale, const std::vector<MaterialPtr>&list_materials)
{
constant cc;
Matrix4x4 temp;
cc.m_world.setIdentity();
temp.setIdentity();
temp.setScale(scale);
cc.m_world *= temp;
temp.setIdentity();
temp.setRotationX(rotation.m_x);
cc.m_world *= temp;
temp.setIdentity();
temp.setRotationY(rotation.m_y);
cc.m_world *= temp;
temp.setIdentity();
temp.setRotationZ(rotation.m_z);
cc.m_world *= temp;
temp.setIdentity();
temp.setTranslation(position);
cc.m_world *= temp;
cc.m_view = mViewCam;
cc.m_proj = mProjCam;
cc.m_camera_position = mWorldCam.getTranslation();
cc.m_light_position = mLightPosition;
cc.m_light_direction = mLightRotMatrix.getZDirection();
cc.m_time = m_time;
for (size_t m = 0; m < list_materials.size(); m++)
{
list_materials[m]->setData(&cc, sizeof(constant));
}
}
void PostProcessingDemo::updateThirdPersonCamera()
{
Matrix4x4 world_cam, temp;
world_cam.setIdentity();
m_cam_rotation.m_x += m_delta_mouse_y * m_delta_time * 0.1f;
m_cam_rotation.m_y += m_delta_mouse_x * m_delta_time * 0.1f;
// Make the camera to not be able to do 360 degrees spin on the x axis
if (m_cam_rotation.m_x >= 1.57f)
m_cam_rotation.m_x = 1.57f;
else if (m_cam_rotation.m_x <= -1.57f)
m_cam_rotation.m_x = -1.57f;
// Smooth camera roation animation
//m_current_cam_rotation = Vec3::lerp(m_current_cam_rotation, m_cam_rotation, 3.0f * m_delta_time);
// No animation
m_current_cam_rotation = m_cam_rotation;
temp.setIdentity();
temp.setRotationX(m_current_cam_rotation.m_x);
world_cam *= temp;
temp.setIdentity();
temp.setRotationY(m_current_cam_rotation.m_y);
world_cam *= temp;
// Smooth camera animation
//m_current_cam_distance = lerp(m_current_cam_distance, m_cam_distance, 2.0f * m_delta_time);
// No animation
//m_current_cam_distance = m_cam_distance;
m_cam_position = (
m_cam_position +
world_cam.getZDirection() * (m_forward) * 15.0f * m_delta_time +
world_cam.getXDirection() * (m_rightward) * 15.0f * m_delta_time
);
// Used to set the camera behind the player and up a bit
//Vec3 new_pos = m_cam_position + world_cam.getZDirection() * (-m_current_cam_distance);
// Move the camera behind the player a bit on the Y axis (up)
//new_pos = new_pos + world_cam.getYDirection() * (1.4f);
world_cam.setTranslation(m_cam_position);
this->mWorldCam = world_cam;
// Make the camera matrix a view matrix -> invert it
world_cam.inverse();
this->mViewCam = world_cam;
}
void PostProcessingDemo::updateSkyBox()
{
constant cc;
cc.m_world.setIdentity();
cc.m_world.setScale(Vec3(4000.0f, 4000.0f, 4000.0f));
cc.m_world.setTranslation(mWorldCam.getTranslation());
cc.m_view = mViewCam;
cc.m_proj = mProjCam;
mSkyMat->setData(&cc, sizeof(constant));
}
void PostProcessingDemo::updateLight()
{
Matrix4x4 temp;
mLightRotMatrix.setIdentity();
temp.setIdentity();
temp.setRotationX(-0.707f);
mLightRotMatrix *= temp;
temp.setIdentity();
temp.setRotationY(-m_time * .5f);
mLightRotMatrix *= temp;
}
void PostProcessingDemo::drawMesh(const MeshPtr & mesh, const std::vector<MaterialPtr>&list_materials)
{
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->setVertexBuffer(mesh->getVertexBuffer());
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->setIndexBuffer(mesh->getIndexBuffer());
for (size_t m = 0; m < mesh->getNumMaterialSlots(); m++)
{
if (m >= list_materials.size()) break;
MaterialSlot mat = mesh->getMaterialSlot(m);
GraphicsEngine::get()->setMaterial(list_materials[m]);
GraphicsEngine::get()->getRenderSystem()->getImmediateDeviceContext()->drawIndexedTriangleList(mat.NumIndices, 0, mat.StartIndex);
}
}