diff --git a/src/graphics/core/Batch2D.hpp b/src/graphics/core/Batch2D.hpp index 03b2a866..6f29a1df 100644 --- a/src/graphics/core/Batch2D.hpp +++ b/src/graphics/core/Batch2D.hpp @@ -17,11 +17,10 @@ struct Batch2DVertex { glm::vec4 color; static constexpr VertexAttribute ATTRIBUTES[] { - {GL_FLOAT, false, 2}, - {GL_FLOAT, false,2}, - {GL_FLOAT, false, 4}, - {0} - }; + {VertexAttribute::Type::FLOAT, false, 2}, + {VertexAttribute::Type::FLOAT, false, 2}, + {VertexAttribute::Type::FLOAT, false, 4}, + {{}, 0}}; }; class Batch2D : public Flushable { diff --git a/src/graphics/core/Batch3D.hpp b/src/graphics/core/Batch3D.hpp index 6cb03ac0..c3fb3775 100644 --- a/src/graphics/core/Batch3D.hpp +++ b/src/graphics/core/Batch3D.hpp @@ -19,11 +19,10 @@ struct Batch3DVertex { glm::vec4 color; static constexpr VertexAttribute ATTRIBUTES[] { - {GL_FLOAT, false, 3}, - {GL_FLOAT, false, 2}, - {GL_FLOAT, false, 4}, - {0} - }; + {VertexAttribute::Type::FLOAT, false, 3}, + {VertexAttribute::Type::FLOAT, false, 2}, + {VertexAttribute::Type::FLOAT, false, 4}, + {{}, 0}}; }; class Batch3D : public Flushable { diff --git a/src/graphics/core/LineBatch.hpp b/src/graphics/core/LineBatch.hpp index 123c5407..eb32e1ee 100644 --- a/src/graphics/core/LineBatch.hpp +++ b/src/graphics/core/LineBatch.hpp @@ -14,7 +14,10 @@ struct LineVertex { glm::vec3 position; glm::vec4 color; - static constexpr VertexAttribute ATTRIBUTES[] { {GL_FLOAT,false,3},{GL_FLOAT,false,4}, {0} }; + static constexpr VertexAttribute ATTRIBUTES[] { + {VertexAttribute::Type::FLOAT, false, 3}, + {VertexAttribute::Type::FLOAT, false, 4}, + {{}, 0}}; }; class LineBatch : public Flushable { diff --git a/src/graphics/core/Mesh.inl b/src/graphics/core/Mesh.inl index 72351dd4..33812958 100644 --- a/src/graphics/core/Mesh.inl +++ b/src/graphics/core/Mesh.inl @@ -1,29 +1,34 @@ #pragma once #include "MeshData.hpp" +#include "gl_util.hpp" -template +template Mesh::Mesh(const MeshData& data) - : Mesh(data.vertices.data(), - data.vertices.size(), - data.indices.data(), - data.indices.size(), - data.attrs.data()) {} + : Mesh( + data.vertices.data(), + data.vertices.size(), + data.indices.data(), + data.indices.size(), + data.attrs.data() + ) { +} -template -Mesh::Mesh(const VertexStructure* vertexBuffer, size_t vertices, const uint32_t* indexBuffer, size_t indices, const VertexAttribute* attrs) : - vao(0), - vbo(0), - ibo(0), - vertexCount(0), - indexCount(0) -{ +template +Mesh::Mesh( + const VertexStructure* vertexBuffer, + size_t vertices, + const uint32_t* indexBuffer, + size_t indices, + const VertexAttribute* attrs +) + : vao(0), vbo(0), ibo(0), vertexCount(0), indexCount(0) { MeshStats::meshesCount++; vertexSize = 0; for (int i = 0; attrs[i].count; i++) { vertexSize += attrs[i].size(); } - if(vertexSize!=sizeof(VertexStructure)){ + if (vertexSize != sizeof(VertexStructure)) { throw std::runtime_error("Vertex size mismatch!"); } @@ -35,45 +40,69 @@ Mesh::Mesh(const VertexStructure* vertexBuffer, size_t vertices // attributes int offset = 0; for (int i = 0; attrs[i].count; i++) { - const VertexAttribute &attr = attrs[i]; - glVertexAttribPointer(i, attr.count, attr.type, attr.normalized, sizeof(VertexStructure), (GLvoid*)(size_t)offset); + const VertexAttribute& attr = attrs[i]; + glVertexAttribPointer( + i, + attr.count, + gl::to_glenum(attr.type), + attr.normalized, + sizeof(VertexStructure), + (GLvoid*)(size_t)offset + ); glEnableVertexAttribArray(i); offset += attr.size(); } glBindVertexArray(0); } -template -Mesh::~Mesh(){ + +template +Mesh::~Mesh() { MeshStats::meshesCount--; glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); - if (ibo != 0) glDeleteBuffers(1, &ibo); + if (ibo != 0) { + glDeleteBuffers(1, &ibo); + } } -template -void Mesh::reload(const VertexStructure *vertexBuffer, size_t vertexCount, const uint32_t *indexBuffer, size_t indexCount) { + +template +void Mesh::reload( + const VertexStructure* vertexBuffer, + size_t vertexCount, + const uint32_t* indexBuffer, + size_t indexCount +) { this->vertexCount = vertexCount; this->indexCount = indexCount; glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); if (vertexBuffer != nullptr && vertexCount != 0) { - glBufferData(GL_ARRAY_BUFFER, vertexCount * vertexSize, vertexBuffer, GL_STREAM_DRAW); + glBufferData( + GL_ARRAY_BUFFER, + vertexCount * vertexSize, + vertexBuffer, + GL_STREAM_DRAW + ); } else { glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW); } if (indexBuffer != nullptr && indexCount != 0) { - if (ibo == 0) - glGenBuffers(1, &ibo); + if (ibo == 0) glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32_t) * indexCount, indexBuffer, GL_STATIC_DRAW); + glBufferData( + GL_ELEMENT_ARRAY_BUFFER, + sizeof(uint32_t) * indexCount, + indexBuffer, + GL_STATIC_DRAW + ); } else if (ibo != 0) { glDeleteBuffers(1, &ibo); } - } -template +template void Mesh::draw(unsigned int primitive) const { MeshStats::drawCalls++; glBindVertexArray(vao); @@ -85,7 +114,7 @@ void Mesh::draw(unsigned int primitive) const { glBindVertexArray(0); } -template +template void Mesh::draw() const { draw(GL_TRIANGLES); } diff --git a/src/graphics/core/MeshData.hpp b/src/graphics/core/MeshData.hpp index 53229584..80f66366 100644 --- a/src/graphics/core/MeshData.hpp +++ b/src/graphics/core/MeshData.hpp @@ -1,30 +1,36 @@ #pragma once #include -#include #include "typedefs.hpp" #include "util/Buffer.hpp" /// @brief Vertex attribute info struct VertexAttribute { - uint32_t type = 0; + enum class Type { + FLOAT, + INT, UNSIGNED_INT, + SHORT, UNSIGNED_SHORT, + BYTE, UNSIGNED_BYTE + }; + + Type type = Type::FLOAT; bool normalized = false; ubyte count = 0; [[nodiscard]] uint32_t size() const { switch (type) { - case GL_FLOAT: - return count * sizeof(GLfloat); - case GL_UNSIGNED_INT: - case GL_INT: - return count * sizeof(GLint); - case GL_UNSIGNED_SHORT: - case GL_SHORT: - return count * sizeof(GLshort); - case GL_UNSIGNED_BYTE: - case GL_BYTE: - return count * sizeof(GLbyte); + case Type::FLOAT: + return count * sizeof(float); + case Type::UNSIGNED_INT: + case Type::INT: + return count * sizeof(int32_t); + case Type::UNSIGNED_SHORT: + case Type::SHORT: + return count * sizeof(int16_t); + case Type::UNSIGNED_BYTE: + case Type::BYTE: + return count * sizeof(int8_t); default: throw std::runtime_error("VertexAttribute type is not supported"); } diff --git a/src/graphics/core/PostProcessing.hpp b/src/graphics/core/PostProcessing.hpp index 7252c239..c6b3ae14 100644 --- a/src/graphics/core/PostProcessing.hpp +++ b/src/graphics/core/PostProcessing.hpp @@ -16,9 +16,8 @@ struct PostProcessingVertex { glm::vec2 position; static constexpr VertexAttribute ATTRIBUTES[] { - {GL_FLOAT,false,2}, - {0} - }; + {VertexAttribute::Type::FLOAT, false, 2}, + {{}, 0}}; }; /// @brief Framebuffer with blitting with shaders. diff --git a/src/graphics/core/gl_util.hpp b/src/graphics/core/gl_util.hpp index 3e34b923..f3446fea 100644 --- a/src/graphics/core/gl_util.hpp +++ b/src/graphics/core/gl_util.hpp @@ -2,6 +2,7 @@ #include "commons.hpp" #include "ImageData.hpp" +#include "MeshData.hpp" #include @@ -23,4 +24,24 @@ namespace gl { }; return primitives[static_cast(primitive)]; } + + inline GLenum to_glenum(VertexAttribute::Type type) { + using Type = VertexAttribute::Type; + switch (type) { + case Type::FLOAT: + return GL_FLOAT; + case Type::UNSIGNED_INT: + return GL_UNSIGNED_INT; + case Type::INT: + return GL_INT; + case Type::UNSIGNED_SHORT: + return GL_UNSIGNED_SHORT; + case Type::SHORT: + return GL_SHORT; + case Type::UNSIGNED_BYTE: + return GL_UNSIGNED_BYTE; + case Type::BYTE: + return GL_BYTE; + } + } } diff --git a/src/graphics/render/MainBatch.hpp b/src/graphics/render/MainBatch.hpp index d5afb569..2ca1d3d0 100644 --- a/src/graphics/render/MainBatch.hpp +++ b/src/graphics/render/MainBatch.hpp @@ -21,12 +21,11 @@ struct MainBatchVertex { std::array color; static constexpr VertexAttribute ATTRIBUTES[] = { - {GL_FLOAT, false, 3}, - {GL_FLOAT, false, 2}, - {GL_FLOAT, false, 3}, - {GL_UNSIGNED_BYTE, true, 4}, - {0} - }; + {VertexAttribute::Type::FLOAT, false, 3}, + {VertexAttribute::Type::FLOAT, false, 2}, + {VertexAttribute::Type::FLOAT, false, 3}, + {VertexAttribute::Type::UNSIGNED_BYTE, true, 4}, + {{}, 0}}; }; class MainBatch { diff --git a/src/graphics/render/Skybox.hpp b/src/graphics/render/Skybox.hpp index 6b01cd1e..f469b155 100644 --- a/src/graphics/render/Skybox.hpp +++ b/src/graphics/render/Skybox.hpp @@ -20,7 +20,9 @@ class DrawContext; struct SkyboxVertex { glm::vec2 position; - static constexpr VertexAttribute ATTRIBUTES[] {{GL_FLOAT,false,2}, {0}}; + static constexpr VertexAttribute ATTRIBUTES[] { + {VertexAttribute::Type::FLOAT, false, 2}, + {{}, 0}}; }; struct skysprite { diff --git a/src/graphics/render/commons.hpp b/src/graphics/render/commons.hpp index 96e53864..3f193500 100644 --- a/src/graphics/render/commons.hpp +++ b/src/graphics/render/commons.hpp @@ -3,13 +3,12 @@ #include #include #include +#include #include #include "graphics/core/MeshData.hpp" #include "util/Buffer.hpp" - - /// @brief Chunk mesh vertex format struct ChunkVertex { glm::vec3 position; @@ -17,11 +16,10 @@ struct ChunkVertex { std::array color; static constexpr VertexAttribute ATTRIBUTES[] = { - {GL_FLOAT, false, 3}, - {GL_FLOAT, false, 2}, - {GL_UNSIGNED_BYTE, true, 4}, - {0} - }; + {VertexAttribute::Type::FLOAT, false, 3}, + {VertexAttribute::Type::FLOAT, false, 2}, + {VertexAttribute::Type::UNSIGNED_BYTE, true, 4}, + {{}, 0}}; }; /// @brief Chunk mesh vertex attributes