From feb895a5d804e0bf1d4ae4023b7dfd3f30b9dc8b Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 6 Oct 2019 22:44:29 +0100 Subject: [PATCH] Actually rendering faces in a cube (no textures yet, im lazy) --- src/game.cpp | 14 ++++----- src/renderer/face.cpp | 63 +++++++++++++++++++++------------------- src/renderer/face.hpp | 6 ++-- src/renderer/texture.cpp | 3 ++ src/renderer/texture.hpp | 8 +++++ src/renderer/voxel.hpp | 8 +++++ src/world/block.cpp | 12 ++++++++ src/world/block.hpp | 17 +++++++++++ 8 files changed, 90 insertions(+), 41 deletions(-) create mode 100644 src/world/block.cpp create mode 100644 src/world/block.hpp diff --git a/src/game.cpp b/src/game.cpp index 7644833..7b2d687 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -69,8 +69,6 @@ void Game::Setup(int w, int h) { gladLoadGLLoader(SDL_GL_GetProcAddress); glEnable(GL_MULTISAMPLE); glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); #ifdef __IMGUI @@ -129,12 +127,12 @@ void Game::Run() { m_renderer = std::make_unique(); m_world = std::make_unique(); - m_world->Faces.push_back(std::make_shared(FaceDirection::Top, 1)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Bottom, 1)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Right, 1)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Left, 1)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Front, 1)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Back, 1)); + m_world->Faces.push_back(std::make_shared(FaceDirection::Top, 1, 1)); + m_world->Faces.push_back(std::make_shared(FaceDirection::Bottom, 1, 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->Shaders["Basic"] = std::make_shared(); m_world->Shaders["Basic"]->Load("E:/Games/minecraft/resources/shaders/simple"); diff --git a/src/renderer/face.cpp b/src/renderer/face.cpp index 0af23cf..d591aca 100644 --- a/src/renderer/face.cpp +++ b/src/renderer/face.cpp @@ -3,7 +3,7 @@ #include "shader.hpp" #include "camera.hpp" -Face::Face(FaceDirection direction, int textureID) { +Face::Face(FaceDirection direction, int textureID, int asdf) { Direction = direction; Texture = textureID; @@ -18,64 +18,66 @@ Face::Face(FaceDirection direction, int textureID) { if (Direction == FaceDirection::Top) { m_verticies = { - { -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 }, + { 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 }, }; } else if (Direction == FaceDirection::Bottom) { m_verticies = { - { -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 }, - { 0.5f, 0.5f, 0.5f }, - { -0.5f, 0.5f, 0.5f }, { -0.5f, -0.5f, 0.5f }, + { -0.5f, -0.5f, -0.5f }, }; } else if (Direction == FaceDirection::Front) { m_verticies = { - { -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 }, + { 0.5f, 0.5f, 0.5f }, + { 0.5f, 0.5f, 0.5f }, { -0.5f, 0.5f, 0.5f }, + { -0.5f, -0.5f, 0.5f }, }; } else if (Direction == FaceDirection::Back) { m_verticies = { - { 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 }, - { 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 }, }; } else if (Direction == FaceDirection::Left) { m_verticies = { + { -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 }, - { 0.5f, -0.5f, 0.5f }, { -0.5f, -0.5f, 0.5f }, - { -0.5f, -0.5f, -0.5f }, + { -0.5f, 0.5f, 0.5f }, }; } else if (Direction == FaceDirection::Right) { m_verticies = { - { -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 }, { 0.5f, 0.5f, 0.5f }, - { 0.5f, 0.5f, 0.5f }, - { -0.5f, 0.5f, 0.5f }, - { -0.5f, 0.5f, -0.5f }, }; } - + + m_vao = asdf; + glGenVertexArrays(1, &m_vao); glBindVertexArray(m_vao); @@ -100,7 +102,7 @@ Face::Face(FaceDirection direction, int textureID) { glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(m_verticies.size() * sizeof(glm::vec3))); - glBindVertexArray(1); + glBindVertexArray(m_vao); } @@ -111,6 +113,7 @@ void Face::GetMesh(std::vector& verts, std::vector& uvs) { void Face::Render(std::shared_ptr camera, std::shared_ptr shader) { shader->Use(); + glBindVertexArray(m_vao); glm::mat4 model = glm::mat4(1.0f); GLint uniTrans = glGetUniformLocation(shader->Program, "model"); @@ -120,7 +123,7 @@ void Face::Render(std::shared_ptr camera, std::shared_ptr shader glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(camera->GetViewMatrix())); // Projection matrice - glm::mat4 proj = glm::perspective(glm::radians(45.0f), 1080.0f / 720.0f, 1.0f, 1000.0f); + glm::mat4 proj = glm::perspective(glm::radians(45.0f), 1080.0f / 720.0f, 0.1f, 1000.0f); // Get uniform and send it to the GPU GLint uniProj = glGetUniformLocation(shader->Program, "proj"); glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj)); diff --git a/src/renderer/face.hpp b/src/renderer/face.hpp index 949f56d..3ce0ca0 100644 --- a/src/renderer/face.hpp +++ b/src/renderer/face.hpp @@ -17,7 +17,7 @@ enum FaceDirection { class Face { public: - Face(FaceDirection direction, int textureID); + Face(FaceDirection direction, int textureID, int id); void GetMesh(std::vector& verts, std::vector& uvs); void Render(std::shared_ptr camera, std::shared_ptr shader); @@ -31,8 +31,8 @@ private: std::vector m_verticies; std::vector m_uvs; - GLuint m_vao; - GLuint m_vbo; + GLuint m_vao = 0; + GLuint m_vbo = 0; }; diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp index e69de29..2b550dd 100644 --- a/src/renderer/texture.cpp +++ b/src/renderer/texture.cpp @@ -0,0 +1,3 @@ +#include "texture.hpp" + + diff --git a/src/renderer/texture.hpp b/src/renderer/texture.hpp index e69de29..8c540e2 100644 --- a/src/renderer/texture.hpp +++ b/src/renderer/texture.hpp @@ -0,0 +1,8 @@ +#ifndef MINECRAFT_RENDERER_TEXTURE_H_ +#define MINECRAFT_RENDERER_TEXTURE_H_ + +class Texture { + +}; + +#endif diff --git a/src/renderer/voxel.hpp b/src/renderer/voxel.hpp index e69de29..5977763 100644 --- a/src/renderer/voxel.hpp +++ b/src/renderer/voxel.hpp @@ -0,0 +1,8 @@ +#ifndef MINECRAFT_RENDERER_VOXEL_H_ +#define MINECRAFT_RENDERER_VOXEL_H_ + +class Voxel { + +}; + +#endif diff --git a/src/world/block.cpp b/src/world/block.cpp new file mode 100644 index 0000000..662e344 --- /dev/null +++ b/src/world/block.cpp @@ -0,0 +1,12 @@ +#include "block.hpp" + +std::vector BlockAtlas() { + static bool Constructed = false; + + if (!Constructed) { + + } + + + +} diff --git a/src/world/block.hpp b/src/world/block.hpp new file mode 100644 index 0000000..6a0acb4 --- /dev/null +++ b/src/world/block.hpp @@ -0,0 +1,17 @@ +#ifndef MINECRAFT_WORLD_BLOCK_H_ +#define MINECRAFT_WORLD_BLOCK_H_ + +#include "../common.hpp" + +struct Block { + std::string Name; + int ID; + + std::vector> Textures; + +}; + + +std::vector BlockAtlas(); + +#endif