CG_Labs 2021.2
Loading...
Searching...
No Matches
TRSTransform.inl
Go to the documentation of this file.
1#include <cmath>
2#include "TRSTransform.h"
3
4#include <glm/gtc/matrix_transform.hpp>
5
6/*----------------------------------------------------------------------------*/
7
8template<typename T, glm::precision P>
10{
11 ResetTransform();
12}
13
14/*----------------------------------------------------------------------------*/
15
16template<typename T, glm::precision P>
20
21/*----------------------------------------------------------------------------*/
22
23template<typename T, glm::precision P>
25{
26 mT = glm::tvec3<T, P>(static_cast<T>(0));
27 mS = glm::tvec3<T, P>(static_cast<T>(1));
28 mR = glm::tmat3x3<T, P>(static_cast<T>(1));
29}
30
31/*----------------------------------------------------------------------------*/
32
33template<typename T, glm::precision P>
34void TRSTransform<T, P>::Translate(glm::tvec3<T, P> v)
35{
36 mT += v;
39/*----------------------------------------------------------------------------*/
40
41template<typename T, glm::precision P>
42void TRSTransform<T, P>::Scale(glm::tvec3<T, P> v)
43{
44 mS *= v;
45}
46
47/*----------------------------------------------------------------------------*/
49template<typename T, glm::precision P>
51{
52 mS *= uniform;
53}
55/*----------------------------------------------------------------------------*/
57template<typename T, glm::precision P>
58void TRSTransform<T, P>::Rotate(T angle, glm::tvec3<T, P> v)
60 mR = glm::tmat3x3<T, P>(glm::rotate(glm::tmat4x4<T, P>(mR), angle, v));
63/*----------------------------------------------------------------------------*/
64
65template<typename T, glm::precision P>
67{
68 T C = std::cos(angle);
69 T S = std::sin(angle);
70 mR = glm::tmat3x3<T, P>(
71 mR[0][0], C * mR[0][1] - mR[0][2] * S, C * mR[0][2] + mR[0][1] * S,
72 mR[1][0], C * mR[1][1] - mR[1][2] * S, C * mR[1][2] + mR[1][1] * S,
73 mR[2][0], C * mR[2][1] - mR[2][2] * S, C * mR[2][2] + mR[2][1] * S);
76/*----------------------------------------------------------------------------*/
78template<typename T, glm::precision P>
81 T C = std::cos(angle);
82 T S = std::sin(angle);
83 mR = glm::tmat3x3<T, P>(
84 C * mR[0][0] + mR[0][2] * S, mR[0][1], C * mR[0][2] - mR[0][0] * S,
85 C * mR[1][0] + mR[1][2] * S, mR[1][1], C * mR[1][2] - mR[1][0] * S,
86 C * mR[2][0] + mR[2][2] * S, mR[2][1], C * mR[2][2] - mR[2][0] * S);
87}
88
89/*----------------------------------------------------------------------------*/
91template<typename T, glm::precision P>
94 T C = std::cos(angle);
95 T S = std::sin(angle);
96 mR = glm::tmat3x3<T, P>(
97 C * mR[0][0] - mR[0][1] * S, C * mR[0][1] + mR[0][0] * S, mR[0][2],
98 C * mR[1][0] - mR[1][1] * S, C * mR[1][1] + mR[1][0] * S, mR[1][2],
99 C * mR[2][0] - mR[2][1] * S, C * mR[2][1] + mR[2][0] * S, mR[2][2]);
100}
102/*----------------------------------------------------------------------------*/
104template<typename T, glm::precision P>
105void TRSTransform<T, P>::PreRotate(T angle, glm::tvec3<T, P> v)
106{
107 mR = glm::tmat3x3<T, P>::RotationMatrix(angle, v) * mR;
110/*----------------------------------------------------------------------------*/
112template<typename T, glm::precision P>
114{
115 T C = cos(angle);
116 T S = sin(angle);
117 mR = glm::tmat3x3<T, P>(
118 mR[0][0], mR[0][1], mR[0][2],
119 C * mR[1][0] + mR[2][0] * S, C * mR[1][1] + mR[2][1] * S, C * mR[1][2] + mR[2][2] * S,
120 C * mR[2][0] - mR[1][0] * S, C * mR[2][1] - mR[1][1] * S, C * mR[2][2] - mR[1][2] * S);
121}
122
123/*----------------------------------------------------------------------------*/
124
125template<typename T, glm::precision P>
127{
128 T C = cos(angle);
129 T S = sin(angle);
130 mR = glm::tmat3x3<T, P>(
131 C * mR[0][0] - mR[2][0] * S, C * mR[0][1] - mR[2][1] * S, C * mR[0][2] - mR[2][2] * S,
132 mR[1][0], mR[1][1], mR[1][2],
133 C * mR[2][0] + mR[0][0] * S, C * mR[2][1] + mR[0][1] * S, C * mR[2][2] + mR[0][2] * S);
134}
135
136/*----------------------------------------------------------------------------*/
137
138template<typename T, glm::precision P>
140{
141 T C = cos(angle);
142 T S = sin(angle);
143 mR = glm::tmat3x3<T, P>(
144 C * mR[0][0] + mR[1][0] * S, C * mR[0][1] + mR[1][1] * S, C * mR[0][2] + mR[1][2] * S,
145 C * mR[1][0] - mR[0][0] * S, C * mR[1][1] - mR[0][1] * S, C * mR[1][2] - mR[0][2] * S,
146 mR[2][0], mR[2][1], mR[2][2]);
147}
148
149/*----------------------------------------------------------------------------*/
150
151template<typename T, glm::precision P>
152void TRSTransform<T, P>::SetTranslate(glm::tvec3<T, P> v)
153{
154 mT = v;
155}
156
157/*----------------------------------------------------------------------------*/
158
159template<typename T, glm::precision P>
160void TRSTransform<T, P>::SetScale(glm::tvec3<T, P> v)
161{
162 mS = v;
163}
164
165/*----------------------------------------------------------------------------*/
166
167template<typename T, glm::precision P>
169{
170 mS = glm::tvec3<T, P>(uniform);
171}
172
173/*----------------------------------------------------------------------------*/
174
175template<typename T, glm::precision P>
176void TRSTransform<T, P>::SetRotate(T angle, glm::tvec3<T, P> v)
177{
178 mR = glm::tmat3x3<T, P>(glm::rotate(glm::tmat4x4<T, P>(T(1)), angle, v));
179}
180
181/*----------------------------------------------------------------------------*/
182
183template<typename T, glm::precision P>
185{
186 mR = glm::tmat3x3<T, P>(glm::rotate(glm::tmat4x4<T, P>(T(1)), angle, glm::tvec3<T, P>(1, 0, 0)));
187}
188
189/*----------------------------------------------------------------------------*/
190
191template<typename T, glm::precision P>
193{
194 mR = glm::tmat3x3<T, P>(glm::rotate(glm::tmat4x4<T, P>(T(1)), angle, glm::tvec3<T, P>(0, 1, 0)));
195}
196
197/*----------------------------------------------------------------------------*/
198
199template<typename T, glm::precision P>
201{
202 mR = glm::tmat3x3<T, P>(glm::rotate(glm::tmat4x4<T, P>(T(1)), angle, glm::tvec3<T, P>(0, 0, 1)));
203}
204
205/*----------------------------------------------------------------------------*/
206
207template<typename T, glm::precision P>
208void TRSTransform<T, P>::LookTowards(glm::tvec3<T, P> front_vec, glm::tvec3<T, P> up_vec)
209{
210 front_vec = normalize(front_vec);
211 up_vec = normalize(up_vec);
212
213 if (std::abs(dot(up_vec, front_vec)) > 0.99999f)
214 return;
215
216 glm::tvec3<T, P> prev_up = up_vec;
217
218 glm::tvec3<T, P> right = cross(front_vec, prev_up);
219 glm::tvec3<T, P> up = cross(right, front_vec);
220
221 right = normalize(right);
222 up = normalize(up);
223
224 mR[0] = right;
225 mR[1] = up;
226 mR[2] = -front_vec;
227}
228
229/*----------------------------------------------------------------------------*/
230
231template<typename T, glm::precision P>
232void TRSTransform<T, P>::LookTowards(glm::tvec3<T, P> front_vec)
233{
234 LookTowards(front_vec, glm::tvec3<T, P>(0, 1, 0));
235}
236
237/*----------------------------------------------------------------------------*/
238
239template<typename T, glm::precision P>
240void TRSTransform<T, P>::LookAt(glm::tvec3<T, P> point, glm::tvec3<T, P> up_vec)
241{
242 LookTowards(point - mT, up_vec);
243}
244
245/*----------------------------------------------------------------------------*/
246
247template<typename T, glm::precision P>
248void TRSTransform<T, P>::LookAt(glm::tvec3<T, P> point)
249{
250 LookTowards(point - mT);
251}
252
253/*----------------------------------------------------------------------------*/
254
255template<typename T, glm::precision P>
257{
258 return glm::tmat4x4<T, P>(
259 1, 0, 0, 0,
260 0, 1, 0, 0,
261 0, 0, 1, 0,
262 mT.x, mT.y, mT.z, 1);
263}
264
265/*----------------------------------------------------------------------------*/
266
267template<typename T, glm::precision P>
269{
270 return glm::tmat4x4<T, P>(
271 mR[0][0], mR[0][1], mR[0][2], 0,
272 mR[1][0], mR[1][1], mR[1][2], 0,
273 mR[2][0], mR[2][1], mR[2][2], 0,
274 0 , 0 , 0 , 1);
275}
276
277/*----------------------------------------------------------------------------*/
278
279template<typename T, glm::precision P>
280glm::tmat4x4<T, P> TRSTransform<T, P>::GetScaleMatrix() const
281{
282 return glm::tmat4x4<T, P>(
283 mS.x, 0 , 0 , 0,
284 0 , mS.y, 0 , 0,
285 0 , 0 , mS.z, 0,
286 0 , 0 , 0 , 1);
287}
288
289/*----------------------------------------------------------------------------*/
290
291template<typename T, glm::precision P>
293{
294 return glm::tmat4x4<T, P>(
295 1, 0, 0, 0,
296 0, 1, 0, 0,
297 0, 0, 1, 0,
298 -mT.x, -mT.y, -mT.z, 1);
299}
300
301/*----------------------------------------------------------------------------*/
302
303template<typename T, glm::precision P>
305{
306 return glm::tmat4x4<T, P>(
307 mR[0][0], mR[1][0], mR[2][0], 0,
308 mR[0][1], mR[1][1], mR[2][1], 0,
309 mR[0][2], mR[1][2], mR[2][2], 0,
310 0, 0, 0, 1);
311}
312
313/*----------------------------------------------------------------------------*/
314
315template<typename T, glm::precision P>
317{
318 return glm::tmat4x4<T, P>(
319 T(1)/mS.x, 0, 0, 0,
320 0, T(1)/mS.y, 0, 0,
321 0, 0, T(1)/mS.z, 0,
322 0, 0, 0, 1);
323}
324
325/*----------------------------------------------------------------------------*/
326
327template<typename T, glm::precision P>
329{
330 return glm::tmat4x4<T, P>(
331 mR[0][0], mR[0][1], mR[0][2], 0,
332 mR[1][0], mR[1][1], mR[1][2], 0,
333 mR[2][0], mR[2][1], mR[2][2], 0,
334 mT.x, mT.y, mT.z, 1);
335}
336
337/*----------------------------------------------------------------------------*/
338
339template<typename T, glm::precision P>
340glm::tmat4x4<T, P> TRSTransform<T, P>::GetMatrix() const
341{
342 return glm::tmat4x4<T, P>(
343 mR[0][0]*mS.x, mR[0][1]*mS.x, mR[0][2]*mS.x, 0,
344 mR[1][0]*mS.y, mR[1][1]*mS.y, mR[1][2]*mS.y, 0,
345 mR[2][0]*mS.z, mR[2][1]*mS.z, mR[2][2]*mS.z, 0,
346 mT.x, mT.y, mT.z, 1);
347}
348
349/*----------------------------------------------------------------------------*/
350
351template<typename T, glm::precision P>
352glm::tmat4x4<T, P> TRSTransform<T, P>::GetMatrixInverse() const
353{
354 glm::tvec3<T, P> X = glm::tvec3<T, P>(T(1) / mS.x, T(1) / mS.y, T(1) / mS.z);
355
356 T a = mR[0][0] * X.x;
357 T b = mR[1][0] * X.y;
358 T c = mR[2][0] * X.z;
359 T d = mR[0][1] * X.x;
360 T e = mR[1][1] * X.y;
361 T f = mR[2][1] * X.z;
362 T g = mR[0][2] * X.x;
363 T h = mR[1][2] * X.y;
364 T i = mR[2][2] * X.z;
365
366 return glm::tmat4x4<T, P>(
367 a, b, c, 0,
368 d, e, f, 0,
369 g, h, i, 0,
370 -(mT.x * a + mT.y * d + mT.z * g), -(mT.x * b + mT.y * e + mT.z * h), -(mT.x * c + mT.y * f + mT.z * i), 1);
371}
372
373/*----------------------------------------------------------------------------*/
374
375template<typename T, glm::precision P>
376glm::tmat3x3<T, P> TRSTransform<T, P>::GetRotation() const
377{
378 return mR;
379}
380
381/*----------------------------------------------------------------------------*/
382
383template<typename T, glm::precision P>
385{
386 return mT;
387}
388
389/*----------------------------------------------------------------------------*/
390
391template<typename T, glm::precision P>
392glm::tvec3<T, P> TRSTransform<T, P>::GetScale() const
393{
394 return mS;
395}
396
397/*----------------------------------------------------------------------------*/
398
399template<typename T, glm::precision P>
400glm::tvec3<T, P> TRSTransform<T, P>::GetUp() const
401{
402 return glm::tvec3<T, P>(mR[1][0]*mS.y, mR[1][1]*mS.y, mR[1][2]*mS.y);
403}
404
405template<typename T, glm::precision P>
406glm::tvec3<T, P> TRSTransform<T, P>::GetDown() const
407{
408 return -GetUp();
409}
410
411template<typename T, glm::precision P>
412glm::tvec3<T, P> TRSTransform<T, P>::GetLeft() const
413{
414 return -GetRight();
415}
416
417template<typename T, glm::precision P>
418glm::tvec3<T, P> TRSTransform<T, P>::GetRight() const
419{
420 return glm::tvec3<T, P>(mR[0][0]*mS.x, mR[0][1]*mS.x, mR[0][2]*mS.x);
421}
422
423template<typename T, glm::precision P>
424glm::tvec3<T, P> TRSTransform<T, P>::GetFront() const
425{
426 return -GetBack();
427}
428
429template<typename T, glm::precision P>
430glm::tvec3<T, P> TRSTransform<T, P>::GetBack() const
431{
432 return glm::tvec3<T, P>(mR[2][0]*mS.z, mR[2][1]*mS.z, mR[2][2]*mS.z);
433}
434
435/*----------------------------------------------------------------------------*/
glm::tmat3x3< T, P > GetRotation() const
Definition TRSTransform.inl:376
glm::tvec3< T, P > GetDown() const
Definition TRSTransform.inl:406
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
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
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