Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/tests/test_Camera.h
/* Copyright (C) 2019 Wildfire Games. | |||||
* This file is part of 0 A.D. | |||||
* | |||||
* 0 A.D. is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 2 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* 0 A.D. is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "lib/self_test.h" | |||||
#include "graphics/Camera.h" | |||||
#include "maths/MathUtil.h" | |||||
#include "maths/Vector3D.h" | |||||
#include <vector> | |||||
class TestCamera : public CxxTest::TestSuite | |||||
{ | |||||
public: | |||||
void test_frustum_perspective() | |||||
{ | |||||
std::cout << std::endl; | |||||
SViewPort viewPort; | |||||
viewPort.m_X = 0; | |||||
viewPort.m_Y = 0; | |||||
viewPort.m_Width = 512; | |||||
Stan: Might want to try with a non-square and non power of 2 resolution. | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsYep, but it won't have so nice normals. Probably in next tests. vladislavbelov: Yep, but it won't have so nice normals. Probably in next tests. | |||||
StanUnsubmitted Not Done Inline ActionsMore likely to break then :P Stan: More likely to break then :P | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsThe patch is more about a valid frustum. I'll add few tests in next patches. vladislavbelov: The patch is more about a valid frustum. I'll add few tests in next patches. | |||||
viewPort.m_Height = 512; | |||||
CCamera camera; | |||||
camera.SetViewPort(viewPort); | |||||
camera.LookAlong( | |||||
CVector3D(0.0f, 0.0f, 0.0f), | |||||
CVector3D(0.0f, 0.0f, 1.0f), | |||||
CVector3D(0.0f, 1.0f, 0.0f) | |||||
); | |||||
camera.SetProjection(1.0f, 101.0f, DEGTORAD(90.0f)); | |||||
camera.UpdateFrustum(); | |||||
const float sqrt2 = sqrtf(2.0f) / 2.0f; | |||||
const std::vector<CPlane> expectedPlanes = { | |||||
CVector4D(sqrt2, 0.0f, sqrt2, 0.0f), | |||||
CVector4D(-sqrt2, 0.0f, sqrt2, 0.0f), | |||||
CVector4D(0.0f, sqrt2, sqrt2, 0.0f), | |||||
CVector4D(0.0f, -sqrt2, sqrt2, 0.0f), | |||||
CVector4D(0.0f, 0.0f, -1.0f, 101.0f), | |||||
CVector4D(0.0f, 0.0f, 1.0f, -1.0f), | |||||
}; | |||||
CheckFrustumPlanes(camera.GetFrustum(), expectedPlanes); | |||||
} | |||||
// We don't have any asumption about order of planes. So use interactive checker. | |||||
StanUnsubmitted Not Done Inline ActionsOrder of planes is unknown maybe. Stan: Order of planes is unknown maybe. | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsSounds ok. vladislavbelov: Sounds ok. | |||||
void CheckFrustumPlanes(const CFrustum& frustum, const std::vector<CPlane>& expectedPlanes) | |||||
{ | |||||
std::cout.precision(7); | |||||
std::cout << std::fixed; | |||||
TS_ASSERT_EQUALS(frustum.GetNumPlanes(), expectedPlanes.size()); | |||||
std::set<size_t> indices; | |||||
for (size_t i = 0; i < expectedPlanes.size(); ++i) | |||||
indices.insert(i); | |||||
for (size_t i = 0; i < frustum.GetNumPlanes(); ++i) | |||||
{ | |||||
bool found = false; | |||||
for (size_t j : indices) | |||||
{ | |||||
if (EqualPlanes(frustum[i], expectedPlanes[j])) | |||||
{ | |||||
found = true; | |||||
indices.erase(j); | |||||
break; | |||||
} | |||||
} | |||||
TS_ASSERT(found); | |||||
} | |||||
} | |||||
bool EqualPlanes(const CPlane& p1, const CPlane& p2) const | |||||
{ | |||||
const float EPS = 1e-3f; | |||||
if (std::fabsf(p1.m_Dist - p2.m_Dist) >= EPS) | |||||
StanUnsubmitted Not Done Inline ActionsI didn't know this existed. nice. Is it slower than a standard check ? Doesn't matter here anyway. Stan: I didn't know this existed. nice. Is it slower than a standard check ? Doesn't matter here… | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsWhat do mean by standard? std::abs? vladislavbelov: What do mean by standard? `std::abs`? | |||||
StanUnsubmitted Not Done Inline ActionsNah the => <= way :) Stan: Nah the => <= way :) | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsAh, we don't compare by less/greater, we check equality. But we can't just compare float point numbers, we have to use epsilon. So |x - expected| < EPS means that the x is lying inside an epsilon neighbourhood of expected. vladislavbelov: Ah, we don't compare by less/greater, we check equality. But we can't just compare float point… | |||||
return false; | |||||
return | |||||
std::fabsf(p1.m_Norm.X - p2.m_Norm.X) < EPS && | |||||
std::fabsf(p1.m_Norm.Y - p2.m_Norm.Y) < EPS && | |||||
std::fabsf(p1.m_Norm.Z - p2.m_Norm.Z) < EPS; | |||||
} | |||||
}; |
Wildfire Games · Phabricator
Might want to try with a non-square and non power of 2 resolution.