voxels init

This commit is contained in:
Ben
2019-10-10 22:30:10 +01:00
parent b2bfa01fc3
commit 8ae9838266
7 changed files with 53 additions and 52 deletions

View File

@@ -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<Camera>();
m_cameras["Default"] = std::make_shared<Camera>(w, h);
m_activeCamera = m_cameras["Default"];
m_world = std::make_unique<World>();
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Top, 2, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Bottom, 0, 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->Voxels.push_back(std::make_shared<Voxel>(0, 0, 0));
//m_world->Voxels.push_back(std::make_shared<Voxel>(1, 0, 0));
//m_world->Voxels.push_back(std::make_shared<Voxel>(-1, 0, 0));
//m_world->Voxels.push_back(std::make_shared<Voxel>(0, 0, 1));
//m_world->Voxels.push_back(std::make_shared<Voxel>(0, 0, -1));
//m_world->Voxels.push_back(std::make_shared<Voxel>(-1, 0, -1));
//m_world->Voxels.push_back(std::make_shared<Voxel>(-1, 0, 1));
//m_world->Voxels.push_back(std::make_shared<Voxel>(1, 0, 1));
//m_world->Voxels.push_back(std::make_shared<Voxel>(1, 0, -1));
m_world->Shaders["Basic"] = std::make_shared<Shader>();
m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple");

View File

@@ -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<glm::vec3> uvs;
for (auto& uv : m_uvs) {
uvs.push_back({ uv.x, uv.y, textureID });
}
std::vector<glm::vec3> 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<glm::vec3>& verts, std::vector<glm::vec3>& uvs) {
verts = m_verticies;
verts = m_vertices;
std::vector<glm::vec3> UVs;

View File

@@ -27,7 +27,7 @@ public:
private:
std::vector<glm::vec3> m_verticies;
std::vector<glm::vec3> m_vertices;
std::vector<glm::vec2> m_uvs;
};

View File

@@ -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> world, std::shared_ptr<Camera> 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"]);
}

View File

@@ -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<glm::vec3> topVert;
std::vector<glm::vec3> 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<glm::vec3> 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> camera, std::shared_ptr<Shader> 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());
}

View File

@@ -9,6 +9,7 @@ class Shader;
class Face;
class Voxel {
public:
Voxel(int x, int y, int z);
void Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader);
@@ -20,8 +21,8 @@ private:
glm::mat4 m_model;
std::vector<glm::vec3> m_vertices;
std::vector<glm::vec3> m_uvs;
};
#endif

View File

@@ -4,13 +4,13 @@
#include "../common.hpp"
class Shader;
class Face;
class Voxel;
class World {
public:
std::map<std::string, std::shared_ptr<Shader>> Shaders;
std::vector<std::shared_ptr<Face>> Faces;
std::vector<std::shared_ptr<Voxel>> Voxels;
GLuint TextureID;