Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
scene_component_2d_tests.cpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
8
9using namespace rendering_engine;
11
12TEST(SceneComponent2D, DefaultsToIdentity)
13{
14 SceneComponent2D component;
15 ExpectNear(component.GetPosition(), glm::vec2(0));
16 EXPECT_NEAR(component.GetRotation(), 0.0f, 1e-5f);
17 ExpectNear(component.GetScale(), glm::vec2(1));
18 ExpectNear(component.GetWorldMatrix(), glm::mat4(1));
19}
20
21TEST(SceneComponent2D, SettersComposeScaleRotationTranslation)
22{
23 SceneComponent2D component;
24 component.GetWorldMatrix();
25 component.SetPosition(glm::vec2(10, 20));
26 component.SetRotation(90);
27 component.SetScale(glm::vec2(2, 3));
28 ExpectNear(component.GetPosition(), glm::vec2(10, 20));
29 EXPECT_NEAR(component.GetRotation(), 90.0f, 1e-5f);
30 ExpectNear(component.GetScale(), glm::vec2(2, 3));
31 ExpectNear(component.GetWorldPosition(), glm::vec2(10, 20));
32 ExpectNear(component.GetWorldScale(), glm::vec2(2, 3));
33 // (1,2) -> (2,6) -> (-6,2) -> (4,22).
34 ExpectNear(component.GetWorldMatrix() * glm::vec4(1, 2, 0, 1), glm::vec4(4, 22, 0, 1));
35 ExpectNear(component.GetWorldMatrix() * glm::vec4(1, 2, 0, 0), glm::vec4(-6, 2, 0, 0));
36 glm::mat4 expected(1);
37 expected[0] = glm::vec4(0, 2, 0, 0);
38 expected[1] = glm::vec4(-3, 0, 0, 0);
39 expected[3] = glm::vec4(10, 20, 0, 1);
40 ExpectNear(component.GetWorldMatrix(), expected);
41}
42
43TEST(SceneComponent2D, ParentTranslationRotationAndScaleAffectChild)
44{
45 SceneComponent2D parent;
46 SceneComponent2D child;
47 child.SetPosition(glm::vec2(1, 2));
48 child.AttachTo(&parent);
49 parent.SetPosition(glm::vec2(10, 20));
50 parent.UpdateWorldMatrix();
51 child.UpdateWorldMatrix();
52 ExpectNear(child.GetWorldPosition(), glm::vec2(11, 22));
53
54 parent.SetPosition(glm::vec2(0));
55 parent.SetRotation(90);
56 parent.UpdateWorldMatrix();
57 child.UpdateWorldMatrix();
58 ExpectNear(child.GetWorldPosition(), glm::vec2(-2, 1));
59
60 parent.SetRotation(0);
61 parent.SetScale(glm::vec2(2, 3));
62 child.SetScale(glm::vec2(3, 2));
63 parent.UpdateWorldMatrix();
64 child.UpdateWorldMatrix();
65 ExpectNear(child.GetWorldPosition(), glm::vec2(2, 6));
66 ExpectNear(child.GetWorldScale(), glm::vec2(6));
67 // GetWorldRotation's extraction convention is deferred; matrices are tested.
68}
69
70TEST(SceneComponent2D, NestedTransformsRefreshWithExplicitUpdates)
71{
72 SceneComponent2D root;
73 SceneComponent2D child;
75 root.SetPosition(glm::vec2(10, 20));
76 root.SetRotation(90);
77 root.SetScale(glm::vec2(2));
78 child.SetPosition(glm::vec2(1, 0));
79 child.SetRotation(90);
80 child.SetScale(glm::vec2(3));
81 child.AttachTo(&root);
82 leaf.SetPosition(glm::vec2(1, 0));
83 leaf.AttachTo(&child);
84 root.UpdateWorldMatrix();
85 child.UpdateWorldMatrix();
86 leaf.UpdateWorldMatrix();
87 glm::mat4 expected(1);
88 expected[0][0] = -6;
89 expected[1][1] = -6;
90 expected[3] = glm::vec4(4, 22, 0, 1);
91 ExpectNear(leaf.GetWorldMatrix(), expected);
92 ExpectNear(leaf.GetWorldPosition(), glm::vec2(4, 22));
93 ExpectNear(leaf.GetWorldScale(), glm::vec2(6));
94
95 root.SetPosition(glm::vec2(20, 30));
96 root.UpdateWorldMatrix();
97 child.UpdateWorldMatrix();
98 leaf.UpdateWorldMatrix();
99 expected[3] = glm::vec4(14, 32, 0, 1);
100 ExpectNear(leaf.GetWorldMatrix(), expected);
101}
102
103TEST(SceneComponent2D, DetachRetainsLocalTransform)
104{
105 SceneComponent2D parent;
106 SceneComponent2D child;
107 child.SetPosition(glm::vec2(1, 2));
108 child.SetRotation(30);
109 child.SetScale(glm::vec2(2, 3));
110 const glm::mat4 local = child.GetWorldMatrix();
111 parent.SetPosition(glm::vec2(10, 0));
112 child.AttachTo(&parent);
113 child.UpdateWorldMatrix();
114 ExpectNear(child.GetWorldPosition(), glm::vec2(11, 2));
115 child.AttachTo(nullptr);
116 ExpectNear(child.GetWorldMatrix(), local);
117 ExpectNear(child.GetPosition(), glm::vec2(1, 2));
118 EXPECT_NEAR(child.GetRotation(), 30.0f, 1e-5f);
119 ExpectNear(child.GetScale(), glm::vec2(2, 3));
120}
Represents a hierarchical 2D transform component.
const glm::vec2 & GetScale() const
Gets the current scale.
void UpdateWorldMatrix()
Recomputes the world transformation matrix.
glm::vec2 GetWorldScale() const
Returns the accumulated world-space scale.
const glm::vec2 & GetPosition() const
Gets the current position.
glm::vec2 GetWorldPosition() const
Returns the world-space position of this component.
void AttachTo(SceneComponent2D *parent)
Attaches this scene component to a parent scene component.
void SetScale(const glm::vec2 &scale)
Sets the scale in each dimension.
float GetRotation() const
Gets the current rotation angle (in degrees).
const glm::mat4 & GetWorldMatrix()
Returns the world transformation matrix.
void SetRotation(float angleDegrees)
Sets the rotation angle in degrees.
void SetPosition(const glm::vec2 &position)
Sets the local position of this component.
void ExpectNear(const glm::vec< N, T, Q > &actual, const glm::vec< N, T, Q > &expected, float tolerance=1e-5f)
TEST(SceneComponent2D, DefaultsToIdentity)