diff --git a/resources/shaders/simple.frag b/resources/shaders/simple.frag index 34d93dd..8b1656d 100644 --- a/resources/shaders/simple.frag +++ b/resources/shaders/simple.frag @@ -7,6 +7,5 @@ out vec4 outColour; uniform sampler2DArray tex; void main() { - // outColour = vec4(TexCoord, 1.0); outColour = texture(tex, TexCoord); } diff --git a/src/game.cpp b/src/game.cpp index b24505c..c44909d 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/voxel.hpp" +#include "renderer/chunk.hpp" #include "world/world.hpp" #include "world/block.hpp" @@ -84,15 +84,7 @@ void Game::Setup(int w, int h) { m_world = std::make_unique(); - 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->Chunks.push_back(std::make_shared()); m_world->Shaders["Basic"] = std::make_shared(); m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple"); @@ -151,7 +143,6 @@ void Game::Run() { m_renderer = std::make_unique(); - while (IsDisplayOpen) { Input(&e); diff --git a/src/renderer/chunk.cpp b/src/renderer/chunk.cpp new file mode 100644 index 0000000..349ef76 --- /dev/null +++ b/src/renderer/chunk.cpp @@ -0,0 +1,93 @@ +#include "chunk.hpp" + +#include "shader.hpp" +#include "camera.hpp" + +#include "voxel.hpp" + +Chunk::Chunk() { + + for (int x = 0; x < CHUNK_WIDTH; x++) + for (int y = 0; y < CHUNK_HEIGHT; y++) + for (int z = 0; z < CHUNK_HEIGHT; z++) { + + Voxels.push_back(std::make_shared(x, y, z)); + + } + + std::cout << Voxels.size() << " voxels" << std::endl; + + m_mesh(); + +} + +Chunk::Chunk(std::vector> voxels) { + + Voxels = voxels; + m_mesh(); + +} + + +void Chunk::Render(std::shared_ptr camera, std::shared_ptr shader) { + + shader->Use(); + glBindVertexArray(m_vao); + + GLint uniTrans = glGetUniformLocation(shader->Program, "model"); + glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(m_model)); + + GLint uniView = glGetUniformLocation(shader->Program, "view"); + glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(camera->GetViewMatrix())); + + GLint uniProj = glGetUniformLocation(shader->Program, "proj"); + glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(camera->GetProjectionMatrix())); + + glDrawArrays(GL_TRIANGLES, 0, m_vertices.size()); + +} + +void Chunk::Update() { + + m_mesh(); + +} + +void Chunk::m_mesh() { + + for (auto& voxel : Voxels) { + + std::vectortempVerts; + std::vectortempUVs; + + voxel->GetMesh(tempVerts, tempUVs); + + for (auto& vert : tempVerts) + m_vertices.push_back(vert); + + for (auto& uv : tempUVs) + m_uvs.push_back(uv); + + } + + 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); + +} diff --git a/src/renderer/chunk.hpp b/src/renderer/chunk.hpp new file mode 100644 index 0000000..a42f777 --- /dev/null +++ b/src/renderer/chunk.hpp @@ -0,0 +1,44 @@ +#ifndef MINECRAFT_RENDERER_CHUNK_H_ +#define MINECRAFT_RENDERER_CHUNK_H_ + +#include "../common.hpp" + +#define CHUNK_HEIGHT 1 +#define CHUNK_WIDTH 1 +#define CHUNK_DEPTH 1 + +class Camera; +class Shader; + +class Voxel; + +class Chunk { +public: + + Chunk(); + 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; + +private: + + void m_mesh(); + + GLuint m_vao = 0; + GLuint m_vbo = 0; + + // Must be translated by a multiple of 16 in the x or z, nothing in y + glm::mat4 m_model; + + std::vector m_vertices; + std::vector m_uvs; + + +}; + +#endif diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 3c8a4d3..b0e6679 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -2,7 +2,7 @@ #include "../world/world.hpp" #include "shader.hpp" -#include "voxel.hpp" +#include "chunk.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->Voxels.size(); i++) { + for (int i = 0; i < world->Chunks.size(); i++) { - world->Voxels[i]->Render(camera, world->Shaders["Basic"]); + world->Chunks[i]->Render(camera, world->Shaders["Basic"]); } diff --git a/src/renderer/voxel.cpp b/src/renderer/voxel.cpp index 1b8e109..9f666aa 100644 --- a/src/renderer/voxel.cpp +++ b/src/renderer/voxel.cpp @@ -7,19 +7,30 @@ Voxel::Voxel(int x, int y, int z) { - + m_model = glm::translate(glm::mat4(1.0f), { (float)x, (float)y, (float)z }); + + + //glm::mat4 Matrix = { + // {1, 3, 0, 0}, + // {0, 1, 0, 0}, + // {0, 0, 1, 0}, + // {0, 0, 0, 1}, + //}; + + //m_model = Matrix * m_model; + // Texture winding order - top, bottom, left, right, front, back - m_faces.push_back(Face((FaceDirection)0, 2)); - m_faces.push_back(Face((FaceDirection)1, 0)); - m_faces.push_back(Face((FaceDirection)2, 1)); - m_faces.push_back(Face((FaceDirection)3, 1)); - m_faces.push_back(Face((FaceDirection)4, 1)); - m_faces.push_back(Face((FaceDirection)5, 1)); + 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)); - for (auto& face : m_faces) { + for (auto& face : Faces) { std::vector Vert; std::vector UVs; @@ -28,50 +39,23 @@ Voxel::Voxel(int x, int y, int z) { m_uvs.insert(m_uvs.end(), UVs.begin(), UVs.end()); } +/* + for (auto& vert : m_vertices) { - 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()); + glm::vec4 tmp = { vert, 1 }; - glGenVertexArrays(1, &m_vao); - glBindVertexArray(m_vao); + glm::vec4 res = tmp * m_model; - glGenBuffers(1, &m_vbo); - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); + vert = { res.x, res.y, res.z }; - 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); } -void Voxel::Render(std::shared_ptr camera, std::shared_ptr shader) { +void Voxel::GetMesh(std::vector& verts, std::vector& uvs) { - shader->Use(); - glBindVertexArray(m_vao); - - GLint uniTrans = glGetUniformLocation(shader->Program, "model"); - glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(m_model)); - - GLint uniView = glGetUniformLocation(shader->Program, "view"); - glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(camera->GetViewMatrix())); - - GLint uniProj = glGetUniformLocation(shader->Program, "proj"); - glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(camera->GetProjectionMatrix())); - - glDrawArrays(GL_TRIANGLES, 0, m_vertices.size()); + verts = m_vertices; + uvs = m_uvs; } diff --git a/src/renderer/voxel.hpp b/src/renderer/voxel.hpp index 51c60e0..73e78f7 100644 --- a/src/renderer/voxel.hpp +++ b/src/renderer/voxel.hpp @@ -12,15 +12,12 @@ class Voxel { public: Voxel(int x, int y, int z); - void Render(std::shared_ptr camera, std::shared_ptr shader); + void GetMesh(std::vector& verts, std::vector& uvs); + + std::vector Faces; private: - std::vector m_faces; - - GLuint m_vao = 0; - GLuint m_vbo = 0; - glm::mat4 m_model; std::vector m_vertices; diff --git a/src/world/world.hpp b/src/world/world.hpp index 1b206fa..ad774a5 100644 --- a/src/world/world.hpp +++ b/src/world/world.hpp @@ -4,13 +4,13 @@ #include "../common.hpp" class Shader; -class Voxel; +class Chunk; class World { public: std::map> Shaders; - std::vector> Voxels; + std::vector> Chunks; GLuint TextureID;