Loading...
Searching...
No Matches
Transform3D.h
Go to the documentation of this file.
1#pragma once
2#include <nlohmann/json.hpp>
3using json = nlohmann::json;
4
5#include <glm/detail/type_vec3.hpp>
6#include <glm/gtc/matrix_transform.hpp>
7
8
9namespace hellfire {
11 public:
13 : position_(0.0f, 0.0f, 0.0f)
14 , scale_(1.0f, 1.0f, 1.0f)
15 , rotation_axis_(0.0f, 0.0f, 0.0f)
16 , rotation_angle_(0.0f)
17 , local_matrix_(1.0f)
18 , world_matrix_(1.0f)
19 , rotation_matrix_(1.0f)
21 , scale_matrix_(1.0f)
24 , use_scale_matrix_(false) {
25 }
26
27 // Position methods
28 glm::vec3 &get_position() { return position_; }
29 const glm::vec3 &get_position() const { return position_; }
30
31 void set_position(const glm::vec3 &new_position) {
32 position_ = new_position;
34 }
35
36 void set_position(const float x, const float y, const float z) {
37 position_ = glm::vec3(x, y, z);
39 }
40
41 // Scale methods
42 void set_scale(const glm::vec3 &new_scale) {
43 scale_ = new_scale;
45 }
46
47
48 void look_at(const glm::vec3 &target, const glm::vec3 &up = glm::vec3(0.0f, 1.0f, 0.0f));
49
50 void match_orientation(const Transform3D &other);
51
52 glm::vec3 get_scale() const { return scale_; }
53 glm::vec3& get_scale() { return scale_; }
54
55 void set_rotation(const glm::vec3 &angles);
56
57 void set_rotation_quaternion(const glm::quat &q);
58
59 float get_rotation_angle() const { return rotation_angle_; }
60 const glm::vec3 &get_rotation_axis() const { return rotation_axis_; }
61
62 // Matrix methods
63 void set_rotation_matrix(const glm::mat4 &rotation_matrix) {
64 rotation_matrix_ = rotation_matrix;
67 }
68
70 return rotation_matrix_;
71 }
72
73 void set_translation_matrix(const glm::mat4 &translation_matrix) {
74 translation_matrix_ = translation_matrix;
77 }
78
80 return translation_matrix_;
81 }
82
84 return scale_matrix_;
85 }
86
87 void set_scale_matrix(const glm::mat4 &scale_matrix) {
88 scale_matrix_ = scale_matrix;
89 use_scale_matrix_ = true;
91 }
92
93 const glm::mat4 &get_local_matrix() const { return local_matrix_; }
94 const glm::mat4 &get_world_matrix() const { return world_matrix_; }
95
96
98 local_matrix_ = glm::mat4(1.0f);
99
101 local_matrix_ = local_matrix_ * translation_matrix_;
102 } else {
103 local_matrix_ = glm::translate(local_matrix_, position_);
104 }
105
107 local_matrix_ = local_matrix_ * rotation_matrix_;
108
109 // Extract and store euler angles for consistency
110 extract_euler_angles_(rotation_matrix_, rotation_x_, rotation_y_, rotation_z_);
111 } else if (use_euler_angles_) {
112 glm::mat4 rot_x = glm::rotate(glm::mat4(1.0f), rotation_x_, glm::vec3(1, 0, 0));
113 glm::mat4 rot_y = glm::rotate(glm::mat4(1.0f), rotation_y_, glm::vec3(0, 1, 0));
114 glm::mat4 rot_z = glm::rotate(glm::mat4(1.0f), rotation_z_, glm::vec3(0, 0, 1));
115 // Combine rotations
116 glm::mat4 rotation_matrix = rot_z * rot_y * rot_x;
117
118 // Apply to local matrix
119 local_matrix_ = local_matrix_ * rotation_matrix;
120 } else if (glm::length(rotation_axis_) > 0.0001f) {
121 glm::vec3 normalized_axis = glm::normalize(rotation_axis_);
122 local_matrix_ = glm::rotate(local_matrix_, rotation_angle_, normalized_axis);
123 }
124
125 if (use_scale_matrix_) {
126 local_matrix_ = local_matrix_ * scale_matrix_;
127 } else {
128 local_matrix_ = glm::scale(local_matrix_, scale_);
129 }
130 }
131
132 void update_world_matrix(const glm::mat4& parent_world_matrix) {
133 world_matrix_ = parent_world_matrix * local_matrix_;
134 }
135
136 // Reset matrices to identity - useful for initialization
138 local_matrix_ = glm::mat4(1.0f);
139 world_matrix_ = glm::mat4(1.0f);
140 // Reset transform components
141 position_ = glm::vec3(0.0f);
142 scale_ = glm::vec3(1.0f);
143 rotation_axis_ = glm::vec3(0.0f);
144 rotation_angle_ = 0.0f;
145 // Reset custom matrices
146 rotation_matrix_ = glm::mat4(1.0f);
147 translation_matrix_ = glm::mat4(1.0f);
148 scale_matrix_ = glm::mat4(1.0f);
149 // Reset flags
150 use_rotation_matrix_ = false;
152 use_scale_matrix_ = false;
153 }
154
155 const glm::vec3& get_rotation() const;
156
157 private:
162
163 glm::mat4 local_matrix_; // Local transform matrix
164 glm::mat4 world_matrix_; // World transform matrix
165
166 // Transform matrices for direct manipulation
170
174 float rotation_x_ = 0.0f;
175 float rotation_y_ = 0.0f;
176 float rotation_z_ = 0.0f;
178 bool use_euler_angles_ = false;
179
180 void extract_euler_angles_(const glm::mat4 &rotation_matrix, float &x, float &y, float &z);
181 };
182}
#define MODEL_LOADER_DEBUG
Definition Entity.h:20
bool is_looking
Definition Entity.h:21
static std::shared_ptr< Texture > load_cached_texture(const std::string &path, TextureType type)
static void process_mesh_vertices(aiMesh *mesh, std::vector< Vertex > &vertices, std::vector< unsigned int > &indices)
static bool is_identity_transform(const aiMatrix4x4 &matrix)
static std::shared_ptr< Mesh > process_mesh(aiMesh *mesh, const aiScene *scene, const std::string &filepath)
static EntityID process_node(Scene *scene, aiNode *node, const aiScene *ai_scene, const std::string &filepath, EntityID parent_id=0)
static std::string create_material_key(const aiMaterial *ai_material, const std::string &filepath, unsigned int material_index)
static void load_material_textures(const aiMaterial *ai_material, Material &material, const aiScene *scene, const std::string &filepath)
static std::vector< std::string > get_texture_search_paths(const std::string &filepath)
static std::string create_mesh_key(aiMesh *mesh, const std::string &filepath)
static std::unordered_map< std::string, std::shared_ptr< Texture > > texture_cache
static bool try_load_external_texture_unified(const std::string &path_str, const std::string &filepath, TextureType type, Material &material)
static bool try_load_embedded_texture_unified(const std::string &path_str, const aiScene *scene, TextureType type, Material &material)
static bool try_load_embedded_texture(const std::string &path_str, const aiScene *scene, TextureType dcr_type, Material &material, const std::string &property_name)
static std::shared_ptr< Material > create_material(const aiMaterial *ai_material, const aiScene *scene, const std::string &filepath)
static std::unordered_map< std::string, std::shared_ptr< Mesh > > mesh_cache
static std::string capitalize_first(const std::string &str)
static std::unordered_map< std::string, std::shared_ptr< Material > > material_cache
static bool try_load_external_texture(const std::string &path_str, const std::string &filepath, TextureType dcr_type, Material &material, const std::string &property_name)
static void debug_scene_info(const aiScene *scene, const std::string &filepath)
static EntityID load_model(Scene *scene, const std::filesystem::path &filepath, unsigned int import_flags=0)
static void load_essential_material_properties(const aiMaterial *ai_material, Material &material)
static void preprocess_materials(const aiScene *scene, const std::string &filepath)
virtual ~Component()=default
virtual void on_added(Entity *owner)
Definition Component.h:16
virtual void on_removed()
Definition Component.h:17
Entity & get_owner() const
Definition Component.h:13
const std::string & get_name() const
Definition Entity.h:46
const TransformComponent * transform() const
Definition Entity.cpp:52
TransformComponent * transform()
Definition Entity.cpp:42
T * add_component(Args &&... args)
const std::vector< ScriptComponent * > & get_script_components() const
Definition Entity.cpp:11
void initialize_scripts() const
Definition Entity.cpp:16
void cleanup_scripts() const
Definition Entity.cpp:30
bool has_component() const
Definition Entity.h:57
bool remove_component()
std::string name_
Definition Entity.h:86
virtual ~Entity()=default
std::vector< ScriptComponent * > script_components_
Definition Entity.h:88
std::unordered_map< std::type_index, std::unique_ptr< Component > > components_
Definition Entity.h:87
void send_event_to_script(const std::string &event_name, void *data=nullptr)
void set_name(const std::string &name)
Definition Entity.h:47
T * get_component() const
Entity(const EntityID id, const std::string &name)
Definition Entity.h:37
void update_scripts(float delta_time) const
Definition Entity.cpp:22
Entity(const std::string &name)
Definition Entity.h:41
void broadcast_event(const std::string &event_name, void *data=nullptr) const
Definition Entity.cpp:36
EntityID id_
Definition Entity.h:85
uint32_t get_id() const
Definition Entity.h:45
static void bind_property(const Material::Property &property, uint32_t shader_program, int &texture_unit)
static void bind_property_to_shader(const Material::Property &property, uint32_t shader_program, int &texture_unit)
static void bind_material(const Material &material)
void set_roughness(float roughness)
Definition Material.h:177
void set_metallic(float metallic)
Definition Material.h:173
void set_opacity(float opacity)
Definition Material.h:181
void set_shininess(float shininess)
Definition Material.h:169
std::shared_ptr< Material > get_material() const
std::shared_ptr< Material > material_
void set_material(const std::shared_ptr< Material > &material)
Manages a collection of entities and their hierarchical relationships.
Definition Scene.h:24
void set_parent(EntityID child_id, EntityID parent_id)
Sets the parent of an entity.
Definition Scene.cpp:105
void update_world_matrices()
Updates world transformation matrices for all entities Propagates transformations through the entity ...
Definition Scene.cpp:168
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
Definition Scene.cpp:81
EntityID create_entity(const std::string &name="GameObject")
Creates a new entity in the scene.
Definition Scene.cpp:29
const glm::vec3 & get_rotation() const
glm::mat4 get_rotation_matrix() const
Definition Transform3D.h:69
const glm::mat4 & get_world_matrix() const
Definition Transform3D.h:94
glm::vec3 rotation_in_degrees_
glm::mat4 get_translation_matrix() const
Definition Transform3D.h:79
void match_orientation(const Transform3D &other)
void set_position(const float x, const float y, const float z)
Definition Transform3D.h:36
const glm::vec3 & get_position() const
Definition Transform3D.h:29
void set_position(const glm::vec3 &new_position)
Definition Transform3D.h:31
void extract_euler_angles_(const glm::mat4 &rotation_matrix, float &x, float &y, float &z)
float get_rotation_angle() const
Definition Transform3D.h:59
void set_scale_matrix(const glm::mat4 &scale_matrix)
Definition Transform3D.h:87
void set_translation_matrix(const glm::mat4 &translation_matrix)
Definition Transform3D.h:73
glm::mat4 translation_matrix_
glm::mat4 get_scale_matrix() const
Definition Transform3D.h:83
void look_at(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.0f, 1.0f, 0.0f))
const glm::vec3 & get_rotation_axis() const
Definition Transform3D.h:60
glm::vec3 get_scale() const
Definition Transform3D.h:52
void update_world_matrix(const glm::mat4 &parent_world_matrix)
const glm::mat4 & get_local_matrix() const
Definition Transform3D.h:93
void set_rotation(const glm::vec3 &angles)
void set_rotation_quaternion(const glm::quat &q)
void set_rotation_matrix(const glm::mat4 &rotation_matrix)
Definition Transform3D.h:63
glm::vec3 & get_scale()
Definition Transform3D.h:53
void set_scale(const glm::vec3 &new_scale)
Definition Transform3D.h:42
glm::vec3 & get_position()
Definition Transform3D.h:28
void look_at(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0, 1, 0))
const glm::vec3 & get_position() const
void set_rotation(const glm::vec3 &eulers)
void update_world_matrix(const glm::mat4 &parent_world_matrix)
void set_position(float x, float y, float z)
const glm::vec3 & get_world_position() const
glm::mat4 get_rotation_matrix() const
void set_scale(float x, float y, float z)
const glm::vec3 & get_rotation() const
void set_rotation(float x, float y, float z)
glm::mat4 get_translation_matrix() const
void set_scale(const glm::vec3 &scale)
void set_scale(glm::vec3 &scale)
const glm::mat4 & get_world_matrix() const
const glm::mat4 & get_local_matrix() const
void set_position(const glm::vec3 &position)
glm::mat4 aiMatrix4x4ToGlm(const aiMatrix4x4 &from)
TextureType
Definition Texture.h:13
constexpr AssetID INVALID_ASSET_ID
Definition Vertex.h:5
static constexpr unsigned int PREVIEW
Definition ModelLoader.h:34
static constexpr unsigned int OPTIMIZED
Definition ModelLoader.h:64
static constexpr unsigned int HIGH_QUALITY
Definition ModelLoader.h:55
static constexpr unsigned int RUNTIME
Definition ModelLoader.h:42