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
4namespace ga {
5
15{
16
17public:
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
115protected:
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
Transform represents a basic model matrix: translate * rotate * scale Stored internally as ga::mat4 (...
Definition: transform.h:15
Transform & setMatrix(const mat4 &matrix)
Definition: transform.h:37
void flagDirty()
Definition: transform.h:122
Transform & rotateAround(const vec3 &axis, float radians)
Definition: transform.h:103
Transform & translateX(float x)
Definition: transform.h:97
const mat4 & getMatrix() const
Definition: transform.h:87
Transform & setScale(const vec3 &scale)
Definition: transform.h:68
quat m_rotation
Definition: transform.h:118
Transform & setTranslation(const vec2 &translation)
Definition: transform.h:56
Transform & translate(const vec2 &vec)
Definition: transform.h:96
Transform()
Definition: transform.h:18
Transform operator*(const mat4 &m)
Definition: transform.h:112
void decompose()
Definition: transform.h:137
Transform & setRotation(const quat &rotation)
Definition: transform.h:61
vec3 m_translation
Definition: transform.h:116
Transform(const vec3 &translation, const quat &rotation, const vec3 &scale)
Definition: transform.h:29
const vec3 & getScale() const
Definition: transform.h:84
const quat & getRotation() const
Definition: transform.h:83
mat4 m_transform
Definition: transform.h:119
vec3 getEulerRotation() const
Definition: transform.h:85
bool m_dirty
Definition: transform.h:120
Transform & translateZ(float z)
Definition: transform.h:99
Transform & rotateZ(float radians)
Definition: transform.h:106
Transform & translate(const vec3 &vec)
Definition: transform.h:95
Transform & setEulerRotation(const vec3 &radians)
Definition: transform.h:75
Transform & translateY(float y)
Definition: transform.h:98
const vec3 & getTranslation() const
Definition: transform.h:82
Transform & rotate(const vec3 &eulerRadians)
Definition: transform.h:102
Transform & rotateX(float radians)
Definition: transform.h:104
vec3 m_scale
Definition: transform.h:117
Transform & set(const mat4 &matrix)
Definition: transform.h:44
Transform & rotateY(float radians)
Definition: transform.h:105
Transform & scale(float pct)
Definition: transform.h:101
Transform operator*(const Transform &t)
Definition: transform.h:113
Transform(const mat4 &transform)
Definition: transform.h:24
void clean() const
Definition: transform.h:124
Transform & operator=(const mat4 &m)
Definition: transform.h:111
Transform & setTranslation(const vec3 &translation)
Definition: transform.h:49
Transform & scale(const vec3 &pct)
Definition: transform.h:100
Definition: color.h:9