From c6852883816e1d6c6fd8f44ad8cfe9db46c5b985 Mon Sep 17 00:00:00 2001
From: jwendt <wendt@vr.rwth-aachen.de>
Date: Thu, 14 Jun 2018 09:28:58 +0200
Subject: [PATCH] Adapt to new defualt values

#445
---
 library/phx/display/hmd.cpp                              | 9 ++++++---
 .../phx/rendering/components/mesh_render_settings.cpp    | 6 ++++++
 .../phx/rendering/components/mesh_render_settings.hpp    | 4 ++++
 library/phx/rendering/render_passes/geometry_pass.hpp    | 2 +-
 library/phx/rendering/rendering_system.cpp               | 7 ++-----
 library/phx/rendering/rendering_system.hpp               | 4 ++--
 library/phx/resources/types/material.cpp                 | 3 +++
 library/phx/resources/types/material.hpp                 | 4 ++++
 tests/src/test_material.cpp                              | 2 +-
 9 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/library/phx/display/hmd.cpp b/library/phx/display/hmd.cpp
index c43329e2..5b7e39f3 100644
--- a/library/phx/display/hmd.cpp
+++ b/library/phx/display/hmd.cpp
@@ -206,6 +206,7 @@ std::unique_ptr<Material> HMD::GetControllerMaterial(Controller controller) {
   auto material = std::make_unique<phx::Material>();
   material->SetAmbientColor(glm::vec3(0.1, 0.1, 0.1));
   material->SetSpecularColor(glm::vec3(0.3, 0.3, 0.3));
+  material->SetShininess(1.0f);
 
   auto texture = ResourceManager::instance().DeclareResource<Image>(
       {{"TYPE", "openVR"},
@@ -235,9 +236,11 @@ std::unique_ptr<Image> HMD::GetControllerTexture(int id) {
             texture_map->rubTextureMapData + image_data.size(),
             image_data.begin());
   auto image = std::make_unique<phx::Image>(
-      &image_data[0], std::array<std::size_t, 2>{
-                      {static_cast<std::size_t>(texture_map->unWidth),
-                       static_cast<std::size_t>(texture_map->unHeight)}}, 32);
+      &image_data[0],
+      std::array<std::size_t, 2>{
+          {static_cast<std::size_t>(texture_map->unWidth),
+           static_cast<std::size_t>(texture_map->unHeight)}},
+      32);
 
   return image;
 }
diff --git a/library/phx/rendering/components/mesh_render_settings.cpp b/library/phx/rendering/components/mesh_render_settings.cpp
index 97ed8e9d..23e920fb 100644
--- a/library/phx/rendering/components/mesh_render_settings.cpp
+++ b/library/phx/rendering/components/mesh_render_settings.cpp
@@ -24,10 +24,16 @@
 
 namespace phx {
 
+MeshRenderSettings MeshRenderSettings::default_settings_;
+
 void MeshRenderSettings::SetWireframeMode(const bool enabled) {
   wireframe_mode_ = enabled;
 }
 
 bool MeshRenderSettings::GetWireframeMode() const { return wireframe_mode_; }
 
+MeshRenderSettings* MeshRenderSettings::GetDefault() {
+  return &default_settings_;
+}
+
 }  // namespace phx
diff --git a/library/phx/rendering/components/mesh_render_settings.hpp b/library/phx/rendering/components/mesh_render_settings.hpp
index 4aec1e99..bb9fda67 100644
--- a/library/phx/rendering/components/mesh_render_settings.hpp
+++ b/library/phx/rendering/components/mesh_render_settings.hpp
@@ -36,8 +36,12 @@ class PHOENIX_EXPORT MeshRenderSettings final : public Component {
   void SetWireframeMode(bool enabled);
   bool GetWireframeMode() const;
 
+  static MeshRenderSettings* GetDefault();
+
  private:
   bool wireframe_mode_ = false;
+
+  static MeshRenderSettings default_settings_;
 };
 }  // namespace phx
 
