[Chunk|ChunkBuilder] Small improvements.
This commit is contained in:
parent
91c2e33ae3
commit
5f38eaaeb0
@ -22,7 +22,7 @@ class Block {
|
||||
public:
|
||||
Block(u32 id);
|
||||
|
||||
glm::vec4 getTexCoords(int face);
|
||||
glm::vec4 getTexCoords(int face) const;
|
||||
|
||||
u32 id() const { return m_id; }
|
||||
void setId(u32 id) { m_id = id; }
|
||||
|
@ -14,14 +14,23 @@
|
||||
#ifndef CHUNKBUILDER_HPP_
|
||||
#define CHUNKBUILDER_HPP_
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "Types.hpp"
|
||||
#include "Vertex.hpp"
|
||||
|
||||
class Block;
|
||||
class Chunk;
|
||||
class VertexBuffer;
|
||||
|
||||
class ChunkBuilder {
|
||||
public:
|
||||
std::size_t buildChunk(const Chunk &chunk, const VertexBuffer &vbo);
|
||||
|
||||
private:
|
||||
void addFace(u8 x, u8 y, u8 z, u8 i, const Block *block, const Block *surroundingBlock);
|
||||
|
||||
std::vector<Vertex> m_vertices;
|
||||
};
|
||||
|
||||
#endif // CHUNKBUILDER_HPP_
|
||||
|
@ -17,7 +17,7 @@ Block::Block(u32 id) {
|
||||
m_id = id;
|
||||
}
|
||||
|
||||
glm::vec4 Block::getTexCoords(int face) {
|
||||
glm::vec4 Block::getTexCoords(int face) const {
|
||||
u32 id = m_id;
|
||||
|
||||
// 0 -> right
|
||||
|
@ -67,20 +67,11 @@ void Chunk::draw(RenderTarget &target, RenderStates states) const {
|
||||
// drawOutlines(target, states);
|
||||
}
|
||||
|
||||
// FIXME: Use the renderer to do that
|
||||
void Chunk::drawOutlines(RenderTarget &, RenderStates states) const {
|
||||
Shader::bind(states.shader);
|
||||
VertexBuffer::bind(&m_vbo);
|
||||
|
||||
states.shader->enableVertexAttribArray("coord3d");
|
||||
void Chunk::drawOutlines(RenderTarget &target, RenderStates states) const {
|
||||
states.texture = nullptr;
|
||||
|
||||
for(u32 i = 0 ; i < m_verticesCount ; i += 4) {
|
||||
glDrawArrays(GL_LINE_LOOP, i, 4);
|
||||
target.draw(m_vbo, GL_LINE_LOOP, i, 4, states);
|
||||
}
|
||||
|
||||
states.shader->disableVertexAttribArray("coord3d");
|
||||
|
||||
VertexBuffer::bind(nullptr);
|
||||
Shader::bind(nullptr);
|
||||
}
|
||||
|
||||
|
@ -13,10 +13,8 @@
|
||||
*/
|
||||
#include "Chunk.hpp"
|
||||
#include "ChunkBuilder.hpp"
|
||||
#include "Vertex.hpp"
|
||||
|
||||
std::size_t ChunkBuilder::buildChunk(const Chunk &chunk, const VertexBuffer &vbo) {
|
||||
static const float cubeCoords[6 * 4 * 3] = {
|
||||
static const float cubeCoords[6 * 4 * 3] = {
|
||||
// Left
|
||||
0, 0, 0,
|
||||
0, 0, 1,
|
||||
@ -52,13 +50,10 @@ std::size_t ChunkBuilder::buildChunk(const Chunk &chunk, const VertexBuffer &vbo
|
||||
1, 0, 1,
|
||||
1, 1, 1,
|
||||
0, 1, 1,
|
||||
};
|
||||
};
|
||||
|
||||
std::vector<Vertex> vertices;
|
||||
vertices.reserve(Chunk::width * Chunk::height * Chunk::depth * 6 * 4);
|
||||
|
||||
// Needed in the loop (avoid a lot of glm::vec3 creation)
|
||||
glm::vec3 a, b, c, v1, v2, normal;
|
||||
std::size_t ChunkBuilder::buildChunk(const Chunk &chunk, const VertexBuffer &vbo) {
|
||||
m_vertices.reserve(Chunk::width * Chunk::height * Chunk::depth * 6 * 4);
|
||||
|
||||
for(u8 z = 0 ; z < Chunk::depth ; z++) {
|
||||
for(u8 y = 0 ; y < Chunk::height ; y++) {
|
||||
@ -76,10 +71,30 @@ std::size_t ChunkBuilder::buildChunk(const Chunk &chunk, const VertexBuffer &vbo
|
||||
};
|
||||
|
||||
for(u8 i = 0 ; i < 6 ; i++) {
|
||||
addFace(x, y, z, i, block, surroundingBlocks[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_vertices.shrink_to_fit();
|
||||
|
||||
VertexBuffer::bind(&vbo);
|
||||
vbo.setData(m_vertices.size() * sizeof(Vertex), m_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
VertexBuffer::bind(nullptr);
|
||||
|
||||
std::size_t verticesCount = m_vertices.size();
|
||||
m_vertices.clear();
|
||||
return verticesCount;
|
||||
}
|
||||
|
||||
void ChunkBuilder::addFace(u8 x, u8 y, u8 z, u8 i, const Block *block, const Block *surroundingBlock) {
|
||||
// Skip hidden faces
|
||||
if(surroundingBlocks[i] && surroundingBlocks[i]->id()
|
||||
&& (surroundingBlocks[i]->isOpaque() || block->id() == surroundingBlocks[i]->id()))
|
||||
continue;
|
||||
if(surroundingBlock && surroundingBlock->id()
|
||||
&& (surroundingBlock->isOpaque() || block->id() == surroundingBlock->id()))
|
||||
return;
|
||||
|
||||
static glm::vec3 a, b, c, v1, v2, normal;
|
||||
|
||||
// Three points of the face
|
||||
a.x = cubeCoords[i * 12 + 0];
|
||||
@ -125,19 +140,7 @@ std::size_t ChunkBuilder::buildChunk(const Chunk &chunk, const VertexBuffer &vbo
|
||||
vertex.texCoord[0] = faceTexCoords[j * 2];
|
||||
vertex.texCoord[1] = faceTexCoords[j * 2 + 1];
|
||||
|
||||
vertices.emplace_back(vertex);
|
||||
m_vertices.emplace_back(vertex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vertices.shrink_to_fit();
|
||||
|
||||
VertexBuffer::bind(&vbo);
|
||||
vbo.setData(vertices.size() * sizeof(Vertex), vertices.data(), GL_DYNAMIC_DRAW);
|
||||
VertexBuffer::bind(nullptr);
|
||||
|
||||
return vertices.size();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user