[Chunk] Trying to merge cube faces.
This commit is contained in:
parent
b05f33ffa2
commit
89e7cd1414
@ -18,6 +18,7 @@
|
|||||||
#ifndef CHUNK_HPP_
|
#ifndef CHUNK_HPP_
|
||||||
#define CHUNK_HPP_
|
#define CHUNK_HPP_
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Shader.hpp"
|
#include "Shader.hpp"
|
||||||
@ -36,6 +37,8 @@ class Chunk {
|
|||||||
|
|
||||||
u8 getBlock(s8 x, s8 y, s8 z);
|
u8 getBlock(s8 x, s8 y, s8 z);
|
||||||
|
|
||||||
|
s32 getVertexID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate, u8 nbCoordinates = 3);
|
||||||
|
|
||||||
static float noise2d(float x, float y, int seed, int octaves, float persistence);
|
static float noise2d(float x, float y, int seed, int octaves, float persistence);
|
||||||
static float noise3d_abs(float x, float y, float z, int seed, int octaves, float persistence);
|
static float noise3d_abs(float x, float y, float z, int seed, int octaves, float persistence);
|
||||||
|
|
||||||
@ -73,6 +76,8 @@ class Chunk {
|
|||||||
std::vector<float> m_normals;
|
std::vector<float> m_normals;
|
||||||
std::vector<float> m_texCoords;
|
std::vector<float> m_texCoords;
|
||||||
|
|
||||||
|
std::map<size_t, size_t> m_verticesID;
|
||||||
|
|
||||||
VertexBuffer m_vbo;
|
VertexBuffer m_vbo;
|
||||||
|
|
||||||
Chunk *m_surroundingChunks[4];
|
Chunk *m_surroundingChunks[4];
|
||||||
|
@ -148,52 +148,80 @@ void Chunk::update() {
|
|||||||
m_normals.clear();
|
m_normals.clear();
|
||||||
m_texCoords.clear();
|
m_texCoords.clear();
|
||||||
|
|
||||||
u32 skipped = 0;
|
m_verticesID.clear();
|
||||||
|
|
||||||
for(u8 z = 0 ; z < depth ; z++) {
|
for(u8 z = 0 ; z < depth ; z++) {
|
||||||
for(u8 y = 0 ; y < height ; y++) {
|
for(u8 y = 0 ; y < height ; y++) {
|
||||||
for(u8 x = 0 ; x < width ; x++) {
|
for(u8 x = 0 ; x < width ; x++) {
|
||||||
if(getBlock(x, y, z)) {
|
if(!getBlock(x, y, z)) {
|
||||||
for(u8 i = 0 ; i < 6 ; i++) {
|
continue;
|
||||||
if((x > 0 && getBlock(x - 1, y, z) && i == 0)
|
}
|
||||||
|| (x < width - 1 && getBlock(x + 1, y, z) && i == 1)
|
|
||||||
|| (y > 0 && getBlock(x, y - 1, z) && i == 2)
|
for(u8 i = 0 ; i < 6 ; i++) {
|
||||||
|| (y < height - 1 && getBlock(x, y + 1, z) && i == 3)
|
// Check if the face is visible
|
||||||
|| (z > 0 && getBlock(x, y, z - 1) && i == 5)
|
if((x > 0 && getBlock(x - 1, y, z) && i == 0)
|
||||||
|| (z < depth - 1 && getBlock(x, y, z + 1) && i == 4)
|
|| (x < width - 1 && getBlock(x + 1, y, z) && i == 1)
|
||||||
|| (x == 0 && m_surroundingChunks[0] && m_surroundingChunks[0]->getBlock(width - 1, y, z) && i == 0)
|
|| (y > 0 && getBlock(x, y - 1, z) && i == 2)
|
||||||
|| (x == width - 1 && m_surroundingChunks[1] && m_surroundingChunks[1]->getBlock(0, y, z) && i == 1)
|
|| (y < height - 1 && getBlock(x, y + 1, z) && i == 3)
|
||||||
|| (z == 0 && m_surroundingChunks[2] && m_surroundingChunks[2]->getBlock(x, y, depth - 1) && i == 5)
|
|| (z > 0 && getBlock(x, y, z - 1) && i == 5)
|
||||||
|| (z == depth - 1 && m_surroundingChunks[3] && m_surroundingChunks[3]->getBlock(x, y, 0) && i == 4)
|
|| (z < depth - 1 && getBlock(x, y, z + 1) && i == 4)
|
||||||
) {
|
|| (x == 0 && m_surroundingChunks[0] && m_surroundingChunks[0]->getBlock(width - 1, y, z) && i == 0)
|
||||||
skipped++;
|
|| (x == width - 1 && m_surroundingChunks[1] && m_surroundingChunks[1]->getBlock(0, y, z) && i == 1)
|
||||||
continue;
|
|| (z == 0 && m_surroundingChunks[2] && m_surroundingChunks[2]->getBlock(x, y, depth - 1) && i == 5)
|
||||||
}
|
|| (z == depth - 1 && m_surroundingChunks[3] && m_surroundingChunks[3]->getBlock(x, y, 0) && i == 4)
|
||||||
|
) {
|
||||||
// Three points of the face
|
continue;
|
||||||
glm::vec3 a(cubeCoords[i * 12 + 0], cubeCoords[i * 12 + 1], cubeCoords[i * 12 + 2]);
|
}
|
||||||
glm::vec3 b(cubeCoords[i * 12 + 3], cubeCoords[i * 12 + 4], cubeCoords[i * 12 + 5]);
|
|
||||||
glm::vec3 c(cubeCoords[i * 12 + 6], cubeCoords[i * 12 + 7], cubeCoords[i * 12 + 8]);
|
// Merge adjacent cube faces
|
||||||
|
if(x > 0 && getBlock(x - 1, y, z) && i == 5) {
|
||||||
// Computing vectors
|
if(m_verticesID.count(getVertexID(x - 1, y, z, 5, 0, 0))) {
|
||||||
glm::vec3 v1 = b - a;
|
//DEBUG(x - 1, (int)y, (int)z, 5, 0, 0, skipped, "=>", m_vertices.size() - getVertexID(x - 1, y, z, 5, 0, 0, skipped));
|
||||||
glm::vec3 v2 = c - a;
|
m_vertices[m_verticesID[getVertexID(x - 1, y, z, 5, 0, 0)]] += 1;
|
||||||
|
m_vertices[m_verticesID[getVertexID(x - 1, y, z, 5, 1, 0)]] += 1;
|
||||||
// Computing face normal (already normalized because cubeCoords are normalized)
|
|
||||||
glm::vec3 normal = glm::cross(v1, v2);
|
|
||||||
|
|
||||||
for(u8 j = 0 ; j < 4 ; j++) {
|
|
||||||
m_vertices.push_back(x + cubeCoords[i * 12 + j * 3]);
|
|
||||||
//DEBUG(m_vertices.back(), "/", m_vertices[(j + i * 4 + z * 4 * 6 + y * 4 * 6 * Chunk::depth + x * 4 * 6 * Chunk::depth * Chunk::height) * 3 - skipped * 3]);
|
|
||||||
m_vertices.push_back(y + cubeCoords[i * 12 + j * 3 + 1]);
|
|
||||||
m_vertices.push_back(z + cubeCoords[i * 12 + j * 3 + 2]);
|
|
||||||
|
|
||||||
m_normals.push_back(normal.x);
|
m_texCoords[m_verticesID[getVertexID(x - 1, y, z, 5, 0, 0, 2)]] += 1;
|
||||||
m_normals.push_back(normal.y);
|
m_texCoords[m_verticesID[getVertexID(x - 1, y, z, 5, 1, 0, 2)]] += 1;
|
||||||
m_normals.push_back(normal.z);
|
|
||||||
|
|
||||||
m_texCoords.push_back(texCoords[i * 8 + j * 2]);
|
//m_texCoords[getTexCoordID(x - 1, y, z, 5, 0, 1, skipped)] += 1;
|
||||||
m_texCoords.push_back(texCoords[i * 8 + j * 2 + 1]);
|
//m_texCoords[getTexCoordID(x - 1, y, z, 5, 1, 1)] += 1;
|
||||||
}
|
}
|
||||||
|
//m_vertices[getVertexID(x - 1, y, z, 1, 2, 0, skipped)] += 1;
|
||||||
|
//m_vertices[getVertexID(x - 1, y, z, 1, 1, 0, skipped)] += 1;
|
||||||
|
//m_vertices[getVertexID(x - 1, y, z, 2, 2, 0, skipped)] += 1;
|
||||||
|
//m_vertices[getVertexID(x - 1, y, z, 3, 1, 0, skipped)] += 1;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Three points of the face
|
||||||
|
glm::vec3 a(cubeCoords[i * 12 + 0], cubeCoords[i * 12 + 1], cubeCoords[i * 12 + 2]);
|
||||||
|
glm::vec3 b(cubeCoords[i * 12 + 3], cubeCoords[i * 12 + 4], cubeCoords[i * 12 + 5]);
|
||||||
|
glm::vec3 c(cubeCoords[i * 12 + 6], cubeCoords[i * 12 + 7], cubeCoords[i * 12 + 8]);
|
||||||
|
|
||||||
|
// Computing two vectors
|
||||||
|
glm::vec3 v1 = b - a;
|
||||||
|
glm::vec3 v2 = c - a;
|
||||||
|
|
||||||
|
// Computing face normal (already normalized because cubeCoords are normalized)
|
||||||
|
glm::vec3 normal = glm::cross(v1, v2);
|
||||||
|
|
||||||
|
// Store vertex information
|
||||||
|
for(u8 j = 0 ; j < 4 ; j++) {
|
||||||
|
m_vertices.push_back(x + cubeCoords[i * 12 + j * 3]);
|
||||||
|
m_vertices.push_back(y + cubeCoords[i * 12 + j * 3 + 1]);
|
||||||
|
m_vertices.push_back(z + cubeCoords[i * 12 + j * 3 + 2]);
|
||||||
|
|
||||||
|
m_normals.push_back(normal.x);
|
||||||
|
m_normals.push_back(normal.y);
|
||||||
|
m_normals.push_back(normal.z);
|
||||||
|
|
||||||
|
m_texCoords.push_back(texCoords[i * 8 + j * 2]);
|
||||||
|
m_texCoords.push_back(texCoords[i * 8 + j * 2 + 1]);
|
||||||
|
|
||||||
|
m_verticesID[getVertexID(x, y, z, i, j, 0)] = m_vertices.size() - 3;
|
||||||
|
m_verticesID[getVertexID(x, y, z, i, j, 1)] = m_vertices.size() - 2;
|
||||||
|
m_verticesID[getVertexID(x, y, z, i, j, 2)] = m_vertices.size() - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -234,6 +262,7 @@ void Chunk::draw(Shader &shader) {
|
|||||||
|
|
||||||
Texture::bind(&m_texture);
|
Texture::bind(&m_texture);
|
||||||
|
|
||||||
|
//glDrawArrays(GL_LINES, 0, m_vertices.size() / 3);
|
||||||
glDrawArrays(GL_QUADS, 0, m_vertices.size() / 3);
|
glDrawArrays(GL_QUADS, 0, m_vertices.size() / 3);
|
||||||
|
|
||||||
Texture::bind(nullptr);
|
Texture::bind(nullptr);
|
||||||
@ -254,6 +283,10 @@ u8 Chunk::getBlock(s8 x, s8 y, s8 z) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s32 Chunk::getVertexID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate, u8 nbCoordinates) {
|
||||||
|
return (j + i * 4 + x * 4 * 6 + y * 4 * 6 * Chunk::width + z * 4 * 6 * Chunk::width * Chunk::height) * nbCoordinates + coordinate;
|
||||||
|
}
|
||||||
|
|
||||||
float Chunk::noise2d(float x, float y, int seed, int octaves, float persistence) {
|
float Chunk::noise2d(float x, float y, int seed, int octaves, float persistence) {
|
||||||
float sum = 0;
|
float sum = 0;
|
||||||
float strength = 1.0;
|
float strength = 1.0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user