我正在创建3D体素引擎,并且想知道如何生成将包含块中所有块的块网格。这对于快速渲染块中的所有块非常重要。
这是我的块类:
const short CHUNK_SIZE = 16;
class Chunk
{
public:
Chunk();
~Chunk();
void setBlock(int x, int y, int z, BlockType type);
Block* getBlock(int x, int y, int z);
void createMesh();
void draw();
private:
Block blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
GLuint verticesBuffer;
int lastVerticesSize;
};
这是一个无效的Chunk :: createMesh()函数:
void Chunk::createMesh()
{
vector<GLfloat> vertices;
for (int x = 0; x < CHUNK_SIZE; x++)
{
for (int y = 0; y < CHUNK_SIZE; y++)
{
for (int z = 0; z < CHUNK_SIZE; z++)
{
Block* block = Chunk::getBlock(x, y, z);
if (block->type != BlockType::BLOCK_AIR)
{
for (int i = 0; i < 180; i += 5)
{
// Here should be adding each vertex to vertices vector
// But I don't know how to do it
}
}
}
}
}
glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
Chunk::lastVerticesSize = vertices.size();
}
这是无效的Chunk :: draw():
void Chunk::draw()
{
glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glDrawArrays(GL_TRIANGLES, 0, lastVerticesSize/3);
}
我希望以块的形式渲染所有块,但实际上它仅渲染第一个块,其他只是不规则渲染的三角形的一部分。