Chunks meshes indexing
This commit is contained in:
parent
45d90cc693
commit
cd3cf70710
@ -17,31 +17,42 @@ using glm::vec4;
|
||||
|
||||
#define VERTEX_SIZE 9
|
||||
|
||||
BlocksRenderer::BlocksRenderer(size_t capacity) : offset(0), capacity(capacity) {
|
||||
buffer = new float[capacity];
|
||||
BlocksRenderer::BlocksRenderer(size_t capacity) : vertexOffset(0), indexOffset(0), indexSize(0), capacity(capacity) {
|
||||
vertexBuffer = new float[capacity];
|
||||
indexBuffer = new int[capacity];
|
||||
voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2);
|
||||
}
|
||||
|
||||
|
||||
BlocksRenderer::~BlocksRenderer() {
|
||||
delete voxelsBuffer;
|
||||
delete[] buffer;
|
||||
delete[] vertexBuffer;
|
||||
delete[] indexBuffer;
|
||||
}
|
||||
|
||||
void BlocksRenderer::vertex(const vec3& coord,
|
||||
float u, float v,
|
||||
const vec4& light) {
|
||||
buffer[offset++] = coord.x;
|
||||
buffer[offset++] = coord.y;
|
||||
buffer[offset++] = coord.z;
|
||||
vertexBuffer[vertexOffset++] = coord.x;
|
||||
vertexBuffer[vertexOffset++] = coord.y;
|
||||
vertexBuffer[vertexOffset++] = coord.z;
|
||||
|
||||
buffer[offset++] = u;
|
||||
buffer[offset++] = v;
|
||||
vertexBuffer[vertexOffset++] = u;
|
||||
vertexBuffer[vertexOffset++] = v;
|
||||
|
||||
buffer[offset++] = light.r;
|
||||
buffer[offset++] = light.g;
|
||||
buffer[offset++] = light.b;
|
||||
buffer[offset++] = light.a;
|
||||
vertexBuffer[vertexOffset++] = light.r;
|
||||
vertexBuffer[vertexOffset++] = light.g;
|
||||
vertexBuffer[vertexOffset++] = light.b;
|
||||
vertexBuffer[vertexOffset++] = light.a;
|
||||
}
|
||||
|
||||
void BlocksRenderer::index(int a, int b, int c, int d, int e, int f) {
|
||||
indexBuffer[indexSize++] = indexOffset + a;
|
||||
indexBuffer[indexSize++] = indexOffset + b;
|
||||
indexBuffer[indexSize++] = indexOffset + c;
|
||||
indexBuffer[indexSize++] = indexOffset + d;
|
||||
indexBuffer[indexSize++] = indexOffset + e;
|
||||
indexBuffer[indexSize++] = indexOffset + f;
|
||||
indexOffset += 4;
|
||||
}
|
||||
|
||||
void BlocksRenderer::face(const vec3& coord, float w, float h,
|
||||
@ -50,17 +61,15 @@ void BlocksRenderer::face(const vec3& coord, float w, float h,
|
||||
const UVRegion& region,
|
||||
const vec4(&lights)[4],
|
||||
const vec4& tint) {
|
||||
if (offset + VERTEX_SIZE * 6 > capacity) {
|
||||
if (vertexOffset + VERTEX_SIZE * 6 > capacity) {
|
||||
overflow = true;
|
||||
return;
|
||||
}
|
||||
vertex(coord, region.u1, region.v1, lights[0] * tint);
|
||||
vertex(coord + axisX * w, region.u2, region.v1, lights[1] * tint);
|
||||
vertex(coord + axisX * w + axisY * h, region.u2, region.v2, lights[2] * tint);
|
||||
|
||||
vertex(coord, region.u1, region.v1, lights[0] * tint);
|
||||
vertex(coord + axisX * w + axisY * h, region.u2, region.v2, lights[2] * tint);
|
||||
vertex(coord + axisY * h, region.u1, region.v2, lights[3] * tint);
|
||||
index(0, 1, 3, 0, 2, 3);
|
||||
}
|
||||
|
||||
void BlocksRenderer::face(const vec3& coord, float w, float h,
|
||||
@ -70,7 +79,7 @@ void BlocksRenderer::face(const vec3& coord, float w, float h,
|
||||
const vec4(&lights)[4],
|
||||
const vec4& tint,
|
||||
bool rotated) {
|
||||
if (offset + VERTEX_SIZE * 6 > capacity) {
|
||||
if (vertexOffset + VERTEX_SIZE * 6 > capacity) {
|
||||
overflow = true;
|
||||
return;
|
||||
}
|
||||
@ -78,19 +87,15 @@ void BlocksRenderer::face(const vec3& coord, float w, float h,
|
||||
vertex(coord, region.u2, region.v1, lights[0] * tint);
|
||||
vertex(coord + axisX * w, region.u2, region.v2, lights[1] * tint);
|
||||
vertex(coord + axisX * w + axisY * h, region.u1, region.v2, lights[2] * tint);
|
||||
|
||||
vertex(coord, region.u2, region.v1, lights[0] * tint);
|
||||
vertex(coord + axisX * w + axisY * h, region.u1, region.v2, lights[2] * tint);
|
||||
vertex(coord + axisY * h, region.u1, region.v1, lights[3] * tint);
|
||||
index(0, 1, 2, 0, 2, 3);
|
||||
}
|
||||
else {
|
||||
vertex(coord, region.u1, region.v1, lights[0] * tint);
|
||||
vertex(coord + axisX * w, region.u2, region.v1, lights[1] * tint);
|
||||
vertex(coord + axisX * w + axisY * h, region.u2, region.v2, lights[2] * tint);
|
||||
|
||||
vertex(coord, region.u1, region.v1, lights[0] * tint);
|
||||
vertex(coord + axisX * w + axisY * h, region.u2, region.v2, lights[2] * tint);
|
||||
vertex(coord + axisY * h, region.u1, region.v2, lights[3] * tint);
|
||||
index(0, 1, 2, 0, 2, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@ -342,12 +347,13 @@ Mesh* BlocksRenderer::render(const Chunk* chunk, int atlas_size, const ChunksSto
|
||||
voxelsBuffer->setPosition(chunk->x * CHUNK_W - 1, 0, chunk->z * CHUNK_D - 1);
|
||||
chunks->getVoxels(voxelsBuffer);
|
||||
overflow = false;
|
||||
offset = 0;
|
||||
vertexOffset = 0;
|
||||
indexOffset = indexSize = 0;
|
||||
const voxel* voxels = chunk->voxels;
|
||||
render(voxels, atlas_size);
|
||||
|
||||
const vattr attrs[]{ {3}, {2}, {4}, {0} };
|
||||
Mesh* mesh = new Mesh(buffer, offset / VERTEX_SIZE, attrs);
|
||||
Mesh* mesh = new Mesh(vertexBuffer, vertexOffset / VERTEX_SIZE, indexBuffer, indexSize, attrs);
|
||||
return mesh;
|
||||
}
|
||||
|
||||
|
||||
@ -15,8 +15,10 @@ class VoxelsVolume;
|
||||
class ChunksStorage;
|
||||
|
||||
class BlocksRenderer {
|
||||
float* buffer;
|
||||
size_t offset;
|
||||
float* vertexBuffer;
|
||||
int* indexBuffer;
|
||||
size_t vertexOffset;
|
||||
size_t indexOffset, indexSize;
|
||||
size_t capacity;
|
||||
|
||||
bool overflow = false;
|
||||
@ -25,6 +27,7 @@ class BlocksRenderer {
|
||||
VoxelsVolume* voxelsBuffer;
|
||||
|
||||
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light);
|
||||
void index(int a, int b, int c, int d, int e, int f);
|
||||
|
||||
void face(const glm::vec3& coord, float w, float h,
|
||||
const glm::vec3& axisX,
|
||||
|
||||
@ -3,7 +3,11 @@
|
||||
|
||||
int Mesh::meshesCount = 0;
|
||||
|
||||
Mesh::Mesh(const float* buffer, size_t vertices, const vattr* attrs) : vertices(vertices){
|
||||
Mesh::Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const vattr* attrs) :
|
||||
vertices(vertices),
|
||||
indices(indices),
|
||||
ibo(0)
|
||||
{
|
||||
meshesCount++;
|
||||
vertexSize = 0;
|
||||
for (int i = 0; attrs[i].size; i++) {
|
||||
@ -13,14 +17,7 @@ Mesh::Mesh(const float* buffer, size_t vertices, const vattr* attrs) : vertices(
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
if (buffer){
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW);
|
||||
} else {
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
reload(vertexBuffer, vertices, indexBuffer, indices);
|
||||
|
||||
// attributes
|
||||
int offset = 0;
|
||||
@ -38,18 +35,38 @@ Mesh::~Mesh(){
|
||||
meshesCount--;
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
glDeleteBuffers(1, &vbo);
|
||||
if (ibo != 0) glDeleteBuffers(1, &ibo);
|
||||
}
|
||||
|
||||
void Mesh::reload(const float* buffer, size_t vertices){
|
||||
void Mesh::reload(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices){
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW);
|
||||
if (vertexBuffer != nullptr && vertices != 0) {
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, vertexBuffer, GL_STATIC_DRAW);
|
||||
}
|
||||
else {
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STATIC_DRAW);
|
||||
}
|
||||
if (indexBuffer != nullptr && indices != 0) {
|
||||
if (ibo == 0) glGenBuffers(1, &ibo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * indices, indexBuffer, GL_STATIC_DRAW);
|
||||
}
|
||||
else if (ibo != 0) {
|
||||
glDeleteBuffers(1, &ibo);
|
||||
}
|
||||
this->vertices = vertices;
|
||||
this->indices = indices;
|
||||
}
|
||||
|
||||
void Mesh::draw(unsigned int primitive){
|
||||
glBindVertexArray(vao);
|
||||
if (ibo != 0) {
|
||||
glDrawElements(primitive, indices, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
else {
|
||||
glDrawArrays(primitive, 0, vertices);
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
@ -11,13 +11,17 @@ struct vattr {
|
||||
class Mesh {
|
||||
unsigned int vao;
|
||||
unsigned int vbo;
|
||||
unsigned int ibo;
|
||||
size_t vertices;
|
||||
size_t indices;
|
||||
size_t vertexSize;
|
||||
public:
|
||||
Mesh(const float* buffer, size_t vertices, const vattr* attrs);
|
||||
Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const vattr* attrs);
|
||||
Mesh(const float* vertexBuffer, size_t vertices, const vattr* attrs) :
|
||||
Mesh(vertexBuffer, vertices, nullptr, 0, attrs) {};
|
||||
~Mesh();
|
||||
|
||||
void reload(const float* buffer, size_t vertices);
|
||||
void reload(const float* vertexBuffer, size_t vertices, const int* indexBuffer = nullptr, size_t indices = 0);
|
||||
void draw(unsigned int primitive);
|
||||
void draw();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user