GA::kit
v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
transform.h
Go to the documentation of this file.
1
#pragma once
2
#include "
ga/math.h
"
3
4
namespace
ga
{
5
14
class
Transform
15
{
16
17
public
:
18
Transform
()
19
:
m_transform
( 1.f ),
m_translation
( 0.f ),
m_rotation
(),
m_scale
( 1.f )
20
{
21
flagDirty
();
22
}
23
24
Transform
(
const
mat4& transform )
25
{
26
setMatrix
( transform );
27
}
28
29
Transform
(
const
vec3& translation,
const
quat& rotation,
const
vec3&
scale
)
30
:
m_transform
( 1.f ),
m_translation
( translation ),
m_scale
(
scale
),
m_rotation
( rotation )
31
{
32
flagDirty
();
33
}
34
35
// setters
36
37
inline
Transform
&
setMatrix
(
const
mat4& matrix )
38
{
39
m_transform
= matrix;
40
decompose
();
41
return
*
this
;
42
}
43
44
inline
Transform
&
set
(
const
mat4& matrix )
45
{
46
return
setMatrix
( matrix );
47
}
48
49
inline
Transform
&
setTranslation
(
const
vec3& translation )
50
{
51
m_translation
= translation;
52
flagDirty
();
53
return
*
this
;
54
}
55
56
inline
Transform
&
setTranslation
(
const
vec2& translation )
57
{
58
return
setTranslation
( vec3 { translation.x, translation.y,
m_translation
.z } );
59
}
60
61
inline
Transform
&
setRotation
(
const
quat& rotation )
62
{
63
m_rotation
= rotation;
64
flagDirty
();
65
return
*
this
;
66
}
67
68
inline
Transform
&
setScale
(
const
vec3&
scale
)
69
{
70
m_scale
=
scale
;
71
flagDirty
();
72
return
*
this
;
73
}
74
75
inline
Transform
&
setEulerRotation
(
const
vec3& radians )
76
{
77
return
setRotation
( quat( radians ) );
78
}
79
80
// getters
81
82
inline
const
vec3&
getTranslation
()
const
{
return
m_translation
; }
83
inline
const
quat&
getRotation
()
const
{
return
m_rotation
; }
84
inline
const
vec3&
getScale
()
const
{
return
m_scale
; }
85
inline
vec3
getEulerRotation
()
const
{
return
glm::eulerAngles(
m_rotation
); }
86
87
inline
const
mat4&
getMatrix
()
const
88
{
89
clean
();
90
return
m_transform
;
91
}
92
93
// helpers
94
95
inline
Transform
&
translate
(
const
vec3& vec ) {
return
setTranslation
(
m_translation
+ vec ); }
96
inline
Transform
&
translate
(
const
vec2& vec ) {
return
setTranslation
( {
m_translation
.x + vec.x,
m_translation
.y + vec.y,
m_translation
.z } ); }
97
inline
Transform
&
translateX
(
float
x ) {
return
setTranslation
( {
m_translation
.x + x,
m_translation
.y,
m_translation
.z } ); }
98
inline
Transform
&
translateY
(
float
y ) {
return
setTranslation
( {
m_translation
.x,
m_translation
.y + y,
m_translation
.z } ); }
99
inline
Transform
&
translateZ
(
float
z ) {
return
setTranslation
( {
m_translation
.x,
m_translation
.y,
m_translation
.z + z } ); }
100
inline
Transform
&
scale
(
const
vec3& pct ) {
return
setScale
(
m_scale
* pct ); }
101
inline
Transform
&
scale
(
float
pct ) {
return
setScale
(
m_scale
* pct ); }
102
inline
Transform
&
rotate
(
const
vec3& eulerRadians ) {
return
setEulerRotation
( eulerRadians + glm::eulerAngles(
m_rotation
) ); }
103
inline
Transform
&
rotateAround
(
const
vec3& axis,
float
radians ) {
return
rotate
( axis * radians ); }
104
inline
Transform
&
rotateX
(
float
radians ) {
return
rotateAround
( { 1, 0, 0 }, radians ); }
105
inline
Transform
&
rotateY
(
float
radians ) {
return
rotateAround
( { 0, 1, 0 }, radians ); }
106
inline
Transform
&
rotateZ
(
float
radians ) {
return
rotateAround
( { 0, 0, 1 }, radians ); }
107
108
// implicit conversion to mat4
109
110
inline
operator
const
mat4&()
const
{
return
getMatrix
(); }
111
inline
Transform
&
operator=
(
const
mat4& m ) {
return
setMatrix
( m ); }
112
inline
Transform
operator*
(
const
mat4& m ) {
return
Transform
(
getMatrix
() * m ); }
113
inline
Transform
operator*
(
const
Transform
& t ) {
return
Transform
(
getMatrix
() * t.
getMatrix
() ); }
114
115
protected
:
116
vec3
m_translation
;
117
vec3
m_scale
;
118
quat
m_rotation
;
119
mat4
m_transform
;
120
bool
m_dirty
;
// matrix needs rebuilding
121
122
inline
void
flagDirty
() {
m_dirty
=
true
; }
123
124
inline
void
clean
()
const
125
{
126
if
(
m_dirty
) {
127
// hack to keep internal matrix synced while keeping const-ness
128
auto
* self =
const_cast<
Transform
*
>
( this );
129
// rebuild model matrix - translate, rotate, scale
130
self->
m_transform
= glm::translate( mat4( 1.0 ),
m_translation
);
131
self->m_transform =
m_transform
* glm::toMat4(
m_rotation
);
132
self->m_transform = glm::scale(
m_transform
,
m_scale
);
133
self->m_dirty =
false
;
134
}
135
}
136
137
inline
void
decompose
()
138
{
139
// decomposes the internal transform matrix into translation, rotation, scale components
140
vec3 skew;
141
vec4 perspective;
// unused here but needed by the method signature
142
glm::decompose(
m_transform
,
m_scale
,
m_rotation
,
m_translation
, skew, perspective );
143
m_dirty
=
false
;
144
}
145
};
146
147
}
// namespace ga
ga::Transform
Transform represents a basic model matrix: translate * rotate * scale Stored internally as ga::mat4 (...
Definition:
transform.h:15
ga::Transform::setMatrix
Transform & setMatrix(const mat4 &matrix)
Definition:
transform.h:37
ga::Transform::flagDirty
void flagDirty()
Definition:
transform.h:122
ga::Transform::rotateAround
Transform & rotateAround(const vec3 &axis, float radians)
Definition:
transform.h:103
ga::Transform::translateX
Transform & translateX(float x)
Definition:
transform.h:97
ga::Transform::getMatrix
const mat4 & getMatrix() const
Definition:
transform.h:87
ga::Transform::setScale
Transform & setScale(const vec3 &scale)
Definition:
transform.h:68
ga::Transform::m_rotation
quat m_rotation
Definition:
transform.h:118
ga::Transform::setTranslation
Transform & setTranslation(const vec2 &translation)
Definition:
transform.h:56
ga::Transform::translate
Transform & translate(const vec2 &vec)
Definition:
transform.h:96
ga::Transform::Transform
Transform()
Definition:
transform.h:18
ga::Transform::operator*
Transform operator*(const mat4 &m)
Definition:
transform.h:112
ga::Transform::decompose
void decompose()
Definition:
transform.h:137
ga::Transform::setRotation
Transform & setRotation(const quat &rotation)
Definition:
transform.h:61
ga::Transform::m_translation
vec3 m_translation
Definition:
transform.h:116
ga::Transform::Transform
Transform(const vec3 &translation, const quat &rotation, const vec3 &scale)
Definition:
transform.h:29
ga::Transform::getScale
const vec3 & getScale() const
Definition:
transform.h:84
ga::Transform::getRotation
const quat & getRotation() const
Definition:
transform.h:83
ga::Transform::m_transform
mat4 m_transform
Definition:
transform.h:119
ga::Transform::getEulerRotation
vec3 getEulerRotation() const
Definition:
transform.h:85
ga::Transform::m_dirty
bool m_dirty
Definition:
transform.h:120
ga::Transform::translateZ
Transform & translateZ(float z)
Definition:
transform.h:99
ga::Transform::rotateZ
Transform & rotateZ(float radians)
Definition:
transform.h:106
ga::Transform::translate
Transform & translate(const vec3 &vec)
Definition:
transform.h:95
ga::Transform::setEulerRotation
Transform & setEulerRotation(const vec3 &radians)
Definition:
transform.h:75
ga::Transform::translateY
Transform & translateY(float y)
Definition:
transform.h:98
ga::Transform::getTranslation
const vec3 & getTranslation() const
Definition:
transform.h:82
ga::Transform::rotate
Transform & rotate(const vec3 &eulerRadians)
Definition:
transform.h:102
ga::Transform::rotateX
Transform & rotateX(float radians)
Definition:
transform.h:104
ga::Transform::m_scale
vec3 m_scale
Definition:
transform.h:117
ga::Transform::set
Transform & set(const mat4 &matrix)
Definition:
transform.h:44
ga::Transform::rotateY
Transform & rotateY(float radians)
Definition:
transform.h:105
ga::Transform::scale
Transform & scale(float pct)
Definition:
transform.h:101
ga::Transform::operator*
Transform operator*(const Transform &t)
Definition:
transform.h:113
ga::Transform::Transform
Transform(const mat4 &transform)
Definition:
transform.h:24
ga::Transform::clean
void clean() const
Definition:
transform.h:124
ga::Transform::operator=
Transform & operator=(const mat4 &m)
Definition:
transform.h:111
ga::Transform::setTranslation
Transform & setTranslation(const vec3 &translation)
Definition:
transform.h:49
ga::Transform::scale
Transform & scale(const vec3 &pct)
Definition:
transform.h:100
math.h
ga
Definition:
color.h:9
G&A
oF
of_v0.11.2_vs2017_release
addons
ofxGAKit
libs
gaKit
src
ga
transform.h
Generated by
1.9.6