Loading...
Searching...
No Matches
Transform3D.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 06/04/2025.
3//
4
5#include "hellfire/graphics/Transform3D.h"
6
7#include <glm/gtc/quaternion.hpp>
8
9namespace hellfire {
10 void Transform3D::look_at(const glm::vec3 &target, const glm::vec3 &up) {
11 // Calculate Direction vectors
12 glm::vec3 direction = glm::normalize(target - get_position());
13 glm::vec3 right = glm::normalize(glm::cross(direction, up));
14 glm::vec3 adjusted_up = glm::normalize(glm::cross(right, direction));
15
16 // Create rotation matrix
17 glm::mat4 rotation(1.0f);
18 rotation[0] = glm::vec4(right, 0.0f);
19 rotation[1] = glm::vec4(adjusted_up, 0.0f);
20 rotation[2] = glm::vec4(direction, 0.0f);
21
22 rotation_matrix_ = rotation;
25 }
26
27 void Transform3D::set_rotation(const glm::vec3 &angles) {
28 rotation_x_ = glm::radians(angles.x);
29 rotation_y_ = glm::radians(angles.y);
30 rotation_z_ = glm::radians(angles.z);
31 rotation_in_degrees_ = glm::vec3(glm::degrees(rotation_x_), glm::degrees(rotation_y_), glm::degrees(rotation_z_));
32 use_euler_angles_ = true;
35 }
36
37 void Transform3D::set_rotation_quaternion(const glm::quat &q) {
38 // Convert quaternion to Euler angles (in radians)
39 glm::vec3 euler = glm::eulerAngles(q);
40
41 // Convert to degrees if your engine uses degrees
42 glm::vec3 euler_degrees = glm::degrees(euler);
43
44 // Set rotation using your existing method
45 set_rotation(euler_degrees);
46 }
47
48 const glm::vec3& Transform3D::get_rotation() const {
49 return rotation_in_degrees_;
50
51 }
52
53 void Transform3D::extract_euler_angles_(const glm::mat4 &rotation_matrix, float &x, float &y,
54 float &z) {
55 // Extract rotation part (3x3 upper-left submatrix)
56 glm::mat3 rot3(rotation_matrix);
57
58 // Handle gimbal lock case
59 if (std::abs(rot3[0][2]) > 0.998f) {
60 // Gimbal lock - singularity at pitch = +/-90 degrees
61 y = atan2f(-rot3[2][0], rot3[0][0]) * (rot3[0][2] > 0 ? -1.0f : 1.0f);
62 x = (rot3[0][2] > 0 ? -1.0f : 1.0f) * glm::half_pi<float>();
63 z = 0.0f;
64 } else {
65 // Standard case
66 y = atan2f(rot3[0][2], rot3[2][2]);
67 x = -asinf(rot3[1][2]);
68 z = atan2f(rot3[1][0], rot3[1][1]);
69 }
70 }
71
73 rotation_matrix_ = other.get_rotation_matrix();
76 }
77}
const glm::vec3 & get_rotation() const
void match_orientation(const Transform3D &other)
void extract_euler_angles_(const glm::mat4 &rotation_matrix, float &x, float &y, float &z)
void look_at(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.0f, 1.0f, 0.0f))
void set_rotation(const glm::vec3 &angles)
void set_rotation_quaternion(const glm::quat &q)