/
Transform.cpp
384 lines (325 loc) · 12.5 KB
/
Transform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <Engine/Attribute/Transform.hpp>
#include <glm/gtx/norm.hpp>
#include <Engine/GameObject.hpp>
#include <Engine/Event/AncestorTransformTranslatedEvent.hpp>
#include <Engine/Event/AncestorTransformRotatedEvent.hpp>
#include <Engine/Event/AncestorTransformScaledEvent.hpp>
#include <Engine/Event/ChildGameObjectAttachedEvent.hpp>
namespace Engine
{
namespace Attribute
{
Transform::Transform(std::shared_ptr<Window> window,
std::shared_ptr<ResourceManager> resourceManager,
std::shared_ptr<EventDispatcher> sceneEventDispatcher,
std::shared_ptr<EventDispatcher> gameObjectEventDispatcher,
std::weak_ptr<GameObject> gameObject)
: IAttribute(window, resourceManager, sceneEventDispatcher, gameObjectEventDispatcher, gameObject)
, m_localPosition(0.0f, 0.0f, 0.0f)
, m_localRotation()
, m_localScale(1.0f, 1.0f, 1.0f)
, m_position(m_localPosition)
, m_rotation(m_localRotation)
, m_scale(m_localScale)
, m_localTransformationMatrix()
, m_transformationMatrix()
, m_localTransformationMatrixDirty(true)
, m_transformationMatrixDirty(true)
{
// Subscribe for ancestor transform translated events.
m_ancestorTranslatedSubscription = GetGameObjectEventDispatcher()->Subscribe<Event::AncestorTransformTranslatedEvent>(
[this](const Event::AncestorTransformTranslatedEvent& event) {
m_position += event.GetDelta();
m_transformationMatrixDirty = true;
}
);
// Subscribe for ancestor transform rotated events.
m_ancestorRotatedSubscription = GetGameObjectEventDispatcher()->Subscribe<Event::AncestorTransformRotatedEvent>(
[this](const Event::AncestorTransformRotatedEvent& event) {
m_rotation = event.GetDelta() * m_rotation;
m_transformationMatrixDirty = true;
}
);
// Subscribe for ancestor transform scaled events.
m_ancestorScaledSubscription = GetGameObjectEventDispatcher()->Subscribe<Event::AncestorTransformScaledEvent>(
[this](const Event::AncestorTransformScaledEvent& event) {
m_scale *= event.GetDelta();
m_transformationMatrixDirty = true;
}
);
// Subscribe for child game object attached events.
m_childAttachedSubscription = GetGameObjectEventDispatcher()->Subscribe<Event::ChildGameObjectAttachedEvent>(
[this](const Event::ChildGameObjectAttachedEvent& event)
{
std::shared_ptr<GameObject> child = event.GetChild();
if (child->HasAttribute<Transform>())
{
child->BroadcastDispatch<Event::AncestorTransformTranslatedEvent>(m_position);
child->BroadcastDispatch<Event::AncestorTransformRotatedEvent>(m_rotation);
child->BroadcastDispatch<Event::AncestorTransformScaledEvent>(m_scale);
}
else
{
child->BroadcastEnqueue<Event::AncestorTransformTranslatedEvent>(m_position);
child->BroadcastEnqueue<Event::AncestorTransformRotatedEvent>(m_rotation);
child->BroadcastEnqueue<Event::AncestorTransformScaledEvent>(m_scale);
}
}
);
}
Transform::~Transform()
{
// Unsubscribe for ancestor transform translated events.
GetGameObjectEventDispatcher()->Unsubscribe<Event::AncestorTransformTranslatedEvent>(m_ancestorTranslatedSubscription);
// Unsubscribe for ancestor transform rotated events.
GetGameObjectEventDispatcher()->Unsubscribe<Event::AncestorTransformRotatedEvent>(m_ancestorRotatedSubscription);
// Unsubscribe for ancestor transform scaled events.
GetGameObjectEventDispatcher()->Unsubscribe<Event::AncestorTransformScaledEvent>(m_ancestorScaledSubscription);
// Unsubscribe for child game object attached events.
GetGameObjectEventDispatcher()->Unsubscribe<Event::ChildGameObjectAttachedEvent>(m_childAttachedSubscription);
}
const glm::vec3& Transform::GetLocalPosition() const
{
return m_localPosition;
}
const glm::quat& Transform::GetLocalRotation() const
{
return m_localRotation;
}
const glm::vec3& Transform::GetLocalScale() const
{
return m_localScale;
}
const glm::vec3& Transform::GetPosition() const
{
return m_position;
}
const glm::quat& Transform::GetRotation() const
{
return m_rotation;
}
const glm::vec3& Transform::GetScale() const
{
return m_scale;
}
const glm::mat4& Transform::GetLocalTransformationMatrix() const
{
if (m_localTransformationMatrixDirty)
{
const_cast<Transform*>(this)->CleanLocalTransformationMatrix();
}
return m_localTransformationMatrix;
}
const glm::mat4& Transform::GetTransformationMatrix() const
{
if (m_transformationMatrixDirty)
{
const_cast<Transform*>(this)->CleanTransformationMatrix();
}
return m_transformationMatrix;
}
glm::vec3 Transform::GetForward() const
{
const glm::vec3 localForwardDirection(0.0f, 0.0f, -1.0f);
return glm::normalize(m_rotation * localForwardDirection);
}
void Transform::SetLocalPosition(const glm::vec3& position)
{
const glm::vec3 delta = position - m_localPosition;
m_localPosition = position;
m_position += delta;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast translation delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformTranslatedEvent>(delta);
}
}
void Transform::SetLocalRotation(const glm::quat& rotation)
{
const glm::quat delta = glm::inverse(m_localRotation) * rotation;
m_localRotation = rotation;
m_rotation = delta * m_rotation;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast rotation delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformRotatedEvent>(delta);
}
}
void Transform::SetLocalScale(const glm::vec3& scale)
{
const glm::vec3 delta = scale / m_localScale;
m_localScale = scale;
m_scale *= delta;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast scaling delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformScaledEvent>(delta);
}
}
void Transform::SetLocalScale(float scale)
{
SetLocalScale(glm::vec3(scale, scale, scale));
}
void Transform::SetPosition(const glm::vec3& position)
{
const glm::vec3 delta = position - m_position;
m_position = position;
m_localPosition += delta;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast translation delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformTranslatedEvent>(delta);
}
}
void Transform::SetRotation(const glm::quat& rotation)
{
const glm::quat delta = rotation * glm::inverse(m_rotation);
m_rotation = rotation;
m_localRotation = delta * m_localRotation;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast rotation delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformRotatedEvent>(delta);
}
}
void Transform::SetScale(const glm::vec3& scale)
{
const glm::vec3 delta = scale / m_scale;
m_scale = scale;
m_localScale *= delta;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast scaling delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformScaledEvent>(delta);
}
}
void Transform::SetScale(float scale)
{
SetScale(glm::vec3(scale, scale, scale));
}
void Transform::Translate(const glm::vec3& translation)
{
m_localPosition += translation;
m_position += translation;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast translation delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformTranslatedEvent>(translation);
}
}
void Transform::Rotate(const glm::quat& rotation)
{
m_localRotation = rotation * m_localRotation;
m_rotation = rotation * m_rotation;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast rotation delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformRotatedEvent>(rotation);
}
}
void Transform::Scale(const glm::vec3& scale)
{
m_localScale *= scale;
m_scale *= scale;
m_localTransformationMatrixDirty = true;
m_transformationMatrixDirty = true;
// Recursively broadcast scaling delta to all descended Game
// Objects.
if (std::shared_ptr<GameObject> gameObject = GetGameObject().lock())
{
gameObject->DescendantBroadcastDispatch<Event::AncestorTransformScaledEvent>(scale);
}
}
void Transform::Scale(float scale)
{
Scale(glm::vec3(scale, scale, scale));
}
void Transform::LookAt(const glm::vec3& point, const glm::vec3& up)
{
const glm::vec3 desiredForward = glm::normalize(point - m_position);
Rotate(GetRotationToFaceDirection(desiredForward, up));
}
glm::vec3 Transform::TransformPoint(const glm::vec3& point) const
{
return glm::vec3(GetLocalTransformationMatrix() * glm::vec4(point, 1.0f));
}
glm::quat Transform::GetRotationToFaceDirection(const glm::vec3& direction) const
{
// Normalise the desired forward direction.
const glm::vec3 desiredForward = glm::normalize(direction);
// Get the current forward direction.
const glm::vec3 currentForward = GetForward();
// Return the quaternion that rotates the transform such that its
// forward direction points in the target direction.
const glm::vec3 w1 = glm::cross(currentForward, desiredForward);
return glm::normalize(glm::quat(1.0f + glm::dot(currentForward, desiredForward), w1.x, w1.y, w1.z));
}
glm::quat Transform::GetRotationToFaceDirection(const glm::vec3& direction, const glm::vec3& up) const
{
// Normalise the desired forward direction.
const glm::vec3 desiredForward = glm::normalize(direction);
// Get the current forward direction.
const glm::vec3 currentForward = GetForward();
// Calculate the quaternion that rotates the transform such that its
// forward direction points in the target direction.
const glm::vec3 w1 = glm::cross(currentForward, desiredForward);
const glm::quat q1 = glm::normalize(glm::quat(1.0f + glm::dot(currentForward, desiredForward), w1.x, w1.y, w1.z));
// We can't preserve the upward direction if the transform should
// point in the upward or downward direction.
if (glm::distance2(desiredForward, up) < 0.01f ||
glm::distance2(desiredForward, -up) < 0.01f)
{
return q1;
}
// Calculate the transform's up direction after applying the
// rotation calculated above.
const glm::vec3 currentUp = glm::normalize((q1 * m_rotation) * glm::vec3(0.0f, 1.0f, 0.0f));
// Calculate the vector that points to the right.
const glm::vec3 right = glm::normalize(glm::cross(desiredForward, up));
// Recompute the desired up direction.
const glm::vec3 desiredUp = glm::normalize(glm::cross(right, desiredForward));
// Calculate the quaternion that rotates the transform such that
// its up direction points in the specified up direction.
const glm::vec3 w2 = glm::cross(currentUp, desiredUp);
const glm::quat q2 = glm::normalize(glm::quat(1.0f + glm::dot(currentUp, desiredUp), w2.x, w2.y, w2.z));
// Return the combined rotation.
// Note: There is no need to normalize the result since both q1 and
// q2 are unit quaternions.
return q2 * q1;
}
void Transform::CleanLocalTransformationMatrix()
{
m_localTransformationMatrix = glm::translate(m_localPosition) * glm::toMat4(m_localRotation) * glm::scale(m_localScale);
m_localTransformationMatrixDirty = false;
}
void Transform::CleanTransformationMatrix()
{
m_transformationMatrix = glm::translate(m_position) * glm::toMat4(m_rotation) * glm::scale(m_scale);
m_transformationMatrixDirty = false;
}
}
}