world generation

This commit is contained in:
Kbz-8
2025-05-10 00:49:17 +02:00
parent 694009e6e4
commit 55fa8b2c17
19 changed files with 222 additions and 42 deletions

View File

@@ -17,17 +17,20 @@ Chunk::Chunk(World& world, Scop::Vec2i offset) : m_offset(offset), m_position(st
void Chunk::GenerateChunk()
{
if(p_actor)
return;
#pragma omp parallel for
for(auto& z: m_data)
{
#pragma omp parallel for
for(auto& y: z)
std::fill(y.begin(), y.end(), 0);
}
std::vector<Scop::Vertex> mesh_data;
std::vector<std::uint32_t> indices;
#pragma omp parallel for
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
{
#pragma omp parallel for
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
{
// Implement noise here
@@ -36,6 +39,18 @@ void Chunk::GenerateChunk()
m_data[x][z][y] = 1;
}
}
}
void Chunk::GenerateMesh()
{
if(p_actor)
return;
std::vector<Scop::Vertex> mesh_data;
std::vector<std::uint32_t> indices;
mesh_data.reserve(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
indices.reserve(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
{
@@ -129,8 +144,8 @@ void Chunk::GenerateChunk()
std::uint32_t Chunk::GetBlock(Scop::Vec3i position) const noexcept
{
//if(position.x < 0 || position.x >= m_data.size() || position.z < 0 || position.z >= m_data[position.x >= m_data.size() ? m_data.size() - 1 : position.x].size() || position.y < 0)
// return 1;
if(position.x < 0 || position.x >= m_data.size() || position.z < 0 || position.z >= m_data[position.x >= m_data.size() ? m_data.size() - 1 : position.x].size() || position.y < 0)
return 1;
if(position.x < m_data.size() && position.z < m_data[position.x].size() && position.y < m_data[position.x][position.z].size())
return m_data[position.x][position.z][position.y];
return 0;

View File

@@ -5,7 +5,7 @@
#include <ScopGraphics.h>
#include <ScopMaths.h>
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 16, 256, 16 };
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 256, 32 };
class Chunk
{
@@ -13,7 +13,9 @@ class Chunk
Chunk(class World& world, Scop::Vec2i offset);
void GenerateChunk();
std::uint32_t GetBlock(Scop::Vec3i position) const noexcept;
void GenerateMesh();
[[nodiscard]] std::uint32_t GetBlock(Scop::Vec3i position) const noexcept;
[[nodiscard]] inline Scop::NonOwningPtr<Scop::Actor> GetActor() const noexcept { return p_actor; }
~Chunk() = default;

View File

@@ -2,9 +2,10 @@
#define UTILS_H
#include <unistd.h>
#include <climits>
#include <filesystem>
#include <ScopMaths.h>
inline std::filesystem::path GetExecutablePath()
{
char result[PATH_MAX];
@@ -17,4 +18,28 @@ inline std::filesystem::path GetResourcesPath()
return GetExecutablePath().parent_path().parent_path() / "Resources";
}
inline void HashCombine([[maybe_unused]] std::size_t& seed) noexcept {}
template <typename T, typename... Rest>
inline void HashCombine(std::size_t& seed, const T& v, Rest... rest)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
HashCombine(seed, rest...);
}
namespace std
{
template <>
struct hash<Scop::Vec2i>
{
std::size_t operator()(const Scop::Vec2i& v) const noexcept
{
std::size_t hash = 0;
HashCombine(hash, v.x, v.y);
return hash;
}
};
}
#endif

View File

@@ -3,7 +3,7 @@
#include <World.h>
#include <Utils.h>
World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrator())
World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrator()), m_previous_chunk_position(-1000, 10000)
{
Scop::Vec2ui32 map_size;
Scop::MaterialTextures material_params;
@@ -12,25 +12,61 @@ World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrat
auto narrator_update = [this](Scop::NonOwningPtr<Scop::Scene> scene, Scop::Inputs& input, float delta)
{
if(m_chunks.empty())
GenerateWorld();
};
m_narrator.AttachScript(std::make_shared<Scop::NativeNarratorScript>(std::function<void()>{}, narrator_update, std::function<void()>{}));
}
[[nodiscard]] Chunk& World::GetChunk(Scop::Vec2i position)
[[nodiscard]] Scop::NonOwningPtr<Chunk> World::GetChunk(Scop::Vec2i position)
{
auto it = m_chunks.find(position);
if(it == m_chunks.end())
return nullptr;
return &it->second;
}
void World::GenerateWorld()
{
for(std::int32_t x = 0; x < 4; x++)
Scop::FirstPerson3D* camera = reinterpret_cast<Scop::FirstPerson3D*>(m_scene.GetCamera().get());
std::int32_t x_chunk = static_cast<std::int32_t>(camera->GetPosition().x) / static_cast<std::int32_t>(CHUNK_SIZE.x);
std::int32_t z_chunk = static_cast<std::int32_t>(camera->GetPosition().z) / static_cast<std::int32_t>(CHUNK_SIZE.z);
Scop::Vec2i current_chunk_position{ x_chunk, z_chunk };
if(current_chunk_position == m_previous_chunk_position)
return;
#pragma omp parallel for
for(auto it = m_chunks.begin(); it != m_chunks.end();)
{
for(std::int32_t z = 0; z < 4; z++)
Scop::Vec3i pos = it->first;
float x_dist = std::abs(pos.x - current_chunk_position.x);
float z_dist = std::abs(pos.z - current_chunk_position.y);
if(RENDER_DISTANCE < x_dist || RENDER_DISTANCE < z_dist)
{
m_chunks.emplace_back(*this, Scop::Vec2i{ x, z });
m_chunks.back().GenerateChunk();
if(it->second.GetActor())
m_scene.RemoveActor(*it->second.GetActor());
it = m_chunks.erase(it);
}
else
++it;
}
#pragma omp parallel for
for(std::int32_t x = x_chunk - RENDER_DISTANCE; x <= x_chunk + RENDER_DISTANCE; x++)
{
#pragma omp parallel for
for(std::int32_t z = z_chunk - RENDER_DISTANCE; z <= z_chunk + RENDER_DISTANCE; z++)
{
auto res = m_chunks.try_emplace(Scop::Vec2i{ x, z }, *this, Scop::Vec2i{ x, z });
if(res.second)
res.first->second.GenerateChunk();
}
}
for(auto& chunk : m_chunks)
chunk.second.GenerateMesh();
m_previous_chunk_position = current_chunk_position;
}

View File

@@ -1,11 +1,12 @@
#ifndef WORLD_H
#define WORLD_H
#include <vector>
#include <unordered_map>
#include <ScopGraphics.h>
#include <Chunk.h>
#include <Utils.h>
class World
{
@@ -14,7 +15,7 @@ class World
[[nodiscard]] inline Scop::Scene& GetScene() noexcept { return m_scene; }
[[nodiscard]] inline std::shared_ptr<Scop::Material> GetBlockMaterial() const { return p_block_material; }
[[nodiscard]] Chunk& GetChunk(Scop::Vec2i position);
[[nodiscard]] Scop::NonOwningPtr<Chunk> GetChunk(Scop::Vec2i position);
~World() = default;
@@ -22,12 +23,12 @@ class World
void GenerateWorld();
private:
static inline constexpr std::size_t CHUNKS_SIZE = 16;
std::vector<Chunk> m_chunks;
static constexpr std::uint8_t RENDER_DISTANCE = 2;
std::unordered_map<Scop::Vec2i, Chunk> m_chunks;
std::shared_ptr<Scop::Material> p_block_material;
Scop::Narrator& m_narrator;
Scop::Scene& m_scene;
Scop::Vec2i m_previous_chunk_position;
};
#endif

View File

@@ -13,7 +13,7 @@ int main(int ac, char** av)
Scop::SceneDescriptor main_scene_desc;
main_scene_desc.fragment_shader = Scop::RenderCore::Get().GetDefaultFragmentShader();
main_scene_desc.camera = std::make_shared<Scop::FirstPerson3D>(Scop::Vec3f{ -10.0f, 0.0f, 0.0f }, 80.f);
main_scene_desc.camera = std::make_shared<Scop::FirstPerson3D>(Scop::Vec3f{ 0.0f, 20.0f, 0.0f }, 80.f);
main_scene_desc.culling = Scop::CullMode::None;
Scop::Scene& main_scene = splash_scene->AddChildScene("main", std::move(main_scene_desc));

19
ScopEngine/Runtime/Includes/Core/UUID.h git.filemode.normal_file
View 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

View File

@@ -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;
};
}

View File

@@ -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();
}
};

View File

@@ -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;
};
}

View File

@@ -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;

View File

@@ -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;
};
}

View File

@@ -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:

View File

@@ -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
View 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);
}
}

View File

@@ -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);
}

View File

@@ -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();
}
}

View File

@@ -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(); });

View File

@@ -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)