mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 14:43:34 +00:00
returning range from noise
This commit is contained in:
@@ -2,35 +2,23 @@
|
|||||||
#include <Block.h>
|
#include <Block.h>
|
||||||
#include <World.h>
|
#include <World.h>
|
||||||
|
|
||||||
|
#define POS_TO_INDEX(posx, posz) (posx * CHUNK_SIZE.x + posz)
|
||||||
|
|
||||||
Chunk::Chunk(World& world, Scop::Vec2i offset) : m_offset(offset), m_position(std::move(offset) * Scop::Vec2i{ CHUNK_SIZE.x, CHUNK_SIZE.z }), m_world(world)
|
Chunk::Chunk(World& world, Scop::Vec2i offset) : m_offset(offset), m_position(std::move(offset) * Scop::Vec2i{ CHUNK_SIZE.x, CHUNK_SIZE.z }), m_world(world)
|
||||||
{
|
{
|
||||||
m_data.resize(CHUNK_SIZE.x);
|
|
||||||
for(auto& z: m_data)
|
|
||||||
{
|
|
||||||
z.resize(CHUNK_SIZE.z);
|
|
||||||
for(auto& y: z)
|
|
||||||
y.resize(CHUNK_SIZE.y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunk::GenerateChunk()
|
void Chunk::GenerateChunk()
|
||||||
{
|
{
|
||||||
if(p_actor)
|
if(p_actor)
|
||||||
return;
|
return;
|
||||||
for(auto& z: m_data)
|
for(auto& y: m_data)
|
||||||
{
|
std::memset(y.data(), 0, y.size() * sizeof(std::uint32_t));
|
||||||
for(auto& y: z)
|
|
||||||
std::fill(y.begin(), y.end(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
|
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
|
||||||
{
|
{
|
||||||
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
|
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
|
||||||
{
|
std::memcpy(m_data[POS_TO_INDEX(x, z)].data(), m_world.GetNoiseGenerator().GetHeight(m_position + Scop::Vec2i(x, z)).data(), CHUNK_SIZE.y * sizeof(std::uint32_t));
|
||||||
std::uint32_t height = m_world.GetNoiseGenerator().GetHeight(m_position + Scop::Vec2i(x, z));
|
|
||||||
for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++)
|
|
||||||
m_data[x][z][y] = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,6 +28,8 @@ void Chunk::GenerateMesh()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
std::size_t offset = 0;
|
std::size_t offset = 0;
|
||||||
|
m_mesh_data.reserve(CHUNK_VOLUME);
|
||||||
|
m_mesh_index_data.reserve(CHUNK_VOLUME * 4);
|
||||||
|
|
||||||
for(std::int32_t x = 0; x < CHUNK_SIZE.x; x++)
|
for(std::int32_t x = 0; x < CHUNK_SIZE.x; x++)
|
||||||
{
|
{
|
||||||
@@ -172,29 +162,27 @@ void Chunk::UploadMesh()
|
|||||||
|
|
||||||
std::uint32_t Chunk::GetBlock(Scop::Vec3i position) const noexcept
|
std::uint32_t Chunk::GetBlock(Scop::Vec3i position) const noexcept
|
||||||
{
|
{
|
||||||
if(position.y < 0 || position.y > CHUNK_SIZE.y) // No chunk under or above
|
if(position.y < 0 || position.y >= CHUNK_SIZE.y) [[unlikely]] // No chunk under or above
|
||||||
return 1;
|
return 1;
|
||||||
if(position.x < 0)
|
if(position.x < 0) [[unlikely]]
|
||||||
{
|
{
|
||||||
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x - 1, m_offset.y });
|
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x - 1, m_offset.y });
|
||||||
return neighbour ? neighbour->GetBlock(Scop::Vec3i(CHUNK_SIZE.x - 1, position.y, position.z)) : 1;
|
return neighbour ? neighbour->GetBlock(Scop::Vec3i(CHUNK_SIZE.x - 1, position.y, position.z)) : 1;
|
||||||
}
|
}
|
||||||
if(position.x >= CHUNK_SIZE.x)
|
if(position.x >= CHUNK_SIZE.x) [[unlikely]]
|
||||||
{
|
{
|
||||||
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x + 1, m_offset.y });
|
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x + 1, m_offset.y });
|
||||||
return neighbour ? neighbour->GetBlock(Scop::Vec3i(0, position.y, position.z)) : 1;
|
return neighbour ? neighbour->GetBlock(Scop::Vec3i(0, position.y, position.z)) : 1;
|
||||||
}
|
}
|
||||||
if(position.z < 0)
|
if(position.z < 0) [[unlikely]]
|
||||||
{
|
{
|
||||||
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x, m_offset.y - 1 });
|
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x, m_offset.y - 1 });
|
||||||
return neighbour ? neighbour->GetBlock(Scop::Vec3i(position.x, position.y, CHUNK_SIZE.x - 1)) : 1;
|
return neighbour ? neighbour->GetBlock(Scop::Vec3i(position.x, position.y, CHUNK_SIZE.x - 1)) : 1;
|
||||||
}
|
}
|
||||||
if(position.z >= CHUNK_SIZE.z)
|
if(position.z >= CHUNK_SIZE.z) [[unlikely]]
|
||||||
{
|
{
|
||||||
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x, m_offset.y + 1 });
|
Scop::NonOwningPtr<Chunk> neighbour = m_world.GetChunk(Scop::Vec2i{ m_offset.x, m_offset.y + 1 });
|
||||||
return neighbour ? neighbour->GetBlock(Scop::Vec3i(position.x, position.y, 0)) : 1;
|
return neighbour ? neighbour->GetBlock(Scop::Vec3i(position.x, position.y, 0)) : 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[POS_TO_INDEX(position.x, position.z)][position.y];
|
||||||
return m_data[position.x][position.z][position.y];
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 256, 32 };
|
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 256, 32 };
|
||||||
constexpr Scop::Vec3ui CHUNK_SIZE_HALF = CHUNK_SIZE / 2 ;
|
constexpr Scop::Vec3ui CHUNK_SIZE_HALF = CHUNK_SIZE / 2 ;
|
||||||
|
constexpr std::uint32_t CHUNK_VOLUME = CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z;
|
||||||
|
|
||||||
class Chunk
|
class Chunk
|
||||||
{
|
{
|
||||||
@@ -26,7 +27,7 @@ class Chunk
|
|||||||
private:
|
private:
|
||||||
std::vector<Scop::Vertex> m_mesh_data;
|
std::vector<Scop::Vertex> m_mesh_data;
|
||||||
std::vector<std::uint32_t> m_mesh_index_data;
|
std::vector<std::uint32_t> m_mesh_index_data;
|
||||||
std::vector<std::vector<std::vector<std::uint32_t>>> m_data;
|
std::array<std::array<std::uint32_t, CHUNK_SIZE.y>, CHUNK_SIZE.x * CHUNK_SIZE.z> m_data;
|
||||||
Scop::Vec2i m_offset; // In chunks
|
Scop::Vec2i m_offset; // In chunks
|
||||||
Scop::Vec2i m_position; // In blocks
|
Scop::Vec2i m_position; // In blocks
|
||||||
class World& m_world;
|
class World& m_world;
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
#include <Noise.h>
|
#include <Noise.h>
|
||||||
|
|
||||||
[[nodiscard]] std::uint32_t Noise::GetHeight(Scop::Vec2i pos)
|
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> Noise::GetHeight(Scop::Vec2i pos)
|
||||||
{
|
{
|
||||||
return std::abs(std::sin((float)pos.x / 20.0f) * std::cos((float)pos.y / 20.0f) * 60.0f) + 1;
|
std::array<std::uint32_t, CHUNK_SIZE.y> data;
|
||||||
|
std::memset(data.data(), 0, data.size() * sizeof(std::uint32_t));
|
||||||
|
|
||||||
|
std::uint32_t height = std::abs(std::sin((float)pos.x / 20.0f) * std::cos((float)pos.y / 20.0f) * 60.0f) + 1;
|
||||||
|
for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++)
|
||||||
|
data[y] = 1;
|
||||||
|
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
|
|
||||||
#include <ScopMaths.h>
|
#include <ScopMaths.h>
|
||||||
|
|
||||||
|
#include <Chunk.h>
|
||||||
|
|
||||||
class Noise
|
class Noise
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Noise() = default;
|
Noise() = default;
|
||||||
|
|
||||||
[[nodiscard]] std::uint32_t GetHeight(Scop::Vec2i pos);
|
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> GetHeight(Scop::Vec2i pos);
|
||||||
|
|
||||||
~Noise() = default;
|
~Noise() = default;
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
#include <Utils.h>
|
#include <Utils.h>
|
||||||
#include <Noise.h>
|
#include <Noise.h>
|
||||||
|
|
||||||
constexpr std::uint8_t RENDER_DISTANCE = 12;
|
constexpr std::uint8_t RENDER_DISTANCE = 10;
|
||||||
constexpr std::uint8_t CHUNKS_UPLOAD_PER_FRAME = 3;
|
constexpr std::uint8_t CHUNKS_UPLOAD_PER_FRAME = 3;
|
||||||
|
|
||||||
enum class GenerationState: std::uint8_t
|
enum class GenerationState: std::uint8_t
|
||||||
|
|||||||
Reference in New Issue
Block a user