From 1f5f646a4e5b4c0d34c8c65bbdec04a3ff15c85b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 Jan 2025 18:33:10 +0200 Subject: [PATCH] Add a component for creating meshes via geometry definitions --- source/game/shape.cpp | 27 ++++++++++++++++++++ source/game/shape.h | 49 ++++++++++++++++++++++++++++++++++++ source/gameview/renderer.cpp | 16 ++++++++++++ source/gameview/renderer.h | 2 ++ 4 files changed, 94 insertions(+) create mode 100644 source/game/shape.cpp create mode 100644 source/game/shape.h diff --git a/source/game/shape.cpp b/source/game/shape.cpp new file mode 100644 index 0000000..ba17721 --- /dev/null +++ b/source/game/shape.cpp @@ -0,0 +1,27 @@ +#include "shape.h" +#include + +namespace Msp::Game { + +ShapeSetup::Loader::Loader(ShapeSetup &s): + ObjectLoader(s) +{ + static ActionMap shared_actions; + set_actions(shared_actions); +} + +void ShapeSetup::Loader::init_actions() +{ + add("render_detail", &ShapeSetup::render_detail); + add("shape", &Loader::shape); + add("technique_name", &ShapeSetup::technique_name); +} + +void ShapeSetup::Loader::shape() +{ + Geometry::Loader ldr; + load_sub_with(ldr); + obj.shape = ldr.take_shape(); +} + +} // namespace Msp::Game diff --git a/source/game/shape.h b/source/game/shape.h new file mode 100644 index 0000000..692cf0c --- /dev/null +++ b/source/game/shape.h @@ -0,0 +1,49 @@ +#ifndef MSP_GAME_SHAPE_H_ +#define MSP_GAME_SHAPE_H_ + +#include +#include +#include +#include "component.h" +#include "mspgame_api.h" + +namespace Msp::Game { + +// TODO Make this visible to the setup generator +struct MSPGAME_API ShapeSetup +{ + class MSPGAME_API Loader: public DataFile::ObjectLoader + { + public: + Loader(ShapeSetup &); + + private: + void init_actions() override; + + void shape(); + }; + + std::unique_ptr> shape; + std::string technique_name; + unsigned render_detail = 4; +}; + +class MSPGAME_API Shape: public Component +{ +public: + using Setup = ShapeSetup; + +private: + const Setup &setup; + +public: + Shape(Handle p, const Setup &s): Component(p), setup(s) { } + + const Geometry::Shape *get_shape() const { return setup.shape.get(); } + const std::string &get_technique_name() const { return setup.technique_name; } + unsigned get_render_detail() const { return setup.render_detail; } +}; + +} // namespace Msp::Game + +#endif diff --git a/source/gameview/renderer.cpp b/source/gameview/renderer.cpp index b521ccb..0e71fc3 100644 --- a/source/gameview/renderer.cpp +++ b/source/gameview/renderer.cpp @@ -3,11 +3,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include "dynamicmeshsource.h" #include "lightemitter.h" #include "meshrenderer.h" @@ -106,6 +108,18 @@ void Renderer::create_sequence() void Renderer::component_created(const Game::Events::ComponentCreated &event) { + Game::Handle shape = dynamic_handle_cast(event.component); + if(shape) + { + RenderedEntity &re = get_rendered_entity(event.component->get_entity()); + if(!re.generated_mesh) + { + const GL::Technique &tech = stage.get_resources().get(shape->get_technique_name()); + re.generated_mesh = Game::Owned(re.entity, (GL::VERTEX3, GL::NORMAL3), tech); + GL::ShapeBuilder(*shape->get_shape(), shape->get_render_detail()).build(re.generated_mesh->get_mesh()); + } + } + Game::Handle mesh_source = dynamic_handle_cast(event.component); Game::Handle dyn_mesh_src = dynamic_handle_cast(event.component); if(mesh_source || dyn_mesh_src) @@ -149,6 +163,8 @@ void Renderer::component_destroyed(const Game::Events::ComponentDestroyed &event auto i = lower_bound_member(entities, entity, &RenderedEntity::entity); if(i!=entities.end() && i->entity==entity) { + if(dynamic_handle_cast(event.component)) + i->generated_mesh = nullptr; if(dynamic_handle_cast(event.component)) i->mesh_renderer = nullptr; if(auto light = dynamic_handle_cast(event.component)) diff --git a/source/gameview/renderer.h b/source/gameview/renderer.h index 04d2439..4657e35 100644 --- a/source/gameview/renderer.h +++ b/source/gameview/renderer.h @@ -20,6 +20,7 @@ namespace Msp::GameView { +class DynamicMeshSource; class LightEmitter; class MeshRenderer; @@ -29,6 +30,7 @@ private: struct RenderedEntity { Game::Handle entity; + Game::Owned generated_mesh; Game::Owned mesh_renderer; Game::Owned light_emitter; -- 2.45.2