Skip to content

Commit 2da67c9

Browse files
committed
refactor(gfx/Transformable): cleaned up GetWorldMatrix()
1 parent 9e0382e commit 2da67c9

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

src/examples/demo/DemoApp.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,9 @@ class DemoApp : public system::BaseApp
193193
KeyboardState keys = {false, false, false, false, false};
194194

195195
float deltaTime = 0.f;
196-
auto transform = cube2.GetTransform();
197-
// transform->Origin(lepus::types::Vector3(0.f, 0.f, -2.f));
198-
// transform->SetScale(0.5f, 0.25f, 1.f);
199196

200-
// Parent the second cube to the first cube.
201-
// cubeNode->AddChild(&cube2);
197+
cube2.GetTransform()->SetScale(1.f, 0.5f, 1.f);
198+
cube3.GetTransform()->SetScale(1.f, 2.f, 1.f);
202199

203200
while (isRunning)
204201
{
@@ -213,7 +210,7 @@ class DemoApp : public system::BaseApp
213210
cube.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(0.f, 1.f, 0.f), PI * -0.25f));
214211
}
215212
cube.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(0.f, 1.f, 0.f), -deltaTime));
216-
cube2.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(1.f, 0.f, 0.f), -deltaTime));
213+
// cube2.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(1.f, 0.f, 0.f), -deltaTime));
217214
cube3.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(1.f, 1.f, 1.f), -deltaTime));
218215

219216
// Move the child cube back and forth along the parent's Z-axis

src/lepus/gfx/SceneGraph/Transformable.h

+9-23
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ namespace lepus
7373
{
7474
auto parentTransform = *(leaves[i - 1]->GetTransformable()->GetTransform());
7575

76-
lepus::types::Vector4 rotated(leaves[i]->GetTransformable()->GetTransform()->Origin() * accScale);
77-
rotated.w(1.f);
76+
lepus::types::Vector3 scaled = leaves[i]->GetTransformable()->GetTransform()->Origin() * accScale;
77+
7878
accScale.Multiply(parentTransform.Scale());
79-
//
79+
8080
parentTransform.Origin(lepus::types::Vector3());
81-
//
81+
8282
lepus::types::Quaternion normParentRot = parentTransform.Rotation().Normalised();
8383
lepus::types::Vector3 axis = normParentRot.Axis();
8484
float angle = normParentRot.Angle();
@@ -88,31 +88,17 @@ namespace lepus
8888
lepus::types::Vector4 rotatedAxis = mat.Multiply(lepus::types::Vector4(axis));
8989

9090
parentTransform.Rotation(lepus::types::Quaternion(rotatedAxis.x(), rotatedAxis.y(), rotatedAxis.z(), angle));
91-
mat = parentTransform.BuildMatrix();
92-
// rotated = mat.Multiply(rotated);
93-
// rotated = accRot.Rotate(rotated);
9491

9592
accRot = accRot * (parentTransform.Rotation());
9693
parentTransform.Rotation(accRot);
9794
mat = parentTransform.BuildMatrix();
98-
auto fwd = mat.Multiply(lepus::types::Vector4(0, 0, 1, 1)),
99-
rgt = mat.Multiply(lepus::types::Vector4(1, 0, 0, 1)),
95+
auto forward = mat.Multiply(lepus::types::Vector4(0, 0, 1, 1)),
96+
right = mat.Multiply(lepus::types::Vector4(1, 0, 0, 1)),
10097
up = mat.Multiply(lepus::types::Vector4(0, 1, 0, 1));
10198

102-
auto forward = lepus::types::Vector3(fwd.x(), fwd.y(), fwd.z());
103-
auto right = lepus::types::Vector3(rgt.x(), rgt.y(), rgt.z());
104-
auto newUp = lepus::types::Vector3(up.x(), up.y(), up.z());
105-
106-
// if (i == 1)
107-
// {
108-
// accPos.x(accPos.x() + rotated.x());
109-
// accPos.y(accPos.y() + rotated.y());
110-
// accPos.z(accPos.z() + rotated.z());
111-
// }
112-
// else
113-
{
114-
accPos = accPos + forward * rotated.z() + right * rotated.x() + newUp * rotated.y();
115-
}
99+
accPos.x(accPos.x() + forward.x() * scaled.z() + right.x() * scaled.x() + up.x() * scaled.y());
100+
accPos.y(accPos.y() + forward.y() * scaled.z() + right.y() * scaled.x() + up.y() * scaled.y());
101+
accPos.z(accPos.z() + forward.z() * scaled.z() + right.z() * scaled.x() + up.z() * scaled.y());
116102
}
117103

118104
lepus::math::Transform worldTransform = lepus::math::Transform();

tests/L3D/SceneGraph/SceneGraphTests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TEST(SceneGraphTest, SceneGraphChildIsAddedCorrectly)
4141
/// C is also rotated counter-clockwise around the Y axis by 50 degrees. Before applying A's rotation, D ends up at approx. (-3.1, 0, -0.55) away from A in world units.
4242
/// Once A's 90deg rotation is applied then, D should end up at approx (-0.57, 0, -5.06) in world space.
4343
///
44-
/// (it may be helpful to draw this on a piece of paper)
44+
/// (it may be helpful to draw this on a piece of paper - which means this test will also not cover everything as it's limited to the XZ plane)
4545
TEST(SceneGraphTest, SceneGraphChildTransformsCreateCorrectWorldCoords)
4646
{
4747
lepus::gfx::SceneGraph sceneGraph = lepus::gfx::SceneGraph();

0 commit comments

Comments
 (0)