mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 14:43:34 +00:00
world generation
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user