From 9a1c97ec92852a64d6aca43f0a02112a5ba9b5b2 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 5 Nov 2022 12:54:01 +0000
Subject: [PATCH] code cleanup for graphics subsystem

---
 fggl/data/procedural.cpp         |   3 +-
 fggl/gfx/ogl4/models.cpp         | 202 +------------------------------
 include/fggl/gfx/ogl4/meshes.hpp |  11 +-
 include/fggl/gfx/ogl4/setup.hpp  |  10 +-
 4 files changed, 15 insertions(+), 211 deletions(-)

diff --git a/fggl/data/procedural.cpp b/fggl/data/procedural.cpp
index dfacbe9..cef7587 100644
--- a/fggl/data/procedural.cpp
+++ b/fggl/data/procedural.cpp
@@ -119,7 +119,8 @@ static void populateMesh(fggl::mesh::Mesh3D &mesh,
 	// clion this thinks this loop is infinite, my assumption is it's gone bananas
 	for (std::size_t i = 0; i < posList.size(); ++i) {
 		glm::vec3 position = transform * fggl::math::vec4(posList[i], 1.0F);
-		colIdx[i] = mesh.append(fggl::mesh::Vertex3D::from_pos(position));
+		auto vert = fggl::mesh::Vertex3D::from_pos(position);
+		colIdx[i] = mesh.append(vert);
 	}
 
 	// use the remapped indexes for the mesh
diff --git a/fggl/gfx/ogl4/models.cpp b/fggl/gfx/ogl4/models.cpp
index d2c61b7..5eee6ce 100644
--- a/fggl/gfx/ogl4/models.cpp
+++ b/fggl/gfx/ogl4/models.cpp
@@ -136,7 +136,6 @@ namespace fggl::gfx::ogl4 {
 	}
 
 	#ifdef FGGL_ALLOW_DEFERRED_UPLOAD
-
 		static void setup_meshes(entity::EntityManager& world, ShaderCache* shaders, assets::AssetManager* manager) {
 			auto entsWithModels = world.find<mesh::StaticMesh3D>();
 			for ( const auto& mesher : entsWithModels ) {
@@ -186,30 +185,7 @@ namespace fggl::gfx::ogl4 {
 		}
 
 		void StaticModelRenderer::resolveModels(entity::EntityManager &world) {
-			// FIXME: this needs something reactive or performance will suck.
-			auto renderableEnts = world.find<data::StaticMesh>();
-			for (const auto &renderable : renderableEnts) {
-				auto *currModel = world.tryGet<StaticModel>(renderable);
-				if (currModel != nullptr) {
-					continue;
-				}
-
-				auto &meshComp = world.get<data::StaticMesh>(renderable);
-
-				// model loading (should be via asset system)
-				auto loadedModel = uploadMesh("DEFER_ENT_"+ std::to_string((uint64_t)renderable), meshComp.mesh, false);
-				auto loadedShader = m_shaders->get(meshComp.pipeline);
-
-				// splice the loaded asset into the ecs
-				auto& entityModel = world.add<StaticModel>(renderable);
-				entityModel = *loadedModel;
-				entityModel.pipeline = loadedShader;
-
-				// let the user know we just did this...
-				debug::log(debug::Level::info, "Added static mesh to {}, pipeline was: {}", (uint64_t) renderable, meshComp.pipeline);
-			}
-
-			// multi-meshes
+			// new mesh formats
 			setup_meshes(world, m_shaders, m_assets);
 			setup_multi_meshes(world, m_shaders, m_assets);
 
@@ -239,180 +215,6 @@ namespace fggl::gfx::ogl4 {
 		}
 	#endif
 
-	static void forward_camera_pass(const entity::EntityID& camera, const fggl::entity::EntityManager& world) {
-		// enable required OpenGL state
-		glEnable(GL_CULL_FACE);
-		glCullFace(GL_BACK);
-
-		// enable depth testing
-		glEnable(GL_DEPTH_TEST);
-
-		// set-up camera matrices
-		const auto &camTransform = world.get<fggl::math::Transform>(camera);
-		const auto &camComp = world.get<fggl::gfx::Camera>(camera);
-
-		const math::mat4 projectionMatrix =
-			glm::perspective(camComp.fov, camComp.aspectRatio, camComp.nearPlane, camComp.farPlane);
-		const math::mat4 viewMatrix = glm::lookAt(camTransform.origin(), camComp.target, camTransform.up());
-
-		// TODO lighting needs to not be this...
-		math::vec3 lightPos{0.0f, 10.0f, 0.0f};
-
-		std::shared_ptr<ogl::Shader> shader = nullptr;
-		ogl::Location mvpMatrixUniform = 0;
-		ogl::Location mvMatrixUniform = 0;
-
-		auto renderables = world.find<StaticModel>();
-		for (const auto &entity : renderables) {
-
-			// ensure that the model pipeline actually exists...
-			const auto &model = world.get<StaticModel>(entity);
-			if (model.pipeline == nullptr) {
-				debug::warning("shader was null, aborting render");
-				continue;
-			}
-
-			// check if we switched shaders
-			if (shader == nullptr || shader->shaderID() != model.pipeline->shaderID()) {
-				// new shader - need to re-send the view and projection matrices
-				shader = model.pipeline;
-				shader->use();
-				if (shader->hasUniform("projection")) {
-					shader->setUniformMtx(shader->uniform("view"), viewMatrix);
-					shader->setUniformMtx(shader->uniform("projection"), projectionMatrix);
-				}
-				mvpMatrixUniform = shader->uniform("MVPMatrix");
-				mvMatrixUniform = shader->uniform("MVMatrix");
-			}
-
-			// set model transform
-			const auto &transform = world.get<math::Transform>(entity);
-			shader->setUniformMtx(mvpMatrixUniform, projectionMatrix * viewMatrix * transform.model());
-			shader->setUniformMtx(mvMatrixUniform, viewMatrix * transform.model());
-
-			auto normalMatrix = glm::mat3(glm::transpose(inverse(transform.model())));
-			shader->setUniformMtx(shader->uniform("NormalMatrix"), normalMatrix);
-
-			// setup lighting mode
-			if (shader->hasUniform("lights[0].isEnabled")) {
-				bool local = true;
-
-				shader->setUniformI(shader->uniform("lights[0].isEnabled"), 1);
-				shader->setUniformI(shader->uniform("lights[0].isLocal"), local);
-				shader->setUniformI(shader->uniform("lights[0].isSpot"), 0);
-
-				shader->setUniformF(shader->uniform("lights[0].constantAttenuation"), 5.0f);
-				shader->setUniformF(shader->uniform("lights[0].linearAttenuation"), 0.0f);
-				shader->setUniformF(shader->uniform("lights[0].quadraticAttenuation"), 0.0f);
-
-				shader->setUniformF(shader->uniform("Strength"), 0.6F);
-
-				if (!local) {
-					lightPos = glm::normalize(lightPos);
-					auto viewDir = glm::normalize(camTransform.origin() - transform.origin());
-					auto halfVector = glm::normalize(lightPos + viewDir);
-					shader->setUniformF(shader->uniform("lights[0].halfVector"), halfVector);
-					shader->setUniformF(shader->uniform("EyeDirection"), viewDir);
-					shader->setUniformF(shader->uniform("lights[0].position"), lightPos);
-				} else {
-					auto camModelView = (viewMatrix * camTransform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f));
-					auto modelModelView = (viewMatrix * transform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f));
-					math::vec3 viewDir = glm::normalize(camModelView - modelModelView);
-					shader->setUniformF(shader->uniform("EyeDirection"), viewDir);
-
-					shader->setUniformF(shader->uniform("lights[0].position"),
-										math::vec3(viewMatrix * math::vec4(lightPos, 1.0f)));
-				}
-
-				shader->setUniformF(shader->uniform("lights[0].ambient"), {0.0f, 0.5f, 0.0f});
-				shader->setUniformF(shader->uniform("lights[0].colour"), {0.5f, 0.5f, 0.5f});
-			}
-
-			// material detection with fallback
-			auto* material = world.tryGet<PhongMaterial>(entity);
-			if ( material == nullptr ) {
-				material = &DEFAULT_MATERIAL;
-			}
-
-			if ( shader->hasUniform("materials[0].ambient") ) {
-				shader->setUniformF(shader->uniform("materials[0].emission"), material->emission);
-				shader->setUniformF(shader->uniform("materials[0].ambient"), material->ambient);
-				shader->setUniformF(shader->uniform("materials[0].diffuse"), material->diffuse);
-				shader->setUniformF(shader->uniform("materials[0].specular"), material->specular);
-				shader->setUniformF(shader->uniform("materials[0].shininess"), material->shininess);
-			}
-
-			if (shader->hasUniform("lightPos")) {
-				shader->setUniformF(shader->uniform("lightPos"), lightPos);
-			}
-
-			auto vao = model.vao;
-			vao->bind();
-
-			model.vertexData->bind();
-			if (model.restartIndex != ogl::NO_RESTART_IDX) {
-				glEnable(GL_PRIMITIVE_RESTART);
-				glPrimitiveRestartIndex(model.restartIndex);
-			}
-
-			auto *elements = model.elements.get();
-			vao->drawElements(*elements, model.drawType, model.elementCount);
-			if (model.restartIndex != ogl::NO_RESTART_IDX) {
-				glDisable(GL_PRIMITIVE_RESTART);
-			}
-		}
-	}
-
-	static void forward_normal_pass(const entity::EntityID& camera, const fggl::entity::EntityManager& world, std::shared_ptr<ogl::Shader> shader) {
-		// enable required OpenGL state
-		glEnable(GL_CULL_FACE);
-		glCullFace(GL_BACK);
-
-		// enable depth testing
-		glEnable(GL_DEPTH_TEST);
-
-		// set-up camera matrices
-		const auto &camTransform = world.get<fggl::math::Transform>(camera);
-		const auto &camComp = world.get<fggl::gfx::Camera>(camera);
-
-		const math::mat4 projectionMatrix = camComp.perspective();
-		const math::mat4 viewMatrix = glm::lookAt(camTransform.origin(), camComp.target, camTransform.up());
-
-		ogl::Location modelUniform = shader->uniform("model");
-		ogl::Location viewUniform = shader->uniform("view");
-		ogl::Location projUniform = shader->uniform("projection");
-
-		shader->use();
-		shader->setUniformMtx(projUniform, projectionMatrix);
-		shader->setUniformMtx(viewUniform, viewMatrix);
-
-		auto renderables = world.find<StaticModel>();
-		for (const auto &entity : renderables) {
-
-			// ensure that the model pipeline actually exists...
-			const auto &model = world.get<StaticModel>(entity);
-
-			// set model transform
-			const auto &transform = world.get<math::Transform>(entity);
-			shader->setUniformMtx(modelUniform, transform.model());
-
-			// render model
-			auto vao = model.vao;
-			vao->bind();
-
-			model.vertexData->bind();
-			if (model.restartIndex != ogl::NO_RESTART_IDX) {
-				glEnable(GL_PRIMITIVE_RESTART);
-				glPrimitiveRestartIndex(model.restartIndex);
-			}
-
-			auto *elements = model.elements.get();
-			vao->drawElements(*elements, model.drawType, model.elementCount);
-			if (model.restartIndex != ogl::NO_RESTART_IDX) {
-				glDisable(GL_PRIMITIVE_RESTART);
-			}
-		}
-	}
 
 	void StaticModelRenderer::renderModelsForward(const entity::EntityManager &world, bool debugMode) {
 
@@ -438,7 +240,7 @@ namespace fggl::gfx::ogl4 {
 				auto normalShader = m_shaders->get("normals");
 				forward_pass_normals<StaticMesh>(cameraEnt, world, normalShader);
 				forward_pass_normals<StaticMultiMesh>(cameraEnt, world, normalShader);
-				forward_normal_pass(cameraEnt, world, normalShader);
+				//forward_normal_pass(cameraEnt, world, normalShader);
 			}
 		}
 	}
diff --git a/include/fggl/gfx/ogl4/meshes.hpp b/include/fggl/gfx/ogl4/meshes.hpp
index 7cae65b..09a97a5 100644
--- a/include/fggl/gfx/ogl4/meshes.hpp
+++ b/include/fggl/gfx/ogl4/meshes.hpp
@@ -106,16 +106,12 @@ namespace fggl::gfx::ogl4 {
 		ogl::Location mvMatrixUniform = 0;
 
 		auto entityView = world.find<T>();
-		debug::info("Triggering rendering pass for {} entities", entityView.size());
 
 		// find directional light in scene
+		const DirectionalLight* light = nullptr;
 		auto lightEnts = world.find<DirectionalLight>();
-		const DirectionalLight* light;
 		if ( !lightEnts.empty() ) {
 			light = world.tryGet<DirectionalLight>(lightEnts[0]);
-		} else {
-			debug::warning("no light component in scene, it's gunna be dark...");
-			light = nullptr;
 		}
 
 		for (const auto &entity : entityView) {
@@ -152,16 +148,13 @@ namespace fggl::gfx::ogl4 {
 			shader->setUniformMtx(mvpMatrixUniform, projectionMatrix * viewMatrix * transform.model());
 			shader->setUniformMtx(mvMatrixUniform, viewMatrix * transform.model());
 
-			/*auto normalMatrix = glm::mat3(glm::transpose(inverse(transform.model())));
-			shader->setUniformMtx(shader->uniform("NormalMatrix"), normalMatrix);*/
-
 			// setup lighting mode
-			//setup_lighting(shader, viewMatrix, camTransform, transform, lightPos);
 			if ( light != nullptr ) {
 				setup_lighting(shader, light);
 			}
 			setup_material(shader, world.tryGet<PhongMaterial>(entity, &DEFAULT_MATERIAL));
 
+			// actually draw it
 			model.draw();
 		}
 	}
diff --git a/include/fggl/gfx/ogl4/setup.hpp b/include/fggl/gfx/ogl4/setup.hpp
index 30d26aa..781279c 100644
--- a/include/fggl/gfx/ogl4/setup.hpp
+++ b/include/fggl/gfx/ogl4/setup.hpp
@@ -30,7 +30,15 @@ namespace fggl::gfx::ogl4 {
 	class WindowGraphics : public gfx::WindowGraphics {
 		public:
 			WindowGraphics(data::Storage *storage, gui::FontLibrary *fonts, assets::AssetManager* assets) : m_storage(storage), m_fonts(fonts), m_assets(assets) {};
-			virtual ~WindowGraphics() = default;
+			~WindowGraphics() override = default;
+
+			// no copy
+			WindowGraphics(WindowGraphics& gfx) = delete;
+			WindowGraphics& operator=(const WindowGraphics& gfx) = delete;
+
+			// no move
+			WindowGraphics(WindowGraphics&& gfx) = delete;
+			WindowGraphics& operator=(WindowGraphics&& gfx) = delete;
 
 			fggl::gfx::Graphics *create(display::Window &window) override;
 
-- 
GitLab