lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
mesh.hpp
Go to the documentation of this file.
1
13
14#pragma once
15
20
21#include <memory>
22
23namespace lmgl {
24
25namespace scene {
26
34struct Vertex {
35
37 glm::vec3 position;
38
40 glm::vec3 normal;
41
43 glm::vec4 color;
44
46 glm::vec2 uvs;
47
49 glm::vec3 tangent;
50
52 glm::vec3 bitangent;
53
65 Vertex(const glm::vec3 &pos = glm::vec3(0.0f), const glm::vec3 &norm = glm::vec3(0.0f, 1.0f, 0.0f),
66 const glm::vec4 &col = glm::vec4(1.0f), const glm::vec2 &uv = glm::vec2(0.0f))
67 : position(pos), normal(norm), color(col), uvs(uv) {}
68};
69
77class Mesh {
78 public:
88 Mesh(const std::vector<Vertex> &vert, const std::vector<unsigned int> &indices,
89 std::shared_ptr<renderer::Shader> shader);
90
100 Mesh(std::shared_ptr<renderer::VertexArray> vao, std::shared_ptr<renderer::Shader> shader, unsigned int idx_count);
101
103 ~Mesh() = default;
104
110 void bind() const;
111
117 void unbind() const;
118
124 void render() const;
125
133 inline std::shared_ptr<renderer::VertexArray> get_vertex_array() const { return m_vertex_array; }
134
142 inline std::shared_ptr<renderer::Shader> get_shader() const { return m_shader; }
143
151 inline void set_shader(std::shared_ptr<renderer::Shader> shader) { m_shader = shader; }
152
160 inline unsigned int get_index_count() const { return m_index_count; }
161
169 inline const std::vector<Vertex> &get_vertices() const { return m_vertices; }
170
178 inline const std::vector<unsigned int> &get_indices() const { return m_indices; }
179
187 inline bool has_vert_data() const { return !m_vertices.empty() && !m_indices.empty(); }
188
194 inline std::shared_ptr<Material> get_material() const { return m_material; }
195
201 inline void set_material(std::shared_ptr<Material> material) { m_material = material; }
202
210 inline const AABB &get_bounding_box() const { return m_bounding_box; }
211
219 inline const BoundingSphere &get_bounding_sphere() const { return m_bounding_sphere; }
220
221 // Factory Methods
222
232 static std::shared_ptr<Mesh> create_cube(std::shared_ptr<renderer::Shader> shader, unsigned int subdivs = 1);
233
244 static std::shared_ptr<Mesh> create_quad(std::shared_ptr<renderer::Shader> shader, float width = 1.0f,
245 float height = 1.0f);
246
258 static std::shared_ptr<Mesh> create_sphere(std::shared_ptr<renderer::Shader> shader, float radius = 0.5f,
259 unsigned int lonsegs = 32, unsigned int latsegs = 32);
260
261 private:
263 std::shared_ptr<Material> m_material;
264
266 std::shared_ptr<renderer::VertexArray> m_vertex_array;
267
269 std::shared_ptr<renderer::Shader> m_shader;
270
272 unsigned int m_index_count;
273
275 std::vector<Vertex> m_vertices;
276
278 std::vector<unsigned int> m_indices;
279
281 AABB m_bounding_box;
282
284 BoundingSphere m_bounding_sphere;
285
291 void setup_mesh();
292
298 void calculate_bounds();
299};
300
301} // namespace scene
302} // namespace lmgl
const std::vector< Vertex > & get_vertices() const
Getter for vertices.
Definition mesh.hpp:169
void unbind() const
Unbinds the mesh after rendering.
Definition mesh.cpp:60
const BoundingSphere & get_bounding_sphere() const
Getter for bounding sphere.
Definition mesh.hpp:219
bool has_vert_data() const
Check if the mesh has vertex data.
Definition mesh.hpp:187
void set_material(std::shared_ptr< Material > material)
Sets the material associated to the mesh.
Definition mesh.hpp:201
~Mesh()=default
Destructor for the Mesh class.
void set_shader(std::shared_ptr< renderer::Shader > shader)
Setter for shader.
Definition mesh.hpp:151
std::shared_ptr< Material > get_material() const
Retrieve the material associated with the mesh.
Definition mesh.hpp:194
void bind() const
Binds the mesh for rendering.
Definition mesh.cpp:53
const AABB & get_bounding_box() const
Getter for bounding box.
Definition mesh.hpp:210
void render() const
Renders the mesh.
Definition mesh.cpp:67
std::shared_ptr< renderer::VertexArray > get_vertex_array() const
Getter for vertex array.
Definition mesh.hpp:133
static std::shared_ptr< Mesh > create_cube(std::shared_ptr< renderer::Shader > shader, unsigned int subdivs=1)
Creates a cube mesh.
Definition mesh.cpp:69
Mesh(const std::vector< Vertex > &vert, const std::vector< unsigned int > &indices, std::shared_ptr< renderer::Shader > shader)
Constructor for the Mesh class.
Definition mesh.cpp:16
static std::shared_ptr< Mesh > create_sphere(std::shared_ptr< renderer::Shader > shader, float radius=0.5f, unsigned int lonsegs=32, unsigned int latsegs=32)
Creates a sphere mesh.
Definition mesh.cpp:123
std::shared_ptr< renderer::Shader > get_shader() const
Getter for shader.
Definition mesh.hpp:142
const std::vector< unsigned int > & get_indices() const
Getter for indices.
Definition mesh.hpp:178
unsigned int get_index_count() const
Getter for index count.
Definition mesh.hpp:160
static std::shared_ptr< Mesh > create_quad(std::shared_ptr< renderer::Shader > shader, float width=1.0f, float height=1.0f)
Creates a quad mesh.
Definition mesh.cpp:111
Defines frustum and bounding volume structures for 3D scene management.
Definition of the Material class for 3D rendering.
Namespace for scene-related classes and functions.
Definition camera.cpp:7
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12
Declaration of the Shader class for managing shader programs in OpenGL.
Axis-Aligned Bounding Box (AABB) structure.
Definition frustum.hpp:27
Bounding Sphere structure.
Definition frustum.hpp:105
glm::vec2 uvs
Texture coordinates (UVs) of the vertex.
Definition mesh.hpp:46
glm::vec3 position
Position of the vertex in 3D space.
Definition mesh.hpp:37
Vertex(const glm::vec3 &pos=glm::vec3(0.0f), const glm::vec3 &norm=glm::vec3(0.0f, 1.0f, 0.0f), const glm::vec4 &col=glm::vec4(1.0f), const glm::vec2 &uv=glm::vec2(0.0f))
Constructor for the Vertex struct.
Definition mesh.hpp:65
glm::vec3 normal
Normal vector at the vertex.
Definition mesh.hpp:40
glm::vec3 bitangent
Texture bitangetn.
Definition mesh.hpp:52
glm::vec4 color
Color of the vertex.
Definition mesh.hpp:43
glm::vec3 tangent
Texture tangent.
Definition mesh.hpp:49
Declaration of the VertexArray class for managing Vertex Array Objects (VAOs) in OpenGL.