working on chunk generation

This commit is contained in:
2025-05-09 09:37:23 +02:00
parent 2336835250
commit 8c94d86fd4
18 changed files with 242 additions and 155 deletions

6
Application/Block.h git.filemode.normal_file
View File

@@ -0,0 +1,6 @@
#ifndef BLOCK_H
#define BLOCK_H
#include <ScopGraphics.h>
#endif

29
Application/Chunk.cpp git.filemode.normal_file
View File

@@ -0,0 +1,29 @@
#include <Chunk.h>
#include <algorithm>
Chunk::Chunk(Scop::Scene& world, Scop::Vec2ui offset) : m_data(CHUNK_VOLUME), m_offset(offset), m_position(std::move(offset) * Scop::Vec2ui{ CHUNK_SIZE })
{
}
void Chunk::GenerateChunk()
{
std::fill(m_data.begin(), m_data.end(), 0);
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
{
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
{
std::uint32_t pos_x = m_position.x + x;
std::uint32_t pos_z = m_position.y + z;
for(std::uint32_t y = 0; y < CHUNK_SIZE.y; y++)
{
std::uint32_t index = (z * CHUNK_SIZE.x * CHUNK_SIZE.y) + (y * CHUNK_SIZE.x) + x;
// Implement noise here
m_data[index] = y < 10 ? 1 : 0;
}
}
}
}

27
Application/Chunk.h git.filemode.normal_file
View File

@@ -0,0 +1,27 @@
#ifndef CHUNK_H
#define CHUNK_H
#include <vector>
#include <ScopGraphics.h>
#include <ScopMaths.h>
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 16, 256, 16 };
constexpr std::uint32_t CHUNK_VOLUME = CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z;
class Chunk
{
public:
Chunk(Scop::Scene& world, Scop::Vec2ui offset);
void GenerateChunk();
~Chunk() = default;
private:
std::vector<std::uint32_t> m_data;
Scop::Vec2ui m_offset; // In chunks
Scop::Vec2ui m_position; // In blocks
Scop::NonOwningPtr<Scop::Actor> p_actor = nullptr;
};
#endif

View File

@@ -1,118 +0,0 @@
#include <ScriptSubRoutines.h>
constexpr const float SPEED = 40.0f;
void WireframeHandler(Scop::NonOwningPtr<Scop::Scene> scene, Scop::Inputs& inputs)
{
static bool key_pressed_last_frame = false;
if(inputs.IsKeyPressed(SDL_SCANCODE_F))
key_pressed_last_frame = true;
else if(key_pressed_last_frame)
{
scene->GetForwardData().wireframe = !scene->GetForwardData().wireframe;
Scop::RenderCore::Get().WaitDeviceIdle();
scene->GetPipeline().Destroy();
key_pressed_last_frame = false;
}
static Scop::Vec3f rotations{ 0.0f, 0.0f, 0.0f };
}
void MovementHandler(Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& inputs, float delta)
{
static Scop::Vec3f position{ 0.0f, 0.0f, 0.0f };
if(inputs.IsKeyPressed(SDL_SCANCODE_I))
position.x += SPEED * delta;
if(inputs.IsKeyPressed(SDL_SCANCODE_K))
position.x -= SPEED * delta;
if(inputs.IsKeyPressed(SDL_SCANCODE_L))
position.z += SPEED * delta;
if(inputs.IsKeyPressed(SDL_SCANCODE_J))
position.z -= SPEED * delta;
if(inputs.IsKeyPressed(SDL_SCANCODE_U))
position.y += SPEED * delta;
if(inputs.IsKeyPressed(SDL_SCANCODE_O))
position.y -= SPEED * delta;
actor->SetPosition(position);
}
void ColorsTransitionHandler(Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& inputs, float delta, Scop::MaterialData& data)
{
static bool colors_transition = false;
static bool colors_key_pressed_last_frame = false;
static std::uint8_t colors_transition_way = 0;
if(inputs.IsKeyPressed(SDL_SCANCODE_C) && !colors_transition)
colors_key_pressed_last_frame = true;
else if(colors_key_pressed_last_frame)
{
colors_key_pressed_last_frame = false;
colors_transition = true;
colors_transition_way = (data.dissolve_black_white_colors_factor >= 0.5f);
}
if(colors_transition)
{
if(colors_transition_way == 1)
{
data.dissolve_black_white_colors_factor -= 1.0f * delta;
colors_transition = (data.dissolve_black_white_colors_factor > 0.0f);
}
else
{
data.dissolve_black_white_colors_factor += 1.0f * delta;
colors_transition = (data.dissolve_black_white_colors_factor < 1.0f);
}
}
static bool normals_transition = false;
static bool normals_key_pressed_last_frame = false;
static std::uint8_t normals_transition_way = 0;
if(inputs.IsKeyPressed(SDL_SCANCODE_N) && !normals_transition)
normals_key_pressed_last_frame = true;
else if(normals_key_pressed_last_frame)
{
normals_key_pressed_last_frame = false;
normals_transition = true;
normals_transition_way = (data.dissolve_normals_colors_factor >= 0.5f);
}
if(normals_transition)
{
if(normals_transition_way == 1)
{
data.dissolve_normals_colors_factor -= 1.0f * delta;
normals_transition = (data.dissolve_normals_colors_factor > 0.0f);
}
else
{
data.dissolve_normals_colors_factor += 1.0f * delta;
normals_transition = (data.dissolve_normals_colors_factor < 1.0f);
}
}
}
void TextureTransitionHandler(Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& inputs, float delta, Scop::MaterialData& data)
{
static bool texture_transition = false;
static bool key_pressed_last_frame = false;
static std::uint8_t transition_way = 0;
if(inputs.IsKeyPressed(SDL_SCANCODE_T) && !texture_transition)
key_pressed_last_frame = true;
else if(key_pressed_last_frame)
{
texture_transition = true;
key_pressed_last_frame = false;
transition_way = (data.dissolve_texture_factor >= 0.5f);
}
if(texture_transition)
{
if(transition_way == 1)
{
data.dissolve_texture_factor -= 1.0f * delta;
texture_transition = (data.dissolve_texture_factor > 0.0f);
}
else
{
data.dissolve_texture_factor += 1.0f * delta;
texture_transition = (data.dissolve_texture_factor < 1.0f);
}
}
}

