Refactor mesh handling to use templated vertex structures for improved type safety and flexibility

This commit is contained in:
REDxEYE
2025-04-13 17:14:28 +03:00
parent bba647db5c
commit 1ffbeb8148
28 changed files with 591 additions and 521 deletions
+30 -24
View File
@@ -1,30 +1,47 @@
#pragma once
#include <array>
#include <memory>
#include <stdint.h>
#include <cstdint>
#include <glm/glm.hpp>
#include "typedefs.hpp"
#include "maths/UVRegion.hpp"
#include "graphics/core/MeshData.hpp"
template<typename VertexStructure>
class Mesh;
class Texture;
class Chunks;
struct MainBatchVertex {
glm::vec3 position;
glm::vec2 uv;
glm::vec3 tint;
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}
};
};
class MainBatch {
std::unique_ptr<float[]> const buffer;
std::unique_ptr<MainBatchVertex[]> const buffer;
size_t const capacity;
size_t index;
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Mesh<MainBatchVertex>> mesh;
std::unique_ptr<Texture> blank;
const Texture* texture = nullptr;
public:
/// xyz, uv, color, compressed lights
static inline constexpr uint VERTEX_SIZE = 9;
MainBatch(size_t capacity);
@@ -47,27 +64,16 @@ public:
const glm::vec4& light,
const glm::vec3& tint
) {
float* buffer = this->buffer.get();
buffer[index++] = pos.x;
buffer[index++] = pos.y;
buffer[index++] = pos.z;
buffer[index++] = uv.x * region.getWidth() + region.u1;
buffer[index++] = uv.y * region.getHeight() + region.v1;
buffer[index++] = tint.x;
buffer[index++] = tint.y;
buffer[index++] = tint.z;
MainBatchVertex* buffer = this->buffer.get();
buffer[index].position = pos;
buffer[index].uv = {uv.x * region.getWidth() + region.u1,uv.y * region.getHeight() + region.v1};
buffer[index].tint = tint;
union {
float floating;
uint32_t integer;
} compressed;
compressed.integer = (static_cast<uint32_t>(light.r * 255) & 0xff) << 24;
compressed.integer |= (static_cast<uint32_t>(light.g * 255) & 0xff) << 16;
compressed.integer |= (static_cast<uint32_t>(light.b * 255) & 0xff) << 8;
compressed.integer |= (static_cast<uint32_t>(light.a * 255) & 0xff);
buffer[index++] = compressed.floating;
buffer[index].color[0] = static_cast<uint8_t>(light.r * 255);
buffer[index].color[1] = static_cast<uint8_t>(light.g * 255);
buffer[index].color[2] = static_cast<uint8_t>(light.b * 255);
buffer[index].color[3] = static_cast<uint8_t>(light.a * 255);
index++;
}
inline void quad(