|
| 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