Actually rendering faces in a cube (no textures yet, im lazy)

This commit is contained in:
Ben
2019-10-06 22:44:29 +01:00
parent 33735ef2e8
commit feb895a5d8
8 changed files with 90 additions and 41 deletions

View File

@@ -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<Renderer>();
m_world = std::make_unique<World>();
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Top, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Bottom, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Right, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Left, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Front, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Back, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Top, 1, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Bottom, 1, 2));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Right, 1, 3));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Left, 1, 4));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Front, 1, 5));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Back, 1, 6));
m_world->Shaders["Basic"] = std::make_shared<Shader>();
m_world->Shaders["Basic"]->Load("E:/Games/minecraft/resources/shaders/simple");

View File

@@ -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<glm::vec3>& verts, std::vector<glm::vec2>& uvs) {
void Face::Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> 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> camera, std::shared_ptr<Shader> 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));

View File

@@ -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<glm::vec3>& verts, std::vector<glm::vec2>& uvs);
void Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader);
@@ -31,8 +31,8 @@ private:
std::vector<glm::vec3> m_verticies;
std::vector<glm::vec2> m_uvs;
GLuint m_vao;
GLuint m_vbo;
GLuint m_vao = 0;
GLuint m_vbo = 0;
};

View File

@@ -0,0 +1,3 @@
#include "texture.hpp"

View File

@@ -0,0 +1,8 @@
#ifndef MINECRAFT_RENDERER_TEXTURE_H_
#define MINECRAFT_RENDERER_TEXTURE_H_
class Texture {
};
#endif

View File

@@ -0,0 +1,8 @@
#ifndef MINECRAFT_RENDERER_VOXEL_H_
#define MINECRAFT_RENDERER_VOXEL_H_
class Voxel {
};
#endif

12
src/world/block.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include "block.hpp"
std::vector<std::shared_ptr> BlockAtlas() {
static bool Constructed = false;
if (!Constructed) {
}
}

17
src/world/block.hpp Normal file
View File

@@ -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<std::pair<int, std::string>> Textures;
};
std::vector<std::shared_ptr> BlockAtlas();
#endif