Skip to content

Commit 146f08c

Browse files
authored
Merge pull request #54 from MrScriptX/engine
clean buffers when updating mesh
2 parents c69831f + 04a0349 commit 146f08c

572 files changed

Lines changed: 660 additions & 42 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Engine/Engine.cpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Engine::~Engine()
3030
for (size_t t = 0; t < mp_scene->getObjects()[i]->getMeshesCount(); t++)
3131
{
3232
mp_scene->getObjects()[i]->getMesh(t).getMaterial()->DestroyTexture();
33-
mp_scene->getObjects()[i]->getMesh(t).destroyMesh();
33+
mp_scene->getObjects()[i]->getMesh(t).DestroyBuffers();
3434
}
3535

3636
mp_scene->getObjects()[i]->destroy();
@@ -80,6 +80,49 @@ const std::shared_ptr<GameObject> Engine::CreateGameObject(const std::string& ob
8080
return go;
8181
}
8282

83+
const std::shared_ptr<GameObject> Engine::CreateCube(const glm::vec3& position, const float& size, const glm::vec3& vcolor)
84+
{
85+
std::shared_ptr<GameObject> cube = std::make_shared<GameObject>(mp_renderer);
86+
87+
Geometry g;
88+
89+
const float half_size = size / 2;
90+
91+
//vertices
92+
g.addVertex({ -half_size, -half_size, -half_size }, vcolor, { .0f, .0f });
93+
g.addVertex({ half_size, -half_size, -half_size }, vcolor, { .0f, 2.0f });
94+
g.addVertex({ -half_size, half_size, -half_size }, vcolor, { 2.0f, .0f });
95+
g.addVertex({ half_size, half_size, -half_size }, vcolor, { 2.0f, .0f });
96+
g.addVertex({ -half_size, -half_size, half_size }, vcolor, { .0f, .0f });
97+
g.addVertex({ half_size, -half_size, half_size }, vcolor, { .0f, 2.0f });
98+
g.addVertex({ -half_size, half_size, half_size }, vcolor, { 2.0f, .0f });
99+
g.addVertex({ half_size, half_size, half_size }, vcolor, { 2.0f, 2.0f });
100+
101+
//indices
102+
g.addIndices(0, 2, 1);
103+
g.addIndices(1, 2, 3);
104+
105+
g.addIndices(5, 7, 4);
106+
g.addIndices(4, 7, 6);
107+
108+
g.addIndices(1, 3, 5);
109+
g.addIndices(5, 3, 7);
110+
111+
g.addIndices(4, 6, 0);
112+
g.addIndices(0, 6, 2);
113+
114+
g.addIndices(2, 6, 3);
115+
g.addIndices(3, 6, 7);
116+
117+
g.addIndices(4, 0, 5);
118+
g.addIndices(5, 0, 1);
119+
120+
cube->LoadMesh(g.vertices, g.indices);
121+
cube->setPosition(position);
122+
123+
return cube;
124+
}
125+
83126
void Engine::BindKeyToFunc(const int& key, std::function<void()>& func, const ActionType& type)
84127
{
85128
mp_controller->SetKeyToFunc(key, func, type);
@@ -122,13 +165,18 @@ void Engine::update()
122165
m_last_time = current_time;
123166

124167
const int32_t frame = mp_renderer->AcquireNextImage();
125-
if (frame != -1 && (mp_scene->isUpdate(frame) || mp_renderer->IsUpdated(frame)))
168+
if (frame != -1 && (mp_scene->isUpdate(frame) || mp_renderer->NeedUpdate(frame)))
126169
{
127170
mp_renderer->beginRecordCommandBuffers(mp_renderer->getCommandBuffer(frame), mp_renderer->getFrameBuffer(frame));
128171
mp_scene->render(mp_renderer->getCommandBuffer(frame), frame);
129172
mp_renderer->endRecordCommandBuffers(mp_renderer->getCommandBuffer(frame));
130173

131-
mp_renderer->SetUpdate(frame);
174+
mp_renderer->SetUpdated(frame);
175+
176+
if (mp_renderer->IsUpdated())
177+
{
178+
mp_scene->Clean();
179+
}
132180
}
133181

134182
mp_main_camera->UpdateUBO(static_cast<float>(mp_config->width), static_cast<float>(mp_config->height));

Engine/Engine.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#include "Window.h"
55

6-
#include "world/GameObject.h"
76
#include "world/Scene.h"
7+
#include "world/GameObject.h"
8+
9+
#include "graphics/Geometry.h"
810

911
class Engine
1012
{
@@ -26,6 +28,8 @@ class Engine
2628
// CREATE GAMEOBJECT
2729
const std::shared_ptr<GameObject> CreateGameObject();
2830
const std::shared_ptr<GameObject> CreateGameObject(const std::string& object_file);
31+
32+
const std::shared_ptr<GameObject> CreateCube(const glm::vec3& position, const float& size, const glm::vec3& vcolor);
2933

3034
// CONTROLLER
3135
void BindKeyToFunc(const int& key, std::function<void()>& func, const ActionType& type = ActionType::R3D_PRESS);
@@ -49,7 +53,7 @@ class Engine
4953
std::shared_ptr<Camera> mp_main_camera;
5054
std::shared_ptr<Controller> mp_controller;
5155
std::shared_ptr<Config> mp_config;
52-
56+
5357
std::chrono::steady_clock::time_point m_last_time;
5458
};
5559

Engine/Engine.vcxproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<ClInclude Include="Engine.h" />
7272
<ClInclude Include="errors.h" />
7373
<ClInclude Include="graphics\Config.h" />
74+
<ClInclude Include="graphics\Geometry.h" />
7475
<ClInclude Include="graphics\Graphics.h" />
7576
<ClInclude Include="graphics\Math.h" />
7677
<ClInclude Include="graphics\Shaders.h" />
@@ -134,7 +135,7 @@
134135
<CharacterSet>NotSet</CharacterSet>
135136
</PropertyGroup>
136137
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
137-
<ConfigurationType>Application</ConfigurationType>
138+
<ConfigurationType>StaticLibrary</ConfigurationType>
138139
<UseDebugLibraries>true</UseDebugLibraries>
139140
<PlatformToolset>v142</PlatformToolset>
140141
<CharacterSet>MultiByte</CharacterSet>
@@ -229,11 +230,11 @@
229230
<Optimization>Disabled</Optimization>
230231
<SDLCheck>true</SDLCheck>
231232
<ConformanceMode>true</ConformanceMode>
232-
<AdditionalIncludeDirectories>$(ProjectDir)\libs\stb_image;$(VULKAN_SDK)\Include;$(ProjectDir)\libs\glfw-3.3.4\include;$(ProjectDir)\libs\glm;$(ProjectDir)\libs\assimp-5.0.1\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
233+
<AdditionalIncludeDirectories>$(VULKAN_SDK)\Include;$(SolutionDir)\dependencies\stb_image;$(SolutionDir)\dependencies\glfw-3.3.4\include;$(SolutionDir)\dependencies\glm;$(SolutionDir)\dependencies\assimp-5.0.1\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
233234
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
234235
</ClCompile>
235236
<Link>
236-
<AdditionalLibraryDirectories>$(VULKAN_SDK)\Lib;$(ProjectDir)\libs\glfw-3.3.4\lib;$(ProjectDir)\glew-2.1.0\lib;$(ProjectDir)\libs\assimp-5.0.1\lib\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
237+
<AdditionalLibraryDirectories>$(VULKAN_SDK)\Lib;$(SolutionDir)\libs\glfw-3.3.4\lib;$(SolutionDir)\libs\glew-2.1.0\lib;$(SolutionDir)\libs\assimp-5.0.1\lib\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
237238
<AdditionalDependencies>vulkan-1.lib;glfw3.lib;assimp-vc142-mtd.lib;%(AdditionalDependencies)</AdditionalDependencies>
238239
</Link>
239240
</ItemDefinitionGroup>

Engine/Engine.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
<ClInclude Include="graphics\Shaders.h">
177177
<Filter>Engine\Graphics</Filter>
178178
</ClInclude>
179+
<ClInclude Include="graphics\Geometry.h">
180+
<Filter>Engine\Graphics</Filter>
181+
</ClInclude>
179182
</ItemGroup>
180183
<ItemGroup>
181184
<None Include="assets\shaders\HLSL\no_texture_shader.frag">

Engine/Source.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,58 @@ int main()
1313
std::shared_ptr<GameObject> room = engine.CreateGameObject();
1414
room->loadMesh("assets/models/viking_room.obj");
1515
room->bindMatToMesh(0, room_texture);
16-
room->setPosition({ 3.0f, 0.0f, 0.0f });
16+
room->setPosition({ 3.0f, 0.0f, 3.0f });
1717

1818
std::shared_ptr<Material> room2_texture = engine.CreateMaterial(TSHADER::NO_TEXTURE);
1919
std::shared_ptr<GameObject> room2 = engine.CreateGameObject();
2020
room2->loadMesh("assets/models/viking_room.obj");
2121
room2->bindMatToMesh(0, room2_texture);
2222
room2->setPosition({ 6.0f, 0.0f, 0.0f });
2323

24+
std::shared_ptr<Material> cube_texture = engine.CreateMaterial(TSHADER::NO_TEXTURE);
25+
std::shared_ptr<GameObject> cube = engine.CreateCube({ 2.0f, .0f, .0f }, 1.f, {1.f, .0f, .0f});
26+
cube->bindMatToMesh(0, cube_texture);
27+
28+
const float half_size = 1.f / 2.f;
29+
Geometry voxel;
30+
31+
//vertices
32+
uint32_t index_0 = voxel.addVertex({ 0.f - half_size, 0.f - half_size, -2.f - half_size }, { 0.f, 1.f, 0.f }, { .0f, .0f });
33+
uint32_t index_1 = voxel.addVertex({ 0.f + half_size, 0.f - half_size, -2.f - half_size }, { 0.f, 1.f, 0.f }, { .0f, 2.0f });
34+
uint32_t index_2 = voxel.addVertex({ 0.f - half_size, 0.f + half_size, -2.f - half_size }, { 0.f, 1.f, 0.f }, { 2.0f, .0f });
35+
uint32_t index_3 = voxel.addVertex({ 0.f + half_size, 0.f + half_size, -2.f - half_size }, { 0.f, 1.f, 0.f }, { 2.0f, .0f });
36+
uint32_t index_4 = voxel.addVertex({ 0.f - half_size, 0.f - half_size, -2.f + half_size }, { 0.f, 1.f, 0.f }, { .0f, .0f });
37+
uint32_t index_5 = voxel.addVertex({ 0.f + half_size, 0.f - half_size, -2.f + half_size }, { 0.f, 1.f, 0.f }, { .0f, 2.0f });
38+
uint32_t index_6 = voxel.addVertex({ 0.f - half_size, 0.f + half_size, -2.f + half_size }, { 0.f, 1.f, 0.f }, { 2.0f, .0f });
39+
uint32_t index_7 = voxel.addVertex({ 0.f + half_size, 0.f + half_size, -2.f + half_size }, { 0.f, 1.f, 0.f }, { 2.0f, 2.0f });
40+
41+
//indices
42+
voxel.addIndices(index_0, index_2, index_1);
43+
voxel.addIndices(index_1, index_2, index_3);
44+
45+
voxel.addIndices(index_5, index_7, index_4);
46+
voxel.addIndices(index_4, index_7, index_6);
47+
48+
voxel.addIndices(index_1, index_3, index_5);
49+
voxel.addIndices(index_5, index_3, index_7);
50+
51+
voxel.addIndices(index_4, index_6, index_0);
52+
voxel.addIndices(index_0, index_6, index_2);
53+
54+
voxel.addIndices(index_2, index_6, index_3);
55+
voxel.addIndices(index_3, index_6, index_7);
56+
57+
voxel.addIndices(index_4, index_0, index_5);
58+
voxel.addIndices(index_5, index_0, index_1);
59+
60+
cube->LoadMesh(voxel.vertices, voxel.indices);
61+
62+
cube->bindMatToMesh(1, cube_texture);
63+
2464
std::shared_ptr<Scene> scene = std::make_shared<Scene>();
2565
scene->addGameObject(room);
66+
scene->addGameObject(room2);
67+
scene->addGameObject(cube);
2668

2769
engine.setScene(scene);
2870

@@ -45,9 +87,17 @@ int main()
4587
// running loop
4688
do
4789
{
48-
if (init++ == 30000)
90+
//cube->setPosition(cube->getPosition() + glm::vec3{0.0001f, 0.f, 0.f});
91+
92+
if (init++ == 10000)
4993
{
50-
scene->addGameObject(room2);
94+
Geometry v;
95+
v.vertices = cube->GetVertices(1);
96+
v.indices = cube->GetIndices(1);
97+
v.vertices[0].color = { .0f, .0f, 1.0f };
98+
99+
cube->UpdateMesh(1, v.vertices, v.indices);
100+
scene->Update();
51101
}
52102

53103
engine.update();

Engine/graphics/Geometry.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _GEOMETRY_H
2+
#define _GEOMETRY_H
3+
4+
#include "Vertex.h"
5+
6+
struct Geometry
7+
{
8+
std::vector<Vertex> vertices;
9+
std::vector<uint32_t> indices;
10+
11+
int32_t nb_vertices = -1;
12+
13+
uint32_t addVertex(glm::vec3 pos, glm::vec3 color, glm::vec2 texCoord)
14+
{
15+
Vertex vertex;
16+
vertex.pos = pos;
17+
vertex.color = color;
18+
vertex.texCoord = texCoord;
19+
20+
vertices.push_back(vertex);
21+
22+
nb_vertices++;
23+
24+
return nb_vertices;
25+
}
26+
27+
void addIndices(uint32_t x, uint32_t y, uint32_t z)
28+
{
29+
indices.push_back(x);
30+
indices.push_back(y);
31+
indices.push_back(z);
32+
}
33+
};
34+
35+
#endif

Engine/graphics/Vertex.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <glm/gtx/hash.hpp>
1212

1313

14-
1514
struct Vertex
1615
{
1716
glm::vec3 pos;

Engine/renderer/Renderer.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Renderer::Renderer(GLFWwindow& window, uint32_t width, uint32_t height)
1616
{
1717
WIDTH = std::make_unique<uint32_t>(width);
1818
HEIGHT = std::make_unique<uint32_t>(height);
19-
m_is_updated.fill(false);
19+
m_is_updated = { false };
2020

2121
m_pBufferFactory = std::make_unique<VulkanBuffer>(m_graphic);
2222

@@ -223,14 +223,19 @@ const int Renderer::getFrameIndex()
223223
return m_current_image;
224224
}
225225

226-
const bool& Renderer::IsUpdated(const size_t& i)
226+
const bool Renderer::IsUpdated()
227+
{
228+
return m_is_updated == false;
229+
}
230+
231+
const bool Renderer::NeedUpdate(const size_t& i)
227232
{
228233
return m_is_updated[i];
229234
}
230235

231-
void Renderer::SetUpdate(const size_t& i)
236+
void Renderer::SetUpdated(const size_t& i)
232237
{
233-
m_is_updated[i] = false;
238+
m_is_updated.set(i, false);
234239
}
235240

236241
const uint32_t& Renderer::GetHeight()
@@ -668,7 +673,7 @@ void Renderer::recreateSwapchain()
668673
createFramebuffer();
669674
allocateCommandBuffers();
670675

671-
m_is_updated.fill(true);
676+
m_is_updated.set(true);
672677
}
673678

674679
void Renderer::cleanSwapchain()

Engine/renderer/Renderer.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <iostream>
1414
#include <memory>
1515
#include <algorithm>
16+
#include <bitset>
1617

1718
#include "VulkanInstance.h"
1819
#include "VulkanDevice.h"
@@ -57,10 +58,12 @@ class Renderer
5758
std::unique_ptr<VulkanBuffer>& getBufferFactory();
5859
std::unique_ptr<VulkanPipeline>& GetPipelineFactory();
5960
const int getFrameIndex();
60-
const bool& IsUpdated(const size_t& i);
61-
void SetUpdate(const size_t& i);
6261
const uint32_t& GetHeight();
6362

63+
const bool IsUpdated();
64+
const bool NeedUpdate(const size_t& i);
65+
void SetUpdated(const size_t& i);
66+
6467
//rendering
6568
void createVerticesBuffer(std::shared_ptr<std::vector<Vertex>> vertices, Buffer& buffer);
6669
void createIndicesBuffer(std::shared_ptr<std::vector<uint32_t>> indices, Buffer& buffer);
@@ -148,7 +151,7 @@ class Renderer
148151

149152
uint32_t m_current_image = 0;
150153
uint32_t m_last_image = 0;
151-
std::array<bool, 3> m_is_updated;
154+
std::bitset<3> m_is_updated;
152155
};
153156

154157
#endif _RENDERER_H

0 commit comments

Comments
 (0)