SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
MeshData.hpp
Go to the documentation of this file.
1#ifndef _MESHDATA_HPP_
2#define _MESHDATA_HPP_
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
8
9namespace Sleak {
10
11 using IndexType = uint32_t;
13
14 /// Standard 96-byte vertex format: position, normal, tangent, color, UV, and up to 4 skinning bone influences.
15 /// @ingroup rendering
16 struct Vertex {
17 float px, py, pz;
18 float nx, ny, nz;
19 float tx, ty, tz, tw;
20 float r, g, b, a;
21 float u, v;
22 int boneIDs[4] = {-1, -1, -1, -1};
23 float boneWeights[4] = {0, 0, 0, 0};
24
25 Vertex() = default;
26
27 Vertex(float px, float py, float pz,
28 float nx, float ny, float nz,
29 float tx, float ty, float tz, float tw,
30 float u = 0.0f, float v = 0.0f)
31 : px(px), py(py), pz(pz),
32 nx(nx), ny(ny), nz(nz),
33 tx(tx), ty(ty), tz(tz), tw(tw),
34 r(1.0f), g(1.0f), b(1.0f), a(1.0f),
35 u(u), v(v) {
36 }
37
38 void SetPosition(float x, float y, float z) {
39 px = x; py = y; pz = z;
40 }
41
42 void SetNormal(float x, float y, float z) {
43 nx = x; ny = y; nz = z;
44 }
45
46 void SetColor(float r, float g, float b, float a) {
47 this->r = r; this->g = g; this->b = b; this->a = a;
48 }
49
50 void SetTexCoord(float u, float v) {
51 this->u = u; this->v = v;
52 }
53
54 static constexpr std::array<size_t, 6> GetAttributeSizes() {
55 return { 3, 3, 4, 2, 4, 4 };
56 }
57
58 static constexpr size_t GetSize() {
59 return sizeof(Vertex);
60 }
61
62 static constexpr std::array<size_t, 6> GetAttributeOffsets() {
63 return {
64 offsetof(Vertex, px),
65 offsetof(Vertex, nx),
66 offsetof(Vertex, r),
67 offsetof(Vertex, u),
68 offsetof(Vertex, boneIDs),
69 offsetof(Vertex, boneWeights)
70 };
71 }
72 };
73
74 /// Growable CPU-side buffer of Vertex, ready to hand to MeshBatch::CreateMesh.
75 /// @ingroup rendering
77 public:
78 VertexGroup() = default;
79
80 void AddVertex(const Vertex& vertex) {
81 vertices.add(vertex);
82 }
83
84 void AddVertex(std::initializer_list<Vertex> vertexList) {
85 vertices.add(vertexList);
86 }
87
88 const Vertex* GetData() const { return vertices.GetData(); }
89 Vertex* GetMutableData() { return const_cast<Vertex*>(vertices.GetData()); }
90 void* GetRawData() { return vertices.GetRawData(); }
91 size_t GetSize() const { return vertices.GetSize(); }
92 size_t GetSizeInBytes() const { return vertices.GetSize() * sizeof(Vertex); }
93
94 void clear() { vertices.clear(); }
95 void release() { vertices.release(); }
96
97 private:
98 Sleak::List<Vertex> vertices;
99 };
100
101 /// CPU-side vertex and index buffers for one mesh, ready for upload.
102 /// @ingroup rendering
107
108}
109
110#endif
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:20
VertexGroup()=default
Vertex * GetMutableData()
Definition MeshData.hpp:89
void AddVertex(const Vertex &vertex)
Definition MeshData.hpp:80
void * GetRawData()
Definition MeshData.hpp:90
void AddVertex(std::initializer_list< Vertex > vertexList)
Definition MeshData.hpp:84
size_t GetSize() const
Definition MeshData.hpp:91
size_t GetSizeInBytes() const
Definition MeshData.hpp:92
const Vertex * GetData() const
Definition MeshData.hpp:88
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
uint32_t IndexType
Definition MeshData.hpp:11
List< IndexType > IndexGroup
Definition MeshData.hpp:12
IndexGroup indices
Definition MeshData.hpp:105
VertexGroup vertices
Definition MeshData.hpp:104
static constexpr std::array< size_t, 6 > GetAttributeOffsets()
Definition MeshData.hpp:62
Vertex(float px, float py, float pz, float nx, float ny, float nz, float tx, float ty, float tz, float tw, float u=0.0f, float v=0.0f)
Definition MeshData.hpp:27
static constexpr std::array< size_t, 6 > GetAttributeSizes()
Definition MeshData.hpp:54
static constexpr size_t GetSize()
Definition MeshData.hpp:58
void SetColor(float r, float g, float b, float a)
Definition MeshData.hpp:46
void SetTexCoord(float u, float v)
Definition MeshData.hpp:50
Vertex()=default
void SetNormal(float x, float y, float z)
Definition MeshData.hpp:42
void SetPosition(float x, float y, float z)
Definition MeshData.hpp:38
float boneWeights[4]
Definition MeshData.hpp:23
int boneIDs[4]
Definition MeshData.hpp:22