diff --git a/library/phx/rendering/render_passes/geometry_pass.hpp b/library/phx/rendering/render_passes/geometry_pass.hpp
index 378d7ec0..6e19b2dc 100644
--- a/library/phx/rendering/render_passes/geometry_pass.hpp
+++ b/library/phx/rendering/render_passes/geometry_pass.hpp
@@ -65,7 +65,7 @@ class PHOENIX_EXPORT GeometryPass : public RenderPass {
     Mesh* mesh = nullptr;
     Material* material = nullptr;
     Transform* transform = nullptr;
-    MeshRenderSettings* mesh_render_settings = nullptr;
+    MeshRenderSettings* mesh_render_settings = MeshRenderSettings::GetDefault();
   };
   struct RenderOffset {
     std::size_t vertex_offset;
diff --git a/library/phx/rendering/rendering_system.cpp b/library/phx/rendering/rendering_system.cpp
index 68836045..781dfb22 100644
--- a/library/phx/rendering/rendering_system.cpp
+++ b/library/phx/rendering/rendering_system.cpp
@@ -70,9 +70,6 @@ void RenderingSystem::Update(const FrameTimer::TimeInfo&) {
     return;
   }
 
-  Material default_material;
-  MeshRenderSettings default_mesh_render_settings;
-
   for (auto& entity : GetEngine()->GetEntities()) {
     auto mesh_handle = entity->GetFirstComponent<MeshHandle>();
     auto light = entity->GetFirstComponent<Light>();
@@ -85,11 +82,11 @@ void RenderingSystem::Update(const FrameTimer::TimeInfo&) {
         rendering_instances.push_back(
             {mesh_handle->GetMesh().Get(),
              (material_handle != nullptr ? material_handle->GetMaterial().Get()
-                                         : &default_material),
+                                         : Material::GetDefault()),
              transform,
              (mesh_render_settings != nullptr
                   ? mesh_render_settings
-                  : &default_mesh_render_settings)});
+                  : MeshRenderSettings::GetDefault())});
       } else if (light != nullptr) {
         light_transform_pairs.push_back(
             std::pair<Light*, Transform*>(light, transform));
diff --git a/library/phx/rendering/rendering_system.hpp b/library/phx/rendering/rendering_system.hpp
index 6d56ee71..1641f260 100644
--- a/library/phx/rendering/rendering_system.hpp
+++ b/library/phx/rendering/rendering_system.hpp
@@ -32,10 +32,10 @@
 #include "phx/core/scene.hpp"
 #include "phx/core/system.hpp"
 #include "phx/display/display_system.hpp"
+#include "phx/export.hpp"
 #include "phx/rendering/backend/render_target.hpp"
-#include "phx/rendering/render_passes/geometry_pass.hpp"
 #include "phx/rendering/frame_graph.hpp"
-#include "phx/export.hpp"
+#include "phx/rendering/render_passes/geometry_pass.hpp"
 
 namespace phx {
 
diff --git a/library/phx/resources/types/material.cpp b/library/phx/resources/types/material.cpp
index f79e5d33..d8ab57d8 100644
--- a/library/phx/resources/types/material.cpp
+++ b/library/phx/resources/types/material.cpp
@@ -32,6 +32,7 @@
 namespace phx {
 
 const char Material::UNNAMED[] = "UnnamedMaterial";
+Material Material::default_material_;
 
 glm::vec3 Material::GetDiffuseColor() const { return diffuse_color_; }
 void Material::SetDiffuseColor(glm::vec3 color) { diffuse_color_ = color; }
@@ -93,6 +94,8 @@ void Material::SetShininess(float shininess) {
   }
 }
 
+Material* Material::GetDefault() { return &default_material_; }
+
 void Material::SetTexture(ResourcePointer<Image> image,
                           std::shared_ptr<gl::texture_2d>* texture) {
   if (image == nullptr) return;
diff --git a/library/phx/resources/types/material.hpp b/library/phx/resources/types/material.hpp
index 1ed43762..19565287 100644
--- a/library/phx/resources/types/material.hpp
+++ b/library/phx/resources/types/material.hpp
@@ -84,6 +84,8 @@ class PHOENIX_EXPORT Material : public Resource, public Nameable {
   float GetShininess() const;
   void SetShininess(float shininess);
 
+  static Material* GetDefault();
+
  private:
   void SetTexture(ResourcePointer<Image> image,
                   std::shared_ptr<gl::texture_2d>* texture);
@@ -98,6 +100,8 @@ class PHOENIX_EXPORT Material : public Resource, public Nameable {
   glm::vec3 diffuse_color_ = glm::vec3(1, 0, 0);
   glm::vec3 specular_color_ = glm::vec3(1, 1, 1);
   float shininess_ = 64.0f;
+
+  static Material default_material_;
 };
 
 }  // namespace phx
diff --git a/tests/src/test_material.cpp b/tests/src/test_material.cpp
index 34f05525..843bd61e 100644
--- a/tests/src/test_material.cpp
+++ b/tests/src/test_material.cpp
@@ -42,7 +42,7 @@ SCENARIO("The material component keeps track of its attributes",
       REQUIRE(material.GetAmbientColor() == glm::vec3(0, 0, 0));
       REQUIRE(material.GetSpecularColor() == glm::vec3(1, 1, 1));
       REQUIRE(material.GetDiffuseColor() == glm::vec3(1, 0, 0));
-      REQUIRE(material.GetShininess() == 1.0f);
+      REQUIRE(material.GetShininess() == 64.0f);
     }
     WHEN("we assign it to be some other material") {
       glm::vec3 blue(0, 0, 1), white(1, 1, 1), green(0, 1, 0);
-- 
GitLab