[dali_2.1.31] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-ViewProjection.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // Enable debug log for test coverage
19 #define DEBUG_ENABLED 1
20
21 #include "dali-scene3d/public-api/loader/view-projection.h"
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25 using namespace Dali::Scene3D::Loader;
26
27 namespace
28 {
29 void SetPerspectiveProjection(Matrix& m)
30 {
31   auto data = m.AsFloat();
32   float near = 1.f;
33   float far = 10.f;
34   float right = 1.f;
35   float left = -right;
36   float top = .75f;
37   float bottom = -top;
38   data[0] = 2.f * near / (right - left);
39   data[5] = 2.f * near / (top - bottom);
40   data[8] = (right + left) / (right - left);
41   data[9] = (top + bottom) / (top - bottom);
42   data[10] = (far + near) / (far - near);
43   data[11] = -1.f;
44   data[14] = 2 * far * near / (far - near);
45 }
46
47 }
48
49 int UtcDaliViewProjection(void)
50 {
51   ViewProjection vp;
52   Matrix viewMatrix;
53   viewMatrix.SetTransformComponents( Vector3::ONE,
54     Quaternion(Radian(Degree(90.f)), Vector3::YAXIS),
55     Vector3::XAXIS * 200.f);
56   vp.GetView() = viewMatrix;
57
58   Matrix projectionMatrix;
59   SetPerspectiveProjection(projectionMatrix);
60   vp.GetProjection() = projectionMatrix;
61
62   vp.Update();
63
64   [&](const ViewProjection& vp) {
65     DALI_TEST_EQUAL(vp.GetView(), viewMatrix);
66     DALI_TEST_EQUAL(vp.GetProjection(), projectionMatrix);
67   }(vp);
68
69   Matrix expectedViewProjection{ false };
70   Matrix::Multiply(expectedViewProjection, viewMatrix, projectionMatrix);
71   auto viewProjectionResult = vp.GetViewProjection();
72   DALI_TEST_EQUAL(viewProjectionResult, expectedViewProjection);
73
74   Matrix expectedInverseProjection{ projectionMatrix };
75   expectedInverseProjection.Invert();
76   auto inverseProjectionResult = vp.GetInverseProjection();
77   DALI_TEST_EQUAL(inverseProjectionResult, expectedInverseProjection);
78
79   END_TEST;
80 }
81
82 int UtcDaliViewProjectionUpdateFail(void)
83 {
84   ViewProjection vp;
85   DALI_TEST_ASSERTION(vp.Update(), "Failed to find inverse");
86
87   END_TEST;
88 }