Ive done literally nothing and this build doesnt work but need to transfer for laptop tomorrow

This commit is contained in:
Ben
2019-10-14 22:30:12 +01:00
parent dc1b7ab83f
commit fdc87ba343
8 changed files with 175 additions and 67 deletions

View File

@@ -7,6 +7,5 @@ out vec4 outColour;
uniform sampler2DArray tex;
void main() {
// outColour = vec4(TexCoord, 1.0);
outColour = texture(tex, TexCoord);
}

View File

@@ -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<World>();
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->Chunks.push_back(std::make_shared<Chunk>());
m_world->Shaders["Basic"] = std::make_shared<Shader>();
m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple");
@@ -151,7 +143,6 @@ void Game::Run() {
m_renderer = std::make_unique<Renderer>();
while (IsDisplayOpen) {
Input(&e);

93
src/renderer/chunk.cpp Normal file
View File

@@ -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<Voxel>(x, y, z));
}
std::cout << Voxels.size() << " voxels" << std::endl;
m_mesh();
}
Chunk::Chunk(std::vector<std::shared_ptr<Voxel>> voxels) {
Voxels = voxels;
m_mesh();
}
void Chunk::Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> 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::vector<glm::vec3>tempVerts;
std::vector<glm::vec3>tempUVs;
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<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);
}

44
src/renderer/chunk.hpp Normal file
View File

@@ -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<std::shared_ptr<Voxel>> voxels);
void Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader);
void Update();
// Indexed sequentially [x + WIDTH * (y + HEIGHT * z)] = voxel
std::vector<std::shared_ptr<Voxel>> 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<glm::vec3> m_vertices;
std::vector<glm::vec3> m_uvs;
};
#endif

View File

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

View File

@@ -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<glm::vec3> Vert;
std::vector<glm::vec3> 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<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());
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<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);
}
void Voxel::Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader) {
void Voxel::GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& 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;
}

View File

@@ -12,15 +12,12 @@ class Voxel {
public:
Voxel(int x, int y, int z);
void Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader);
void GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& uvs);
std::vector<Face> Faces;
private:
std::vector<Face> m_faces;
GLuint m_vao = 0;
GLuint m_vbo = 0;
glm::mat4 m_model;
std::vector<glm::vec3> m_vertices;

View File

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