Symphony Of Empires
mesh.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // mesh.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <vector>
29 #include <string>
30 #include <memory>
31 #include <array>
32 
33 #include <glm/vec3.hpp>
34 #include <glm/vec2.hpp>
35 
37 namespace Eng3D::OpenGL {
38  class VAO {
39  unsigned int id;
40  public:
41  VAO(unsigned int _id);
42  VAO();
43  ~VAO();
44  VAO(const VAO&) = delete;
45  VAO(VAO&& rhs) noexcept;
46  VAO& operator=(VAO& rhs);
47  void bind() const;
48  unsigned int get_id() const;
49  };
50 
51  class VBO {
52  unsigned int id;
53  public:
54  VBO(unsigned int _id);
55  VBO();
56  ~VBO();
57  VBO(const VBO&) = delete;
58  VBO(VBO&& rhs) noexcept;
59  VBO& operator=(VBO& rhs);
60  void bind() const;
61  unsigned int get_id() const;
62  };
63 
64  class EBO {
65  unsigned int id;
66  public:
67  EBO(unsigned int _id);
68  EBO();
69  ~EBO();
70  EBO(const EBO&) = delete;
71  EBO(EBO&& rhs) noexcept;
72  EBO& operator=(EBO& rhs);
73  void bind() const;
74  unsigned int get_id() const;
75  };
76 
77  class Program;
78 }
79 
80 namespace Eng3D {
81  enum class MeshMode {
84  TRIANGLES,
85  LINES,
86  LINE_STRIP,
87  };
88 
89  void draw(const Eng3D::OpenGL::VAO& vao, MeshMode mode, const void* indices, size_t n_indices, const void* buffer, size_t n_buffer, int instances);
90  void upload(const Eng3D::OpenGL::VAO& vao, const Eng3D::OpenGL::VBO& vbo, const Eng3D::OpenGL::EBO& ebo, const void *buffer, size_t n_buffer, size_t sz_buffer, size_t sz_buffer_vert, size_t sz_buffer_tex, size_t v_len, size_t t_len, size_t c_len, const void *indices, size_t n_indices, size_t sz_indices, bool has_color);
91  void instancing(const Eng3D::OpenGL::VAO& vao, const Eng3D::OpenGL::VBO& i_vbo, const void* buffer, size_t index_size, int instances);
92 
93  template<typename V = glm::vec3, typename T = glm::vec2, typename C = void>
94  struct MeshData {
95  constexpr static bool has_color = true;
96 
97  constexpr MeshData() = default;
98  constexpr MeshData(V _vert, T _tex)
99  : vert{ _vert },
100  tex{ _tex }
101  {
102 
103  }
104 
105  constexpr MeshData(V _vert, T _tex, C _color)
106  : vert{ _vert },
107  tex{ _tex },
108  color{ _color }
109  {
110 
111  }
112  ~MeshData() = default;
113  MeshData(const MeshData&) = delete;
114  MeshData(MeshData&&) noexcept = default;
115  MeshData& operator=(const MeshData&) = default;
116 
117  V vert;
118  T tex;
120  };
121 
122  template<typename V, typename T>
123  struct MeshData<V, T, void> {
124  constexpr static bool has_color = false;
125 
126  constexpr MeshData() = default;
127  constexpr MeshData(V _vert, T _tex)
128  : vert{ _vert },
129  tex{ _tex }
130  {
131 
132  }
133  ~MeshData() = default;
134  MeshData(const MeshData&) = delete;
135  MeshData(MeshData&&) noexcept = default;
136  MeshData& operator=(const MeshData&) = default;
137 
138  V vert;
139  T tex;
140  };
141 
143  template<typename V = glm::vec3, typename T = glm::vec2, typename C = void>
144  struct Mesh {
146 
147  Mesh(enum Eng3D::MeshMode _mode)
148  : mode{ _mode }
149  {
150 
151  }
152  virtual ~Mesh() = default;
153  Mesh(const Mesh&) = delete;
154  Mesh(Mesh&&) noexcept = default;
155  Mesh& operator=(const Mesh&) = delete;
156 
157  void draw(int instances = 0) const {
158  Eng3D::draw(vao, mode, indices.data(), indices.size(), buffer.data(), buffer.size(), instances);
159  }
160 
161  virtual void upload() const {
162  size_t c_len = 0;
163  if constexpr(DataType::has_color) c_len += C::length();
164  Eng3D::upload(vao, vbo, ebo, buffer.data(), buffer.size(), sizeof(buffer[0]), sizeof(buffer[0].vert), sizeof(buffer[0].tex), V::length(), T::length(), c_len, indices.data(), indices.size(), sizeof(indices), DataType::has_color);
165  }
166 
170  template<typename I = glm::vec2>
171  void instancing(I& instances_buffer, int instances = 0) {
172  Eng3D::instancing(vao, i_vbo, &instances_buffer, sizeof(I), instances);
173  }
174 
175  std::vector<DataType> buffer;
176  std::vector<unsigned int> indices;
177  enum Eng3D::MeshMode mode;
178 #if defined E3D_BACKEND_OPENGL || defined E3D_BACKEND_GLES
179  // The initialization should be done in this order, first the VAO
180  // then initialize the VBO!
181  Eng3D::OpenGL::VAO vao;
182  Eng3D::OpenGL::VBO vbo;
183  Eng3D::OpenGL::EBO ebo;
184  Eng3D::OpenGL::VBO i_vbo; // Instanced VBO, optional
185 #else
186 # error not implemented
187 #endif
188  };
189 
191  template<std::size_t vtc_size = 0, std::size_t i_size = 0, typename V = glm::vec3, typename T = glm::vec2, typename C = void>
192  struct MeshStatic {
194 
196  : mode{ _mode }
197  {
198 
199  }
200  virtual ~MeshStatic() = default;
201  MeshStatic(const MeshStatic&) = delete;
202  MeshStatic(MeshStatic&&) noexcept = default;
203  MeshStatic& operator=(const MeshStatic&) = delete;
204 
205  void draw(int instances = 0) const {
206  Eng3D::draw(vao, mode, indices.data(), indices.size(), buffer.data(), buffer.size(), instances);
207  }
208 
209  virtual void upload() const {
210  size_t c_len = 0;
211  if constexpr(DataType::has_color) c_len += C::length();
212  Eng3D::upload(vao, vbo, ebo, buffer.data(), buffer.size(), sizeof(buffer[0]), sizeof(buffer[0].vert), sizeof(buffer[0].tex), V::length(), T::length(), c_len, indices.data(), indices.size(), sizeof(indices), DataType::has_color);
213  }
214 
218  template<typename I = glm::vec2>
219  void instancing(I& instances_buffer, int instances = 0) {
220  Eng3D::instancing(vao, i_vbo, &instances_buffer, sizeof(I), instances);
221  }
222 
223  std::array<DataType, vtc_size> buffer;
224  std::array<unsigned int, i_size> indices;
225  enum Eng3D::MeshMode mode;
226 #if defined E3D_BACKEND_OPENGL || defined E3D_BACKEND_GLES
227  // The initialization should be done in this order, first the VAO
228  // then initialize the VBO!
229  Eng3D::OpenGL::VAO vao;
230  Eng3D::OpenGL::VBO vbo;
231  Eng3D::OpenGL::EBO ebo;
232  Eng3D::OpenGL::VBO i_vbo; // Instanced VBO, optional
233 #else
234 # error not implemented
235 #endif
236  };
237 }
void bind() const
Definition: mesh.cpp:128
EBO & operator=(EBO &rhs)
Definition: mesh.cpp:122
EBO(const EBO &)=delete
unsigned int get_id() const
Definition: mesh.cpp:132
VAO & operator=(VAO &rhs)
Definition: mesh.cpp:56
void bind() const
Definition: mesh.cpp:62
unsigned int get_id() const
Definition: mesh.cpp:66
VAO(const VAO &)=delete
void bind() const
Definition: mesh.cpp:95
VBO & operator=(VBO &rhs)
Definition: mesh.cpp:89
unsigned int get_id() const
Definition: mesh.cpp:99
VBO(const VBO &)=delete
void upload(const Eng3D::OpenGL::VAO &vao, const Eng3D::OpenGL::VBO &vbo, const Eng3D::OpenGL::EBO &ebo, const void *buffer, size_t n_buffer, size_t sz_buffer, size_t sz_buffer_vert, size_t sz_buffer_tex, size_t v_len, size_t t_len, size_t c_len, const void *indices, size_t n_indices, size_t sz_indices, bool has_color)
Definition: mesh.cpp:176
void draw(const Eng3D::OpenGL::VAO &vao, MeshMode mode, const void *indices, size_t n_indices, const void *buffer, size_t n_buffer, int instances)
Definition: mesh.cpp:136
void instancing(const Eng3D::OpenGL::VAO &vao, const Eng3D::OpenGL::VBO &i_vbo, const void *buffer, size_t index_size, int instances)
Definition: mesh.cpp:202
MeshMode
Definition: mesh.hpp:81
#define C
Definition: stb_vorbis.c:5132
MeshData(MeshData &&) noexcept=default
constexpr MeshData(V _vert, T _tex)
Definition: mesh.hpp:127
constexpr MeshData()=default
MeshData(const MeshData &)=delete
constexpr MeshData(V _vert, T _tex, C _color)
Definition: mesh.hpp:105
constexpr MeshData(V _vert, T _tex)
Definition: mesh.hpp:98
constexpr static bool has_color
Definition: mesh.hpp:95
MeshData(MeshData &&) noexcept=default
MeshData(const MeshData &)=delete
~MeshData()=default
constexpr MeshData()=default
Packed model - packs both vertices and texcoords into the same buffer.
Definition: mesh.hpp:144
virtual void upload() const
Definition: mesh.hpp:161
std::vector< DataType > buffer
Definition: mesh.hpp:175
void instancing(I &instances_buffer, int instances=0)
Enables instances on this simple mesh.
Definition: mesh.hpp:171
Mesh(Mesh &&) noexcept=default
virtual ~Mesh()=default
Mesh(const Mesh &)=delete
std::vector< unsigned int > indices
Definition: mesh.hpp:176
Mesh(enum Eng3D::MeshMode _mode)
Definition: mesh.hpp:147
A static mesh with fixed number of elements.
Definition: mesh.hpp:192
MeshStatic(enum Eng3D::MeshMode _mode)
Definition: mesh.hpp:195
void instancing(I &instances_buffer, int instances=0)
Enables instances on this simple mesh.
Definition: mesh.hpp:219
std::array< unsigned int, i_size > indices
Definition: mesh.hpp:224
MeshStatic(MeshStatic &&) noexcept=default
virtual void upload() const
Definition: mesh.hpp:209
MeshStatic(const MeshStatic &)=delete
virtual ~MeshStatic()=default
std::array< DataType, vtc_size > buffer
Definition: mesh.hpp:223