Move test assets to devtest, add corresponding entities

This commit is contained in:
Lars Mueller 2024-06-08 17:31:39 +02:00
parent be03a435b8
commit 37c3edfc38
13 changed files with 61 additions and 47 deletions

@ -0,0 +1,22 @@
local function register_entity(name, textures)
minetest.register_entity("gltf:" .. name, {
initial_properties = {
visual = "mesh",
mesh = name .. ".gltf",
textures = textures,
},
})
end
-- These do not have texture coordinates; they simple render as black surfaces.
register_entity("minimal_triangle", {})
register_entity("triangle_with_vertex_stride", {})
register_entity("triangle_without_indices", {})
-- TODO provide proper textures
do
local cube_textures = {"no_texture.png"}
register_entity("blender_cube", cube_textures)
register_entity("blender_cube_scaled", cube_textures)
register_entity("blender_cube_matrix_transform", cube_textures)
end
register_entity("snow_man", {"no_texture.png"})

@ -645,11 +645,6 @@ if(BUILD_CLIENT)
if (USE_SPATIAL) if (USE_SPATIAL)
target_link_libraries(${PROJECT_NAME} ${SPATIAL_LIBRARY}) target_link_libraries(${PROJECT_NAME} ${SPATIAL_LIBRARY})
endif() endif()
if(BUILD_UNITTESTS)
target_compile_definitions(${PROJECT_NAME} PRIVATE
UNITTEST_ASSETS_DIRECTORY=${UNITTEST_ASSETS_DIRECTORY}
)
endif()
if(BUILD_UNITTESTS OR BUILD_BENCHMARKS) if(BUILD_UNITTESTS OR BUILD_BENCHMARKS)
target_link_libraries(${PROJECT_NAME} Catch2::Catch2) target_link_libraries(${PROJECT_NAME} Catch2::Catch2)
endif() endif()
@ -717,11 +712,6 @@ if(BUILD_SERVER)
${CURL_LIBRARY} ${CURL_LIBRARY}
) )
endif() endif()
if(BUILD_UNITTESTS)
target_compile_definitions(${PROJECT_NAME}server PRIVATE
UNITTEST_ASSETS_DIRECTORY=${UNITTEST_ASSETS_DIRECTORY}
)
endif()
if(BUILD_UNITTESTS OR BUILD_BENCHMARKS) if(BUILD_UNITTESTS OR BUILD_BENCHMARKS)
target_link_libraries(${PROJECT_NAME}server Catch2::Catch2) target_link_libraries(${PROJECT_NAME}server Catch2::Catch2)
endif() endif()

