Skip to content
Snippets Groups Projects
Select Git revision
  • b7d6afb23b8a56683b3f3d63e5bf780b19a230cb
  • stable default protected
  • MA_Pape_2018
  • MA_2018_Lopatin
  • feature/mesh_viewer
  • feature/#468_access_isosurface_scalar
  • feature/#459_default_primitives
  • master protected
  • feature/#470_Create_a_color_lookup_table
  • feature/#473_resize_companion_window
  • feature/#462_do_not_use_arb_extensions
  • feature/#495_Provide_data_for_larger_isosurfaces
  • feature/#323_default_image
  • feature/#480_Create_a_smaller_test_mesh_for_combustion_demo
  • feature/#236_Get_Integration_tests_running_on_CI
  • feature/#447_Copy_standard_assets_to_build_folder
  • 447-copy-standard-assets-to-build-folder-and-remove-resource-path
  • feature/#445_mesh_render_settings_component
  • feature/#251_Make_sure_tests_cpp_is_compiled_once
  • feature/#455_Remove_navigation_and_improve_interaction_for_combustion_demo
  • feature/446_strange_txt_files
  • v18.06.0
  • v18.05.0
  • #251_bad
  • #251_good
  • v18.03.0
  • v18.02.0
  • v18.01.0
  • v17.12.0
  • v17.11.0
  • v17.10.0
  • v17.09.0
  • v17.07.0
33 results

viewer.cpp

