mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 06:33:36 +00:00
world generation
This commit is contained in:
19
ScopEngine/Runtime/Includes/Core/UUID.h
git.filemode.normal_file
19
ScopEngine/Runtime/Includes/Core/UUID.h
git.filemode.normal_file
@@ -0,0 +1,19 @@
|
||||
#ifndef __SCOP_CORE_UUID__
|
||||
#define __SCOP_CORE_UUID__
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
class UUID
|
||||
{
|
||||
public:
|
||||
UUID();
|
||||
inline operator std::uint64_t() const noexcept { return m_uuid; }
|
||||
|
||||
private:
|
||||
std::uint64_t m_uuid;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <Maths/Quaternions.h>
|
||||
#include <Core/Script.h>
|
||||
#include <Graphics/Model.h>
|
||||
#include <Core/UUID.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
@@ -30,6 +31,7 @@ namespace Scop
|
||||
[[nodiscard]] inline const Quatf& GetOrientation() const noexcept { return m_orientation; }
|
||||
[[nodiscard]] inline const Model& GetModel() const noexcept { return m_model; }
|
||||
[[nodiscard]] inline Model& GetModelRef() noexcept { return m_model; }
|
||||
[[nodiscard]] inline std::uint32_t GetUUID() const noexcept { return m_uuid; }
|
||||
|
||||
~Actor();
|
||||
|
||||
@@ -43,6 +45,7 @@ namespace Scop
|
||||
Vec3f m_position = Vec3f{ 0.0f, 0.0f, 0.0f };
|
||||
Vec3f m_scale = Vec3f{ 1.0f, 1.0f, 1.0f };
|
||||
std::shared_ptr<ActorScript> p_script;
|
||||
std::uint64_t m_uuid;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -16,23 +16,18 @@ namespace Scop
|
||||
public:
|
||||
struct SubMesh
|
||||
{
|
||||
VertexBuffer vbo;
|
||||
IndexBuffer ibo;
|
||||
MeshBuffer buffer;
|
||||
std::size_t index_size;
|
||||
std::size_t triangle_count = 0;
|
||||
|
||||
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
|
||||
{
|
||||
CPUBuffer vb(vertices.size() * sizeof(Vertex));
|
||||
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
|
||||
vbo.Init(vb.GetSize());
|
||||
vbo.SetData(std::move(vb));
|
||||
|
||||
CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
|
||||
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
|
||||
ibo.Init(ib.GetSize());
|
||||
ibo.SetData(std::move(ib));
|
||||
|
||||
CPUBuffer data(vertices.size() * sizeof(Vertex) + indices.size() * sizeof(std::uint32_t));
|
||||
std::memcpy(data.GetData(), vertices.data(), vertices.size() * sizeof(Vertex));
|
||||
std::memcpy(data.GetData() + vertices.size() * sizeof(Vertex), indices.data(), indices.size() * sizeof(std::uint32_t));
|
||||
buffer.Init(vertices.size() * sizeof(Vertex), indices.size() * sizeof(std::uint32_t), 0, std::move(data));
|
||||
triangle_count = vertices.size() / 3;
|
||||
index_size = indices.size();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <Maths/Quaternions.h>
|
||||
#include <Core/Script.h>
|
||||
#include <Graphics/Model.h>
|
||||
#include <Core/UUID.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
@@ -14,8 +15,9 @@ namespace Scop
|
||||
friend Scene;
|
||||
|
||||
public:
|
||||
Narrator() = default;
|
||||
Narrator() : m_uuid(UUID()) {}
|
||||
inline void AttachScript(std::shared_ptr<NarratorScript> script) { p_script = script; }
|
||||
[[nodiscard]] inline std::uint32_t GetUUID() const noexcept { return m_uuid; }
|
||||
inline ~Narrator()
|
||||
{
|
||||
if(p_script)
|
||||
@@ -31,6 +33,7 @@ namespace Scop
|
||||
|
||||
private:
|
||||
std::shared_ptr<NarratorScript> p_script;
|
||||
std::uint64_t m_uuid;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,10 @@ namespace Scop
|
||||
Sprite& CreateSprite(std::shared_ptr<Texture> texture) noexcept;
|
||||
Sprite& CreateSprite(std::string_view name, std::shared_ptr<Texture> texture);
|
||||
|
||||
void RemoveActor(Actor& actor) noexcept;
|
||||
void RemoveNarrator(Narrator& narrator) noexcept;
|
||||
void RemoveSprite(Sprite& sprite) noexcept;
|
||||
|
||||
[[nodiscard]] inline Scene& AddChildScene(std::string_view name, SceneDescriptor desc) { return m_scene_children.emplace_back(name, std::move(desc), this); }
|
||||
inline void AddSkybox(std::shared_ptr<CubeTexture> cubemap) { p_skybox = cubemap; }
|
||||
void SwitchToChild(std::string_view name) const noexcept;
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace Scop
|
||||
[[nodiscard]] inline const Vec2f& GetScale() const noexcept { return m_scale; }
|
||||
[[nodiscard]] inline std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
|
||||
[[nodiscard]] inline std::shared_ptr<Texture> GetTexture() const { return p_texture; }
|
||||
[[nodiscard]] inline std::uint32_t GetUUID() const noexcept { return m_uuid; }
|
||||
|
||||
~Sprite();
|
||||
|
||||
@@ -57,6 +58,7 @@ namespace Scop
|
||||
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
Vec2ui m_position = Vec2ui{ 0, 0 };
|
||||
Vec2f m_scale = Vec2f{ 1.0f, 1.0f };
|
||||
std::uint64_t m_uuid;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,24 @@ namespace Scop
|
||||
inline void Bind(VkCommandBuffer cmd) const noexcept { RenderCore::Get().vkCmdBindIndexBuffer(cmd, m_buffer, 0, VK_INDEX_TYPE_UINT32); }
|
||||
};
|
||||
|
||||
class MeshBuffer : public GPUBuffer
|
||||
{
|
||||
public:
|
||||
inline void Init(std::uint32_t vertex_size, std::uint32_t index_size, VkBufferUsageFlags additional_flags = 0, CPUBuffer data = {}, std::string_view name = {})
|
||||
{
|
||||
m_vertex_offset = 0;
|
||||
m_index_offset = vertex_size;
|
||||
GPUBuffer::Init(BufferType::LowDynamic, vertex_size + index_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | additional_flags, std::move(data), std::move(name));
|
||||
}
|
||||
// Full flemme de faire les fonctions SetData
|
||||
inline void BindVertex(VkCommandBuffer cmd) const noexcept { RenderCore::Get().vkCmdBindVertexBuffers(cmd, 0, 1, &m_buffer, &m_vertex_offset); }
|
||||
inline void BindIndex(VkCommandBuffer cmd) const noexcept { RenderCore::Get().vkCmdBindIndexBuffer(cmd, m_buffer, m_index_offset, VK_INDEX_TYPE_UINT32); }
|
||||
|
||||
private:
|
||||
VkDeviceSize m_vertex_offset;
|
||||
VkDeviceSize m_index_offset;
|
||||
};
|
||||
|
||||
class UniformBuffer
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -10,5 +10,6 @@
|
||||
#include <Core/Logs.h>
|
||||
#include <Core/NativeScript.h>
|
||||
#include <Core/Script.h>
|
||||
#include <Core/UUID.h>
|
||||
|
||||
#endif
|
||||
|
||||
21
ScopEngine/Runtime/Sources/Core/UUID.cpp
git.filemode.normal_file
21
ScopEngine/Runtime/Sources/Core/UUID.cpp
git.filemode.normal_file
@@ -0,0 +1,21 @@
|
||||
#include <random>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <Core/UUID.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
static std::random_device random_device;
|
||||
static std::mt19937_64 engine(random_device());
|
||||
static std::uniform_int_distribution<std::uint64_t> uniform_distribution;
|
||||
static std::unordered_set<std::uint64_t> registry;
|
||||
|
||||
UUID::UUID()
|
||||
{
|
||||
do
|
||||
{
|
||||
m_uuid = uniform_distribution(engine);
|
||||
} while(registry.contains(m_uuid));
|
||||
registry.emplace(m_uuid);
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,14 @@ namespace Scop
|
||||
{
|
||||
Actor::Actor()
|
||||
{
|
||||
m_uuid = UUID();
|
||||
if(p_script)
|
||||
p_script->OnInit(this);
|
||||
}
|
||||
|
||||
Actor::Actor(Model model) : m_model(std::move(model))
|
||||
{
|
||||
m_uuid = UUID();
|
||||
if(p_script)
|
||||
p_script->OnInit(this);
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ namespace Scop
|
||||
void Mesh::Draw(VkCommandBuffer cmd, std::size_t& drawcalls, std::size_t& polygondrawn, std::size_t submesh_index) const noexcept
|
||||
{
|
||||
Verify(submesh_index < m_sub_meshes.size(), "invalid submesh index");
|
||||
m_sub_meshes[submesh_index].vbo.Bind(cmd);
|
||||
m_sub_meshes[submesh_index].ibo.Bind(cmd);
|
||||
RenderCore::Get().vkCmdDrawIndexed(cmd, static_cast<std::uint32_t>(m_sub_meshes[submesh_index].ibo.GetSize() / sizeof(std::uint32_t)), 1, 0, 0, 0);
|
||||
m_sub_meshes[submesh_index].buffer.BindVertex(cmd);
|
||||
m_sub_meshes[submesh_index].buffer.BindIndex(cmd);
|
||||
RenderCore::Get().vkCmdDrawIndexed(cmd, m_sub_meshes[submesh_index].index_size, 1, 0, 0, 0);
|
||||
polygondrawn += m_sub_meshes[submesh_index].triangle_count;
|
||||
drawcalls++;
|
||||
}
|
||||
@@ -23,9 +23,6 @@ namespace Scop
|
||||
Mesh::~Mesh()
|
||||
{
|
||||
for(auto& mesh : m_sub_meshes)
|
||||
{
|
||||
mesh.vbo.Destroy();
|
||||
mesh.ibo.Destroy();
|
||||
}
|
||||
mesh.buffer.Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "Graphics/Narrator.h"
|
||||
#include <Graphics/Scene.h>
|
||||
#include <Renderer/Renderer.h>
|
||||
#include <Renderer/RenderCore.h>
|
||||
@@ -63,6 +64,39 @@ namespace Scop
|
||||
return *sprite;
|
||||
}
|
||||
|
||||
void Scene::RemoveActor(Actor& actor) noexcept
|
||||
{
|
||||
auto it = std::find_if(m_actors.begin(), m_actors.end(), [actor](const std::shared_ptr<Actor> lhs) { return actor.GetUUID() == lhs->GetUUID(); });
|
||||
if(it == m_actors.end())
|
||||
{
|
||||
Error("Actor not found");
|
||||
return;
|
||||
}
|
||||
m_actors.erase(it);
|
||||
}
|
||||
|
||||
void Scene::RemoveNarrator(Narrator& narrator) noexcept
|
||||
{
|
||||
auto it = std::find_if(m_narrators.begin(), m_narrators.end(), [narrator](const std::shared_ptr<Narrator> lhs) { return narrator.GetUUID() == lhs->GetUUID(); });
|
||||
if(it == m_narrators.end())
|
||||
{
|
||||
Error("Narrator not found");
|
||||
return;
|
||||
}
|
||||
m_narrators.erase(it);
|
||||
}
|
||||
|
||||
void Scene::RemoveSprite(Sprite& sprite) noexcept
|
||||
{
|
||||
auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [sprite](const std::shared_ptr<Sprite> lhs) { return sprite.GetUUID() == lhs->GetUUID(); });
|
||||
if(it == m_sprites.end())
|
||||
{
|
||||
Error("Sprite not found");
|
||||
return;
|
||||
}
|
||||
m_sprites.erase(it);
|
||||
}
|
||||
|
||||
void Scene::SwitchToChild(std::string_view name) const noexcept
|
||||
{
|
||||
auto it = std::find_if(m_scene_children.begin(), m_scene_children.end(), [name](const Scene& scene){ return name == scene.GetName(); });
|
||||
|
||||
@@ -3,12 +3,14 @@
|
||||
#include <Renderer/Image.h>
|
||||
#include <Graphics/MeshFactory.h>
|
||||
#include <Core/Logs.h>
|
||||
#include <Core/UUID.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
Sprite::Sprite(std::shared_ptr<Texture> texture)
|
||||
{
|
||||
Verify((bool)texture, "Sprite: invalid texture");
|
||||
m_uuid = UUID();
|
||||
p_mesh = CreateQuad(0, 0, texture->GetWidth(), texture->GetHeight());
|
||||
p_texture = texture;
|
||||
if(p_script)
|
||||
|
||||
Reference in New Issue
Block a user