Skip to content

Commit

Permalink
Updated Scene Objects (#66)
Browse files Browse the repository at this point in the history
* Merged mesh factory branch to main

* Working Vertex buffer class that replaces vk::VulkanModel to contains the vertices of the mesh

* Working index buffers and added mesh class that contains both the vertex and index buffers

* Working mesh class can also now load 3D models and work with new vertex and index buffers as well
  • Loading branch information
SpinnerX authored Nov 23, 2024
1 parent 7697929 commit d8499cf
Show file tree
Hide file tree
Showing 35 changed files with 627 additions and 528 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ project(engine3d CXX)
set(CMAKE_CXX_STANDARD 23)
set(ENGINE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/engine3d)

find_package(tinyobjloader REQUIRED)

build_library(
DIRECTORIES src Editor TestApp
LINK_LIBRARIES tinyobjloader::tinyobjloader
)
3 changes: 2 additions & 1 deletion Editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ project(Editor CXX)
# Editor/Editor.hpp
# Editor/Editor.cpp
# )
find_package(tinyobjloader REQUIRED)

build_demos(
SOURCES
Expand All @@ -14,5 +15,5 @@ build_demos(

Editor/EditorScene.hpp
Editor/EditorScene.cpp
LINK_LIBRARIES engine3d
LINK_LIBRARIES tinyobjloader::tinyobjloader engine3d
)
2 changes: 0 additions & 2 deletions Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "Editor.hpp"
#include "EditorScene.hpp"
#include <Core/internal/Vulkan2Showcase/VulkanModel.hpp>
#include <Core/Scene/SceneTest.hpp>
#include <Core/TimeManagement/GlobalUpdateManager.hpp>
#include <Core/Renderer/Renderer.hpp>

Expand Down
11 changes: 0 additions & 11 deletions Editor/Editor/Editor.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once
// #include <Core/internal/Vulkan2Showcase/VulkanModel.hpp>
#include <Core/ApplicationInstance.hpp>
// #include <Core/Scene/SceneObject.hpp>
// #include <Core/TimeManagement/GlobalUpdateManager.hpp>
#include "EditorScene.hpp"

namespace engine3d{
Expand All @@ -22,15 +19,7 @@ namespace engine3d{
void OnApplicationUpdate();

private:
// Ref<vk::VulkanModel> m_SquareMesh;
// Ref<vk::VulkanModel> m_CircleMesh;
EditorScene* m_EditorScene;
// std::vector<SceneObject> m_SceneGameObjects;
// std::vector<SceneObject> m_VectorFields;

// GravityPhysicsSystem m_GravitySystem;
// Vec2FieldSystem m_VectorFieldSystem;
// Ref<vk::VulkanModel> m_CubeMesh;



Expand Down
120 changes: 58 additions & 62 deletions Editor/Editor/EditorScene.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "EditorScene.hpp"
#include "Core/GraphicDrivers/VertexBuffer.hpp"
#include <Core/SceneManagment/Components/SPComps/Transform.hpp>
#include <Core/internal/Vulkan2Showcase/VulkanModel.hpp>
#include <Core/EngineLogger.hpp>
#include <Core/SceneManagment/Components/SPComps/EditorCamera.hpp>
#include <Core/TimeManagement/UpdateManagers/SyncUpdateManager.hpp>
Expand All @@ -9,69 +9,67 @@
#include <Core/Event/InputPoll.hpp>

namespace engine3d{
Ref<vk::VulkanModel> CreateCubeMesh(glm::vec3 offset){
std::vector<vk::VulkanModel::Vertex> vertices{
// left Face (white)
vk::VulkanModel::Vertex{.Position{-.5f, -.5f, -.5f}, .Color{.9f, .9f, .9f}},
vk::VulkanModel::Vertex{.Position{-.5f, .5f, .5f}, .Color{.9f, .9f, .9f}},
vk::VulkanModel::Vertex{.Position ={-.5f, -.5f, .5f}, .Color{.9f, .9f, .9f}},
vk::VulkanModel::Vertex{.Position ={-.5f, -.5f, -.5f},.Color {.9f, .9f, .9f}},
vk::VulkanModel::Vertex{.Position ={-.5f, .5f, -.5f}, .Color{.9f, .9f, .9f}},
vk::VulkanModel::Vertex{.Position ={-.5f, .5f, .5f}, .Color{.9f, .9f, .9f}},

// right face (yellow)
{{.5f, -.5f, -.5f}, {.8f, .8f, .1f}},
{{.5f, .5f, .5f}, {.8f, .8f, .1f}},
{{.5f, -.5f, .5f}, {.8f, .8f, .1f}},
{{.5f, -.5f, -.5f}, {.8f, .8f, .1f}},
{{.5f, .5f, -.5f}, {.8f, .8f, .1f}},
{{.5f, .5f, .5f}, {.8f, .8f, .1f}},

// top face (orange, remember y axis points down)
{{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
{{.5f, -.5f, .5f}, {.9f, .6f, .1f}},
{{-.5f, -.5f, .5f}, {.9f, .6f, .1f}},
{{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
{{.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
{{.5f, -.5f, .5f}, {.9f, .6f, .1f}},

// bottom face (red)
{{-.5f, .5f, -.5f}, {.8f, .1f, .1f}},
{{.5f, .5f, .5f}, {.8f, .1f, .1f}},
{{-.5f, .5f, .5f}, {.8f, .1f, .1f}},
{{-.5f, .5f, -.5f}, {.8f, .1f, .1f}},
{{.5f, .5f, -.5f}, {.8f, .1f, .1f}},
{{.5f, .5f, .5f}, {.8f, .1f, .1f}},

// nose face (blue)
{{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
{{.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
{{-.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
{{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
{{.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
{{.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
static Mesh CreateCubeMesh(glm::vec3 offset){
std::vector<Vertex> vertices = { // left face (white)
{{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}},
{{-.5f, .5f, .5f}, {.9f, .9f, .9f}},
{{-.5f, -.5f, .5f}, {.9f, .9f, .9f}},
{{-.5f, .5f, -.5f}, {.9f, .9f, .9f}},

// right face (yellow)
{{.5f, -.5f, -.5f}, {.8f, .8f, .1f}},
{{.5f, .5f, .5f}, {.8f, .8f, .1f}},
{{.5f, -.5f, .5f}, {.8f, .8f, .1f}},
{{.5f, .5f, -.5f}, {.8f, .8f, .1f}},

// top face (orange, remember y axis points down)
{{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
{{.5f, -.5f, .5f}, {.9f, .6f, .1f}},
{{-.5f, -.5f, .5f}, {.9f, .6f, .1f}},
{{.5f, -.5f, -.5f}, {.9f, .6f, .1f}},

// bottom face (red)
{{-.5f, .5f, -.5f}, {.8f, .1f, .1f}},
{{.5f, .5f, .5f}, {.8f, .1f, .1f}},
{{-.5f, .5f, .5f}, {.8f, .1f, .1f}},
{{.5f, .5f, -.5f}, {.8f, .1f, .1f}},

// nose face (blue)
{{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
{{.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
{{-.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
{{.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},

// tail face (green)
{{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
{{-.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
};
for (auto& v : vertices) {
v.Position += offset;
}

std::vector<uint32_t> indices = {0, 1, 2, 0, 3, 1, 4, 5, 6, 4, 7, 5, 8, 9, 10, 8, 11, 9,
12, 13, 14, 12, 15, 13, 16, 17, 18, 16, 19, 17, 20, 21, 22, 20, 23, 21};

// tail face (green)
{{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
{{-.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
{{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
};

for (auto& v : vertices) {
v.Position += offset;
}
return CreateRef<vk::VulkanModel>(vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Ref<VertexBuffer> vb = VertexBuffer::Create(vertices);
Ref<IndexBuffer> ib = IndexBuffer::Create(indices);
// return VertexBuffer::Create(vertices);
return Mesh(vb, ib);
}

EditorScene::EditorScene(){
m_Scene = new Scene();

// SyncUpdateManager::GetInstance()->Subscribe(this, &EditorScene::OnUpdate);

auto cube_mesh = CreateCubeMesh({.0f, .0f, .0f});
// auto cube_mesh = CreateCubeMesh({.0f, .0f, .0f});

//! @note Instead of loading in primitive cubes by hand, we load in .obj's instead.
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/smooth_vase.obj");
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/colored_cube.obj");
auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj");

//! @note Make this scene object as part of our current scene.

Expand All @@ -89,10 +87,9 @@ namespace engine3d{
// -----------------------------
SceneObject* cube1 = new SceneObject(m_Scene);
auto& cube1_transform = cube1->SceneGetComponent<Transform>();
// auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio();
cube1_transform.m_Position = {.0f, .0f, 2.5};
cube1_transform.m_Scale = {.5f, .5f, 0.5};
cube1->SetModal(cube_mesh);
cube1->SetMesh(cube_mesh);

// -----------------------------
// Cube 2 Scene object Creation
Expand All @@ -102,8 +99,7 @@ namespace engine3d{
// auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio();
cube2_transform.m_Position = {5.f, .0f, -7.f};
cube2_transform.m_Scale = {.5f, .5f, 0.5};
cube2->SetModal(cube_mesh);

cube2->SetMesh(cube_mesh);



Expand Down Expand Up @@ -133,9 +129,9 @@ namespace engine3d{
auto cube_transform = m_SceneObjects[0]->SceneGetComponent<Transform>();
// float tempDt_Y;
glm::vec2 temp_position = {0.f, 0.f};
constexpr float sensitivity = 5.0f;
constexpr float sensitivity = 2.0f;
constexpr float pos_sensitivity = 2.f;
constexpr glm::vec2 invert_pos = {-1, 1};
constexpr glm::vec2 invert_pos = {1, -1};
// ConsoleLogInfo("x = {}, y = {}, z = {}", transform.m_Position.x, transform.m_Position.y, transform.m_Position.z);
// ConsoleLogInfo("x = {}, y = {}, z = {}\n", cube_transform.m_Position.x, cube_transform.m_Position.y, cube_transform.m_Position.z);

Expand Down
1 change: 1 addition & 0 deletions Editor/Editor/EditorScene.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <entt/entt.hpp>
// #include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <Core/ApplicationManager/Scene.hpp>

Expand Down
5 changes: 3 additions & 2 deletions TestApp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.25)
project(TestApp CXX)

find_package(tinyobjloader REQUIRED)

build_demos(
SOURCES
Application.cpp
Expand Down Expand Up @@ -40,9 +42,8 @@ build_demos(
src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/SphereMesh.cpp
Scenes/Assets/Components/Graphics/Meshes/MeshContainer.hpp
src/Scenes/Assets/Components/Graphics/Meshes/MeshContainer.cpp


LINK_LIBRARIES engine3d
LINK_LIBRARIES tinyobjloader::tinyobjloader engine3d
)

target_include_directories(${PROJECT_NAME} PUBLIC ./)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#pragma once
#include <Core/Core.hpp>
#include <engine3d/Core/GraphicDrivers/VertexBuffer.hpp>
#include <glm/glm.hpp>
#include <vector>
#include <engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp>

//! @brief Factory
class MeshContainer
{
public:
MeshContainer() = default;
std::vector<engine3d::vk::VulkanModel::Vertex>* GetVertices(){return vertices;}
engine3d::Ref<engine3d::VertexBuffer> GetVertices(){return vertices;}
std::vector<glm::vec3> GetNormals(){return normals;}
std::vector<glm::vec2> GettexCoords(){return texCoords;}

protected:
std::vector<engine3d::vk::VulkanModel::Vertex>* vertices;
engine3d::Ref<engine3d::VertexBuffer> vertices;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> texCoords;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "Core/internal/Vulkan2Showcase/VulkanModel.hpp"
#include "Core/EngineLogger.hpp"
#include "Core/GraphicDrivers/VertexBuffer.hpp"
#include <Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/CubeMesh.hpp>
#include <glm/fwd.hpp>
#include <vector>
Expand Down Expand Up @@ -75,6 +76,8 @@ CubeMesh::CubeMesh()
{.1f, .8f, .1f}
};

vertices = engine3d::vk::VulkanModel::CreateVertexVector(verts, colors);

vertices = engine3d::VertexBuffer::CreateVertexVector(verts, colors);
if(vertices != nullptr){
ConsoleLogWarn("SphereMesh vertices are a nullptr. Implement VertexBuffer::CreateVertexVector() in VulkanVertexBuffer to get this working and not nullptr");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Core/internal/Vulkan2Showcase/VulkanModel.hpp"
#include "Core/EngineLogger.hpp"
#include "Core/GraphicDrivers/VertexBuffer.hpp"
#include <Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/SphereMesh.hpp>
#include <numbers>

SphereMesh::SphereMesh(int Radius, int SectorCount, int StackCount)
{

Expand Down Expand Up @@ -47,5 +49,9 @@ SphereMesh::SphereMesh(int Radius, int SectorCount, int StackCount)
}
}

vertices = engine3d::vk::VulkanModel::CreateVertexVector(verts);
vertices = engine3d::VertexBuffer::CreateVertexVector(verts);

if(vertices != nullptr){
ConsoleLogWarn("SphereMesh vertices are a nullptr. Implement VertexBuffer::CreateVertexVector() in VulkanVertexBuffer to get this working and not nullptr");
}
}
2 changes: 2 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def requirements(self):
self.requires("imguidocking/1.0")
self.requires("entt/3.13.2")

self.requires("tinyobjloader/2.0.0-rc10")


self.requires("joltphysics/1.0")

Expand Down
21 changes: 21 additions & 0 deletions engine3d/Core/GraphicDrivers/IndexBuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <Core/Core.hpp>
#include <vector>
#include <vulkan/vulkan_core.h>

namespace engine3d{
class IndexBuffer{
public:
static Ref<IndexBuffer> Create(std::vector<uint32_t> p_Indices);

void Bind(VkCommandBuffer p_CommandBuffer);
void Draw(VkCommandBuffer p_CommandBuffer);

bool HasIndicesPresent() const;

protected:
virtual void BindToIndexBuffer(VkCommandBuffer p_CommandBuffer) = 0;
virtual void RenderIndexBuffer(VkCommandBuffer p_CommandBuffer) = 0;
virtual bool HasIndices() const = 0;
};
};
28 changes: 28 additions & 0 deletions engine3d/Core/GraphicDrivers/Mesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <Core/Core.hpp>
#include <Core/GraphicDrivers/VertexBuffer.hpp>
#include <Core/GraphicDrivers/IndexBuffer.hpp>

#define TINYOBJLOADER_IMPLEMENTATION
#include <tiny_obj_loader.h>

namespace engine3d{

class Mesh{
public:
Mesh() = default;
Mesh(const Ref<VertexBuffer>& p_Vb, const Ref<IndexBuffer>& p_Ib);

static Mesh LoadModel(const std::string& p_Filename);

Ref<VertexBuffer>& GetVertices() { return m_Vertices; }

Ref<IndexBuffer>& GetIndices() { return m_Indices; }

private:
Ref<VertexBuffer> m_Vertices;
Ref<IndexBuffer> m_Indices;
};

};
Loading

0 comments on commit d8499cf

Please sign in to comment.