Blame
  • viewer.cpp 6.72 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 <chrono>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <utility>
    #include <vector>
    
    #include "phx/assimp_model_loader.hpp"
    #include "phx/display_system.hpp"
    #include "phx/engine.hpp"
    #include "phx/entity.hpp"
    #include "phx/input_system.hpp"
    #include "phx/light.hpp"
    #include "phx/logger.hpp"
    #include "phx/material_handle.hpp"
    #include "phx/mesh.hpp"
    #include "phx/mesh_handle.hpp"
    #include "phx/openvr_resource_loader.hpp"
    #include "phx/phoenix.hpp"
    #include "phx/rendering_system.hpp"
    #include "phx/resource_declaration.hpp"
    #include "phx/resource_manager.hpp"
    #include "phx/resource_proxy.hpp"
    #include "phx/runtime_component.hpp"
    #include "phx/scene.hpp"
    #include "phx/scene_loader.hpp"
    #include "phx/setup.hpp"
    #include "phx/splash_screen.hpp"
    #include "phx/transform.hpp"
    #include "phx/window.hpp"
    
    #include "controller_behavior.hpp"
    #include "navigation_behavior.hpp"
    #include "rotation_behavior.hpp"
    #include "viewer_system.hpp"
    
    void AddControllerEntity(const std::shared_ptr<phx::Scene>& scene,
                             phx::ResourceProxy* mesh_proxy,
                             phx::ResourceProxy* material_proxy,
                             ControllerBehavior::Side side) {
      phx::Entity* controller = scene->CreateEntity();
      controller->AddComponent<phx::MeshHandle>()->SetMeshProxy(mesh_proxy);
      controller->AddComponent<phx::Transform>();
      controller->AddComponent<phx::MaterialHandle>()->SetMaterialProxy(
          material_proxy);
      controller->AddComponent<ControllerBehavior>()->SetSide(side);
    }
    
    void AddController(const std::shared_ptr<phx::Scene>& scene,
                       ControllerBehavior::Side side) {
      auto& resource_manager = phx::ResourceManager::instance();
    
      phx::ResourceDeclaration mesh_declaration{
          {"TYPE", "openVR"},
          {"OpenVR_type", "mesh"},
          {"side", (side == ControllerBehavior::RIGHT ? "right" : "left")}};
      auto mesh_proxy =
          phx::ResourceManager::instance().DeclareResource(mesh_declaration);
      mesh_proxy->Load();
    
      phx::ResourceDeclaration material_declaration{{"TYPE", "openVR"},
                                                    {"OpenVR_type", "material"}};
      auto material_proxy = resource_manager.DeclareResource(material_declaration);
      material_proxy->Load();
    
      if (mesh_proxy->GetAs<phx::Mesh>() != nullptr) {
        AddControllerEntity(scene, mesh_proxy, material_proxy, side);
      }
    }
    
    void SetupOpenVRController(const std::shared_ptr<phx::Scene>& scene,
                               phx::HMD* hmd) {
      auto& resource_manager = phx::ResourceManager::instance();
      auto openvr_loader = std::make_unique<phx::OpenVRResourceLoader>(hmd);
      resource_manager.RegisterResourceType("openVR", std::move(openvr_loader));
    
      AddController(scene, ControllerBehavior::RIGHT);
      AddController(scene, ControllerBehavior::LEFT);
    }
    
    int main(int, char**) {
      std::unique_ptr<phx::Engine> engine = phx::Setup::CreateDefaultEngine();
      auto scene = engine->GetScene();
    
      phx::SplashScreen splash(
          engine->GetSystem<phx::DisplaySystem>()->GetWindow());
      splash.Draw();
      auto assimp_loader = std::make_unique<phx::AssimpModelLoader>();
      assimp_loader->SetProgressUpdateCallback([&splash](float progress) {
        splash.SetLoadProgress(progress);
        splash.Draw();
      });
    
      phx::InputSystem* input_system = engine->GetSystem<phx::InputSystem>();
      ViewerSystem* viewer_system = engine->CreateSystem<ViewerSystem>();
    
      input_system->AddKeyPressCallback([&engine, &viewer_system](char key) {
        if (key == 'q') engine->Stop();
        if (key == 'f')
          viewer_system->SetShowFramerate(!viewer_system->GetShowFramerate());
      });
    
      auto hmd = engine->GetSystem<phx::DisplaySystem>()->GetHMD();
      if (hmd != nullptr) {
        SetupOpenVRController(scene, hmd);
      }
    
      phx::SceneLoader::InsertModelIntoScene(
          "models/UniversityScene/Univers20171013.obj", scene.get());
    
      std::vector<glm::quat> light_dirs{
          glm::quat(glm::angleAxis(-0.25f * glm::pi<float>(), glm::vec3(1, 0, 0))),
          glm::quat(glm::angleAxis(0.25f * glm::pi<float>(), glm::vec3(0, 1, 0))),
          glm::quat(glm::angleAxis(-0.25f * glm::pi<float>(), glm::vec3(0, 1, 0))),
          glm::quat(glm::angleAxis(0.75f * glm::pi<float>(), glm::vec3(1, 0, 0)))};
      std::vector<glm::vec3> light_colors{
          glm::vec3(1.0, 1.0, 1.0), glm::vec3(1.0, 1.0, 1.0),
          glm::vec3(1.0, 1.0, 1.0), glm::vec3(1.0, 1.0, 1.0)};
      std::vector<float> light_intensities{1.0f, 0.9f, 0.8f, 0.7f};
    
      for (std::size_t i = 0; i < light_dirs.size(); i++) {
        phx::Entity* light_entity = scene->CreateEntity();
        phx::Transform* light_transform =
            light_entity->AddComponent<phx::Transform>();
        light_transform->SetLocalRotation(light_dirs[i]);
        phx::Light* light = light_entity->AddComponent<phx::Light>();
        light->SetType(phx::Light::Type::kDirectional);
        light->SetColor(light_colors[i]);
        light->SetIntensity(light_intensities[i]);
      }
    
      auto virtual_platform_transform =
          scene
              ->GetEntitiesWithComponents<
                  phx::RuntimeComponent<phx::USER_PLATFORM>>()[0]
              ->GetFirstComponent<phx::Transform>();
    
      virtual_platform_transform->SetLocalTranslation(glm::vec3(0, 1.0, 5.0));
    
      phx::Entity* camera = scene->CreateEntity();
      auto camera_transform = camera->AddComponent<phx::Transform>();
      auto camera_projection = camera->AddComponent<phx::Projection>();
      camera->AddComponent<NavigationBehavior>();
      camera_projection->SetPerspective(glm::radians(68.0f), 4.0f / 3.0f, 0.01f,
                                        1000.0f);
      camera_transform->SetLocalTranslation(glm::vec3(0, 0, 0));
      camera_transform->SetParent(virtual_platform_transform, false);
      auto virtual_platform = scene->GetEntitiesWithComponents<
          phx::RuntimeComponent<phx::USER_PLATFORM>>()[0];
      virtual_platform->AddComponent<NavigationBehavior>();
    
      engine->Run();
    
      return EXIT_SUCCESS;
    }