Skip to content

Commit

Permalink
test(gfx/scenegraph): corrected expected values in world coords test …
Browse files Browse the repository at this point in the history
…and updated the demo app
  • Loading branch information
tomezpl committed Oct 14, 2024
1 parent d458b6d commit 5cc821e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
26 changes: 21 additions & 5 deletions src/examples/demo/DemoApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ class DemoApp : public system::BaseApp
auto cubeMesh = lepus::gfx::GLMesh(lepus::utility::Primitives::Cube());
auto cube = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());
auto cube2 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());
auto cube3 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());
auto cube4 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());

cube.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, -2.f));
cube2.GetTransform()->Origin(lepus::types::Vector3(2.f, 0.f, 0.f));
cube3.GetTransform()->Origin(lepus::types::Vector3(-3.f, 0.f, -3.f));
cube4.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, 6.f));

auto rootNode = api.GetSceneGraph().AddChild(&cube);
auto childNode1 = rootNode->AddChild(&cube2);
auto childNode2 = childNode1->AddChild(&cube3);
auto childNode3 = childNode2->AddChild(&cube4);

cube.GetTransform()->Rotate(lepus::types::Quaternion(0.f, 1.f, 0.f, (float)PI * -0.5f));
cube2.GetTransform()->SetScale(1.f / 1.5f);
cube3.GetTransform()->Rotate(lepus::types::Quaternion(0.f, 1.f, 0.f, (float)PI * (-50.f / 180.f)));

auto cubeNode = api.GetSceneGraph().AddChild(&cube);

Expand Down Expand Up @@ -178,11 +194,11 @@ class DemoApp : public system::BaseApp

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

// Parent the second cube to the first cube.
cubeNode->AddChild(&cube2);
// cubeNode->AddChild(&cube2);

while (isRunning)
{
Expand All @@ -192,10 +208,10 @@ class DemoApp : public system::BaseApp
UpdateInput(keys, windowing);

// Rotate the parent cube
cube.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(0.f, 1.f, 0.f), deltaTime));
cube.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(0.f, 1.f, 0.f), deltaTime * 0.5f));

// Move the child cube back and forth along the parent's Z-axis
cube2.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, -1.f + ((sinf(runningTime) + 1.f) * 0.5f) * -2.f));
// cube2.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, -1.f + ((sinf(runningTime) + 1.f) * 0.5f) * -2.f));

Tick(deltaTime, keys);
UpdateUniforms(&api);
Expand Down
24 changes: 21 additions & 3 deletions tests/L3D/SceneGraph/SceneGraphTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,46 @@ TEST(SceneGraphTest, SceneGraphChildIsAddedCorrectly)

/// @brief Test world transforms in a scene graph.
///
/// This scene sets up transform A at (0, 0, -2), B at (2, 0, 0) relative to A, C at (-3, 0, -3) relative to B.
/// This scene sets up transform A at (0, 0, -2), B at (2, 0, 0) relative to A, C at (-3, 0, -3) relative to B, D at (0, 0, 6) relative to C.
/// B has a scale of (1.0/1.5). A is rotated counter-clockwise around the Y axis by 90 degrees.
/// A's anchor point is its center of mass so it remains in the same spot after the rotation.
/// B ends up at (0, 0, 0) in world space.
/// C's local coords are scaled by (1.0/1.5), and are therefore (-2, 0, -2) away from B in world units, and (0, 0, -2) away fom A in world units.
/// Therefore, C should end up at (2, 0, -2) in world space, effectively taking B's original world-space position.
/// 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.
/// Once A's 90deg rotation is applied then, D should end up at approx (-0.55, 0, -5.06) in world space.
///
/// (it may be helpful to draw this on a piece of paper)
TEST(SceneGraphTest, SceneGraphChildTransformsCreateCorrectWorldCoords)
{
lepus::gfx::SceneGraph sceneGraph = lepus::gfx::SceneGraph();
lepus::gfx::Transformable rootTransformable = lepus::gfx::Transformable();
lepus::gfx::Transformable childTransformable1 = lepus::gfx::Transformable();
lepus::gfx::Transformable childTransformable2 = lepus::gfx::Transformable();
lepus::gfx::Transformable childTransformable3 = lepus::gfx::Transformable();

rootTransformable.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, -2.f));
childTransformable1.GetTransform()->Origin(lepus::types::Vector3(2.f, 0.f, 0.f));
childTransformable2.GetTransform()->Origin(lepus::types::Vector3(-3.f, 0.f, -3.f));
childTransformable3.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, 6.f));

auto rootNode = sceneGraph.AddChild(&rootTransformable);
auto childNode1 = rootNode->AddChild(&childTransformable1);
auto childNode2 = childNode1->AddChild(&childTransformable2);
auto childNode3 = childNode2->AddChild(&childTransformable3);

rootTransformable.GetTransform()->Rotate(lepus::types::Quaternion(0.f, 1.f, 0.f, (float)PI * -0.5f));
childTransformable1.GetTransform()->SetScale(1.f / 1.5f);
childTransformable2.GetTransform()->Rotate(lepus::types::Quaternion(0.f, 1.f, 0.f, (float)PI * (-50.f / 180.f)));

auto childNode3WorldMat = childTransformable3.GetWorldMatrix(childNode3);
lepus::types::Vector3 childNode3World(childNode3WorldMat.get(0, 3), childNode3WorldMat.get(1, 3), childNode3WorldMat.get(2, 3));

auto childNode2World = childTransformable2.GetWorldMatrix(childNode2).Multiply(lepus::types::Vector4(0.f, 0.f, 0.f, 1.f));
auto childNode2WorldMat = childTransformable2.GetWorldMatrix(childNode2);
lepus::types::Vector3 childNode2World(childNode2WorldMat.get(0, 3), childNode2WorldMat.get(1, 3), childNode2WorldMat.get(2, 3));

auto childNode1World = childTransformable2.GetWorldMatrix(childNode1).Multiply(lepus::types::Vector4(0.f, 0.f, 0.f, 1.f));
auto childNode1WorldMat = childTransformable1.GetWorldMatrix(childNode1);
lepus::types::Vector3 childNode1World(childNode1WorldMat.get(0, 3), childNode1WorldMat.get(1, 3), childNode1WorldMat.get(2, 3));

auto finalRootPos = rootTransformable.GetTransform()->Origin();
ASSERT_EQ(finalRootPos.x(), 0.f);
Expand All @@ -74,4 +87,9 @@ TEST(SceneGraphTest, SceneGraphChildTransformsCreateCorrectWorldCoords)
ASSERT_NEAR(cn2X, 2.f, 0.0001f);
ASSERT_NEAR(cn2Y, 0.f, 0.0001f);
ASSERT_NEAR(cn2Z, -2.f, 0.0001f);

float cn3X = childNode3World.x(), cn3Y = childNode3World.y(), cn3Z = childNode3World.z();
ASSERT_NEAR(cn3X, -0.55f, 0.03f);
ASSERT_NEAR(cn3Y, 0.f, 0.0001f);
ASSERT_NEAR(cn3Z, -5.06f, 0.01f);
}

0 comments on commit 5cc821e

Please sign in to comment.