From 7c7011037602c64c762278ed76ea3c61e1bfcae1 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Tue, 19 Apr 2022 22:11:52 +0100
Subject: [PATCH] move static mesh into data rather than opengl graphics

---
 demo/main.cpp                   |  8 ++++----
 fggl/gfx/ogl4/models.cpp        |  7 ++++---
 include/fggl/data/model.hpp     | 13 +++++++++++++
 include/fggl/gfx/common.hpp     | 10 ----------
 include/fggl/gfx/ogl/compat.hpp | 27 +++++++++++++++------------
 5 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/demo/main.cpp b/demo/main.cpp
index 90a71da..2a06613 100644
--- a/demo/main.cpp
+++ b/demo/main.cpp
@@ -226,8 +226,8 @@ public:
 
             // add mesh as a component
             constexpr char shader[] = "phong";
-            fggl::gfx::StaticMesh staticMesh{mesh, shader};
-            m_world->set<fggl::gfx::StaticMesh>(foundation, &staticMesh);
+            fggl::data::StaticMesh staticMesh{mesh, shader};
+            m_world->set<fggl::data::StaticMesh>(foundation, &staticMesh);
         }
 
         // create building prototype
@@ -259,8 +259,8 @@ public:
             }
             mesh.removeDups();
 
-            fggl::gfx::StaticMesh staticMesh{mesh, shader};
-            m_world->set<fggl::gfx::StaticMesh>(bunker, &staticMesh);
+            fggl::data::StaticMesh staticMesh{mesh, shader};
+            m_world->set<fggl::data::StaticMesh>(bunker, &staticMesh);
         }
 
         for (int i=0; i<3; ++i) {
diff --git a/fggl/gfx/ogl4/models.cpp b/fggl/gfx/ogl4/models.cpp
index d6ff011..5e8e762 100644
--- a/fggl/gfx/ogl4/models.cpp
+++ b/fggl/gfx/ogl4/models.cpp
@@ -70,14 +70,14 @@ namespace fggl::gfx::ogl4 {
 
 	void StaticModelRenderer::resolveModels(ecs3::World &world) {
 		// FIXME: this needs something reactive or performance will suck.
-		auto renderables = world.findMatching<gfx::StaticMesh>();
+		auto renderables = world.findMatching<data::StaticMesh>();
 		for (auto& renderable : renderables){
 			auto currModel = world.get<StaticModel>( renderable );
 			if ( currModel != nullptr ){
 				continue;
 			}
 
-			auto* meshComp = world.get<gfx::StaticMesh>(renderable);
+			auto* meshComp = world.get<data::StaticMesh>(renderable);
 			auto* modelComp = world.add<StaticModel>(renderable);
 			setupComponent(modelComp, m_phong, meshComp->mesh);
 
@@ -152,9 +152,10 @@ namespace fggl::gfx::ogl4 {
 				auto shader = model->pipeline;
 				if ( shader == nullptr ) {
 					spdlog::warn("shader was null, aborting render");
-					return;
+					continue;
 				}
 
+				// setup shader uniforms
 				shader->use();
 				shader->setUniformMtx(shader->uniform("model"), transform->model());
 				shader->setUniformMtx(shader->uniform("view"), viewMatrix);
diff --git a/include/fggl/data/model.hpp b/include/fggl/data/model.hpp
index f59db12..6dfa2cf 100644
--- a/include/fggl/data/model.hpp
+++ b/include/fggl/data/model.hpp
@@ -3,6 +3,8 @@
 
 #include <tuple>
 #include <vector>
+#include <string>
+
 #include <fggl/math/types.hpp>
 
 namespace fggl::data {
@@ -127,6 +129,17 @@ namespace fggl::data {
 			std::vector<unsigned int> m_index;
 	};
 
+	struct StaticMesh {
+		constexpr static const char name[] = "StaticMesh";
+		data::Mesh mesh;
+		std::string pipeline;
+
+		inline StaticMesh() : mesh(), pipeline() {}
+
+		inline StaticMesh(const data::Mesh &aMesh, std::string aPipeline) :
+			mesh(aMesh), pipeline(std::move(aPipeline)) {}
+	};
+
 	class Model {
 		public:
 			Model() = default;
diff --git a/include/fggl/gfx/common.hpp b/include/fggl/gfx/common.hpp
index 82845e2..302a195 100644
--- a/include/fggl/gfx/common.hpp
+++ b/include/fggl/gfx/common.hpp
@@ -13,16 +13,6 @@
 
 namespace fggl::gfx {
 
-	struct StaticMesh {
-		constexpr static const char name[] = "StaticMesh";
-		data::Mesh mesh;
-		std::string pipeline;
-
-		inline StaticMesh() : mesh(), pipeline() {}
-
-		inline StaticMesh(const data::Mesh &aMesh, std::string aPipeline) :
-			mesh(aMesh), pipeline(std::move(aPipeline)) {}
-	};
 
 }
 
diff --git a/include/fggl/gfx/ogl/compat.hpp b/include/fggl/gfx/ogl/compat.hpp
index 8592b40..c68c7f2 100644
--- a/include/fggl/gfx/ogl/compat.hpp
+++ b/include/fggl/gfx/ogl/compat.hpp
@@ -42,7 +42,7 @@ namespace fggl::gfx {
 		}
 
 		void uploadMesh(ecs3::World *world, ecs::entity_t entity) {
-			auto *meshData = world->get<gfx::StaticMesh>(entity);
+			auto *meshData = world->get<data::StaticMesh>(entity);
 
 			auto pipeline = cache.get(meshData->pipeline);
 			auto glMesh = renderer.upload(meshData->mesh);
@@ -65,21 +65,24 @@ namespace fggl::gfx {
 		}
 
 		void onLoad(ecs3::ModuleManager &manager, ecs3::TypeRegistry &types) override {
-			// TODO implement dependencies
-			types.make<fggl::gfx::StaticMesh>();
-			types.make<fggl::data::HeightMap>();
-			types.make<fggl::gfx::Camera>();
+			// mesh dependencies
+			types.make<data::StaticMesh>();
+			types.make<data::HeightMap>();
 
-			// FIXME probably shouldn't be doing this...
+			// camera dependencies
+			types.make<fggl::gfx::Camera>();
 			types.make<fggl::input::FreeCamKeys>();
 
-			// opengl
-			types.make<fggl::gfx::GlRenderToken>();
+			/*
+			 * old API stuff - not used
+				// opengl
+				types.make<fggl::gfx::GlRenderToken>();
 
-			// callbacks
-			auto upload_cb = [this](auto a, auto b) { this->uploadMesh(a, b); };
-			manager.onAdd<fggl::gfx::StaticMesh>(upload_cb);
-			manager.onAdd<fggl::data::HeightMap>([this](auto a, auto b) { this->uploadHeightmap(a, b); });
+				// callbacks
+				auto upload_cb = [this](auto a, auto b) { this->uploadMesh(a, b); };
+				manager.onAdd<fggl::gfx::StaticMesh>(upload_cb);
+				manager.onAdd<fggl::data::HeightMap>([this](auto a, auto b) { this->uploadHeightmap(a, b); });
+			 */
 		}
 
 	};
-- 
GitLab