Skip to content

Commit d056f3c

Browse files
author
RubioJr9
committed
Added test for camera transform and simplified getWorldViewTransform()
1 parent b8f9e61 commit d056f3c

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

src/Externals/spire/arc-look-at/ArcLookAt.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "ArcLookAt.hpp"
3333
#include <arc-ball/ArcBall.hpp>
3434
#include <glm/gtx/vec_swizzle.hpp>
35+
#include <glm/gtx/transform.hpp>
3536

3637
namespace spire {
3738

@@ -104,13 +105,8 @@ void ArcLookAt::doZoom(const glm::vec2& ssPos)
104105
//------------------------------------------------------------------------------
105106
glm::mat4 ArcLookAt::getWorldViewTransform() const
106107
{
107-
glm::mat4 finalTrafo(1.0);
108-
for (int i = 0; i < mCamLookAt.length(); ++i)
109-
finalTrafo[3][i] = -mCamLookAt[i];
110-
111-
finalTrafo = mArcBall->getTransformation() * finalTrafo;
108+
auto finalTrafo = mArcBall->getTransformation() * glm::translate(glm::mat4(1.0f), -mCamLookAt);
112109
finalTrafo[3][2] -= mCamDistance;
113-
114110
return finalTrafo;
115111
}
116112

src/Graphics/Datatypes/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ IF(BUILD_SHARED_LIBS)
5050
ADD_DEFINITIONS(-DBUILD_Graphics_Datatypes)
5151
ENDIF(BUILD_SHARED_LIBS)
5252

53-
#SCIRUN_ADD_TEST_DIR(Tests)
53+
SCIRUN_ADD_TEST_DIR(Tests)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# For more information, please see: http://software.sci.utah.edu
3+
#
4+
# The MIT License
5+
#
6+
# Copyright (c) 2020 Scientific Computing and Imaging Institute,
7+
# University of Utah.
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a
10+
# copy of this software and associated documentation files (the "Software"),
11+
# to deal in the Software without restriction, including without limitation
12+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
13+
# and/or sell copies of the Software, and to permit persons to whom the
14+
# Software is furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included
17+
# in all copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25+
# DEALINGS IN THE SOFTWARE.
26+
#
27+
28+
29+
SET(Graphics_Datatypes_Tests_SRCS
30+
GLMTests.cc
31+
)
32+
33+
SCIRUN_ADD_UNIT_TEST(Graphics_Datatypes_Tests
34+
${Graphics_Datatypes_Tests_SRCS}
35+
)
36+
37+
TARGET_LINK_LIBRARIES(Graphics_Datatypes_Tests
38+
gtest_main
39+
gtest
40+
gmock
41+
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2020 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a
10+
copy of this software and associated documentation files (the "Software"),
11+
to deal in the Software without restriction, including without limitation
12+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
13+
and/or sell copies of the Software, and to permit persons to whom the
14+
Software is furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included
17+
in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25+
DEALINGS IN THE SOFTWARE.
26+
*/
27+
28+
#include <Core/Utils/Exception.h>
29+
#include <gtest/gtest.h>
30+
#include <glm/gtc/matrix_transform.hpp>
31+
#include <glm/gtc/type_ptr.hpp>
32+
#include <glm/gtx/string_cast.hpp>
33+
#include <glm/mat4x4.hpp>
34+
#include <glm/vec3.hpp>
35+
#include <glm/vec4.hpp>
36+
#include <iostream>
37+
#include <sstream>
38+
39+
using namespace ::testing;
40+
41+
const static float epsilon = 0.2e-5;
42+
43+
TEST(GLMTests, CameraTransform)
44+
{
45+
glm::mat4 t(1.0);
46+
float identityList[16] = {
47+
1, 0, 0, 0,
48+
0, 1, 0, 0,
49+
0, 0, 1, 0,
50+
0, 0, 0, 1};
51+
glm::mat4 identity = glm::make_mat4(identityList);
52+
ASSERT_EQ(t, identity);
53+
54+
glm::vec3 lookAt = glm::vec3(2, 4.5, 7);
55+
t = glm::translate(t, -lookAt);
56+
float translateList[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -2, -4.5, -7, 1};
57+
glm::mat4 translatedMat = glm::make_mat4(translateList);
58+
ASSERT_EQ(t, translatedMat);
59+
60+
const static float theta = static_cast<float>(M_PI) * 0.25f;
61+
auto rot = glm::rotate(glm::mat4(1.0f), theta, glm::vec3(0.f, 0.f, 1.f));
62+
t = rot * t;
63+
float rotList[16] = {std::cos(theta), std::sin(theta), 0, 0, -std::sin(theta), std::cos(theta), 0,
64+
0, 0, 0, 1, 0, 1.7677669, -4.5961941, -7, 1};
65+
glm::mat4 rotMat = glm::make_mat4(rotList);
66+
for (int i = 0; i < 4; ++i)
67+
for (int j = 0; j < 4; ++j)
68+
ASSERT_NEAR(t[i][j], rotMat[i][j], epsilon);
69+
70+
float camDist = 0.7;
71+
t[3][2] -= camDist;
72+
float camDistList[16] = {std::cos(theta), std::sin(theta), 0, 0, -std::sin(theta), std::cos(theta), 0,
73+
0, 0, 0, 1, 0, 1.7677669, -4.5961941, -7.7, 1};
74+
glm::mat4 camDistMat = glm::make_mat4(camDistList);
75+
for (int i = 0; i < 4; ++i)
76+
for (int j = 0; j < 4; ++j) ASSERT_NEAR(t[i][j], camDistMat[i][j], epsilon);
77+
}

0 commit comments

Comments
 (0)