diff --git a/src/Magnum/DebugTools/Test/CMakeLists.txt b/src/Magnum/DebugTools/Test/CMakeLists.txt index 7847ed2be..fb2ab7c5f 100644 --- a/src/Magnum/DebugTools/Test/CMakeLists.txt +++ b/src/Magnum/DebugTools/Test/CMakeLists.txt @@ -135,5 +135,29 @@ if(TARGET_GL) endif() endif() endif() + + if(WITH_SCENEGRAPH AND WITH_TRADE) + corrade_add_test(DebugToolsForceRendererGLTest ForceRendererGLTest.cpp LIBRARIES MagnumDebugTools MagnumOpenGLTester) + corrade_add_test(DebugToolsObjectRendererGLTest ObjectRendererGLTest.cpp LIBRARIES MagnumDebugTools MagnumOpenGLTester) + set_target_properties( + DebugToolsForceRendererGLTest + DebugToolsObjectRendererGLTest + PROPERTIES FOLDER "Magnum/DebugTools/Test") + if(NOT BUILD_PLUGINS_STATIC) + target_include_directories(DebugToolsForceRendererGLTest PRIVATE $) + target_include_directories(DebugToolsObjectRendererGLTest PRIVATE $) + else() + target_include_directories(DebugToolsForceRendererGLTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + target_include_directories(DebugToolsObjectRendererGLTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + if(WITH_ANYIMAGEIMPORTER) + target_link_libraries(DebugToolsForceRendererGLTest PRIVATE AnyImageImporter) + target_link_libraries(DebugToolsObjectRendererGLTest PRIVATE AnyImageImporter) + endif() + if(WITH_TGAIMPORTER) + target_link_libraries(DebugToolsForceRendererGLTest PRIVATE TgaImporter) + target_link_libraries(DebugToolsObjectRendererGLTest PRIVATE TgaImporter) + endif() + endif() + endif() endif() endif() diff --git a/src/Magnum/DebugTools/Test/ForceRenderer2D.tga b/src/Magnum/DebugTools/Test/ForceRenderer2D.tga new file mode 100644 index 000000000..f01e2c7f7 Binary files /dev/null and b/src/Magnum/DebugTools/Test/ForceRenderer2D.tga differ diff --git a/src/Magnum/DebugTools/Test/ForceRenderer3D.tga b/src/Magnum/DebugTools/Test/ForceRenderer3D.tga new file mode 100644 index 000000000..bf5345619 Binary files /dev/null and b/src/Magnum/DebugTools/Test/ForceRenderer3D.tga differ diff --git a/src/Magnum/DebugTools/Test/ForceRendererGLTest.cpp b/src/Magnum/DebugTools/Test/ForceRendererGLTest.cpp new file mode 100644 index 000000000..4463632ed --- /dev/null +++ b/src/Magnum/DebugTools/Test/ForceRendererGLTest.cpp @@ -0,0 +1,161 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include +#include + +#include "Magnum/Image.h" +#include "Magnum/PixelFormat.h" +#include "Magnum/DebugTools/CompareImage.h" +#include "Magnum/DebugTools/ForceRenderer.h" +#include "Magnum/DebugTools/ResourceManager.h" +#include "Magnum/GL/Framebuffer.h" +#include "Magnum/GL/OpenGLTester.h" +#include "Magnum/GL/Renderbuffer.h" +#include "Magnum/GL/RenderbufferFormat.h" +#include "Magnum/SceneGraph/Camera.h" +#include "Magnum/SceneGraph/Scene.h" +#include "Magnum/SceneGraph/MatrixTransformation2D.h" +#include "Magnum/SceneGraph/MatrixTransformation3D.h" +#include "Magnum/Trade/AbstractImporter.h" + +#include "configure.h" + +namespace Magnum { namespace DebugTools { namespace Test { namespace { + +struct ForceRendererGLTest: GL::OpenGLTester { + explicit ForceRendererGLTest(); + + void render2D(); + void render3D(); + + private: + PluginManager::Manager _manager{"nonexistent"}; +}; + +ForceRendererGLTest::ForceRendererGLTest() { + addTests({&ForceRendererGLTest::render2D, + &ForceRendererGLTest::render3D}); + + /* Load the plugins directly from the build tree. Otherwise they're either + static and already loaded or not present in the build tree */ + #ifdef ANYIMAGEIMPORTER_PLUGIN_FILENAME + CORRADE_INTERNAL_ASSERT(_manager.load(ANYIMAGEIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); + #endif + #ifdef TGAIMPORTER_PLUGIN_FILENAME + CORRADE_INTERNAL_ASSERT(_manager.load(TGAIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); + #endif +} + +using namespace Math::Literals; + +void ForceRendererGLTest::render2D() { + SceneGraph::Scene scene; + + SceneGraph::DrawableGroup2D drawables; + SceneGraph::Camera2D camera{scene}; + camera.setProjectionMatrix(Matrix3::projection({4.0f, 4.0f})); + + ResourceManager manager; + manager.set("my", ForceRendererOptions{}.setColor(0xff3366_rgbf)); + + SceneGraph::Object object{&scene}; + object.translate({-1.0f, -1.0f}); + Vector2 force{2.0f, 2.0f}; + ForceRenderer2D renderer{object, {}, force, "my", &drawables}; + + GL::Renderbuffer color; + color.setStorage( + #if !defined(MAGNUM_TARGET_GLES2) || !defined(MAGNUM_TARGET_WEBGL) + GL::RenderbufferFormat::RGBA8, + #else + GL::RenderbufferFormat::RGBA4, + #endif + {64, 64}); + GL::Framebuffer framebuffer{{{}, {64, 64}}}; + framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, color) + .clear(GL::FramebufferClear::Color) + .bind(); + + camera.draw(drawables); + + MAGNUM_VERIFY_NO_GL_ERROR(); + CORRADE_COMPARE_WITH( + framebuffer.read({{}, {64, 64}}, {PixelFormat::RGBA8Unorm}), + Utility::Directory::join(DEBUGTOOLS_TEST_DIR, "ForceRenderer2D.tga"), + CompareImageToFile{_manager}); +} + +void ForceRendererGLTest::render3D() { + SceneGraph::Scene scene; + + SceneGraph::DrawableGroup3D drawables; + SceneGraph::Object cameraObject{&scene}; + cameraObject.rotateY(90.0_degf); + SceneGraph::Camera3D camera{cameraObject}; + camera.setProjectionMatrix(Matrix4::orthographicProjection({4.0f, 4.0f}, 0.1f, 1.0f)); + + ResourceManager manager; + manager.set("my", ForceRendererOptions{}.setColor(0xff3366_rgbf)); + + SceneGraph::Object object{&scene}; + object + .rotateY(-90.0_degf) + .translate({-0.5f, -1.0f, 1.0f}); + Vector3 force{2.0f, 2.0f, 0.0f}; + ForceRenderer3D renderer{object, {}, force, "my", &drawables}; + + GL::Renderbuffer color; + color.setStorage( + #if !defined(MAGNUM_TARGET_GLES2) || !defined(MAGNUM_TARGET_WEBGL) + GL::RenderbufferFormat::RGBA8, + #else + GL::RenderbufferFormat::RGBA4, + #endif + {64, 64}); + GL::Framebuffer framebuffer{{{}, {64, 64}}}; + framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, color) + .clear(GL::FramebufferClear::Color) + .bind(); + + camera.draw(drawables); + + MAGNUM_VERIFY_NO_GL_ERROR(); + { + CORRADE_EXPECT_FAIL("I'm unable to convince it to orient the arrowhead correctly in 3D."); + CORRADE_COMPARE_WITH( + framebuffer.read({{}, {64, 64}}, {PixelFormat::RGBA8Unorm}), + Utility::Directory::join(DEBUGTOOLS_TEST_DIR, "ForceRenderer2D.tga"), + CompareImageToFile{_manager}); + } + CORRADE_COMPARE_WITH( + framebuffer.read({{}, {64, 64}}, {PixelFormat::RGBA8Unorm}), + Utility::Directory::join(DEBUGTOOLS_TEST_DIR, "ForceRenderer3D.tga"), + CompareImageToFile{_manager}); +} + +}}}} + +CORRADE_TEST_MAIN(Magnum::DebugTools::Test::ForceRendererGLTest) diff --git a/src/Magnum/DebugTools/Test/ObjectRenderer2D.tga b/src/Magnum/DebugTools/Test/ObjectRenderer2D.tga new file mode 100644 index 000000000..cbb922570 Binary files /dev/null and b/src/Magnum/DebugTools/Test/ObjectRenderer2D.tga differ diff --git a/src/Magnum/DebugTools/Test/ObjectRenderer3D.tga b/src/Magnum/DebugTools/Test/ObjectRenderer3D.tga new file mode 100644 index 000000000..bfe59d3cb Binary files /dev/null and b/src/Magnum/DebugTools/Test/ObjectRenderer3D.tga differ diff --git a/src/Magnum/DebugTools/Test/ObjectRendererGLTest.cpp b/src/Magnum/DebugTools/Test/ObjectRendererGLTest.cpp new file mode 100644 index 000000000..82907337c --- /dev/null +++ b/src/Magnum/DebugTools/Test/ObjectRendererGLTest.cpp @@ -0,0 +1,153 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include +#include + +#include "Magnum/Image.h" +#include "Magnum/PixelFormat.h" +#include "Magnum/DebugTools/CompareImage.h" +#include "Magnum/DebugTools/ObjectRenderer.h" +#include "Magnum/DebugTools/ResourceManager.h" +#include "Magnum/GL/Framebuffer.h" +#include "Magnum/GL/OpenGLTester.h" +#include "Magnum/GL/Renderbuffer.h" +#include "Magnum/GL/RenderbufferFormat.h" +#include "Magnum/SceneGraph/Camera.h" +#include "Magnum/SceneGraph/Scene.h" +#include "Magnum/SceneGraph/MatrixTransformation2D.h" +#include "Magnum/SceneGraph/MatrixTransformation3D.h" +#include "Magnum/Trade/AbstractImporter.h" + +#include "configure.h" + +namespace Magnum { namespace DebugTools { namespace Test { namespace { + +struct ObjectRendererGLTest: GL::OpenGLTester { + explicit ObjectRendererGLTest(); + + void render2D(); + void render3D(); + + private: + PluginManager::Manager _manager{"nonexistent"}; +}; + +ObjectRendererGLTest::ObjectRendererGLTest() { + addTests({&ObjectRendererGLTest::render2D, + &ObjectRendererGLTest::render3D}); + + /* Load the plugins directly from the build tree. Otherwise they're either + static and already loaded or not present in the build tree */ + #ifdef ANYIMAGEIMPORTER_PLUGIN_FILENAME + CORRADE_INTERNAL_ASSERT(_manager.load(ANYIMAGEIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); + #endif + #ifdef TGAIMPORTER_PLUGIN_FILENAME + CORRADE_INTERNAL_ASSERT(_manager.load(TGAIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); + #endif +} + +using namespace Math::Literals; + +void ObjectRendererGLTest::render2D() { + SceneGraph::Scene scene; + + SceneGraph::DrawableGroup2D drawables; + SceneGraph::Camera2D camera{scene}; + camera.setProjectionMatrix(Matrix3::projection({4.0f, 4.0f})); + + ResourceManager manager; + manager.set("my", ObjectRendererOptions{}.setSize(2.2f)); + + SceneGraph::Object object{&scene}; + object + .rotate(-17.3_degf) + .translate({-1.0f, -1.0f}); + ObjectRenderer2D renderer{object, "my", &drawables}; + + GL::Renderbuffer color; + color.setStorage( + #if !defined(MAGNUM_TARGET_GLES2) || !defined(MAGNUM_TARGET_WEBGL) + GL::RenderbufferFormat::RGBA8, + #else + GL::RenderbufferFormat::RGBA4, + #endif + {64, 64}); + GL::Framebuffer framebuffer{{{}, {64, 64}}}; + framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, color) + .clear(GL::FramebufferClear::Color) + .bind(); + + camera.draw(drawables); + + MAGNUM_VERIFY_NO_GL_ERROR(); + CORRADE_COMPARE_WITH( + framebuffer.read({{}, {64, 64}}, {PixelFormat::RGBA8Unorm}), + Utility::Directory::join(DEBUGTOOLS_TEST_DIR, "ObjectRenderer2D.tga"), + CompareImageToFile{_manager}); +} + +void ObjectRendererGLTest::render3D() { + SceneGraph::Scene scene; + + SceneGraph::DrawableGroup3D drawables; + SceneGraph::Camera3D camera{scene}; + camera.setProjectionMatrix(Matrix4::orthographicProjection({4.0f, 4.0f}, 0.1f, 2.0f)); + + ResourceManager manager; + manager.set("my", ObjectRendererOptions{}.setSize(2.2f)); + + SceneGraph::Object object{&scene}; + object + .rotateZ(17.3_degf) + .rotateY(45.0_degf) + .translate({-1.0f, -1.0f, -1.0f}); + ObjectRenderer3D renderer{object, "my", &drawables}; + + GL::Renderbuffer color; + color.setStorage( + #if !defined(MAGNUM_TARGET_GLES2) || !defined(MAGNUM_TARGET_WEBGL) + GL::RenderbufferFormat::RGBA8, + #else + GL::RenderbufferFormat::RGBA4, + #endif + {64, 64}); + GL::Framebuffer framebuffer{{{}, {64, 64}}}; + framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, color) + .clear(GL::FramebufferClear::Color) + .bind(); + + camera.draw(drawables); + + MAGNUM_VERIFY_NO_GL_ERROR(); + CORRADE_COMPARE_WITH( + framebuffer.read({{}, {64, 64}}, {PixelFormat::RGBA8Unorm}), + Utility::Directory::join(DEBUGTOOLS_TEST_DIR, "ObjectRenderer3D.tga"), + CompareImageToFile{_manager}); +} + +}}}} + +CORRADE_TEST_MAIN(Magnum::DebugTools::Test::ObjectRendererGLTest)