Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
scene_component_2d_tests.cpp File Reference

Go to the source code of this file.

Functions

 TEST (SceneComponent2D, DefaultsToIdentity)
 
 TEST (SceneComponent2D, SettersComposeScaleRotationTranslation)
 
 TEST (SceneComponent2D, ParentTranslationRotationAndScaleAffectChild)
 
 TEST (SceneComponent2D, NestedTransformsRefreshWithExplicitUpdates)
 
 TEST (SceneComponent2D, DetachRetainsLocalTransform)
 

Function Documentation

◆ TEST() [1/5]

TEST ( SceneComponent2D  ,
DefaultsToIdentity   
)

Definition at line 12 of file scene_component_2d_tests.cpp.

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}
Represents a hierarchical 2D transform component.
const glm::vec2 & GetScale() const
Gets the current scale.
const glm::vec2 & GetPosition() const
Gets the current position.
float GetRotation() const
Gets the current rotation angle (in degrees).
const glm::mat4 & GetWorldMatrix()
Returns the world transformation matrix.
void ExpectNear(const glm::vec< N, T, Q > &actual, const glm::vec< N, T, Q > &expected, float tolerance=1e-5f)

◆ TEST() [2/5]

TEST ( SceneComponent2D  ,
DetachRetainsLocalTransform   
)

Definition at line 103 of file scene_component_2d_tests.cpp.

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}
void UpdateWorldMatrix()
Recomputes the world transformation matrix.
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.
void SetRotation(float angleDegrees)
Sets the rotation angle in degrees.
void SetPosition(const glm::vec2 &position)
Sets the local position of this component.

◆ TEST() [3/5]

TEST ( SceneComponent2D  ,
NestedTransformsRefreshWithExplicitUpdates   
)

Definition at line 70 of file scene_component_2d_tests.cpp.

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}
glm::vec2 GetWorldScale() const
Returns the accumulated world-space scale.

◆ TEST() [4/5]

TEST ( SceneComponent2D  ,
ParentTranslationRotationAndScaleAffectChild   
)

Definition at line 43 of file scene_component_2d_tests.cpp.

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}

◆ TEST() [5/5]

TEST ( SceneComponent2D  ,
SettersComposeScaleRotationTranslation   
)

Definition at line 21 of file scene_component_2d_tests.cpp.

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}