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