From 9dffa6f24f953a1a596a542b1f46d9366a50ec5e Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Thu, 17 Oct 2019 16:43:50 +0100 Subject: [PATCH] Started rewrite for chunks to come. Left old face and voxel code --- resources/shaders/simple.frag | 2 +- resources/shaders/simple.vert | 2 +- src/game.cpp | 7 +++++-- src/game.hpp | 1 - src/renderer/camera.hpp | 2 +- src/renderer/chunk.cpp | 8 ++++--- src/renderer/chunk.hpp | 14 +++++++------ src/renderer/texture.cpp | 5 ++--- src/renderer/texture.hpp | 2 +- src/world/block.hpp | 39 +++++++++++++++++++++++++++++++---- 10 files changed, 59 insertions(+), 23 deletions(-) diff --git a/resources/shaders/simple.frag b/resources/shaders/simple.frag index 8b1656d..7a2f794 100644 --- a/resources/shaders/simple.frag +++ b/resources/shaders/simple.frag @@ -1,4 +1,4 @@ -#version 330 +#version 450 in vec3 TexCoord; diff --git a/resources/shaders/simple.vert b/resources/shaders/simple.vert index 862f199..030929d 100644 --- a/resources/shaders/simple.vert +++ b/resources/shaders/simple.vert @@ -1,4 +1,4 @@ -#version 330 +#version 450 layout (location = 0) in vec3 position; layout (location = 1) in vec3 texcoord; diff --git a/src/game.cpp b/src/game.cpp index 03d546b..f6a3294 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -82,17 +82,20 @@ void Game::Setup(int w, int h) { m_cameras["Default"] = std::make_shared(w, h); m_activeCamera = m_cameras["Default"]; + BlockDictionary.Build(); m_world = std::make_unique(); - m_world->Chunks.push_back(std::make_shared()); + m_world->Chunks.push_back(std::make_shared(0, 0)); + m_world->Chunks.push_back(std::make_shared(1, 1)); + m_world->Chunks.push_back(std::make_shared(1, 3)); m_world->Shaders["Basic"] = std::make_shared(); m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple"); m_world->Shaders["Basic"]->Link(); Texture texture; - m_world->TextureID = texture.LoadTextures(TextureIdsAndPaths); + m_world->TextureID = texture.LoadTextures(BlockDictionary.Textures); } diff --git a/src/game.hpp b/src/game.hpp index 7f56e21..f415445 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -12,7 +12,6 @@ #include #include - #if _WIN32 #include #else diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index 4e8ab79..4ca8147 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -17,7 +17,7 @@ public: void MouseMoved(glm::vec2 mouseDelta); float MouseSensitivity = 0.1f; - float CameraSpeed = 0.1f; + float CameraSpeed = 0.2f; private: float roll, pitch, yaw; diff --git a/src/renderer/chunk.cpp b/src/renderer/chunk.cpp index a0ad6f2..c530ec6 100644 --- a/src/renderer/chunk.cpp +++ b/src/renderer/chunk.cpp @@ -5,13 +5,15 @@ #include "voxel.hpp" -Chunk::Chunk() { +Chunk::Chunk(int x, int z) { - m_model = glm::mat4(1.0f); + m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH }); + + for (int x = 0; x < CHUNK_WIDTH; x++) for (int y = 0; y < CHUNK_HEIGHT; y++) - for (int z = 0; z < CHUNK_HEIGHT; z++) { + for (int z = 0; z < CHUNK_DEPTH; z++) { Voxels.push_back(std::make_shared(x, y, z)); diff --git a/src/renderer/chunk.hpp b/src/renderer/chunk.hpp index 2404587..599aecf 100644 --- a/src/renderer/chunk.hpp +++ b/src/renderer/chunk.hpp @@ -3,9 +3,9 @@ #include "../common.hpp" -#define CHUNK_HEIGHT 10 -#define CHUNK_WIDTH 10 -#define CHUNK_DEPTH 10 +#define CHUNK_HEIGHT 32 +#define CHUNK_WIDTH 16 +#define CHUNK_DEPTH 16 class Camera; class Shader; @@ -15,15 +15,17 @@ class Voxel; class Chunk { public: - Chunk(); + Chunk(int x, int z); Chunk(std::vector> voxels); void Render(std::shared_ptr camera, std::shared_ptr shader); void Update(); - // Indexed sequentially [x + WIDTH * (y + HEIGHT * z)] = voxel - std::vector> Voxels; + // Indexed sequentially [x + WIDTH * (y + HEIGHT * z)] = voxelID + // the voxel id is used to index the block dictionary to get properties + // to generate a mesh and send it to the GPU + std::vector Voxels; private: diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp index 1282167..ac70556 100644 --- a/src/renderer/texture.cpp +++ b/src/renderer/texture.cpp @@ -7,7 +7,7 @@ #define STB_IMAGE_IMPLEMENTATION #include "../util/stb_image.hpp" -GLuint Texture::LoadTextures(std::vector> textures) { +GLuint Texture::LoadTextures(std::vector textures) { Logger logger; @@ -21,7 +21,7 @@ GLuint Texture::LoadTextures(std::vector> textures) for (int i = 0; i < layers; i++) { - std::string path = basePath + textures[i].second; + std::string path = basePath + textures[i]; int xR = 0; int yR = 0; @@ -36,7 +36,6 @@ GLuint Texture::LoadTextures(std::vector> textures) } - GLuint textureArray = 0; glGenTextures(1, &textureArray); diff --git a/src/renderer/texture.hpp b/src/renderer/texture.hpp index ff5d077..f7c633e 100644 --- a/src/renderer/texture.hpp +++ b/src/renderer/texture.hpp @@ -5,7 +5,7 @@ class Texture { public: - GLuint LoadTextures(std::vector> locations); + GLuint Texture::LoadTextures(std::vector textures); }; #endif diff --git a/src/world/block.hpp b/src/world/block.hpp index b4ab8ac..767aa9a 100644 --- a/src/world/block.hpp +++ b/src/world/block.hpp @@ -3,10 +3,41 @@ #include "../common.hpp" -static std::vector> TextureIdsAndPaths { - {0, "dirt.png"}, - {1, "grass_side.png"}, - {2, "grass_top.png"} +class CBlockEntry { +public: + uint8_t ID; + }; +// TODO: Make design of the class data oriented +class CBlockDictionary { +public: + + void Build(); + + // The index of the texutres path in this array is equal to + // that textures ID, to be referenced in the block entry + std::vector Textures; + + // Only supports up to 255 blocs, 0 being air + // word stores vectors of chunks which are 16x16x256 + // vectors of uint8_t which reference the block dictionary + std::map BlockEntries; + +private: + + void registerTexture(); + void registerBlock(); + +}; + + +static CBlockDictionary BlockDictionary; + +// static std::vector> TextureIdsAndPaths { +// {0, "dirt.png"}, +// {1, "grass_side.png"}, +// {2, "grass_top.png"} +// }; + #endif