From 5e13de6a3e875b62011d1a456286bc179729897d Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Fri, 18 Oct 2019 11:59:53 +0100 Subject: [PATCH] Rewrite done - broken --- CMakeLists.txt | 3 + src/game.cpp | 6 +- src/renderer/chunk.cpp | 50 +++++++++++++---- src/renderer/chunk.hpp | 4 +- src/renderer/face.hpp | 101 ++++++++++++++++++--------------- src/renderer/texture.hpp | 2 +- src/renderer/voxel.cpp | 118 ++++++++++++++++++++++++++++++++------- src/renderer/voxel.hpp | 9 ++- src/world/block.cpp | 10 +++- 9 files changed, 222 insertions(+), 81 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bbbfc92..3ad937a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,9 @@ project(OpenGLPlayground) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeModules/) cmake_policy(SET CMP0037 OLD) + set(CMAKE_BUILD_TYPE Debug) +# set(CMAKE_CXX_FLAGS "-Ofast") set(executable output) set(SrcDIR ./src) @@ -43,6 +45,7 @@ file(GLOB SourceFiles ${SrcDIR}/* ${SrcDIR}/util/* ${SrcDIR}/game/* + ${SrcDIR}/world/* ${SrcDIR}/renderer/* ) diff --git a/src/game.cpp b/src/game.cpp index f6a3294..054fcde 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -85,6 +85,9 @@ void Game::Setup(int w, int h) { BlockDictionary.Build(); m_world = std::make_unique(); + + Texture texture; + m_world->TextureID = texture.LoadTextures(BlockDictionary.Textures); m_world->Chunks.push_back(std::make_shared(0, 0)); m_world->Chunks.push_back(std::make_shared(1, 1)); @@ -93,9 +96,6 @@ void Game::Setup(int w, int h) { 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(BlockDictionary.Textures); } diff --git a/src/renderer/chunk.cpp b/src/renderer/chunk.cpp index c530ec6..3f890a9 100644 --- a/src/renderer/chunk.cpp +++ b/src/renderer/chunk.cpp @@ -5,17 +5,27 @@ #include "voxel.hpp" +#include "../world/block.hpp" + Chunk::Chunk(int x, int z) { m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH }); - - + // [x + WIDTH * (y + HEIGHT * z)] for (int x = 0; x < CHUNK_WIDTH; x++) for (int y = 0; y < CHUNK_HEIGHT; y++) for (int z = 0; z < CHUNK_DEPTH; z++) { - Voxels.push_back(std::make_shared(x, y, z)); + // Grass on the top layer + if (y == CHUNK_HEIGHT - 1) { + + Voxels.push_back((uint8_t)EBlockType::Grass); + + } else { + + Voxels.push_back((uint8_t)EBlockType::Dirt); + + } } @@ -25,10 +35,12 @@ Chunk::Chunk(int x, int z) { } -Chunk::Chunk(std::vector> voxels) { +Chunk::Chunk(int x, int z, std::vector voxels) { - m_model = glm::mat4(1.0f); + m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH }); + Voxels = voxels; + m_mesh(); } @@ -58,17 +70,35 @@ void Chunk::Update() { } +uint8_t Chunk::BlockAt(int x, int y, int z) { + + return Voxels[x + CHUNK_WIDTH * (y + CHUNK_HEIGHT * z)]; + +} + void Chunk::m_mesh() { - for (auto& voxel : Voxels) { + for (int x = 0; x < CHUNK_WIDTH; x++) + for (int y = 0; y < CHUNK_HEIGHT; y++) + for (int z = 0; z < CHUNK_DEPTH; z++) { std::vector tempVerts; std::vector tempUVs; - voxel->GetMesh(tempVerts, tempUVs); - - for (auto& vert : tempVerts) - m_vertices.push_back(vert); + std::cout << x << " " << y << " " << z << std::endl; + + uint8_t block = BlockAt(x, y, z); + + std::cout << "Block ID " << (int)block << std::endl; + + Voxel tmp({x, y, z}, BlockAt(x, y, z)); + + tmp.AddFace(EFaceType::Top); + tmp.AddFace(EFaceType::Bottom); + tmp.AddFace(EFaceType::Left); + tmp.AddFace(EFaceType::Right); + tmp.AddFace(EFaceType::Front); + tmp.AddFace(EFaceType::Back); for (auto& uv : tempUVs) m_uvs.push_back(uv); diff --git a/src/renderer/chunk.hpp b/src/renderer/chunk.hpp index 599aecf..bf3a2ab 100644 --- a/src/renderer/chunk.hpp +++ b/src/renderer/chunk.hpp @@ -16,12 +16,14 @@ class Chunk { public: Chunk(int x, int z); - Chunk(std::vector> voxels); + Chunk(int x, int z, std::vector voxels); void Render(std::shared_ptr camera, std::shared_ptr shader); void Update(); + uint8_t BlockAt(int x, int y, int z); + // 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 diff --git a/src/renderer/face.hpp b/src/renderer/face.hpp index fa4fb6d..c29b7e8 100644 --- a/src/renderer/face.hpp +++ b/src/renderer/face.hpp @@ -3,7 +3,20 @@ #include "../common.hpp" -std::vector CubeTopFace = { +namespace EFaceType { + + enum Face : uint8_t { + Top, + Bottom, + Left, + Right, + Front, + Back, + }; + +} + +static std::vector CubeTopFace = { { -0.5f, 0.5f, -0.5f }, { 0.5f, 0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }, @@ -12,7 +25,7 @@ std::vector CubeTopFace = { { -0.5f, 0.5f, -0.5f } }; -std::vector CubeTopFaceUVs = { +static std::vector CubeTopFaceUVs = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, @@ -21,7 +34,7 @@ std::vector CubeTopFaceUVs = { { 0.0f, 0.0f } }; -std::vector CubeBottomFace = { +static std::vector CubeBottomFace = { { -0.5f, -0.5f, -0.5f }, { 0.5f, -0.5f, -0.5f }, { 0.5f, -0.5f, 0.5f }, @@ -30,7 +43,7 @@ std::vector CubeBottomFace = { { -0.5f, -0.5f, -0.5f } }; -std::vector CubeBottomFaceUVs = { +static std::vector CubeBottomFaceUVs = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, @@ -39,43 +52,7 @@ std::vector CubeBottomFaceUVs = { { 0.0f, 0.0f } }; -std::vector CubeFrontFace = { - { -0.5f, -0.5f, 0.5f }, - { 0.5f, -0.5f, 0.5f }, - { 0.5f, 0.5f, 0.5f }, - { 0.5f, 0.5f, 0.5f }, - { -0.5f, 0.5f, 0.5f }, - { -0.5f, -0.5f, 0.5f } -}; - -std::vector CubeFrontFaceUVs = { - { 1.0f, 1.0f }, - { 0.0f, 1.0f }, - { 0.0f, 0.0f }, - { 0.0f, 0.0f }, - { 1.0f, 0.0f }, - { 1.0f, 1.0f } -}; - -std::vector CubeBackFace = { - { -0.5f, -0.5f, -0.5f }, - { 0.5f, -0.5f, -0.5f }, - { 0.5f, 0.5f, -0.5f }, - { 0.5f, 0.5f, -0.5f }, - { -0.5f, 0.5f, -0.5f }, - { -0.5f, -0.5f, -0.5f } -}; - -std::vector CubeBackFaceUVs = { - { 1.0f, 1.0f }, - { 0.0f, 1.0f }, - { 0.0f, 0.0f }, - { 0.0f, 0.0f }, - { 1.0f, 0.0f }, - { 1.0f, 1.0f } -}; - -std::vector CubeLeftFace = { +static std::vector CubeLeftFace = { { -0.5f, 0.5f, 0.5f }, { -0.5f, 0.5f, -0.5f }, { -0.5f, -0.5f, -0.5f }, @@ -84,7 +61,7 @@ std::vector CubeLeftFace = { { -0.5f, 0.5f, 0.5f } }; -std::vector CubeLeftFaceUVs = { +static std::vector CubeLeftFaceUVs = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, @@ -93,7 +70,7 @@ std::vector CubeLeftFaceUVs = { { 0.0f, 0.0f } }; -std::vector CubeRightFace = { +static std::vector CubeRightFace = { { 0.5f, 0.5f, 0.5f }, { 0.5f, 0.5f, -0.5f }, { 0.5f, -0.5f, -0.5f }, @@ -102,7 +79,7 @@ std::vector CubeRightFace = { { 0.5f, 0.5f, 0.5f }, }; -std::vector CubeRightFaceUVs = { +static std::vector CubeRightFaceUVs = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, @@ -111,4 +88,40 @@ std::vector CubeRightFaceUVs = { { 0.0f, 0.0f } }; +static std::vector CubeFrontFace = { + { -0.5f, -0.5f, 0.5f }, + { 0.5f, -0.5f, 0.5f }, + { 0.5f, 0.5f, 0.5f }, + { 0.5f, 0.5f, 0.5f }, + { -0.5f, 0.5f, 0.5f }, + { -0.5f, -0.5f, 0.5f } +}; + +static std::vector CubeFrontFaceUVs = { + { 1.0f, 1.0f }, + { 0.0f, 1.0f }, + { 0.0f, 0.0f }, + { 0.0f, 0.0f }, + { 1.0f, 0.0f }, + { 1.0f, 1.0f } +}; + +static std::vector CubeBackFace = { + { -0.5f, -0.5f, -0.5f }, + { 0.5f, -0.5f, -0.5f }, + { 0.5f, 0.5f, -0.5f }, + { 0.5f, 0.5f, -0.5f }, + { -0.5f, 0.5f, -0.5f }, + { -0.5f, -0.5f, -0.5f } +}; + +static std::vector CubeBackFaceUVs = { + { 1.0f, 1.0f }, + { 0.0f, 1.0f }, + { 0.0f, 0.0f }, + { 0.0f, 0.0f }, + { 1.0f, 0.0f }, + { 1.0f, 1.0f } +}; + #endif diff --git a/src/renderer/texture.hpp b/src/renderer/texture.hpp index f7c633e..b83300f 100644 --- a/src/renderer/texture.hpp +++ b/src/renderer/texture.hpp @@ -5,7 +5,7 @@ class Texture { public: - GLuint Texture::LoadTextures(std::vector textures); + GLuint LoadTextures(std::vector textures); }; #endif diff --git a/src/renderer/voxel.cpp b/src/renderer/voxel.cpp index 6583625..65dcc6c 100644 --- a/src/renderer/voxel.cpp +++ b/src/renderer/voxel.cpp @@ -1,42 +1,108 @@ #include "voxel.hpp" +#include + #include "shader.hpp" #include "camera.hpp" #include "face.hpp" +#include "../world/block.hpp" -Voxel::Voxel(int x, int y, int z) { +Voxel::Voxel(glm::vec3 coordsInChunk, uint8_t block) { // Texture winding order - top, bottom, left, right, front, back - //Faces.push_back(Face((FaceDirection)0, 2)); - //Faces.push_back(Face((FaceDirection)1, 0)); - //Faces.push_back(Face((FaceDirection)2, 1)); - //Faces.push_back(Face((FaceDirection)3, 1)); - //Faces.push_back(Face((FaceDirection)4, 1)); - //Faces.push_back(Face((FaceDirection)5, 1)); + Block = block; + m_coordsInChunk = coordsInChunk; - //for (auto& face : Faces) { +} - // std::vector Vert; - // std::vector UVs; +void Voxel::AddFace(EFaceType::Face face) { - // face.GetMesh(Vert, UVs); - // - // m_vertices.insert(m_vertices.end(), Vert.begin(), Vert.end()); - // m_uvs.insert(m_uvs.end(), UVs.begin(), UVs.end()); + std::vector verts; + std::vector uvs; - //} + switch (face) { - for (int i = 0; i < m_vertices.size(); i++) { + case EFaceType::Top: + { - m_vertices[i].x += x; - m_vertices[i].y += y; - m_vertices[i].z += z; + verts = CubeTopFace; + uvs = CubeTopFaceUVs; + + break; + } + + case EFaceType::Bottom: + { + + verts = CubeBottomFace; + uvs = CubeBottomFaceUVs; + + break; + } + + case EFaceType::Left: + { + + verts = CubeTopFace; + uvs = CubeLeftFaceUVs; + + break; + } + + case EFaceType::Right: + { + + verts = CubeRightFace; + uvs = CubeRightFaceUVs; + + break; + } + + case EFaceType::Front: + { + + verts = CubeFrontFace; + uvs = CubeFrontFaceUVs; + + break; + } + + case EFaceType::Back: + { + + verts = CubeBackFace; + uvs = CubeBackFaceUVs; + + break; + } } + + verts = m_translateIntoChunk(verts, m_coordsInChunk); + m_vertices.insert(m_vertices.end(), verts.begin(), verts.end()); + + std::cout << "Voxel ID " << (int)Block << std::endl; + + CBlockEntry block = BlockDictionary.BlockEntries[Block]; + std::cout << "Block ID " << (int)block.ID << std::endl; + + uint16_t tex = block.FaceTextures[face]; + std::cout << "Texture ID " << (int)tex << std::endl; + + std::vector uvws; + + for (auto& uv : uvs) { + + uvws.push_back({ uv.x, uv.y, (float)tex }); + + } + + m_uvs.insert(m_uvs.end(), uvws.begin(), uvws.end()); + } @@ -46,3 +112,17 @@ void Voxel::GetMesh(std::vector& verts, std::vector& uvs) uvs = m_uvs; } + +std::vector Voxel::m_translateIntoChunk(std::vector verts, glm::vec3 trans) { + + for (int i = 0; i < verts.size(); i++) { + + verts[i].x += trans.x; + verts[i].y += trans.y; + verts[i].z += trans.z; + + } + + return verts; + +} diff --git a/src/renderer/voxel.hpp b/src/renderer/voxel.hpp index df3beec..04e2d89 100644 --- a/src/renderer/voxel.hpp +++ b/src/renderer/voxel.hpp @@ -10,12 +10,19 @@ class Shader; class Voxel { public: - Voxel(int x, int y, int z); + Voxel(glm::vec3 coordsInChunk, uint8_t block); + void AddFace(EFaceType::Face face); void GetMesh(std::vector& verts, std::vector& uvs); + uint8_t Block; + private: + glm::vec3 m_coordsInChunk; + + std::vector m_translateIntoChunk(std::vector verts, glm::vec3 trans); + std::vector m_vertices; std::vector m_uvs; diff --git a/src/world/block.cpp b/src/world/block.cpp index d4d55da..abc7fa2 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -2,11 +2,17 @@ #include "../config.hpp" +#include + void CBlockDictionary::Build() { + registerTexture("dirt.png"); + registerTexture("grass_side.png"); + registerTexture("grass_top.png"); + // Texture winding order - top, bottom, left, right, front, back - registerBlock(EBlockType::Air, { }); - registerBlock(EBlockType::Dirt, { EFaceTexture::Dirt, EFaceTexture::Dirt, FaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt }); + registerBlock(EBlockType::Air, { EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air }); + registerBlock(EBlockType::Dirt, { EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt }); registerBlock(EBlockType::Grass, { EFaceTexture::Grass, EFaceTexture::Dirt, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide }); }