From 4bc5d02c52cb6f076f3a54339bd581401875c963 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 11 Jun 2022 15:42:44 +0100
Subject: [PATCH] allow colours to be defined in scene files

---
 demo/data/rollball.yml                 | 10 ++++++++++
 demo/demo/rollball.cpp                 | 22 +++-------------------
 include/fggl/debug/logging.hpp         |  2 ++
 include/fggl/ecs/component.hpp         |  3 ++-
 include/fggl/ecs3/prototype/loader.hpp |  9 +++++++++
 include/fggl/ecs3/types.hpp            |  3 ++-
 include/fggl/gfx/phong.hpp             |  5 +++++
 7 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/demo/data/rollball.yml b/demo/data/rollball.yml
index ee508ba..defd6c0 100644
--- a/demo/data/rollball.yml
+++ b/demo/data/rollball.yml
@@ -47,6 +47,11 @@ prefabs:
         pipeline: phong
         shape:
           type: sphere
+      gfx::material:
+        ambient: [0.25, 0.25, 0.25]
+        diffuse: [0.4, 0.4, 0.4]
+        specular: [0.774597,0.774597,0.774597]
+        shininess: 0.6
       phys::Body:
         shape:
           type: sphere
@@ -58,6 +63,11 @@ prefabs:
         pipeline: phong
         shape:
           type: box
+      gfx::material:
+        ambient: [0.0215, 0.1754, 0.0215]
+        diffuse: [1, 1, 1]
+        specular: [0.0633, 0.727811, 0.633]
+        shininess: 0.6
       phys::Body:
         type: kinematic
         shape:
