Skip to content
Snippets Groups Projects
Select Git revision
  • 00326b620dc1a21034ec95aef1ef8109001ae02c
  • 5.4 default protected
  • 5.5
  • dev/5.5
  • dev/5.4
  • dev/5.3_downgrade
  • feature/experimenttime_hack
  • 5.3 protected
  • _IntenSelect5.3
  • IntenSelect5.3
  • 4.27 protected
  • 4.26 protected
  • 5.0 protected
  • 4.22 protected
  • 4.21 protected
  • UE5.4-2024.1
  • UE5.4-2024.1-rc1
  • UE5.3-2023.1-rc3
  • UE5.3-2023.1-rc2
  • UE5.3-2023.1-rc
20 results

VirtualRealityPawn.cpp

Blame
  • integration_test_model_rendering.cpp 5.99 KiB
    //------------------------------------------------------------------------------
    // Project Phoenix
    //
    // Copyright (c) 2017-2018 RWTH Aachen University, Germany,
    // Virtual Reality & Immersive Visualization Group.
    //------------------------------------------------------------------------------
    //                                 License
    //
    // Licensed under the 3-Clause BSD License (the "License");
    // you may not use this file except in compliance with the License.
    // See the file LICENSE for the full text.
    // You may obtain a copy of the License at
    //
    //     https://opensource.org/licenses/BSD-3-Clause
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    //------------------------------------------------------------------------------
    
    #include <memory>
    
    #include "catch/catch.hpp"
    
    #include "phx/assimp_model_loader.hpp"
    #include "phx/display_system.hpp"
    #include "phx/frame_timer.hpp"
    #include "phx/mesh.hpp"
    #include "phx/mesh_handle.hpp"
    #include "phx/opengl_image_buffer_data.hpp"
    #include "phx/rendering_system.hpp"
    #include "phx/resource_declaration.hpp"
    #include "phx/resource_manager.hpp"
    #include "phx/resource_proxy.hpp"
    #include "phx/resource_utils.hpp"
    #include "phx/scene.hpp"
    #include "phx/setup.hpp"
    #include "phx/window.hpp"
    
    #include "test_utilities/opengl_buffer_data_comparison.hpp"
    
    SUPPRESS_WARNINGS_BEGIN
    #include "mocks/openvr_mock.hpp"
    SUPPRESS_WARNINGS_END
    
    #include "trompeloeil.hpp"
    
    extern template struct trompeloeil::reporter<trompeloeil::specialized>;
    
    phx::Entity* LoadBunny(glm::vec3 pos, float angleDeg,
                           unsigned int material_index, phx::Scene* scene) {
      auto bunny_proxy = phx::ResourceUtils::LoadResourceFromFile(
          "models/bunny.obj", {{"mesh_index", 0}});
    
      phx::Entity* bunny = scene->CreateEntity();
    
      phx::MeshHandle* bunny_handle = bunny->AddComponent<phx::MeshHandle>();
      bunny_handle->SetMeshProxy(bunny_proxy);
    
      phx::MaterialHandle* bunny_material_handle =
          bunny->AddComponent<phx::MaterialHandle>();
    
      auto bunny_material_proxy = phx::ResourceUtils::LoadResourceFromFile(
          "models/bunny.obj", {{"material_index", material_index}});
      bunny_material_handle->SetMaterialProxy(bunny_material_proxy);
    
      phx::Transform* bunny_transform = bunny->AddComponent<phx::Transform>();
      bunny_transform->SetLocalTranslation(pos);
      bunny_transform->SetLocalRotation(
          glm::angleAxis(angleDeg / 180.0f * glm::pi<float>(), glm::vec3(0, 1, 0)));
    
      return bunny;
    }
    
    void SetupLightAndCamera(phx::Scene* scene);
    void SetupLightAndCamera(phx::Scene* scene) {
      phx::Entity* main_light = scene->CreateEntity();
      phx::Transform* light_transform = main_light->AddComponent<phx::Transform>();
      light_transform->SetLocalRotation(
          glm::angleAxis(1.2f * glm::pi<float>(), glm::vec3(0, 1, 0)));
      phx::Light* light = main_light->AddComponent<phx::Light>();
      light->SetType(phx::Light::Type::kDirectional);
      light->SetColor(glm::vec3(1.0f, 1.0f, 1.0f));
      light->SetIntensity(1.0f);
    
      phx::Entity* camera = scene->CreateEntity();
      auto camera_transform = camera->AddComponent<phx::Transform>();
      auto camera_projection = camera->AddComponent<phx::Projection>();
      camera_projection->SetPerspective(glm::radians(68.0f), 4.0f / 3.0f, 0.01f,
                                        1000.0f);
      camera_transform->SetLocalTranslation(glm::vec3(0, 0, -0.3));
      camera_transform->LookAt(glm::vec3(0, 0, 0));
    }
    
    SCENARIO(
        "A model can be loaded and rendered with different trasformations and "
        "materials.",
        "[phx][phx::ModelLoading]") {
      ALLOW_CALL(openvr_mock.Get(), VR_IsHmdPresent()).RETURN(false);
      GIVEN("A complete scene with two differently colored bunnies.") {
        std::unique_ptr<phx::Engine> engine = phx::Setup::CreateDefaultEngine();
        auto scene = engine->GetScene();
    
        SetupLightAndCamera(scene.get());
    
        phx::Entity* bunny1 =
            LoadBunny(glm::vec3(0.15f, -0.1f, 0.0f), 0.0f, 1, scene.get());
        phx::Entity* bunny2 =
            LoadBunny(glm::vec3(-0.15f, -0.1f, 0.0f), 180.0f, 2, scene.get());
    
        auto rendering_system = engine->GetSystem<phx::RenderingSystem>();
        auto display_system = engine->GetSystem<phx::DisplaySystem>();
    
        WHEN("We render the scene") {
          rendering_system->Update(phx::FrameTimer::TimeInfo());
          display_system->Update(phx::FrameTimer::TimeInfo());
          THEN("the rendering matches our reference image") {
            phx::OpenGLImageBufferData<phx::OpenGLImageBufferDataType_RGB> buffer(
                1024, 768);
            buffer.ReadColorPixels(true);
            test_utilities::OpenGLBufferComparison::
                REQUIRE_REFERENCE_IMAGE_SIMILARITY(buffer, "model_loading.png",
                                                   1.0);
          }
        }
      }
    }
    
    SCENARIO(
        "If no light and camera are give the model rendering takes default values",
        "[phx][phx::ModelLoading]") {
      ALLOW_CALL(openvr_mock.Get(), VR_IsHmdPresent()).RETURN(false);
      GIVEN("A complete scene with two differently colored bunnies.") {
        std::unique_ptr<phx::Engine> engine = phx::Setup::CreateDefaultEngine();
        auto scene = engine->GetScene();
    
        phx::Entity* bunny1 =
            LoadBunny(glm::vec3(0.0f, -0.1f, -0.3f), 0.0f, 1, scene.get());
    
        auto rendering_system = engine->GetSystem<phx::RenderingSystem>();
        auto display_system = engine->GetSystem<phx::DisplaySystem>();
    
        WHEN("We render the scene") {
          rendering_system->Update(phx::FrameTimer::TimeInfo{});
          display_system->Update(phx::FrameTimer::TimeInfo());
          THEN("the rendering matches our reference image") {
            phx::OpenGLImageBufferData<phx::OpenGLImageBufferDataType_RGB> buffer(
                1024, 768);
            buffer.ReadColorPixels(true);
            test_utilities::OpenGLBufferComparison::
                REQUIRE_REFERENCE_IMAGE_SIMILARITY(
                    buffer, "model_loading_default_cam_light.png", 1.0);
          }
        }
      }
    }