Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 36 additions & 54 deletions src/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::unordered_map<int, std::vector<std::pair<int, int>>> textureCoordMap = {

class Block {
static void addColCubeFace(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3, glm::vec3 p4, glm::vec3 col, float brightness,
float * vertices, unsigned int * indices, int *vCount, int *iCount);
std::vector<float> &vertices, std::vector<unsigned int> &indices, int &vCount, int &iCount);
public:
static constexpr int BLOCK_RENDER_SIZE = 2;
// TODO: do we keep this in CPU or in GPU ?
Expand All @@ -46,10 +46,10 @@ class Block {


static void creatColourCube(glm::vec3 pos, glm::vec3 sie, glm::vec3 colour,
float *vertices, unsigned int *indices, int *vCount, int *iCount);
std::vector<float> &vertices, std::vector<unsigned int> &indices, int &vCount, int &iCount);

// static void createTexCube(glm::vec3 pos, glm::vec3 size,
// float *vertices, unsigned int *indices, int *vCount, int *iCount,
// std::vector<float> &vertices, std::vector<unsigned int> &indices, int *vCount, int *iCount,
// BlockType blockType);


Expand All @@ -58,48 +58,39 @@ class Block {


void Block::addColCubeFace(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3, glm::vec3 p4, glm::vec3 col, float brightness,
float * vertices, unsigned int * indices, int *vCount, int *iCount) {

std::vector<float> &vertices, std::vector<unsigned int> &indices, int &vCount, int &iCount) {
// 7 floats per vertex: x, y, z, r, g, b, brightness
int v1 = *vCount;
int index_offset = v1 / 7;

// arr of points
glm::vec3 points[4] = {p1, p2, p3, p4};


// Add vertices
for(int i = 0; i < 4; i++){
int io = i * 7;
// x y z for each point
vertices[v1 + io] = points[i].x;
vertices[v1 + io + 1] = points[i].y;
vertices[v1 + io + 2] = points[i].z;

vertices[v1 + io + 3] = col.x;
vertices[v1 + io + 4] = col.y;
vertices[v1 + io + 5] = col.z;

vertices[v1 + io + 6] = brightness; // shadow value
int index_offset = vCount / 7;

// Add vertices using push_back
for (int i = 0; i < 4; i++) {
vertices.push_back(points[i].x);
vertices.push_back(points[i].y);
vertices.push_back(points[i].z);
vertices.push_back(col.x);
vertices.push_back(col.y);
vertices.push_back(col.z);
vertices.push_back(brightness);
}

// Add indices
indices[*iCount] = index_offset;
indices[*iCount + 1] = index_offset + 1;
indices[*iCount + 2] = index_offset + 2;
indices[*iCount + 3] = index_offset;
indices[*iCount + 4] = index_offset + 2;
indices[*iCount + 5] = index_offset + 3;
// Add indices using push_back
indices.push_back(index_offset);
indices.push_back(index_offset + 1);
indices.push_back(index_offset + 2);
indices.push_back(index_offset);
indices.push_back(index_offset + 2);
indices.push_back(index_offset + 3);

*vCount += 28; // 7 floats per vertex, 4 vertices per face
*iCount += 6;
vCount += 28; // 7 floats per vertex, 4 vertices per face
iCount += 6;
}


// no neighbouring checking
// no compression
void Block::creatColourCube(glm::vec3 pos, glm::vec3 size, glm::vec3 colour,
float *vertices, unsigned int *indices, int *vCount, int *iCount){
std::vector<float> &vertices, std::vector<unsigned int> &indices, int &vCount, int &iCount){
float blockX = pos.x;
float blockY = pos.y;
float blockZ = pos.z;
Expand Down Expand Up @@ -136,26 +127,17 @@ void Block::creatColourCube(glm::vec3 pos, glm::vec3 size, glm::vec3 colour,
Block::BLOCK_RENDER_SIZE * blockY + size.y,
Block::BLOCK_RENDER_SIZE * blockZ - size.z};

// front, back, left, right, top, bottom

glm::vec3 n1; // for normal??, not used.
// front face
addColCubeFace(p1, p2, p3, p4, colour, 0.86f, vertices, indices, vCount, iCount);

// back face
addColCubeFace(p5, p6, p7, p8, colour, 0.86f, vertices, indices, vCount, iCount);

// left face
addColCubeFace(p2, p5, p8, p3, colour, 0.8f, vertices, indices, vCount, iCount);

// right face
addColCubeFace(p6, p1, p4, p7, colour, 0.8f, vertices, indices, vCount, iCount);

// top face
addColCubeFace(p4, p3, p8, p7, colour, 1.0f, vertices, indices, vCount, iCount);

// bottom face
addColCubeFace(p6, p5, p2, p1, colour, 0.7f, vertices, indices, vCount, iCount);
// Reserve space for vertices and indices to avoid repeated reallocations
vertices.reserve(vertices.size() + 28 * 6); // 6 faces, 28 floats per face
indices.reserve(indices.size() + 6 * 6); // 6 faces, 6 indices per face

// front, back, left, right, top, bottom
addColCubeFace(p1, p2, p3, p4, colour, 0.86f, vertices, indices, vCount, iCount); // front
addColCubeFace(p5, p6, p7, p8, colour, 0.86f, vertices, indices, vCount, iCount); // back
addColCubeFace(p2, p5, p8, p3, colour, 0.8f, vertices, indices, vCount, iCount); // left
addColCubeFace(p6, p1, p4, p7, colour, 0.8f, vertices, indices, vCount, iCount); // right
addColCubeFace(p4, p3, p8, p7, colour, 1.0f, vertices, indices, vCount, iCount); // top
addColCubeFace(p6, p5, p2, p1, colour, 0.7f, vertices, indices, vCount, iCount); // bottom
}


Expand Down
64 changes: 36 additions & 28 deletions src/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "TerrainGenerator.h"
#include "Renderer.h"
#include <glm/glm.hpp>
#include <memory>
#include <learnopengl/shader_m.h>

/*
Expand All @@ -31,9 +32,9 @@ struct Chunk {
// ChunkModel model;
glm::vec3 chunkPosition; // minimum corner of the chunk
Material material;
Renderer<int> *renderer; // reference to renderer
Renderer<int> *renderer; // reference to renderer (will update to weak_ptr in impl)

Chunk(glm::vec3 position, Shader *shader, Renderer<int> * renderer);
Chunk(glm::vec3 position, std::shared_ptr<Shader> shader, Renderer<int> * renderer);
~Chunk();

void createMesh();
Expand Down Expand Up @@ -64,7 +65,7 @@ struct Chunk {

bool Chunk::debugMode = false;

Chunk::Chunk(glm::vec3 position, Shader *shader, Renderer<int> * _renderer) {
Chunk::Chunk(glm::vec3 position, std::shared_ptr<Shader> shader, Renderer<int> * _renderer) {
// blocks = new Block[CHUNK_SIZE_CUBED];
chunkPosition = position;
// material = LoadMaterialDefault();
Expand All @@ -88,14 +89,16 @@ void Chunk::createMesh() {
int totalVertices = CHUNK_SIZE_CUBED * 6 * 4 * 2;
int totalIndices = CHUNK_SIZE_CUBED * 6 * 6 * 2;

unsigned int *indices =
(unsigned int *)malloc(totalIndices * sizeof(unsigned int));
// unsigned int *indices =
// (unsigned int *)malloc(totalIndices * sizeof(unsigned int));

mesh = {0};
mesh.vertexCount = 0;
mesh.triangleCount = 0;
mesh.vertices = (int *)malloc(totalVertices * sizeof(int));
mesh.indices = indices;
mesh.vertices.clear();
mesh.indices.clear();
mesh.vertices.reserve(totalVertices);
mesh.indices.reserve(totalIndices);

for (int x = 0; x < CHUNK_SIZE; x++) {
for (int y = 0; y < CHUNK_SIZE; y++) {
Expand All @@ -110,11 +113,12 @@ void Chunk::createMesh() {
}
}

mesh.triangleCount = indexCount / 3;
mesh.vertexCount = mesh.vertices.size();
mesh.triangleCount = mesh.indices.size() / 3;

// set up vao and vbo
renderer->upload(&mesh.vaoId, &mesh.vboId, mesh.vertices,
mesh.vertexCount, mesh.indices, indexCount, false);
mesh.vertexCount, mesh.indices, mesh.indices.size(), false);
// model = LoadChunkModelFromMesh(mesh, material);
// model = LoadModelFromMesh(mesh);
}
Expand All @@ -126,13 +130,20 @@ void Chunk::unload() {

// free allocated memory
renderer->unload(&mesh.vaoId, &mesh.vboId[0], mesh.vertices, mesh.indices);

mesh.vertices.clear();
mesh.indices.clear();
mesh.vertexCount = 0;
mesh.triangleCount = 0;
loaded = false;
hasSetup = false;
}

void Chunk::rebuildMesh() {
renderer->unload(&mesh.vaoId, &mesh.vboId[0], mesh.vertices, mesh.indices);
mesh.vertices.clear();
mesh.indices.clear();
mesh.vertexCount = 0;
mesh.triangleCount = 0;
createMesh();
}

Expand Down Expand Up @@ -177,24 +188,21 @@ void Chunk::initialize(TerrainGenerator *generator) {

void Chunk::AddCubeFace(ChunkMesh<int> *mesh, int p1, int p2, int p3, int p4,
int *vCount, int *iCount) {
int v1 = *vCount;
int v2 = *vCount + 1;
int v3 = *vCount + 2;
int v4 = *vCount + 3;

// Add vertices
mesh->vertices[v1] = p1;
mesh->vertices[v2] = p2;
mesh->vertices[v3] = p3;
mesh->vertices[v4] = p4;

// Add indices
mesh->indices[*iCount] = v1;
mesh->indices[*iCount + 1] = v2;
mesh->indices[*iCount + 2] = v3;
mesh->indices[*iCount + 3] = v1;
mesh->indices[*iCount + 4] = v3;
mesh->indices[*iCount + 5] = v4;
int index_offset = mesh->vertices.size();

// Add vertices using push_back
mesh->vertices.push_back(p1);
mesh->vertices.push_back(p2);
mesh->vertices.push_back(p3);
mesh->vertices.push_back(p4);

// Add indices using push_back
mesh->indices.push_back(index_offset);
mesh->indices.push_back(index_offset + 1);
mesh->indices.push_back(index_offset + 2);
mesh->indices.push_back(index_offset);
mesh->indices.push_back(index_offset + 2);
mesh->indices.push_back(index_offset + 3);

*vCount += 4;
*iCount += 6;
Expand Down
6 changes: 3 additions & 3 deletions src/ChunkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct ChunkManager {
std::shared_ptr<std::mutex> visibilityMutex;
ChunkManager();
ChunkManager(unsigned int _chunkGenDistance,
unsigned int _chunkRenderDistance, Shader *_terrainShader, Renderer<int> *_renderer,
unsigned int _chunkRenderDistance, std::shared_ptr<Shader> _terrainShader, Renderer<int> *_renderer,
TerrainGenerator *_terrainGenerator);
~ChunkManager();
void update(float dt, Camera newCamera);
Expand All @@ -95,7 +95,7 @@ struct ChunkManager {
GetChunkRenderRange(glm::vec3 newCameraPosition);
void render(Camera newCamera);

Shader *terrainShader;
std::shared_ptr<Shader> terrainShader;

ChunkList chunkLoadList;
ChunkList chunkSetupList;
Expand All @@ -121,7 +121,7 @@ ChunkManager::ChunkManager() {

ChunkManager::ChunkManager(unsigned int _chunkGenDistance,
unsigned int _chunkRenderDistance,
Shader *_terrainShader,
std::shared_ptr<Shader> _terrainShader,
Renderer<int> *_renderer,
TerrainGenerator *_terrainGenerator) {
chunkGenDistance = _chunkGenDistance;
Expand Down
17 changes: 9 additions & 8 deletions src/ChunkMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@
#include <glm/gtc/type_ptr.hpp>
#include <learnopengl/shader_m.h>
#include <stdlib.h>
#include <memory>

#include <stdio.h>

// Material, includes shader and maps
struct Material {
Shader* shader; // Material shader
std::shared_ptr<Shader> shader;
// MaterialMap *maps; // Material maps array (MAX_MATERIAL_MAPS)
// float params[4]; // Material generic parameters (if required)
Material();
Material(Shader* _shader);
Material(std::shared_ptr<Shader> _shader);
};

Material::Material() {}

Material::Material(Shader* _shader) { shader = _shader; }
Material::Material(std::shared_ptr<Shader> _shader) {
shader = _shader;
}

template <typename VERTEX_TYPE>
class ChunkMesh {
Expand All @@ -33,7 +36,7 @@ class ChunkMesh {
int vertexCount; // Number of vertices stored in arrays
int triangleCount; // Number of triangles stored (indexed or not)

VERTEX_TYPE *vertices;
std::vector<VERTEX_TYPE> vertices;
/*
Represents vertex data by packing them into a 32-bit float:
[start]...ttttttfffzzzzzzyyyyyyxxxxxx[end]
Expand All @@ -43,7 +46,8 @@ class ChunkMesh {
- f: bits occupied to represent the vertex's face's normal vector
- t: block type ID
*/
unsigned int *indices; // Vertex indices (in case vertex data comes indexed)
// unsigned int *indices; // Vertex indices (in case vertex data comes indexed)
std::vector<unsigned int> indices;

// OpenGL identifiers
unsigned int vaoId; // OpenGL Vertex Array Object id
Expand All @@ -59,7 +63,4 @@ struct ChunkModel {
int *meshMaterial; // Mesh material number
};




#endif // MESH_H
Loading