Skip to content

Commit 1b71e68

Browse files
authored
lighting (#80)
* introduce gameobject id * return mesh id on loadmesh * fast forward * directional lighting (#76) * add normals to vertex * vertex normal computed * implement viewing normal map shader * assets are moved so has to not copy them * light shader * add scene descriptorset * shader for light source * send multiple light types * cleanup (#77) * fix styling * fix normalmap example * no light use in examplecube * remove unused functions & moved to private internal functions * remove unused functions & move to private helper fonctions * fix code smells * split camera ubo and sceneubo * clang format * point light (#79) * add normals to vertex * vertex normal computed * implement viewing normal map shader * assets are moved so has to not copy them * light shader * specular light * remove compiled shader files * add binding of light * add scene descriptorset * remove unused code * add allocate descriptor set for ligthing with specific layout * bind new descriptor set for lighting * light transform is passed to shader * light array handle by scene is send to shader * pass number of lights to shader * fix normals * shader for light source * send multiple light types * fix cube geometry * refactoring & renaming * spotlight compute * fix merge issues * fix json
1 parent a4db22e commit 1b71e68

28 files changed

+590
-428
lines changed

Engine/ClassDiagram.cd

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<ClassDiagram />
2+
<ClassDiagram MajorVersion="1" MinorVersion="1">
3+
<Class Name="Engine" Collapsed="true">
4+
<Position X="4.75" Y="1.5" Width="1.5" />
5+
<TypeIdentifier>
6+
<HashCode>IAAAAAAAAAQIAAAAAEIAAAwwBAAIAKEABAAARCCUAgA=</HashCode>
7+
<FileName>Includes\Engine.h</FileName>
8+
</TypeIdentifier>
9+
</Class>
10+
<Class Name="Window" Collapsed="true">
11+
<Position X="4.75" Y="2.5" Width="1.5" />
12+
<TypeIdentifier>
13+
<HashCode>AAAAAACAAAAAAAAAAAAAAEAAAQQAAAABAAAACAAAAAA=</HashCode>
14+
<FileName>Includes\Window.h</FileName>
15+
</TypeIdentifier>
16+
</Class>
17+
<Class Name="Logger" Collapsed="true">
18+
<Position X="6.5" Y="1.5" Width="1.5" />
19+
<TypeIdentifier>
20+
<HashCode>AAAQAAAAAAIAAAAAAAAAAAAAAAAAAAIAAAAAAEAAAAA=</HashCode>
21+
<FileName>Includes\Logger.h</FileName>
22+
</TypeIdentifier>
23+
</Class>
24+
<Enum Name="R3DResult" Collapsed="true">
25+
<Position X="4.75" Y="3.75" Width="1.5" />
26+
<TypeIdentifier>
27+
<HashCode>CAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAEAAgAAAAAA=</HashCode>
28+
<FileName>Includes\errors.h</FileName>
29+
</TypeIdentifier>
30+
</Enum>
31+
<Font Name="Segoe UI" Size="9" />
32+
</ClassDiagram>

Engine/Engine.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include "Includes/Engine.h"
22

3-
Engine::Engine(uint32_t width, uint32_t height)
3+
Engine::Engine(uint32_t width, uint32_t height) : m_last_time(std::chrono::high_resolution_clock::now())
44
{
5-
m_last_time = std::chrono::high_resolution_clock::now();
6-
75
mp_main_camera = std::make_shared<Camera>();
86
mp_controller = std::make_shared<Controller>(mp_main_camera);
97
mp_config = std::make_shared<Config>();
@@ -13,17 +11,11 @@ Engine::Engine(uint32_t width, uint32_t height)
1311
mp_window = std::make_unique<Window>(mp_config, *mp_controller.get());
1412

1513
mp_renderer = std::make_shared<Renderer>(mp_window->getHandle(), mp_config->width, mp_config->height);
16-
17-
mp_renderer->createDepthResources();
18-
mp_renderer->createFramebuffer();
19-
20-
mp_renderer->createDescriptorPool();
21-
mp_renderer->allocateCommandBuffers();
2214
}
2315

2416
Engine::~Engine()
2517
{
26-
vkDeviceWaitIdle(mp_renderer->getDevice());
18+
vkDeviceWaitIdle(mp_renderer->GetDevice());
2719

2820
mp_renderer->cleanSwapchain();
2921

@@ -189,9 +181,9 @@ void Engine::update()
189181

190182
mp_scene->Update(frame);
191183

192-
mp_renderer->beginRecordCommandBuffers(mp_renderer->getCommandBuffer(frame), mp_renderer->getFrameBuffer(frame));
193-
mp_scene->Render(mp_renderer->getCommandBuffer(frame), frame);
194-
mp_renderer->endRecordCommandBuffers(mp_renderer->getCommandBuffer(frame));
184+
mp_renderer->BeginRecordCommandBuffers(mp_renderer->GetCommandBuffer(frame), mp_renderer->GetFrameBuffer(frame));
185+
mp_scene->Render(mp_renderer->GetCommandBuffer(frame), frame);
186+
mp_renderer->EndRecordCommandBuffers(mp_renderer->GetCommandBuffer(frame));
195187

196188
mp_scene->Clean(frame);
197189
mp_renderer->SetUpdated(frame);

Engine/Engine.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@
7272
<ClInclude Include="Includes\camera\Controller.h" />
7373
<ClInclude Include="Includes\Engine.h" />
7474
<ClInclude Include="Includes\errors.h" />
75+
<ClInclude Include="Includes\graphics\CameraUBO.h" />
7576
<ClInclude Include="Includes\graphics\Config.h" />
7677
<ClInclude Include="Includes\graphics\Geometry.h" />
7778
<ClInclude Include="Includes\graphics\Graphics.h" />
79+
<ClInclude Include="Includes\graphics\LightProperties.h" />
7880
<ClInclude Include="Includes\graphics\Math.h" />
81+
<ClInclude Include="Includes\graphics\SceneUBO.h" />
7982
<ClInclude Include="Includes\graphics\Shaders.h" />
8083
<ClInclude Include="Includes\graphics\Vertex.h" />
8184
<ClInclude Include="Includes\Logger.h" />
@@ -104,6 +107,7 @@
104107
<None Include="..\assets\shaders\HLSL\shader.vert" />
105108
<None Include="..\assets\shaders\HLSL\shader_compiler.bat" />
106109
<None Include="..\assets\shaders\HLSL\texture_shader.frag" />
110+
<None Include="ClassDiagram.cd" />
107111
</ItemGroup>
108112
<PropertyGroup Label="Globals">
109113
<VCProjectVersion>15.0</VCProjectVersion>

Engine/Engine.vcxproj.filters

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@
191191
<ClInclude Include="Includes\world\LightObject.h">
192192
<Filter>Engine\World</Filter>
193193
</ClInclude>
194+
<ClInclude Include="Includes\graphics\CameraUBO.h">
195+
<Filter>Engine\Graphics</Filter>
196+
</ClInclude>
197+
<ClInclude Include="Includes\graphics\SceneUBO.h">
198+
<Filter>Engine\Graphics</Filter>
199+
</ClInclude>
200+
<ClInclude Include="Includes\graphics\LightProperties.h">
201+
<Filter>Engine\Graphics</Filter>
202+
</ClInclude>
194203
</ItemGroup>
195204
<ItemGroup>
196205
<None Include="..\assets\shaders\HLSL\no_texture_shader.frag">
@@ -208,5 +217,6 @@
208217
<None Include="..\assets\shaders\HLSL\texture_shader.frag">
209218
<Filter>Asset</Filter>
210219
</None>
220+
<None Include="ClassDiagram.cd" />
211221
</ItemGroup>
212222
</Project>

Engine/Includes/Logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Logger
1616
~Logger();
1717

1818
static void init();
19-
static void registerError(std::string errorMsg);
19+
static void registerError(const std::string& errorMsg);
2020
};
2121

2222
#endif //!_LOGGER_H

Engine/Includes/camera/Camera.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#ifndef R3DENGINE_CAMERA_H_
22
#define R3DENGINE_CAMERA_H_
33

4+
#include <array>
5+
46
#include <bitset>
57
#include <iostream>
68
#include <memory>
79

10+
#include "../graphics/CameraUBO.h"
811
#include "../graphics/Graphics.h"
912
#include "../graphics/Math.h"
10-
#include "../graphics/Vertex.h"
1113

1214
class Camera
1315
{

Engine/Includes/camera/Controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <glm/gtc/matrix_transform.hpp>
77
#include <glm/gtc/quaternion.hpp>
88

9+
#include <functional>
10+
911
#include "Camera.h"
1012

1113
struct Actions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef R3DENGINE_CAMERA_UBO_H_
2+
#define R3DENGINE_CAMERA_UBO_H_
3+
4+
#include <glm/glm.hpp>
5+
6+
struct UniformBufferObject
7+
{
8+
alignas(16) glm::vec3 position;
9+
alignas(16) glm::mat4 model;
10+
alignas(16) glm::mat4 view;
11+
alignas(16) glm::mat4 proj;
12+
};
13+
14+
#endif // !R3DENGINE_CAMERA_UBO_H_
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef R3DENGINE_LIGHT_PROPERTIES_H_
2+
#define R3DENGINE_LIGHT_PROPERTIES_H_
3+
4+
#include <glm/glm.hpp>
5+
6+
struct DirectionalLight
7+
{
8+
alignas(4) float ambient_strength;
9+
alignas(4) float diffuse_strength;
10+
alignas(4) float specular_strength;
11+
alignas(16) glm::vec3 color;
12+
alignas(16) glm::vec3 position;
13+
alignas(16) glm::vec3 direction;
14+
};
15+
16+
struct SpotLight
17+
{
18+
alignas(4) float ambient_strength;
19+
alignas(4) float diffuse_strength;
20+
alignas(4) float specular_strength;
21+
alignas(16) glm::vec3 color;
22+
alignas(16) glm::vec3 position;
23+
alignas(16) glm::vec3 direction;
24+
};
25+
26+
struct PointLight
27+
{
28+
alignas(4) float constant;
29+
alignas(4) float linear;
30+
alignas(4) float quadratic;
31+
32+
alignas(4) float ambient_strength;
33+
alignas(4) float diffuse_strength;
34+
alignas(4) float specular_strength;
35+
36+
alignas(16) glm::vec3 color;
37+
alignas(16) glm::vec3 position;
38+
};
39+
40+
#endif // !R3DENGINE_LIGHT_PROPERTIES_H_
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef R3DENGINE_SCENE_UBO_H_
2+
#define R3DENGINE_SCENE_UBO_H_
3+
4+
#include "LightProperties.h"
5+
6+
constexpr size_t MAX_LIGHT = 10;
7+
8+
struct SceneUBO
9+
{
10+
alignas(4) unsigned int nb_directional = 0;
11+
alignas(4) unsigned int nb_spotlight = 0;
12+
alignas(4) unsigned int nb_pointlight = 0;
13+
DirectionalLight directionals[MAX_LIGHT];
14+
SpotLight spots[MAX_LIGHT];
15+
PointLight points[MAX_LIGHT];
16+
};
17+
18+
#endif // !R3DENGINE_SCENE_UBO_H_

0 commit comments

Comments
 (0)