From a7a447552ee9a4ad13c818a9cd1747e0f515fe94 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Apr 2024 22:50:35 +0300 Subject: [PATCH] idk --- src/frontend/debug_panel.cpp | 1 + src/graphics/core/Mesh.cpp | 104 +++++++++++++++++------------------ src/graphics/core/Mesh.h | 48 ++++++++-------- src/util/stringutil.cpp | 20 +++++++ src/util/stringutil.h | 2 + 5 files changed, 99 insertions(+), 76 deletions(-) diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index 21ff03ad..89bdb28e 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -12,6 +12,7 @@ #include "../physics/Hitbox.h" #include "../util/stringutil.h" #include "../voxels/Block.h" +#include "../voxels/Chunk.h" #include "../voxels/Chunks.h" #include "../world/Level.h" #include "../world/World.h" diff --git a/src/graphics/core/Mesh.cpp b/src/graphics/core/Mesh.cpp index 5b351112..304a6734 100644 --- a/src/graphics/core/Mesh.cpp +++ b/src/graphics/core/Mesh.cpp @@ -4,72 +4,72 @@ int Mesh::meshesCount = 0; Mesh::Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const vattr* attrs) : - ibo(0), - vertices(vertices), - indices(indices) + ibo(0), + vertices(vertices), + indices(indices) { - meshesCount++; - vertexSize = 0; - for (int i = 0; attrs[i].size; i++) { - vertexSize += attrs[i].size; - } + meshesCount++; + vertexSize = 0; + for (int i = 0; attrs[i].size; i++) { + vertexSize += attrs[i].size; + } - glGenVertexArrays(1, &vao); - glGenBuffers(1, &vbo); + glGenVertexArrays(1, &vao); + glGenBuffers(1, &vbo); - reload(vertexBuffer, vertices, indexBuffer, indices); + reload(vertexBuffer, vertices, indexBuffer, indices); - // attributes - int offset = 0; - for (int i = 0; attrs[i].size; i++) { - int size = attrs[i].size; - glVertexAttribPointer(i, size, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (GLvoid*)(offset * sizeof(float))); - glEnableVertexAttribArray(i); - offset += size; - } + // attributes + int offset = 0; + for (int i = 0; attrs[i].size; i++) { + int size = attrs[i].size; + glVertexAttribPointer(i, size, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (GLvoid*)(offset * sizeof(float))); + glEnableVertexAttribArray(i); + offset += size; + } - glBindVertexArray(0); + glBindVertexArray(0); } Mesh::~Mesh(){ - meshesCount--; - glDeleteVertexArrays(1, &vao); - glDeleteBuffers(1, &vbo); - if (ibo != 0) glDeleteBuffers(1, &ibo); + meshesCount--; + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vbo); + if (ibo != 0) glDeleteBuffers(1, &ibo); } void Mesh::reload(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices){ - glBindVertexArray(vao); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - 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; + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + 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); + glBindVertexArray(vao); + if (ibo != 0) { + glDrawElements(primitive, indices, GL_UNSIGNED_INT, 0); + } + else { + glDrawArrays(primitive, 0, vertices); + } + glBindVertexArray(0); } void Mesh::draw() { - draw(GL_TRIANGLES); -} \ No newline at end of file + draw(GL_TRIANGLES); +} diff --git a/src/graphics/core/Mesh.h b/src/graphics/core/Mesh.h index 158f3d46..ccfc6a71 100644 --- a/src/graphics/core/Mesh.h +++ b/src/graphics/core/Mesh.h @@ -5,38 +5,38 @@ #include "../../typedefs.h" struct vattr { - ubyte size; + ubyte size; }; class Mesh { - unsigned int vao; - unsigned int vbo; - unsigned int ibo; - size_t vertices; - size_t indices; - size_t vertexSize; + unsigned int vao; + unsigned int vbo; + unsigned int ibo; + size_t vertices; + size_t indices; + size_t vertexSize; public: - 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(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(); - /// @brief Update GL vertex and index buffers data without changing VAO attributes - /// @param vertexBuffer vertex data buffer - /// @param vertices number of vertices in new buffer - /// @param indexBuffer indices buffer - /// @param indices number of values in indices buffer - void reload(const float* vertexBuffer, size_t vertices, const int* indexBuffer = nullptr, size_t indices = 0); - + /// @brief Update GL vertex and index buffers data without changing VAO attributes + /// @param vertexBuffer vertex data buffer + /// @param vertices number of vertices in new buffer + /// @param indexBuffer indices buffer + /// @param indices number of values in indices buffer + void reload(const float* vertexBuffer, size_t vertices, const int* indexBuffer = nullptr, size_t indices = 0); + /// @brief Draw mesh with specified primitives type - /// @param primitive primitives type - void draw(unsigned int primitive); + /// @param primitive primitives type + void draw(unsigned int primitive); - /// @brief Draw mesh as triangles - void draw(); + /// @brief Draw mesh as triangles + void draw(); - /// @brief Total numbers of alive mesh objects - static int meshesCount; + /// @brief Total numbers of alive mesh objects + static int meshesCount; }; #endif // GRAPHICS_CORE_MESH_H_ diff --git a/src/util/stringutil.cpp b/src/util/stringutil.cpp index 6ae4c437..928e624f 100644 --- a/src/util/stringutil.cpp +++ b/src/util/stringutil.cpp @@ -1,5 +1,6 @@ #include "stringutil.h" +#include #include #include #include @@ -424,3 +425,22 @@ std::vector util::split(const std::wstring& str, char delimiter) { } return result; } + +std::string util::format_data_size(size_t size) { + if (size < 1024) { + return std::to_string(size)+" B"; + } + const std::string postfixes[] { + " B", " KiB", " MiB", " GiB", " TiB", " EiB", " PiB" + }; + int group = 0; + size_t remainder; + while (size >= 1024) { + group++; + remainder = size % 1024; + size /= 1024; + } + return std::to_string(size)+"."+ + std::to_string(static_cast(round(remainder/1024.0f)))+ + postfixes[group]; +} diff --git a/src/util/stringutil.h b/src/util/stringutil.h index d3bb4772..bc67b5a9 100644 --- a/src/util/stringutil.h +++ b/src/util/stringutil.h @@ -54,6 +54,8 @@ namespace util { extern std::vector split(const std::string& str, char delimiter); extern std::vector split(const std::wstring& str, char delimiter); + + extern std::string format_data_size(size_t size); } #endif // UTIL_STRINGUTIL_H_