Skip to content

Commit 907bca1

Browse files
authored
Merge pull request #53 from MrScriptX/engine
engine interface change
2 parents 2ebf5a8 + a7a278e commit 907bca1

570 files changed

Lines changed: 566 additions & 13 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

Engine/Engine.h

Lines changed: 5 additions & 1 deletion
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);

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/world/GameObject.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ void GameObject::bindMatToMesh(const size_t& index, std::shared_ptr<Material> p_
3535
m_meshes[index].bindMaterial(p_material, m_ubo, mp_renderer);
3636
}
3737

38+
void GameObject::LoadMesh(std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
39+
{
40+
Mesh mesh(vertices, indices, mp_renderer);
41+
mesh.createBuffer(mp_renderer);
42+
43+
m_meshes.push_back(mesh);
44+
}
45+
3846
void GameObject::loadMesh(const std::string& mesh_path)
3947
{
4048
Mesh mesh(mesh_path, mp_renderer);
@@ -43,11 +51,34 @@ void GameObject::loadMesh(const std::string& mesh_path)
4351
m_meshes.push_back(mesh);
4452
}
4553

54+
void GameObject::UpdateMesh(const size_t& index, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
55+
{
56+
m_meshes[index].SetVertices(vertices);
57+
m_meshes[index].SetIndices(indices);
58+
m_meshes[index].createBuffer(mp_renderer);
59+
}
60+
61+
std::vector<Vertex> GameObject::GetVertices(const size_t& index)
62+
{
63+
return m_meshes[index].get_vertices();
64+
}
65+
66+
std::vector<uint32_t> GameObject::GetIndices(const size_t& index)
67+
{
68+
return m_meshes[index].get_indices();
69+
}
70+
4671
Mesh& GameObject::getMesh(const size_t& index)
4772
{
4873
return m_meshes[index];
4974
}
5075

76+
void GameObject::setMesh(const size_t& index, std::vector<Vertex> vertices)
77+
{
78+
m_meshes[index].SetVertices(vertices);
79+
m_meshes[index].createBuffer(mp_renderer);
80+
}
81+
5182
void GameObject::setPosition(const glm::vec3& pos)
5283
{
5384
m_position = pos;

Engine/world/GameObject.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ class GameObject
1717
void registerDrawCmd(VkCommandBuffer& command_buffer);
1818
void bindMatToMesh(const size_t& index, std::shared_ptr<Material> p_material);
1919

20+
// MESH
21+
void LoadMesh(std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices);
2022
void loadMesh(const std::string& mesh_path);
23+
24+
void UpdateMesh(const size_t& index, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices);
25+
26+
std::vector<Vertex> GetVertices(const size_t& index);
27+
std::vector<uint32_t> GetIndices(const size_t& index);
28+
2129
Mesh& getMesh(const size_t& index);
30+
void setMesh(const size_t& index, std::vector<Vertex> vertices);
2231

2332
void setPosition(const glm::vec3& pos);
2433
const glm::vec3& getPosition();

Engine/world/Geometry.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _GEOMETRY_H
2+
#define _GEOMETRY_H
3+
4+
struct Voxel
5+
{
6+
std::vector<> vertices;
7+
std::vector<uint16_t> indices;
8+
};
9+
10+
#endif

0 commit comments

Comments
 (0)