Rewrite done - broken
This commit is contained in:
@@ -3,7 +3,9 @@ project(OpenGLPlayground)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeModules/)
|
||||
cmake_policy(SET CMP0037 OLD)
|
||||
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
# set(CMAKE_CXX_FLAGS "-Ofast")
|
||||
|
||||
set(executable output)
|
||||
set(SrcDIR ./src)
|
||||
@@ -43,6 +45,7 @@ file(GLOB SourceFiles
|
||||
${SrcDIR}/*
|
||||
${SrcDIR}/util/*
|
||||
${SrcDIR}/game/*
|
||||
${SrcDIR}/world/*
|
||||
${SrcDIR}/renderer/*
|
||||
)
|
||||
|
||||
|
||||
@@ -86,6 +86,9 @@ void Game::Setup(int w, int h) {
|
||||
|
||||
m_world = std::make_unique<World>();
|
||||
|
||||
Texture texture;
|
||||
m_world->TextureID = texture.LoadTextures(BlockDictionary.Textures);
|
||||
|
||||
m_world->Chunks.push_back(std::make_shared<Chunk>(0, 0));
|
||||
m_world->Chunks.push_back(std::make_shared<Chunk>(1, 1));
|
||||
m_world->Chunks.push_back(std::make_shared<Chunk>(1, 3));
|
||||
@@ -94,9 +97,6 @@ void Game::Setup(int w, int h) {
|
||||
m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple");
|
||||
m_world->Shaders["Basic"]->Link();
|
||||
|
||||
Texture texture;
|
||||
m_world->TextureID = texture.LoadTextures(BlockDictionary.Textures);
|
||||
|
||||
}
|
||||
|
||||
void Game::Input(SDL_Event* e) {
|
||||
|
||||
@@ -5,17 +5,27 @@
|
||||
|
||||
#include "voxel.hpp"
|
||||
|
||||
#include "../world/block.hpp"
|
||||
|
||||
Chunk::Chunk(int x, int z) {
|
||||
|
||||
m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH });
|
||||
|
||||
|
||||
|
||||
// [x + WIDTH * (y + HEIGHT * z)]
|
||||
for (int x = 0; x < CHUNK_WIDTH; x++)
|
||||
for (int y = 0; y < CHUNK_HEIGHT; y++)
|
||||
for (int z = 0; z < CHUNK_DEPTH; z++) {
|
||||
|
||||
Voxels.push_back(std::make_shared<Voxel>(x, y, z));
|
||||
// Grass on the top layer
|
||||
if (y == CHUNK_HEIGHT - 1) {
|
||||
|
||||
Voxels.push_back((uint8_t)EBlockType::Grass);
|
||||
|
||||
} else {
|
||||
|
||||
Voxels.push_back((uint8_t)EBlockType::Dirt);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,10 +35,12 @@ Chunk::Chunk(int x, int z) {
|
||||
|
||||
}
|
||||
|
||||
Chunk::Chunk(std::vector<std::shared_ptr<Voxel>> voxels) {
|
||||
Chunk::Chunk(int x, int z, std::vector<uint8_t> voxels) {
|
||||
|
||||
m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH });
|
||||
|
||||
m_model = glm::mat4(1.0f);
|
||||
Voxels = voxels;
|
||||
|
||||
m_mesh();
|
||||
|
||||
}
|
||||
@@ -58,17 +70,35 @@ void Chunk::Update() {
|
||||
|
||||
}
|
||||
|
||||
uint8_t Chunk::BlockAt(int x, int y, int z) {
|
||||
|
||||
return Voxels[x + CHUNK_WIDTH * (y + CHUNK_HEIGHT * z)];
|
||||
|
||||
}
|
||||
|
||||
void Chunk::m_mesh() {
|
||||
|
||||
for (auto& voxel : Voxels) {
|
||||
for (int x = 0; x < CHUNK_WIDTH; x++)
|
||||
for (int y = 0; y < CHUNK_HEIGHT; y++)
|
||||
for (int z = 0; z < CHUNK_DEPTH; z++) {
|
||||
|
||||
std::vector<glm::vec3> tempVerts;
|
||||
std::vector<glm::vec3> tempUVs;
|
||||
|
||||
voxel->GetMesh(tempVerts, tempUVs);
|
||||
std::cout << x << " " << y << " " << z << std::endl;
|
||||
|
||||
for (auto& vert : tempVerts)
|
||||
m_vertices.push_back(vert);
|
||||
uint8_t block = BlockAt(x, y, z);
|
||||
|
||||
std::cout << "Block ID " << (int)block << std::endl;
|
||||
|
||||
Voxel tmp({x, y, z}, BlockAt(x, y, z));
|
||||
|
||||
tmp.AddFace(EFaceType::Top);
|
||||
tmp.AddFace(EFaceType::Bottom);
|
||||
tmp.AddFace(EFaceType::Left);
|
||||
tmp.AddFace(EFaceType::Right);
|
||||
tmp.AddFace(EFaceType::Front);
|
||||
tmp.AddFace(EFaceType::Back);
|
||||
|
||||
for (auto& uv : tempUVs)
|
||||
m_uvs.push_back(uv);
|
||||
|
||||
@@ -16,12 +16,14 @@ class Chunk {
|
||||
public:
|
||||
|
||||
Chunk(int x, int z);
|
||||
Chunk(std::vector<std::shared_ptr<Voxel>> voxels);
|
||||
Chunk(int x, int z, std::vector<uint8_t> voxels);
|
||||
|
||||
void Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader);
|
||||
|
||||
void Update();
|
||||
|
||||
uint8_t BlockAt(int x, int y, int z);
|
||||
|
||||
// Indexed sequentially [x + WIDTH * (y + HEIGHT * z)] = voxelID
|
||||
// the voxel id is used to index the block dictionary to get properties
|
||||
// to generate a mesh and send it to the GPU
|
||||
|
||||
@@ -3,7 +3,20 @@
|
||||
|
||||
#include "../common.hpp"
|
||||
|
||||
std::vector<glm::vec3> CubeTopFace = {
|
||||
namespace EFaceType {
|
||||
|
||||
enum Face : uint8_t {
|
||||
Top,
|
||||
Bottom,
|
||||
Left,
|
||||
Right,
|
||||
Front,
|
||||
Back,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
static std::vector<glm::vec3> CubeTopFace = {
|
||||
{ -0.5f, 0.5f, -0.5f },
|
||||
{ 0.5f, 0.5f, -0.5f },
|
||||
{ 0.5f, 0.5f, 0.5f },
|
||||
@@ -12,7 +25,7 @@ std::vector<glm::vec3> CubeTopFace = {
|
||||
{ -0.5f, 0.5f, -0.5f }
|
||||
};
|
||||
|
||||
std::vector<glm::vec2> CubeTopFaceUVs = {
|
||||
static std::vector<glm::vec2> CubeTopFaceUVs = {
|
||||
{ 0.0f, 0.0f },
|
||||
{ 1.0f, 0.0f },
|
||||
{ 1.0f, 1.0f },
|
||||
@@ -21,7 +34,7 @@ std::vector<glm::vec2> CubeTopFaceUVs = {
|
||||
{ 0.0f, 0.0f }
|
||||
};
|
||||
|
||||
std::vector<glm::vec3> CubeBottomFace = {
|
||||
static std::vector<glm::vec3> CubeBottomFace = {
|
||||
{ -0.5f, -0.5f, -0.5f },
|
||||
{ 0.5f, -0.5f, -0.5f },
|
||||
{ 0.5f, -0.5f, 0.5f },
|
||||
@@ -30,7 +43,7 @@ std::vector<glm::vec3> CubeBottomFace = {
|
||||
{ -0.5f, -0.5f, -0.5f }
|
||||
};
|
||||
|
||||
std::vector<glm::vec2> CubeBottomFaceUVs = {
|
||||
static std::vector<glm::vec2> CubeBottomFaceUVs = {
|
||||
{ 0.0f, 0.0f },
|
||||
{ 1.0f, 0.0f },
|
||||
{ 1.0f, 1.0f },
|
||||
@@ -39,43 +52,7 @@ std::vector<glm::vec2> CubeBottomFaceUVs = {
|
||||
{ 0.0f, 0.0f }
|
||||
};
|
||||
|
||||
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::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 = {
|
||||
static std::vector<glm::vec3> CubeLeftFace = {
|
||||
{ -0.5f, 0.5f, 0.5f },
|
||||
{ -0.5f, 0.5f, -0.5f },
|
||||
{ -0.5f, -0.5f, -0.5f },
|
||||
@@ -84,7 +61,7 @@ std::vector<glm::vec3> CubeLeftFace = {
|
||||
{ -0.5f, 0.5f, 0.5f }
|
||||
};
|
||||
|
||||
std::vector<glm::vec2> CubeLeftFaceUVs = {
|
||||
static std::vector<glm::vec2> CubeLeftFaceUVs = {
|
||||
{ 0.0f, 0.0f },
|
||||
{ 1.0f, 0.0f },
|
||||
{ 1.0f, 1.0f },
|
||||
@@ -93,7 +70,7 @@ std::vector<glm::vec2> CubeLeftFaceUVs = {
|
||||
{ 0.0f, 0.0f }
|
||||
};
|
||||
|
||||
std::vector<glm::vec3> CubeRightFace = {
|
||||
static std::vector<glm::vec3> CubeRightFace = {
|
||||
{ 0.5f, 0.5f, 0.5f },
|
||||
{ 0.5f, 0.5f, -0.5f },
|
||||
{ 0.5f, -0.5f, -0.5f },
|
||||
@@ -102,7 +79,7 @@ std::vector<glm::vec3> CubeRightFace = {
|
||||
{ 0.5f, 0.5f, 0.5f },
|
||||
};
|
||||
|
||||
std::vector<glm::vec2> CubeRightFaceUVs = {
|
||||
static std::vector<glm::vec2> CubeRightFaceUVs = {
|
||||
{ 0.0f, 0.0f },
|
||||
{ 1.0f, 0.0f },
|
||||
{ 1.0f, 1.0f },
|
||||
@@ -111,4 +88,40 @@ std::vector<glm::vec2> CubeRightFaceUVs = {
|
||||
{ 0.0f, 0.0f }
|
||||
};
|
||||
|
||||
static 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 }
|
||||
};
|
||||
|
||||
static 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 }
|
||||
};
|
||||
|
||||
static 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 }
|
||||
};
|
||||
|
||||
static 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 }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
GLuint Texture::LoadTextures(std::vector<std::string> textures);
|
||||
GLuint LoadTextures(std::vector<std::string> textures);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,43 +1,109 @@
|
||||
#include "voxel.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "shader.hpp"
|
||||
#include "camera.hpp"
|
||||
|
||||
#include "face.hpp"
|
||||
|
||||
#include "../world/block.hpp"
|
||||
|
||||
Voxel::Voxel(int x, int y, int z) {
|
||||
Voxel::Voxel(glm::vec3 coordsInChunk, uint8_t block) {
|
||||
|
||||
// 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));
|
||||
Block = block;
|
||||
m_coordsInChunk = coordsInChunk;
|
||||
|
||||
//for (auto& face : Faces) {
|
||||
}
|
||||
|
||||
// std::vector<glm::vec3> Vert;
|
||||
// std::vector<glm::vec3> UVs;
|
||||
void Voxel::AddFace(EFaceType::Face face) {
|
||||
|
||||
// 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> verts;
|
||||
std::vector<glm::vec2> uvs;
|
||||
|
||||
//}
|
||||
switch (face) {
|
||||
|
||||
for (int i = 0; i < m_vertices.size(); i++) {
|
||||
case EFaceType::Top:
|
||||
{
|
||||
|
||||
m_vertices[i].x += x;
|
||||
m_vertices[i].y += y;
|
||||
m_vertices[i].z += z;
|
||||
verts = CubeTopFace;
|
||||
uvs = CubeTopFaceUVs;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case EFaceType::Bottom:
|
||||
{
|
||||
|
||||
verts = CubeBottomFace;
|
||||
uvs = CubeBottomFaceUVs;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case EFaceType::Left:
|
||||
{
|
||||
|
||||
verts = CubeTopFace;
|
||||
uvs = CubeLeftFaceUVs;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case EFaceType::Right:
|
||||
{
|
||||
|
||||
verts = CubeRightFace;
|
||||
uvs = CubeRightFaceUVs;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case EFaceType::Front:
|
||||
{
|
||||
|
||||
verts = CubeFrontFace;
|
||||
uvs = CubeFrontFaceUVs;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case EFaceType::Back:
|
||||
{
|
||||
|
||||
verts = CubeBackFace;
|
||||
uvs = CubeBackFaceUVs;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
verts = m_translateIntoChunk(verts, m_coordsInChunk);
|
||||
m_vertices.insert(m_vertices.end(), verts.begin(), verts.end());
|
||||
|
||||
std::cout << "Voxel ID " << (int)Block << std::endl;
|
||||
|
||||
CBlockEntry block = BlockDictionary.BlockEntries[Block];
|
||||
std::cout << "Block ID " << (int)block.ID << std::endl;
|
||||
|
||||
uint16_t tex = block.FaceTextures[face];
|
||||
std::cout << "Texture ID " << (int)tex << std::endl;
|
||||
|
||||
std::vector<glm::vec3> uvws;
|
||||
|
||||
for (auto& uv : uvs) {
|
||||
|
||||
uvws.push_back({ uv.x, uv.y, (float)tex });
|
||||
|
||||
}
|
||||
|
||||
m_uvs.insert(m_uvs.end(), uvws.begin(), uvws.end());
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Voxel::GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& uvs) {
|
||||
@@ -46,3 +112,17 @@ void Voxel::GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& uvs)
|
||||
uvs = m_uvs;
|
||||
|
||||
}
|
||||
|
||||
std::vector<glm::vec3> Voxel::m_translateIntoChunk(std::vector<glm::vec3> verts, glm::vec3 trans) {
|
||||
|
||||
for (int i = 0; i < verts.size(); i++) {
|
||||
|
||||
verts[i].x += trans.x;
|
||||
verts[i].y += trans.y;
|
||||
verts[i].z += trans.z;
|
||||
|
||||
}
|
||||
|
||||
return verts;
|
||||
|
||||
}
|
||||
|
||||
@@ -10,12 +10,19 @@ class Shader;
|
||||
|
||||
class Voxel {
|
||||
public:
|
||||
Voxel(int x, int y, int z);
|
||||
Voxel(glm::vec3 coordsInChunk, uint8_t block);
|
||||
|
||||
void AddFace(EFaceType::Face face);
|
||||
void GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec3>& uvs);
|
||||
|
||||
uint8_t Block;
|
||||
|
||||
private:
|
||||
|
||||
glm::vec3 m_coordsInChunk;
|
||||
|
||||
std::vector<glm::vec3> m_translateIntoChunk(std::vector<glm::vec3> verts, glm::vec3 trans);
|
||||
|
||||
std::vector<glm::vec3> m_vertices;
|
||||
std::vector<glm::vec3> m_uvs;
|
||||
|
||||
|
||||
@@ -2,11 +2,17 @@
|
||||
|
||||
#include "../config.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void CBlockDictionary::Build() {
|
||||
|
||||
registerTexture("dirt.png");
|
||||
registerTexture("grass_side.png");
|
||||
registerTexture("grass_top.png");
|
||||
|
||||
// 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::Air, { EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air, EFaceTexture::Air });
|
||||
registerBlock(EBlockType::Dirt, { EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt });
|
||||
registerBlock(EBlockType::Grass, { EFaceTexture::Grass, EFaceTexture::Dirt, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide });
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user