]> git.tdb.fi Git - libs/game.git/commitdiff
Add a component for using dynamic meshes in entities
authorMikko Rasa <tdb@tdb.fi>
Sat, 11 Feb 2023 13:18:20 +0000 (15:18 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 11 Feb 2023 13:18:20 +0000 (15:18 +0200)
source/gameview/dynamicmeshsource.cpp [new file with mode: 0644]
source/gameview/dynamicmeshsource.h [new file with mode: 0644]
source/gameview/renderer.cpp

diff --git a/source/gameview/dynamicmeshsource.cpp b/source/gameview/dynamicmeshsource.cpp
new file mode 100644 (file)
index 0000000..99c40fa
--- /dev/null
@@ -0,0 +1,13 @@
+#include "dynamicmeshsource.h"
+
+using namespace std;
+
+namespace Msp::GameView {
+
+DynamicMeshSource::DynamicMeshSource(Game::Handle<Game::Entity> e, const GL::VertexFormat &fmt, const GL::Technique &tech):
+       Component(e),
+       mesh(fmt),
+       object(&mesh, &tech)
+{ }
+
+} // namespace Msp::GameView
diff --git a/source/gameview/dynamicmeshsource.h b/source/gameview/dynamicmeshsource.h
new file mode 100644 (file)
index 0000000..cbc5e91
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef MSP_GAMEVIEW_DYNAMICMESHSOURCE_H_
+#define MSP_GAMEVIEW_DYNAMICMESHSOURCE_H_
+
+#include <msp/game/component.h>
+#include <msp/gl/mesh.h>
+#include <msp/gl/object.h>
+
+namespace Msp::GameView {
+
+class DynamicMeshSource: public Game::Component
+{
+private:
+       GL::Mesh mesh;
+       GL::Object object;
+
+public:
+       DynamicMeshSource(Game::Handle<Game::Entity>, const GL::VertexFormat &, const GL::Technique &);
+
+       GL::Mesh &get_mesh() { return mesh; }
+       const GL::Object &get_object() const { return object; }
+};
+
+} // namespace Msp::GameView
+
+#endif
index bbdc23c2fbf6f0ea111558d4cfcd357a4b6d20f6..f9dae4b3c7e51c90855148dc7f415944d7a24697 100644 (file)
@@ -7,6 +7,7 @@
 #include <msp/gl/colorcurve.h>
 #include <msp/gl/directionallight.h>
 #include <msp/gl/pointlight.h>
+#include "dynamicmeshsource.h"
 #include "lightemitter.h"
 #include "meshrenderer.h"
 
@@ -42,12 +43,14 @@ Renderer::~Renderer()
 void Renderer::component_created(const Game::Events::ComponentCreated &event)
 {
        Game::Handle<Game::MeshSource> mesh_source = dynamic_handle_cast<Game::MeshSource>(event.component);
-       if(mesh_source)
+       Game::Handle<DynamicMeshSource> dyn_mesh_src = dynamic_handle_cast<DynamicMeshSource>(event.component);
+       if(mesh_source || dyn_mesh_src)
        {
                RenderedEntity &re = get_rendered_entity(event.component->get_entity());
                if(!re.mesh_renderer)
                {
-                       const GL::Object &object = stage.get_resources().get<GL::Object>(mesh_source->get_object_name());
+                       const GL::Object &object = dyn_mesh_src ? dyn_mesh_src->get_object() :
+                               stage.get_resources().get<GL::Object>(mesh_source->get_object_name());
                        re.mesh_renderer = Game::Owned<MeshRenderer>(re.entity, object);
                        re.mesh_renderer->set_scene(&scene);
                }