started rewriting

This commit is contained in:
Ben
2019-10-17 23:58:54 +01:00
parent 9dffa6f24f
commit f59f6e9323
7 changed files with 171 additions and 162 deletions

View File

@@ -8,6 +8,7 @@ public:
Camera(int w, int h);
void UpdateView();
glm::mat4 GetViewMatrix();
glm::mat4 GetProjectionMatrix();
glm::vec3 GetPos();

View File

@@ -1,122 +0,0 @@
#include "face.hpp"
#include "shader.hpp"
#include "camera.hpp"
Face::Face(FaceDirection direction, int textureID) {
Direction = direction;
Texture = textureID;
m_uvs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
if (Direction == FaceDirection::Top) {
m_vertices = {
{ -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_vertices = {
{ -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_vertices = {
{ -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_uvs = {
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
};
} else if (Direction == FaceDirection::Back) {
m_vertices = {
{ -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_uvs = {
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
};
} else if (Direction == FaceDirection::Left) {
m_vertices = {
{ -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_vertices = {
{ 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 },
};
}
}
void Face::GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& uvs) {
verts = m_vertices;
std::vector<glm::vec3> UVs;
for (auto& uv : m_uvs) {
UVs.push_back({ uv.x, uv.y, (float)Texture });
}
uvs = UVs;
}

View File

@@ -3,33 +3,112 @@
#include "../common.hpp"
class Camera;
class Shader;
enum FaceDirection {
Top,
Bottom,
Front,
Back,
Left,
Right
std::vector<glm::vec3> CubeTopFace = {
{ -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 }
};
class Face {
public:
Face(FaceDirection direction, int textureID);
void GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& uvs);
std::vector<glm::vec2> CubeTopFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
int Texture = 0;
std::vector<glm::vec3> CubeBottomFace = {
{ -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 }
};
FaceDirection Direction = FaceDirection::Top;
std::vector<glm::vec2> CubeBottomFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
private:
std::vector<glm::vec3> CubeFrontFace = {
{ -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 }
};
std::vector<glm::vec3> m_vertices;
std::vector<glm::vec2> m_uvs;
std::vector<glm::vec2> CubeFrontFaceUVs = {
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f }
};
std::vector<glm::vec3> CubeBackFace = {
{ -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 }
};
std::vector<glm::vec2> CubeBackFaceUVs = {
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f }
};
std::vector<glm::vec3> CubeLeftFace = {
{ -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 }
};
std::vector<glm::vec2> CubeLeftFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
std::vector<glm::vec3> CubeRightFace = {
{ 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 },
};
std::vector<glm::vec2> CubeRightFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
#endif

View File

@@ -9,25 +9,25 @@
Voxel::Voxel(int x, int y, int z) {
// Texture winding order - top, bottom, left, right, front, back
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 : Faces) {
//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));
std::vector<glm::vec3> Vert;
std::vector<glm::vec3> UVs;
//for (auto& face : Faces) {
face.GetMesh(Vert, UVs);
m_vertices.insert(m_vertices.end(), Vert.begin(), Vert.end());
m_uvs.insert(m_uvs.end(), UVs.begin(), UVs.end());
// std::vector<glm::vec3> Vert;
// std::vector<glm::vec3> UVs;
}
// face.GetMesh(Vert, UVs);
//
// m_vertices.insert(m_vertices.end(), Vert.begin(), Vert.end());
// m_uvs.insert(m_uvs.end(), UVs.begin(), UVs.end());
//}
for (int i = 0; i < m_vertices.size(); i++) {

View File

@@ -14,8 +14,6 @@ public:
void GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& uvs);
std::vector<Face> Faces;
private:
std::vector<glm::vec3> m_vertices;

View File

@@ -2,4 +2,25 @@
#include "../config.hpp"
// Texture winding order - top, bottom, left, right, front, back
void CBlockDictionary::Build() {
// Texture winding order - top, bottom, left, right, front, back
registerBlock(EBlockType::Air, { });
registerBlock(EBlockType::Dirt, { EFaceTexture::Dirt, EFaceTexture::Dirt, FaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt });
registerBlock(EBlockType::Grass, { EFaceTexture::Grass, EFaceTexture::Dirt, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide });
}
void CBlockDictionary::registerTexture(std::string texture) {
Textures.push_back(texture);
}
void CBlockDictionary::registerBlock(EBlockType::Block block, std::vector<uint16_t> faceTextures) {
CBlockEntry entry = { block, faceTextures };
BlockEntries[(uint8_t)block] = entry;
}

View File

@@ -3,13 +3,43 @@
#include "../common.hpp"
namespace EBlockType {
enum Block : uint8_t {
Air = 0,
Dirt,
Grass
};
}
namespace EFaceTexture {
enum Texture : uint16_t {
Air = 0,
Dirt,
GrassSide,
Grass
};
}
class CBlockEntry {
public:
uint8_t ID;
// Texture winding order - top, bottom, left, right, front, back
std::vector<uint16_t> FaceTextures;
};
// TODO: Make design of the class data oriented
// ie, import all the data used in the build from
// files and that
class CBlockDictionary {
public:
@@ -26,8 +56,10 @@ public:
private:
void registerTexture();
void registerBlock();
// Expects textures to be inserted in order, 0-...
void registerTexture(std::string texture);
void registerBlock(EBlockType::Block block, std::vector<uint16_t> faceTextures);
};