diff --git a/demo/demo/rollball.cpp b/demo/demo/rollball.cpp
index dfbd3cd..4bef822 100644
--- a/demo/demo/rollball.cpp
+++ b/demo/demo/rollball.cpp
@@ -43,22 +43,11 @@ static void setup_prefabs(fggl::ecs3::World& world, Prefabs& prefabs) {
 		// player (cube because my sphere function doesn't exist yet
 		prefabs.player = world.findPrototype("player");
 		world.add<fggl::phys::Dynamics>(prefabs.player);
-
-		auto* material = world.add< fggl::gfx::PhongMaterial >( prefabs.player );
-		material->ambient = fggl::math::vec3(0.25F, 0.25F, 0.25F);
-		material->diffuse = fggl::math::vec3(0.4F, 0.4F, 0.4F);
-		material->specular = fggl::math::vec3(0.774597F, 0.774597F, 0.774597F);
-		material->shininess = 0.6F;
 	}
 
 	{
 		// collectable
 		prefabs.collectable = world.findPrototype("collectable");
-		auto* material = world.add< fggl::gfx::PhongMaterial >( prefabs.collectable );
-		material->ambient = fggl::math::vec3(0.0215F, 0.1754F, 0.0215F);
-		material->diffuse = fggl::math::vec3(0.007568F, 0.61424F, 0.07568F);
-		material->specular = fggl::math::vec3(0.633F, 0.727811F, 0.633F);
-		material->shininess = 0.6F;
 
 		// we need both of these for callbacks to trigger.
 		world.add<fggl::phys::CollisionCallbacks>(prefabs.collectable);
@@ -147,6 +136,7 @@ static fggl::ecs3::entity_t setup_environment(fggl::ecs3::World& world, const fg
 namespace demo {
 
 	constexpr fggl::math::vec2 WORLD_SIZE{40.0F, 40.0F };
+	constexpr fggl::math::vec3 COLLECTABLE_ROTATION{15, 30, 45};
 	constexpr fggl::math::vec3 COLOUR_WHITE{1.0F, 1.0F, 1.0F};
 	constexpr fggl::math::vec3 COLOUR_BLUE{0.0F, 0.0F, 1.0F};
 	constexpr float MOVE_FORCE{3.0F};
@@ -315,15 +305,9 @@ namespace demo {
 				continue;
 			}
 
-			auto* transform = world.get<fggl::math::Transform>(entity);
-
 			// rotate the cubes
-			fggl::math::vec3 angles{15, 30, 45};
-			transform->rotateEuler( angles * deltaTime );
-
-
-			//auto* renderer = world.get<fggl::data::StaticMesh>( entity );
-			//renderer->hintColour = fggl::math::vec3(1.0F, 1.0F, 1.0F);
+			auto* transform = world.get<fggl::math::Transform>(entity);
+			transform->rotateEuler(  COLLECTABLE_ROTATION * deltaTime );
 		}
 
 	}
diff --git a/include/fggl/debug/logging.hpp b/include/fggl/debug/logging.hpp
index dda37ce..e920fa7 100644
--- a/include/fggl/debug/logging.hpp
+++ b/include/fggl/debug/logging.hpp
@@ -25,6 +25,8 @@
 #ifndef FGGL_DEBUG_LOGGING_HPP
 #define FGGL_DEBUG_LOGGING_HPP
 
+#include <string_view>
+
 namespace fggl::debug {
 
 	using FmtType = const std::string_view;
diff --git a/include/fggl/ecs/component.hpp b/include/fggl/ecs/component.hpp
index f011adc..ddf12c7 100644
--- a/include/fggl/ecs/component.hpp
+++ b/include/fggl/ecs/component.hpp
@@ -2,6 +2,7 @@
 #define FGGL_ECS_COMPONENT_H
 
 #include "utility.hpp"
+#include "fggl/debug/logging.hpp"
 
 #include <cassert>
 #include <cstring>
@@ -69,7 +70,7 @@ namespace fggl::ecs {
 				C* ptr = new C();
 				bool restored = restore_config<C>(ptr, config);
 				if ( !restored ) {
-					std::cerr << "error restoring " << name() << std::endl;
+					debug::warning("error restoring {}", C::name);
 					assert( false && "failed to restore configuration when loading type!" );
 				}
 				return ptr;
diff --git a/include/fggl/ecs3/prototype/loader.hpp b/include/fggl/ecs3/prototype/loader.hpp
index 1a39331..56d50a6 100644
--- a/include/fggl/ecs3/prototype/loader.hpp
+++ b/include/fggl/ecs3/prototype/loader.hpp
@@ -184,6 +184,15 @@ namespace fggl::ecs {
 		return true;
 	}
 
+	template<>
+	bool restore_config(gfx::PhongMaterial* comp, const YAML::Node& node) {
+		comp->diffuse = node["diffuse"].as<math::vec3>( gfx::DEFAULT_DIFFUSE );
+		comp->ambient = node["ambient"].as<math::vec3>( gfx::DEFAULT_AMBIENT );
+		comp->specular = node["specular"].as<math::vec3>( gfx::DEFAULT_SPECULAR );
+		comp->shininess = node["shininess"].as<float>( gfx::DEFAULT_SHININESS );
+		return true;
+	}
+
 	template<>
 	bool restore_config(data::StaticMesh* meshComp, const YAML::Node& node) {
 		if ( !node["pipeline"] ) {
diff --git a/include/fggl/ecs3/types.hpp b/include/fggl/ecs3/types.hpp
index 3090daf..61096de 100644
--- a/include/fggl/ecs3/types.hpp
+++ b/include/fggl/ecs3/types.hpp
@@ -192,7 +192,8 @@ namespace fggl::ecs3 {
 					}
 				}
 
-				std::cerr << "asked for unknown component type: " << name << std::endl;
+				debug::warning("asked for unknown/unregistered component type: {}", name);
+				assert(false && "unknown component type, are you sure it was registered?");
 				return 0;
 			}
 
diff --git a/include/fggl/gfx/phong.hpp b/include/fggl/gfx/phong.hpp
index 9276c7c..03faae4 100644
--- a/include/fggl/gfx/phong.hpp
+++ b/include/fggl/gfx/phong.hpp
@@ -29,6 +29,11 @@
 
 namespace fggl::gfx {
 
+	constexpr math::vec3 DEFAULT_AMBIENT { 0.05F, 0.05F, 0.05F};
+	constexpr math::vec3 DEFAULT_DIFFUSE { 0.5F, 0.5F, 0.5F};
+	constexpr math::vec3 DEFAULT_SPECULAR { 0.7F, 0.7F, 0.7F};
+	constexpr float DEFAULT_SHININESS = .078125F;
+
 	struct PhongMaterial {
 		constexpr static const char* name = "gfx::material";
 		math::vec3 ambient;
-- 
GitLab