remove GLEW from MeshData.hpp & format Mesh.inl

This commit is contained in:
MihailRis 2025-04-27 00:35:54 +03:00
parent ec0dbae3f7
commit 47b7472231
10 changed files with 125 additions and 70 deletions

View File

@ -17,11 +17,10 @@ struct Batch2DVertex {
glm::vec4 color; glm::vec4 color;
static constexpr VertexAttribute ATTRIBUTES[] { static constexpr VertexAttribute ATTRIBUTES[] {
{GL_FLOAT, false, 2}, {VertexAttribute::Type::FLOAT, false, 2},
{GL_FLOAT, false,2}, {VertexAttribute::Type::FLOAT, false, 2},
{GL_FLOAT, false, 4}, {VertexAttribute::Type::FLOAT, false, 4},
{0} {{}, 0}};
};
}; };
class Batch2D : public Flushable { class Batch2D : public Flushable {

View File

@ -19,11 +19,10 @@ struct Batch3DVertex {
glm::vec4 color; glm::vec4 color;
static constexpr VertexAttribute ATTRIBUTES[] { static constexpr VertexAttribute ATTRIBUTES[] {
{GL_FLOAT, false, 3}, {VertexAttribute::Type::FLOAT, false, 3},
{GL_FLOAT, false, 2}, {VertexAttribute::Type::FLOAT, false, 2},
{GL_FLOAT, false, 4}, {VertexAttribute::Type::FLOAT, false, 4},
{0} {{}, 0}};
};
}; };
class Batch3D : public Flushable { class Batch3D : public Flushable {

View File

@ -14,7 +14,10 @@ struct LineVertex {
glm::vec3 position; glm::vec3 position;
glm::vec4 color; 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 { class LineBatch : public Flushable {

View File

@ -1,29 +1,34 @@
#pragma once #pragma once
#include "MeshData.hpp" #include "MeshData.hpp"
#include "gl_util.hpp"
template<typename VertexStructure> template <typename VertexStructure>
Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& data) Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& data)
: Mesh(data.vertices.data(), : Mesh(
data.vertices.data(),
data.vertices.size(), data.vertices.size(),
data.indices.data(), data.indices.data(),
data.indices.size(), data.indices.size(),
data.attrs.data()) {} data.attrs.data()
) {
}
template<typename VertexStructure> template <typename VertexStructure>
Mesh<VertexStructure>::Mesh(const VertexStructure* vertexBuffer, size_t vertices, const uint32_t* indexBuffer, size_t indices, const VertexAttribute* attrs) : Mesh<VertexStructure>::Mesh(
vao(0), const VertexStructure* vertexBuffer,
vbo(0), size_t vertices,
ibo(0), const uint32_t* indexBuffer,
vertexCount(0), size_t indices,
indexCount(0) const VertexAttribute* attrs
{ )
: vao(0), vbo(0), ibo(0), vertexCount(0), indexCount(0) {
MeshStats::meshesCount++; MeshStats::meshesCount++;
vertexSize = 0; vertexSize = 0;
for (int i = 0; attrs[i].count; i++) { for (int i = 0; attrs[i].count; i++) {
vertexSize += attrs[i].size(); vertexSize += attrs[i].size();
} }
if(vertexSize!=sizeof(VertexStructure)){ if (vertexSize != sizeof(VertexStructure)) {
throw std::runtime_error("Vertex size mismatch!"); throw std::runtime_error("Vertex size mismatch!");
} }
@ -35,45 +40,69 @@ Mesh<VertexStructure>::Mesh(const VertexStructure* vertexBuffer, size_t vertices
// attributes // attributes
int offset = 0; int offset = 0;
for (int i = 0; attrs[i].count; i++) { for (int i = 0; attrs[i].count; i++) {
const VertexAttribute &attr = attrs[i]; const VertexAttribute& attr = attrs[i];
glVertexAttribPointer(i, attr.count, attr.type, attr.normalized, sizeof(VertexStructure), (GLvoid*)(size_t)offset); glVertexAttribPointer(
i,
attr.count,
gl::to_glenum(attr.type),
attr.normalized,
sizeof(VertexStructure),
(GLvoid*)(size_t)offset
);
glEnableVertexAttribArray(i); glEnableVertexAttribArray(i);
offset += attr.size(); offset += attr.size();
} }
glBindVertexArray(0); glBindVertexArray(0);
} }
template<typename VertexStructure>
Mesh<VertexStructure>::~Mesh(){ template <typename VertexStructure>
Mesh<VertexStructure>::~Mesh() {
MeshStats::meshesCount--; MeshStats::meshesCount--;
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &vbo);
if (ibo != 0) glDeleteBuffers(1, &ibo); if (ibo != 0) {
glDeleteBuffers(1, &ibo);
}
} }
template<typename VertexStructure>
void Mesh<VertexStructure>::reload(const VertexStructure *vertexBuffer, size_t vertexCount, const uint32_t *indexBuffer, size_t indexCount) { template <typename VertexStructure>
void Mesh<VertexStructure>::reload(
const VertexStructure* vertexBuffer,
size_t vertexCount,
const uint32_t* indexBuffer,
size_t indexCount
) {
this->vertexCount = vertexCount; this->vertexCount = vertexCount;
this->indexCount = indexCount; this->indexCount = indexCount;
glBindVertexArray(vao); glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
if (vertexBuffer != nullptr && vertexCount != 0) { 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 { } else {
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW);
} }
if (indexBuffer != nullptr && indexCount != 0) { if (indexBuffer != nullptr && indexCount != 0) {
if (ibo == 0) if (ibo == 0) glGenBuffers(1, &ibo);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 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) { } else if (ibo != 0) {
glDeleteBuffers(1, &ibo); glDeleteBuffers(1, &ibo);
} }
} }
template<typename VertexStructure> template <typename VertexStructure>
void Mesh<VertexStructure>::draw(unsigned int primitive) const { void Mesh<VertexStructure>::draw(unsigned int primitive) const {
MeshStats::drawCalls++; MeshStats::drawCalls++;
glBindVertexArray(vao); glBindVertexArray(vao);
@ -85,7 +114,7 @@ void Mesh<VertexStructure>::draw(unsigned int primitive) const {
glBindVertexArray(0); glBindVertexArray(0);
} }
template<typename VertexStructure> template <typename VertexStructure>
void Mesh<VertexStructure>::draw() const { void Mesh<VertexStructure>::draw() const {
draw(GL_TRIANGLES); draw(GL_TRIANGLES);
} }

View File

@ -1,30 +1,36 @@
#pragma once #pragma once
#include <stdexcept> #include <stdexcept>
#include <GL/glew.h>
#include "typedefs.hpp" #include "typedefs.hpp"
#include "util/Buffer.hpp" #include "util/Buffer.hpp"
/// @brief Vertex attribute info /// @brief Vertex attribute info
struct VertexAttribute { 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; bool normalized = false;
ubyte count = 0; ubyte count = 0;
[[nodiscard]] uint32_t size() const { [[nodiscard]] uint32_t size() const {
switch (type) { switch (type) {
case GL_FLOAT: case Type::FLOAT:
return count * sizeof(GLfloat); return count * sizeof(float);
case GL_UNSIGNED_INT: case Type::UNSIGNED_INT:
case GL_INT: case Type::INT:
return count * sizeof(GLint); return count * sizeof(int32_t);
case GL_UNSIGNED_SHORT: case Type::UNSIGNED_SHORT:
case GL_SHORT: case Type::SHORT:
return count * sizeof(GLshort); return count * sizeof(int16_t);
case GL_UNSIGNED_BYTE: case Type::UNSIGNED_BYTE:
case GL_BYTE: case Type::BYTE:
return count * sizeof(GLbyte); return count * sizeof(int8_t);
default: default:
throw std::runtime_error("VertexAttribute type is not supported"); throw std::runtime_error("VertexAttribute type is not supported");
} }

View File

@ -16,9 +16,8 @@ struct PostProcessingVertex {
glm::vec2 position; glm::vec2 position;
static constexpr VertexAttribute ATTRIBUTES[] { static constexpr VertexAttribute ATTRIBUTES[] {
{GL_FLOAT,false,2}, {VertexAttribute::Type::FLOAT, false, 2},
{0} {{}, 0}};
};
}; };
/// @brief Framebuffer with blitting with shaders. /// @brief Framebuffer with blitting with shaders.

View File

@ -2,6 +2,7 @@
#include "commons.hpp" #include "commons.hpp"
#include "ImageData.hpp" #include "ImageData.hpp"
#include "MeshData.hpp"
#include <GL/glew.h> #include <GL/glew.h>
@ -23,4 +24,24 @@ namespace gl {
}; };
return primitives[static_cast<int>(primitive)]; return primitives[static_cast<int>(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;
}
}
} }

View File

@ -21,12 +21,11 @@ struct MainBatchVertex {
std::array<uint8_t,4> color; std::array<uint8_t,4> color;
static constexpr VertexAttribute ATTRIBUTES[] = { static constexpr VertexAttribute ATTRIBUTES[] = {
{GL_FLOAT, false, 3}, {VertexAttribute::Type::FLOAT, false, 3},
{GL_FLOAT, false, 2}, {VertexAttribute::Type::FLOAT, false, 2},
{GL_FLOAT, false, 3}, {VertexAttribute::Type::FLOAT, false, 3},
{GL_UNSIGNED_BYTE, true, 4}, {VertexAttribute::Type::UNSIGNED_BYTE, true, 4},
{0} {{}, 0}};
};
}; };
class MainBatch { class MainBatch {

View File

@ -20,7 +20,9 @@ class DrawContext;
struct SkyboxVertex { struct SkyboxVertex {
glm::vec2 position; glm::vec2 position;
static constexpr VertexAttribute ATTRIBUTES[] {{GL_FLOAT,false,2}, {0}}; static constexpr VertexAttribute ATTRIBUTES[] {
{VertexAttribute::Type::FLOAT, false, 2},
{{}, 0}};
}; };
struct skysprite { struct skysprite {

View File

@ -3,13 +3,12 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include <memory> #include <memory>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include "graphics/core/MeshData.hpp" #include "graphics/core/MeshData.hpp"
#include "util/Buffer.hpp" #include "util/Buffer.hpp"
/// @brief Chunk mesh vertex format /// @brief Chunk mesh vertex format
struct ChunkVertex { struct ChunkVertex {
glm::vec3 position; glm::vec3 position;
@ -17,11 +16,10 @@ struct ChunkVertex {
std::array<uint8_t, 4> color; std::array<uint8_t, 4> color;
static constexpr VertexAttribute ATTRIBUTES[] = { static constexpr VertexAttribute ATTRIBUTES[] = {
{GL_FLOAT, false, 3}, {VertexAttribute::Type::FLOAT, false, 3},
{GL_FLOAT, false, 2}, {VertexAttribute::Type::FLOAT, false, 2},
{GL_UNSIGNED_BYTE, true, 4}, {VertexAttribute::Type::UNSIGNED_BYTE, true, 4},
{0} {{}, 0}};
};
}; };
/// @brief Chunk mesh vertex attributes /// @brief Chunk mesh vertex attributes