From 8ae9838266fae0bdb34168eed0868c427d1cbfbf Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 10 Oct 2019 22:30:10 +0100 Subject: [PATCH] voxels init --- src/game.cpp | 19 ++++++++++-------- src/renderer/face.cpp | 42 +++++++-------------------------------- src/renderer/face.hpp | 2 +- src/renderer/renderer.cpp | 6 +++--- src/renderer/voxel.cpp | 27 ++++++++++++++++++++++++- src/renderer/voxel.hpp | 5 +++-- src/world/world.hpp | 4 ++-- 7 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 27bb59c..510c814 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -7,7 +7,7 @@ #include "renderer/texture.hpp" #include "renderer/shader.hpp" #include "renderer/camera.hpp" -#include "renderer/face.hpp" +#include "renderer/voxel.hpp" #include "world/world.hpp" #include "world/block.hpp" @@ -78,18 +78,21 @@ void Game::Setup(int w, int h) { *m_logger << LOGGER_ENDL; IsDisplayOpen = true; - m_cameras["Default"] = std::make_shared(); + m_cameras["Default"] = std::make_shared(w, h); m_activeCamera = m_cameras["Default"]; m_world = std::make_unique(); - m_world->Faces.push_back(std::make_shared(FaceDirection::Top, 2, 1)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Bottom, 0, 2)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Right, 1, 3)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Left, 1, 4)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Front, 1, 5)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Back, 1, 6)); + m_world->Voxels.push_back(std::make_shared(0, 0, 0)); + //m_world->Voxels.push_back(std::make_shared(1, 0, 0)); + //m_world->Voxels.push_back(std::make_shared(-1, 0, 0)); + //m_world->Voxels.push_back(std::make_shared(0, 0, 1)); + //m_world->Voxels.push_back(std::make_shared(0, 0, -1)); + //m_world->Voxels.push_back(std::make_shared(-1, 0, -1)); + //m_world->Voxels.push_back(std::make_shared(-1, 0, 1)); + //m_world->Voxels.push_back(std::make_shared(1, 0, 1)); + //m_world->Voxels.push_back(std::make_shared(1, 0, -1)); m_world->Shaders["Basic"] = std::make_shared(); m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple"); diff --git a/src/renderer/face.cpp b/src/renderer/face.cpp index 67f22a0..5e0ca91 100644 --- a/src/renderer/face.cpp +++ b/src/renderer/face.cpp @@ -19,7 +19,7 @@ Face::Face(FaceDirection direction, int textureID) { if (Direction == FaceDirection::Top) { - m_verticies = { + m_vertices = { { -0.5f, 0.5f, -0.5f }, { 0.5f, 0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }, @@ -30,7 +30,7 @@ Face::Face(FaceDirection direction, int textureID) { } else if (Direction == FaceDirection::Bottom) { - m_verticies = { + m_vertices = { { -0.5f, -0.5f, -0.5f }, { 0.5f, -0.5f, -0.5f }, { 0.5f, -0.5f, 0.5f }, @@ -41,7 +41,7 @@ Face::Face(FaceDirection direction, int textureID) { } else if (Direction == FaceDirection::Front) { - m_verticies = { + m_vertices = { { -0.5f, -0.5f, 0.5f }, { 0.5f, -0.5f, 0.5f }, { 0.5f, 0.5f, 0.5f }, @@ -61,7 +61,7 @@ Face::Face(FaceDirection direction, int textureID) { } else if (Direction == FaceDirection::Back) { - m_verticies = { + m_vertices = { { -0.5f, -0.5f, -0.5f }, { 0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, -0.5f }, @@ -81,7 +81,7 @@ Face::Face(FaceDirection direction, int textureID) { } else if (Direction == FaceDirection::Left) { - m_verticies = { + m_vertices = { { -0.5f, 0.5f, 0.5f }, { -0.5f, 0.5f, -0.5f }, { -0.5f, -0.5f, -0.5f }, @@ -92,7 +92,7 @@ Face::Face(FaceDirection direction, int textureID) { } else if (Direction == FaceDirection::Right) { - m_verticies = { + m_vertices = { { 0.5f, 0.5f, 0.5f }, { 0.5f, 0.5f, -0.5f }, { 0.5f, -0.5f, -0.5f }, @@ -103,39 +103,11 @@ Face::Face(FaceDirection direction, int textureID) { } - glGenVertexArrays(1, &m_vao); - glBindVertexArray(m_vao); - - glGenBuffers(1, &m_vbo); - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); - - std::vector uvs; - - for (auto& uv : m_uvs) { - - uvs.push_back({ uv.x, uv.y, textureID }); - - } - - std::vector data; - data.insert(data.end(), m_verticies.begin(), m_verticies.end()); - data.insert(data.end(), uvs.begin(), uvs.end()); - - glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(glm::vec3), &data[0], GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const void*)0); - - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(m_verticies.size() * sizeof(glm::vec3))); - - glBindVertexArray(0); - } void Face::GetMesh(std::vector& verts, std::vector& uvs) { - verts = m_verticies; + verts = m_vertices; std::vector UVs; diff --git a/src/renderer/face.hpp b/src/renderer/face.hpp index 946963a..9c2500d 100644 --- a/src/renderer/face.hpp +++ b/src/renderer/face.hpp @@ -27,7 +27,7 @@ public: private: - std::vector m_verticies; + std::vector m_vertices; std::vector m_uvs; }; diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 63cb9ef..3c8a4d3 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -2,7 +2,7 @@ #include "../world/world.hpp" #include "shader.hpp" -#include "face.hpp" +#include "voxel.hpp" Renderer::Renderer() { @@ -13,9 +13,9 @@ void Renderer::Render(std::shared_ptr world, std::shared_ptr came glBindTexture(GL_TEXTURE_2D_ARRAY, world->TextureID); - for (int i = 0; i < world->Faces.size(); i++) { + for (int i = 0; i < world->Voxels.size(); i++) { - world->Faces[i]->Render(camera, world->Shaders["Basic"]); + world->Voxels[i]->Render(camera, world->Shaders["Basic"]); } diff --git a/src/renderer/voxel.cpp b/src/renderer/voxel.cpp index edac215..055237b 100644 --- a/src/renderer/voxel.cpp +++ b/src/renderer/voxel.cpp @@ -9,7 +9,32 @@ Voxel::Voxel(int x, int y, int z) { m_model = glm::translate(glm::mat4(1.0f), { (float)x, (float)y, (float)z }); + Face top((FaceDirection)0, 2); + std::vector topVert; + std::vector topUVs; + top.GetMesh(topVert, topUVs); + m_vertices.insert(m_vertices.end(), topVert.begin(), topVert.end()); + m_uvs.insert(m_uvs.end(), topUVs.begin(), topUVs.end()); + glGenVertexArrays(1, &m_vao); + glBindVertexArray(m_vao); + + glGenBuffers(1, &m_vbo); + glBindBuffer(GL_ARRAY_BUFFER, m_vbo); + + std::vector data; + data.insert(data.end(), m_vertices.begin(), m_vertices.end()); + data.insert(data.end(), m_uvs.begin(), m_uvs.end()); + + glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(glm::vec3), &data[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const void*)0); + + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(m_vertices.size() * sizeof(glm::vec3))); + + glBindVertexArray(0); } @@ -27,6 +52,6 @@ void Voxel::Render(std::shared_ptr camera, std::shared_ptr shade GLint uniProj = glGetUniformLocation(shader->Program, "proj"); glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(camera->GetProjectionMatrix())); - glDrawArrays(GL_TRIANGLES, 0, m_verticies.size()); + glDrawArrays(GL_TRIANGLES, 0, m_vertices.size()); } diff --git a/src/renderer/voxel.hpp b/src/renderer/voxel.hpp index ec92b05..65911fd 100644 --- a/src/renderer/voxel.hpp +++ b/src/renderer/voxel.hpp @@ -9,6 +9,7 @@ class Shader; class Face; class Voxel { +public: Voxel(int x, int y, int z); void Render(std::shared_ptr camera, std::shared_ptr shader); @@ -20,8 +21,8 @@ private: glm::mat4 m_model; - - + std::vector m_vertices; + std::vector m_uvs; }; #endif diff --git a/src/world/world.hpp b/src/world/world.hpp index a3c2042..1b206fa 100644 --- a/src/world/world.hpp +++ b/src/world/world.hpp @@ -4,13 +4,13 @@ #include "../common.hpp" class Shader; -class Face; +class Voxel; class World { public: std::map> Shaders; - std::vector> Faces; + std::vector> Voxels; GLuint TextureID;