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;
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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -1,29 +1,34 @@
#pragma once
#include "MeshData.hpp"
#include "gl_util.hpp"
template<typename VertexStructure>
template <typename VertexStructure>
Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& 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<typename VertexStructure>
Mesh<VertexStructure>::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 <typename VertexStructure>
Mesh<VertexStructure>::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<VertexStructure>::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<typename VertexStructure>
Mesh<VertexStructure>::~Mesh(){
template <typename VertexStructure>
Mesh<VertexStructure>::~Mesh() {
MeshStats::meshesCount--;
glDeleteVertexArrays(1, &vao);
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->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<typename VertexStructure>
template <typename VertexStructure>
void Mesh<VertexStructure>::draw(unsigned int primitive) const {
MeshStats::drawCalls++;
glBindVertexArray(vao);
@ -85,7 +114,7 @@ void Mesh<VertexStructure>::draw(unsigned int primitive) const {
glBindVertexArray(0);
}
template<typename VertexStructure>
template <typename VertexStructure>
void Mesh<VertexStructure>::draw() const {
draw(GL_TRIANGLES);
}

View File

@ -1,30 +1,36 @@
#pragma once
#include <stdexcept>
#include <GL/glew.h>
#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");
}

View File

@ -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.

View File

@ -2,6 +2,7 @@
#include "commons.hpp"
#include "ImageData.hpp"
#include "MeshData.hpp"
#include <GL/glew.h>
@ -23,4 +24,24 @@ namespace gl {
};
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;
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 {

View File

@ -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 {

View File

@ -3,13 +3,12 @@
#include <vector>
#include <array>
#include <memory>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#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<uint8_t, 4> 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