lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
node.hpp
Go to the documentation of this file.
1
13
14#pragma once
15
16#include "lmgl/scene/light.hpp"
17#include "lmgl/scene/mesh.hpp"
18#include "lmgl/scene/lod.hpp"
19
20#include <glm/glm.hpp>
21#include <glm/gtc/matrix_transform.hpp>
22#include <glm/gtc/quaternion.hpp>
23
24#include <memory>
25#include <string>
26#include <vector>
27
28namespace lmgl {
29
30namespace scene {
31
41class Node : public std::enable_shared_from_this<Node> {
42 public:
43 // Transforms
44
52 Node(const std::string &name = "Node");
53
55 ~Node() = default;
56
64 void set_position(const glm::vec3 &position);
65
73 void set_rotation(const glm::quat &rotation);
74
82 void set_rotation(const glm::vec3 &euler_angles);
83
91 void set_scale(const glm::vec3 &scale);
92
100 void set_scale(float scale);
101
110 inline const glm::vec3 &get_position() const { return m_position; }
111
119 inline const glm::quat &get_rotation() const { return m_rotation; }
120
128 inline const glm::vec3 &get_scale() const { return m_scale; }
129
138 void rotate(float angle, const glm::vec3 &axis);
139
147 glm::vec3 get_euler_angles() const;
148
157 void look_at(const glm::vec3 &target, const glm::vec3 &up = glm::vec3(0.0f, 1.0f, 0.0f));
158
166 inline glm::mat4 get_local_transform() const { return m_local_transform; }
167
175 inline glm::mat4 get_world_transform() const { return m_world_transform; }
176
177 // Hierarchy
178
187 void add_child(std::shared_ptr<Node> child);
188
196 void remove_child(std::shared_ptr<Node> child);
197
205 inline std::shared_ptr<Node> get_parent() const { return m_parent.lock(); }
206
214 inline const std::vector<std::shared_ptr<Node>> &get_children() const { return m_children; }
215
221 void detach_from_parent();
222
232 void update_transform(const glm::mat4 &par_transform = glm::mat4(1.0f));
233
241 inline void set_mesh(std::shared_ptr<Mesh> mesh) { m_mesh = mesh; }
242
250 inline std::shared_ptr<Mesh> get_mesh() const { return m_mesh; }
251
259 inline bool has_mesh() const { return m_mesh != nullptr; }
260
268 inline std::string &get_name() { return m_name; }
269
277 inline void set_name(const std::string &name) { m_name = name; }
278
284 inline std::shared_ptr<Light> get_light() const { return m_light; }
285
291 inline void set_light(std::shared_ptr<Light> light) { m_light = light; }
292
296 bool has_light() const { return m_light != nullptr; }
297
303 inline void set_lod(std::shared_ptr<LOD> lod) { m_lod = lod; };
304
310 inline std::shared_ptr<LOD> get_lod() const { return m_lod; }
311
317 inline bool has_lod() const { return m_lod != nullptr && m_lod->has_levels(); }
318
328 std::shared_ptr<Mesh> get_mesh_for_rendering(const glm::vec3 &camera_pos) const;
329
330 private:
332 std::string m_name;
333
335 glm::vec3 m_position{0.0f, 0.0f, 0.0f};
336
338 glm::quat m_rotation{1.0f, 0.0f, 0.0f, 0.0f};
339
341 glm::vec3 m_scale{1.0f, 1.0f, 1.0f};
342
344 glm::mat4 m_local_transform{1.0f};
345
347 glm::mat4 m_world_transform{1.0f};
348
350 std::weak_ptr<Node> m_parent;
351
353 std::vector<std::shared_ptr<Node>> m_children;
354
356 std::shared_ptr<Mesh> m_mesh;
357
359 std::shared_ptr<Light> m_light;
360
362 std::shared_ptr<LOD> m_lod;
363
370 void update_local_transform();
371};
372
373} // namespace scene
374
375} // namespace lmgl
void set_position(const glm::vec3 &position)
Set the position of the node.
Definition node.cpp:16
Node(const std::string &name="Node")
Constructor for the Node class.
Definition node.cpp:12
glm::mat4 get_local_transform() const
Get the local and world transforms of the node.
Definition node.hpp:166
void set_scale(const glm::vec3 &scale)
Set the scale of the node.
Definition node.cpp:32
void add_child(std::shared_ptr< Node > child)
Manage child nodes.
Definition node.cpp:58
bool has_lod() const
Check if the node has an associated LOD with levels.
Definition node.hpp:317
void update_transform(const glm::mat4 &par_transform=glm::mat4(1.0f))
Update transforms.
Definition node.cpp:90
std::shared_ptr< Light > get_light() const
Retrieves the light associated with the node.
Definition node.hpp:284
void look_at(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.0f, 1.0f, 0.0f))
Make the node look at a target point.
Definition node.cpp:50
void set_mesh(std::shared_ptr< Mesh > mesh)
Manage the mesh associated with the node.
Definition node.hpp:241
const glm::quat & get_rotation() const
Get the rotation of the node.
Definition node.hpp:119
glm::vec3 get_euler_angles() const
Get the Euler angles of the node's rotation.
Definition node.cpp:48
void set_lod(std::shared_ptr< LOD > lod)
Set the LOD (Level of Detail) for the node.
Definition node.hpp:303
const glm::vec3 & get_scale() const
Get the scale of the node.
Definition node.hpp:128
const glm::vec3 & get_position() const
Getters for position, rotation, scale, and transforms.
Definition node.hpp:110
void remove_child(std::shared_ptr< Node > child)
Remove a child node.
Definition node.cpp:67
std::shared_ptr< LOD > get_lod() const
Get the LOD (Level of Detail) associated with the node.
Definition node.hpp:310
void set_rotation(const glm::quat &rotation)
Set the rotation of the node.
Definition node.cpp:21
void rotate(float angle, const glm::vec3 &axis)
Rotate the node around a specified axis.
Definition node.cpp:42
const std::vector< std::shared_ptr< Node > > & get_children() const
Get the list of child nodes.
Definition node.hpp:214
std::shared_ptr< Mesh > get_mesh_for_rendering(const glm::vec3 &camera_pos) const
Get the appropriate mesh for rendering based on camera position.
Definition node.cpp:97
bool has_mesh() const
Check if the node has an associated mesh.
Definition node.hpp:259
void detach_from_parent()
Detach the node from its parent.
Definition node.cpp:77
bool has_light() const
Check if node has a light.
Definition node.hpp:296
void set_light(std::shared_ptr< Light > light)
Set the light associated with the node.
Definition node.hpp:291
void set_name(const std::string &name)
Set the name of the node.
Definition node.hpp:277
std::shared_ptr< Node > get_parent() const
Getters for parent and children.
Definition node.hpp:205
glm::mat4 get_world_transform() const
Get the world transform of the node.
Definition node.hpp:175
std::shared_ptr< Mesh > get_mesh() const
Get the mesh associated with the node.
Definition node.hpp:250
~Node()=default
Destructor for the Node class.
std::string & get_name()
Getters and setters for the node's name.
Definition node.hpp:268
Definition of the Light class and LightType enumeration.
Level of Detail (LOD) management for 3D objects.
Defines the Mesh class for managing 3D mesh data.
Namespace for scene-related classes and functions.
Definition camera.cpp:7
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12