@ -53,5 +53,3 @@ set (UNITTEST_CLIENT_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_mesh_compare.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_mesh_compare.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp
PARENT_SCOPE) PARENT_SCOPE)
set (UNITTEST_ASSETS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/" PARENT_SCOPE)

@ -1,9 +1,8 @@
// Minetest // Minetest
// SPDX-License-Identifier: LGPL-2.1-or-later // SPDX-License-Identifier: LGPL-2.1-or-later
#ifndef UNITTEST_ASSETS_DIRECTORY #include "content/subgames.h"
#error "The required definition for UNITTEST_ASSETS_DIRECTORY is missing." #include "filesys.h"
#endif
#include "CReadFile.h" #include "CReadFile.h"
#include "vector3d.h" #include "vector3d.h"
@ -16,27 +15,22 @@
#include <cstring> #include <cstring>
#define XSTR(s) STR(s)
#define STR(s) #s
using namespace std; using namespace std;
class ScopedMesh class ScopedMesh
{ {
public: public:
ScopedMesh(irr::io::IReadFile* file) ScopedMesh(irr::io::IReadFile *file)
: m_device { irr::createDevice(irr::video::EDT_NULL) } : m_device { irr::createDevice(irr::video::EDT_NULL) }
, m_mesh { nullptr }
{ {
auto* smgr = m_device->getSceneManager(); auto *smgr = m_device->getSceneManager();
m_mesh = smgr->getMesh(file); m_mesh = smgr->getMesh(file);
} }
ScopedMesh(const irr::io::path& filepath) ScopedMesh(const irr::io::path& filepath)
: m_device { irr::createDevice(irr::video::EDT_NULL) } : m_device { irr::createDevice(irr::video::EDT_NULL) }
, m_mesh { nullptr }
{ {
auto* smgr = m_device->getSceneManager(); auto *smgr = m_device->getSceneManager();
irr::io::CReadFile f = irr::io::CReadFile(filepath); irr::io::CReadFile f = irr::io::CReadFile(filepath);
m_mesh = smgr->getMesh(&f); m_mesh = smgr->getMesh(&f);
} }
@ -44,17 +38,16 @@ class ScopedMesh
~ScopedMesh() ~ScopedMesh()
{ {
m_device->drop(); m_device->drop();
m_mesh = nullptr;
} }
const irr::scene::IAnimatedMesh* getMesh() const const irr::scene::IAnimatedMesh *getMesh() const
{ {
return m_mesh; return m_mesh;
} }
private: private:
irr::IrrlichtDevice* m_device; irr::IrrlichtDevice *m_device;
irr::scene::IAnimatedMesh* m_mesh; irr::scene::IAnimatedMesh *m_mesh = nullptr;
}; };
using v3f = irr::core::vector3df; using v3f = irr::core::vector3df;
@ -62,17 +55,38 @@ using v2f = irr::core::vector2df;
TEST_CASE("gltf") { TEST_CASE("gltf") {
SECTION("load empty gltf file") { const auto gamespec = findSubgame("devtest");
ScopedMesh sm(XSTR(UNITTEST_ASSETS_DIRECTORY) "empty.gltf");
CHECK(sm.getMesh() == nullptr); REQUIRE(gamespec.isValid());
// TODO use SKIP() when Catch2 is upgraded to v3
const static auto model_path = gamespec.gamemods_path + DIR_DELIM + "gltf" + DIR_DELIM + "models" + DIR_DELIM;
SECTION("error cases") {
const static auto invalid_model_path = gamespec.gamemods_path + DIR_DELIM + "gltf" + DIR_DELIM + "invalid" + DIR_DELIM;
SECTION("empty gltf file") {
ScopedMesh sm(model_path + "empty.gltf");
CHECK(sm.getMesh() == nullptr);
}
SECTION("null file pointer") {
ScopedMesh sm(nullptr);
CHECK(sm.getMesh() == nullptr);
}
SECTION("invalid JSON") {
ScopedMesh sm(model_path + "json_missing_brace.gltf");
CHECK(sm.getMesh() == nullptr);
}
} }
SECTION("minimal triangle") { SECTION("minimal triangle") {
auto path = GENERATE( auto path = GENERATE(
XSTR(UNITTEST_ASSETS_DIRECTORY) "minimal_triangle.gltf", model_path + "minimal_triangle.gltf",
XSTR(UNITTEST_ASSETS_DIRECTORY) "triangle_with_vertex_stride.gltf", model_path + "triangle_with_vertex_stride.gltf",
// Test non-indexed geometry. // Test non-indexed geometry.
XSTR(UNITTEST_ASSETS_DIRECTORY) "triangle_without_indices.gltf"); model_path + "triangle_without_indices.gltf");
INFO(path); INFO(path);
ScopedMesh sm(path); ScopedMesh sm(path);
REQUIRE(sm.getMesh() != nullptr); REQUIRE(sm.getMesh() != nullptr);
@ -98,7 +112,7 @@ SECTION("minimal triangle") {
} }
SECTION("blender cube") { SECTION("blender cube") {
ScopedMesh sm(XSTR(UNITTEST_ASSETS_DIRECTORY) "blender_cube.gltf"); ScopedMesh sm(model_path + "blender_cube.gltf");
REQUIRE(sm.getMesh() != nullptr); REQUIRE(sm.getMesh() != nullptr);
REQUIRE(sm.getMesh()->getMeshBufferCount() == 1); REQUIRE(sm.getMesh()->getMeshBufferCount() == 1);
SECTION("vertex coordinates are correct") { SECTION("vertex coordinates are correct") {
@ -150,18 +164,8 @@ SECTION("blender cube") {
} }
} }
SECTION("mesh loader returns nullptr when given null file pointer") {
ScopedMesh sm(nullptr);
CHECK(sm.getMesh() == nullptr);
}
SECTION("invalid JSON returns nullptr") {
ScopedMesh sm(XSTR(UNITTEST_ASSETS_DIRECTORY) "json_missing_brace.gltf");
CHECK(sm.getMesh() == nullptr);
}
SECTION("blender cube scaled") { SECTION("blender cube scaled") {
ScopedMesh sm(XSTR(UNITTEST_ASSETS_DIRECTORY) "blender_cube_scaled.gltf"); ScopedMesh sm(model_path + "blender_cube_scaled.gltf");
REQUIRE(sm.getMesh() != nullptr); REQUIRE(sm.getMesh() != nullptr);
REQUIRE(sm.getMesh()->getMeshBufferCount() == 1); REQUIRE(sm.getMesh()->getMeshBufferCount() == 1);
@ -182,7 +186,7 @@ SECTION("blender cube scaled") {
} }
SECTION("blender cube matrix transform") { SECTION("blender cube matrix transform") {
ScopedMesh sm(XSTR(UNITTEST_ASSETS_DIRECTORY) "blender_cube_matrix_transform.gltf"); ScopedMesh sm(model_path + "blender_cube_matrix_transform.gltf");
REQUIRE(sm.getMesh() != nullptr); REQUIRE(sm.getMesh() != nullptr);
REQUIRE(sm.getMesh()->getMeshBufferCount() == 1); REQUIRE(sm.getMesh()->getMeshBufferCount() == 1);
@ -208,7 +212,7 @@ SECTION("blender cube matrix transform") {
} }
SECTION("snow man") { SECTION("snow man") {
ScopedMesh sm(XSTR(UNITTEST_ASSETS_DIRECTORY) "snow_man.gltf"); ScopedMesh sm(model_path + "snow_man.gltf");
REQUIRE(sm.getMesh() != nullptr); REQUIRE(sm.getMesh() != nullptr);
REQUIRE(sm.getMesh()->getMeshBufferCount() == 3); REQUIRE(sm.getMesh()->getMeshBufferCount() == 3);