View File

@@ -1,12 +0,0 @@
#ifndef __SCRIPT_SUB_ROUTINES__
#define __SCRIPT_SUB_ROUTINES__
#include <ScopCore.h>
#include <ScopGraphics.h>
void WireframeHandler(Scop::NonOwningPtr<Scop::Scene> scene, Scop::Inputs& inputs);
void MovementHandler(Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& inputs, float delta);
void ColorsTransitionHandler(Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& inputs, float delta, Scop::MaterialData& data);
void TextureTransitionHandler(Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& inputs, float delta, Scop::MaterialData& data);
#endif

20
Application/Utils.h git.filemode.normal_file
View File

@@ -0,0 +1,20 @@
#ifndef UTILS_H
#define UTILS_H
#include <unistd.h>
#include <climits>
#include <filesystem>
inline std::filesystem::path GetExecutablePath()
{
char result[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
return std::string(result, (count > 0) ? count : 0);
}
inline std::filesystem::path GetResourcesPath()
{
return GetExecutablePath().parent_path().parent_path() / "Resources";
}
#endif

18
Application/World.cpp git.filemode.normal_file
View File

@@ -0,0 +1,18 @@
#include <ScopCore.h>
#include <World.h>
#include <Utils.h>
World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrator())
{
Scop::Vec2ui32 map_size;
Scop::MaterialTextures material_params;
material_params.albedo = std::make_shared<Scop::Texture>(Scop::LoadBMPFile(GetResourcesPath() / "prototype.bmp", map_size), map_size.x, map_size.y);
p_block_material = std::make_shared<Scop::Material>(material_params);
auto narrator_update = [](Scop::NonOwningPtr<Scop::Scene> scene, Scop::Inputs& input, float delta)
{
};
m_narrator.AttachScript(std::make_shared<Scop::NativeNarratorScript>(std::function<void()>{}, narrator_update, std::function<void()>{}));
}

28
Application/World.h git.filemode.normal_file
View File

@@ -0,0 +1,28 @@
#ifndef WORLD_H
#define WORLD_H
#include <vector>
#include <ScopGraphics.h>
#include <Chunk.h>
class World
{
public:
World(Scop::Scene& scene);
[[nodiscard]] inline Scop::Scene& GetScene() noexcept { return m_scene; }
~World() = default;
private:
static inline constexpr std::size_t CHUNKS_SIZE = 16;
std::vector<std::vector<Chunk>> m_chunks;
std::shared_ptr<Scop::Material> p_block_material;
Scop::Narrator& m_narrator;
Scop::Scene& m_scene;
};
#endif

View File

@@ -1,27 +1,9 @@
#include <ScopCore.h>
#include <ScopGraphics.h>
#include <ScriptSubRoutines.h>
#include <Splash.h>
#include <climits>
#include <memory>
#include <unistd.h>
#include <filesystem>
#include <string>
#include <cmath>
std::filesystem::path GetExecutablePath()
{
char result[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
return std::string(result, (count > 0) ? count : 0);
}
std::filesystem::path GetResourcesPath()
{
return GetExecutablePath().parent_path().parent_path() / "Resources";
}
#include <Utils.h>
#include <World.h>
int main(int ac, char** av)
{
@@ -37,6 +19,16 @@ int main(int ac, char** av)
Scop::Vec2ui32 skybox_size;
main_scene.AddSkybox(std::make_shared<Scop::CubeTexture>(Scop::LoadBMPFile(GetResourcesPath() / "skybox.bmp", skybox_size), skybox_size.x, skybox_size.y));
World world(main_scene);
Scop::Actor& object = main_scene.CreateActor(Scop::CreateCube());
Scop::Vec2ui32 map_size;
Scop::MaterialTextures material_params;
material_params.albedo = std::make_shared<Scop::Texture>(Scop::LoadBMPFile(GetResourcesPath() / "prototype.bmp", map_size), map_size.x, map_size.y);
std::shared_ptr<Scop::Material> material = std::make_shared<Scop::Material>(material_params);
object.GetModelRef().SetMaterial(material, 0);
engine.RegisterMainScene(splash_scene.get());
engine.Run();
return 0;