CG_Labs  2021.2
TRSTransform.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <glm/glm.hpp>
4 #define GLM_ENABLE_EXPERIMENTAL
5 #include <glm/gtx/io.hpp>
6 
7 #include <iostream>
8 
33 template<typename T, glm::precision P>
34 class TRSTransform {
35 
36 public:
37  TRSTransform();
38  ~TRSTransform();
39 
40 public:
41  // Reset the transformation to the identity matrix
42  void ResetTransform();
43 
45  // Relative transformations: combine new transform with existing ones.
47 
48  void Translate(glm::tvec3<T, P> v);
49  void Scale(glm::tvec3<T, P> v);
50  void Scale(T uniform);
51 
52  // Rotate around vector (x, y, z)
53  // - Perform `new = current * newRotation`
54  void Rotate(T angle, glm::tvec3<T, P> v);
55  void RotateX(T angle);
56  void RotateY(T angle);
57  void RotateZ(T angle);
58  // - Perform `new = newRotation * current`
59  void PreRotate(T angle, glm::tvec3<T, P> v);
60  void PreRotateX(T angle);
61  void PreRotateY(T angle);
62  void PreRotateZ(T angle);
63 
64 
66  // Absolute transformations: overwrite existing transformations with new ones.
68 
69  void SetTranslate(glm::tvec3<T, P> v);
70  void SetScale(glm::tvec3<T, P> v);
71  void SetScale(T uniform);
72 
73  // Rotate around vector (x, y, z)
74  void SetRotate(T angle, glm::tvec3<T, P> v);
75  void SetRotateX(T angle);
76  void SetRotateY(T angle);
77  void SetRotateZ(T angle);
78 
79 
80  void LookTowards(glm::tvec3<T, P> front_vec, glm::tvec3<T, P> up_vec);
81  void LookTowards(glm::tvec3<T, P> front_vec);
82  void LookAt(glm::tvec3<T, P> point, glm::tvec3<T, P> up_vec);
83  void LookAt(glm::tvec3<T, P> point);
84 
85 
87  // Useful getters
89 
90  glm::tmat4x4<T, P> GetMatrix() const;
91  glm::tmat4x4<T, P> GetMatrixInverse() const;
92 
93  glm::tmat3x3<T, P> GetRotation() const;
94  glm::tvec3<T, P> GetTranslation() const;
95  glm::tvec3<T, P> GetScale() const;
96 
97  glm::tmat4x4<T, P> GetTranslationMatrix() const;
98  glm::tmat4x4<T, P> GetRotationMatrix() const;
99  glm::tmat4x4<T, P> GetScaleMatrix() const;
100 
101  glm::tmat4x4<T, P> GetTranslationMatrixInverse() const;
102  glm::tmat4x4<T, P> GetRotationMatrixInverse() const;
103  glm::tmat4x4<T, P> GetScaleMatrixInverse() const;
104 
105  glm::tmat4x4<T, P> GetTranslationRotationMatrix() const;
106 
107  glm::tvec3<T, P> GetUp() const;
108  glm::tvec3<T, P> GetDown() const;
109  glm::tvec3<T, P> GetLeft() const;
110  glm::tvec3<T, P> GetRight() const;
111  glm::tvec3<T, P> GetFront() const;
112  glm::tvec3<T, P> GetBack() const;
113 
114 protected:
115  glm::tmat3x3<T, P> mR;
116  glm::tvec3<T, P> mT;
117  glm::tvec3<T, P> mS;
118 
119 public:
120  friend std::ostream &operator<<(std::ostream &os, TRSTransform<T, P> &v)
121  {
122  os << v.mT << std::endl;
123  os << v.mR << std::endl;
124  os << v.mS << std::endl;
125  return os;
126  }
127  friend std::istream &operator>>(std::istream &is, TRSTransform<T, P> &v)
128  {
129  is >> v.mT;
130  is >> v.mR;
131  is >> v.mS;
132  return is;
133  }
134 };
135 
136 #include "TRSTransform.inl"
137 
Definition: TRSTransform.h:34
glm::tmat3x3< T, P > GetRotation() const
Definition: TRSTransform.inl:376
glm::tvec3< T, P > GetDown() const
Definition: TRSTransform.inl:406
glm::tvec3< T, P > mS
Definition: TRSTransform.h:117
glm::tvec3< T, P > GetLeft() const
Definition: TRSTransform.inl:412
void SetTranslate(glm::tvec3< T, P > v)
Definition: TRSTransform.inl:152
~TRSTransform()
Definition: TRSTransform.inl:17
glm::tvec3< T, P > GetScale() const
Definition: TRSTransform.inl:392
glm::tmat4x4< T, P > GetScaleMatrixInverse() const
Definition: TRSTransform.inl:316
void SetRotateY(T angle)
Definition: TRSTransform.inl:192
glm::tmat4x4< T, P > GetScaleMatrix() const
Definition: TRSTransform.inl:280
void RotateZ(T angle)
Definition: TRSTransform.inl:92
glm::tmat4x4< T, P > GetRotationMatrix() const
Definition: TRSTransform.inl:268
glm::tvec3< T, P > GetTranslation() const
Definition: TRSTransform.inl:384
glm::tmat4x4< T, P > GetMatrix() const
Definition: TRSTransform.inl:340
void PreRotate(T angle, glm::tvec3< T, P > v)
Definition: TRSTransform.inl:105
void RotateY(T angle)
Definition: TRSTransform.inl:79
friend std::ostream & operator<<(std::ostream &os, TRSTransform< T, P > &v)
Definition: TRSTransform.h:120
glm::tvec3< T, P > mT
Definition: TRSTransform.h:116
glm::tmat4x4< T, P > GetTranslationMatrix() const
Definition: TRSTransform.inl:256
void PreRotateX(T angle)
Definition: TRSTransform.inl:113
void Translate(glm::tvec3< T, P > v)
Definition: TRSTransform.inl:34
glm::tvec3< T, P > GetBack() const
Definition: TRSTransform.inl:430
glm::tvec3< T, P > GetUp() const
Definition: TRSTransform.inl:400
glm::tmat3x3< T, P > mR
Definition: TRSTransform.h:115
friend std::istream & operator>>(std::istream &is, TRSTransform< T, P > &v)
Definition: TRSTransform.h:127
TRSTransform()
Definition: TRSTransform.inl:9
glm::tvec3< T, P > GetFront() const
Definition: TRSTransform.inl:424
void LookAt(glm::tvec3< T, P > point, glm::tvec3< T, P > up_vec)
Definition: TRSTransform.inl:240
glm::tmat4x4< T, P > GetRotationMatrixInverse() const
Definition: TRSTransform.inl:304
glm::tvec3< T, P > GetRight() const
Definition: TRSTransform.inl:418
void LookTowards(glm::tvec3< T, P > front_vec, glm::tvec3< T, P > up_vec)
Definition: TRSTransform.inl:208
void Rotate(T angle, glm::tvec3< T, P > v)
Definition: TRSTransform.inl:58
glm::tmat4x4< T, P > GetTranslationRotationMatrix() const
Definition: TRSTransform.inl:328
void RotateX(T angle)
Definition: TRSTransform.inl:66
void ResetTransform()
Definition: TRSTransform.inl:24
void SetRotate(T angle, glm::tvec3< T, P > v)
Definition: TRSTransform.inl:176
glm::tmat4x4< T, P > GetTranslationMatrixInverse() const
Definition: TRSTransform.inl:292
void PreRotateZ(T angle)
Definition: TRSTransform.inl:139
void SetScale(glm::tvec3< T, P > v)
Definition: TRSTransform.inl:160
void SetRotateX(T angle)
Definition: TRSTransform.inl:184
void Scale(glm::tvec3< T, P > v)
Definition: TRSTransform.inl:42
void SetRotateZ(T angle)
Definition: TRSTransform.inl:200
void PreRotateY(T angle)
Definition: TRSTransform.inl:126
glm::tmat4x4< T, P > GetMatrixInverse() const
Definition: TRSTransform.inl:352