Brenta Engine 1.2
Loading...
Searching...
No Matches
transform.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/transform.hpp>
7
8using namespace brenta;
9
10Transform::Transform(double x, double y, double z)
11{
12 this->position = glm::vec3(x, y, z);
13 this->dirty = true;
14 return;
15}
16
17Transform::Transform(glm::vec3 vec)
18{
19 this->position = vec;
20 this->dirty = true;
21 return;
22}
23
24glm::mat4 Transform::get_model_matrix()
25{
26 if (this->dirty)
27 {
28 glm::mat4 T = glm::translate(glm::mat4(1.0f), this->position);
29 glm::mat4 R = glm::mat4_cast(this->rotation);
30 glm::mat4 S = glm::scale(glm::mat4(1.0f), this->scaling);
31
32 this->model_matrix = T * R * S;
33 this->dirty = false;
34 }
35
36 return this->model_matrix;
37}
38
39glm::vec3 Transform::get_pos() const
40{
41 return this->position;
42}
43
44float Transform::get_x() const
45{
46 return this->position.x;
47}
48
49float Transform::get_y() const
50{
51 return this->position.y;
52}
53
54float Transform::get_z() const
55{
56 return this->position.z;
57}
58
59glm::quat Transform::get_rotation() const
60{
61 return this->rotation;
62}
63
64Transform& Transform::set_pos(glm::vec3 new_pos)
65{
66 this->position = new_pos;
67 this->dirty = true;
68 return *this;
69}
70
71Transform& Transform::set_x(float x)
72{
73 this->position.x = x;
74 this->dirty = true;
75 return *this;
76}
77
78Transform& Transform::set_y(float y)
79{
80 this->position.y = y;
81 this->dirty = true;
82 return *this;
83}
84
85Transform& Transform::set_z(float z)
86{
87 this->position.z = z;
88 this->dirty = true;
89 return *this;
90}
91
92Transform& Transform::translate(const glm::vec3& translation)
93{
94 this->position = this->position + translation;
95 this->dirty = true;
96 return *this;
97}
98
99Transform& Transform::rotate(const glm::quat &rotation)
100{
101 this->rotation *= rotation;
102 this->dirty = true;
103 return *this;
104}
105
106Transform& Transform::rotate_x(float degrees)
107{
108 this->rotation *= glm::angleAxis(glm::radians(degrees),
109 glm::vec3(1.0f, 0.0f, 0.0f));
110 this->dirty = true;
111 return *this;
112}
113
114Transform& Transform::rotate_y(float degrees)
115{
116 this->rotation *= glm::angleAxis(glm::radians(degrees),
117 glm::vec3(0.0f, 1.0f, 0.0f));
118 this->dirty = true;
119 return *this;
120}
121
122Transform& Transform::rotate_z(float degrees)
123{
124 this->rotation *= glm::angleAxis(glm::radians(degrees),
125 glm::vec3(0.0f, 0.0f, 1.0f));
126 this->dirty = true;
127 return *this;
128}
129
130Transform& Transform::scale(const glm::vec3& scale)
131{
132 this->scaling = scale;
133 this->dirty = true;
134 return *this;
135}