Brenta Engine 1.2
Loading...
Searching...
No Matches
mesh.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <brenta/renderer/opengl/buffer.hpp>
9#include <brenta/renderer/opengl/texture.hpp>
10#include <brenta/renderer/opengl/vao.hpp>
11
12#include <glm/vec3.hpp>
13#include <glm/vec2.hpp>
14
15#include <tenno/vector.hpp>
16#include <tenno/memory.hpp>
17
18namespace brenta
19{
20
21//
22// Mesh
23// ----
24//
25// The Mesh class represents a collection of 3D vertices
26//
27class Mesh
28{
29public:
30
31 enum Shape {
32 Triangle,
33 Square,
34 Circle,
35 Pyramid,
36 Cube,
37 Sphere,
38 };
39
40 class Vertex;
41 class Config;
42 class Builder;
43
44 tenno::vector<Vertex> vertices;
45 // Indicies are used to reduce the memory footprint required to
46 // represent a 3D model. They are indices in the vertex array, there
47 // can be multiple indices for the same vertex.
48 tenno::vector<unsigned int> indices;
49 tenno::vector<tenno::shared_ptr<Texture>> textures;
50
51 Mesh(const Config& conf);
52
53 constexpr Mesh(const Mesh&) = delete;
54 constexpr Mesh& operator=(const Mesh&) = delete;
55
56 constexpr Mesh(Mesh&& other) noexcept
57 {
58 this->vao = tenno::move(other.vao);
59 this->vbo = tenno::move(other.vbo);
60 this->ebo = tenno::move(other.ebo);
61 this->vertices = tenno::move(other.vertices);
62 this->indices = tenno::move(other.indices);
63 this->textures = tenno::move(other.textures);
64 this->id = other.id;
65 other.id = 0;
66 }
67 constexpr Mesh& operator=(Mesh&& other) noexcept
68 {
69 this->vao = tenno::move(other.vao);
70 this->vbo = tenno::move(other.vbo);
71 this->ebo = tenno::move(other.ebo);
72 this->vertices = tenno::move(other.vertices);
73 this->indices = tenno::move(other.indices);
74 this->textures = tenno::move(other.textures);
75 this->id = other.id;
76 other.id = 0;
77
78 return *this;
79 }
80 ~Mesh();
81
82 void draw() const;
83
84private:
85
86 Vao vao;
87 Vbo vbo;
88 Ebo ebo;
89
90 unsigned int id = 0;
91
92 void init();
93};
94
95// The Vertex struct represents a vertex of a 3D model
97{
98public:
99
100 glm::vec3 position;
101 glm::vec3 normal;
102 glm::vec2 tex_coords;
103
104 Vertex() = default;
105 Vertex(glm::vec3 position)
106 : position(position) {}
107 Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 tex_coords)
108 : position(position), normal(normal), tex_coords(tex_coords) {}
109
110};
111
113{
114public:
115
116 tenno::vector<Vertex> vertices;
117 tenno::vector<unsigned int> indices;
118 tenno::vector<tenno::shared_ptr<Texture>> textures;
119
120};
121
123{
124private:
125
126 Mesh::Config conf = {};
127
128public:
129
130 Builder &vertices(tenno::vector<Vertex> &&vertices);
131 Builder &indices(tenno::vector<unsigned int> &&indices);
132 Builder &texture(Texture &&texture);
133 Builder &texture(tenno::shared_ptr<Texture> texture);
134 Builder &textures(tenno::vector<tenno::shared_ptr<Texture>> textures);
135 Builder &shape(Mesh::Shape shape);
136
137 Mesh build();
138};
139
140} // namespace brenta