Skip to content

Commit

Permalink
2498: fix copy/paste error in EntityModelLoadedFrame::addToSpacialTree
Browse files Browse the repository at this point in the history
add test case for ray->BSP29 model intersection
  • Loading branch information
ericwa committed Apr 20, 2020
1 parent 8fce68c commit f843fcd
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
4 changes: 2 additions & 2 deletions common/src/Assets/EntityModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace TrenchBroom {
const auto& p3 = Renderer::getVertexComponent<0>(vertices[index + i + 1]);
bounds.add(p1);
bounds.add(p2);
bounds.add(p2);
bounds.add(p3);

const size_t triIndex = m_tris.size() / 3u;
m_tris.push_back(p1);
Expand All @@ -142,7 +142,7 @@ namespace TrenchBroom {
const auto& p3 = Renderer::getVertexComponent<0>(vertices[index + i + 2]);
bounds.add(p1);
bounds.add(p2);
bounds.add(p2);
bounds.add(p3);

const size_t triIndex = m_tris.size() / 3u;
if (i % 2 == 0) {
Expand Down
1 change: 1 addition & 0 deletions common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(COMMON_TEST_SOURCE
"${COMMON_TEST_SOURCE_DIR}/IO/DkPakFileSystemTest.cpp"
"${COMMON_TEST_SOURCE_DIR}/IO/ELParserTest.cpp"
"${COMMON_TEST_SOURCE_DIR}/IO/EntParserTest.cpp"
"${COMMON_TEST_SOURCE_DIR}/IO/EntityModelTest.cpp"
"${COMMON_TEST_SOURCE_DIR}/IO/FgdParserTest.cpp"
"${COMMON_TEST_SOURCE_DIR}/IO/FreeImageTextureReaderTest.cpp"
"${COMMON_TEST_SOURCE_DIR}/IO/GameConfigParserTest.cpp"
Expand Down
Binary file added common/test/fixture/Model/Game/Quake/id1/cube.bsp
Binary file not shown.
15 changes: 15 additions & 0 deletions common/test/fixture/Model/Game/Quake/id1/cube.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Game: Quake
// Format: Standard
// entity 0
{
"classname" "worldspawn"
// brush 0
{
( -32 -32 -32 ) ( -32 -31 -32 ) ( -32 -32 -31 ) __TB_empty -0 -0 -0 1 1
( -32 -32 -32 ) ( -32 -32 -31 ) ( -31 -32 -32 ) __TB_empty -0 -0 -0 1 1
( -32 -32 -32 ) ( -31 -32 -32 ) ( -32 -31 -32 ) __TB_empty -0 -0 -0 1 1
( 32 32 32 ) ( 32 33 32 ) ( 33 32 32 ) __TB_empty -0 -0 -0 1 1
( 32 32 -16 ) ( 33 32 -16 ) ( 32 32 -15 ) __TB_empty -0 -0 -0 1 1
( 32 32 -16 ) ( 32 32 -15 ) ( 32 33 -16 ) __TB_empty -0 -0 -0 1 1
}
}
Binary file not shown.
92 changes: 92 additions & 0 deletions common/test/src/IO/EntityModelTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (C) 2010-2017 Kristian Duske
This file is part of TrenchBroom.
TrenchBroom 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 3 of the License, or
(at your option) any later version.
TrenchBroom 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 TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
*/

#include <catch2/catch.hpp>

#include "GTestCompat.h"

#include "TestLogger.h"

#include "Exceptions.h"
#include "IO/EntityModelLoader.h"
#include "Assets/EntityModel.h"
#include "IO/Path.h"
#include "Model/Game.h"

#include <optional>

#include "IO/DiskIO.h"
#include "IO/GameConfigParser.h"
#include "Model/GameImpl.h"
#include "Model/GameConfig.h"

#include <vecmath/bbox.h>
#include <vecmath/intersection.h>
#include <vecmath/ray.h>

namespace TrenchBroom {
namespace IO {
TEST_CASE("BSP model intersection test", "[EntityModelTest]") {
TestLogger logger;

const auto configPath = IO::Disk::getCurrentWorkingDir() + IO::Path("fixture/games/Quake/GameConfig.cfg");
const auto gamePath = IO::Disk::getCurrentWorkingDir() + IO::Path("fixture/test/Model/Game/Quake");
const auto configStr = IO::Disk::readFile(configPath);
auto configParser = IO::GameConfigParser(configStr, configPath);
Model::GameConfig config = configParser.parse();

auto game = Model::GameImpl(config, gamePath, logger);

const auto path = IO::Path("cube.bsp");

std::unique_ptr<Assets::EntityModel> model = game.initializeModel(path, logger);
game.loadFrame(path, 0, *model, logger);

Assets::EntityModelFrame* frame = model->frames().at(0);

const auto box = vm::bbox3f(vm::vec3f::fill(-32), vm::vec3f::fill(32));
CHECK(box == frame->bounds());

// test some hitting rays
for (int x = -45; x <= 45; x += 15) {
for (int y = -45; y <= 45; y += 15) {
for (int z = -45; z <= 45; z += 15) {
// shoot a ray from (x, y, z) to (0, 0, 0), it will hit the box
const auto startPoint = vm::vec3f(x, y, z);
if (box.contains(startPoint)) {
continue;
}
const auto endPoint = vm::vec3f::zero();
const auto ray = vm::ray3f(startPoint, vm::normalize(endPoint - startPoint));

const float aabbDist = frame->intersect(ray);
const float expected = vm::intersect_ray_bbox(ray, box);

CHECK(expected == Approx(aabbDist));
}
}
}

// test a missing ray
const auto missRay = vm::ray3f(vm::vec3f(0, -33, -33), vm::vec3f::pos_y());
CHECK(vm::is_nan(frame->intersect(missRay)));
CHECK(vm::is_nan(vm::intersect_ray_bbox(missRay, box)));
}
}
}

0 comments on commit f843fcd

Please sign in to comment.