]> git.tdb.fi Git - libs/game.git/commitdiff
Add components for creating lights
authorMikko Rasa <tdb@tdb.fi>
Sat, 28 Jan 2023 14:28:47 +0000 (16:28 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 28 Jan 2023 20:55:12 +0000 (22:55 +0200)
source/game/light.cpp [new file with mode: 0644]
source/game/light.h [new file with mode: 0644]
source/game/resources.cpp
source/game/setups.mgs
source/gameview/lightemitter.cpp [new file with mode: 0644]
source/gameview/lightemitter.h [new file with mode: 0644]
source/gameview/renderer.cpp
source/gameview/renderer.h

diff --git a/source/game/light.cpp b/source/game/light.cpp
new file mode 100644 (file)
index 0000000..91db96f
--- /dev/null
@@ -0,0 +1,10 @@
+#include "light.h"
+
+namespace Msp::Game {
+
+Light::Light(Handle<Entity> e, const Setup &s):
+       Component(e),
+       setup(s)
+{ }
+
+} // namespace Msp::Game
diff --git a/source/game/light.h b/source/game/light.h
new file mode 100644 (file)
index 0000000..0df6a42
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef MSP_GAME_LIGHT_H_
+#define MSP_GAME_LIGHT_H_
+
+#include "component.h"
+#include "mspgame_api.h"
+#include "setups.h"
+
+namespace Msp::Game {
+
+class MSPGAME_API Light: public Component
+{
+public:
+       using Setup = LightSetup;
+
+private:
+       const Setup &setup;
+
+public:
+       Light(Handle<Entity>, const Setup &);
+
+       LightType get_type() const { return setup.type; }
+       float get_intensity() const { return setup.intensity; }
+};
+
+} // namespace Msp::Game
+
+#endif
index a4f9e76e7d1e9ee3a3bd63a9ad4b9c48eb9cc144..fb8f1970443e714a00592f45dc523ad71bc01034 100644 (file)
@@ -11,6 +11,7 @@ namespace Msp::Game {
 Resources::Resources()
 {
        add_type<CameraSetup>().suffix(".camera.setup");
+       add_type<LightSetup>().suffix(".light.setup");
 }
 
 
index 3c36d3723a0b2d2533102beaae5fc487d33c2117..6bd13bce23c21a8f8d87062b8e98784697914aa0 100644 (file)
@@ -19,6 +19,18 @@ component Camera
        field sequence_name string;
 };
 
+enum LightType
+{
+       value DIRECTIONAL;
+       value POINT;
+};
+
+component Light
+{
+       field type LightType { default "DIRECTIONAL"; };
+       field intensity float { default "1.0f"; };
+};
+
 component MeshSource
 {
        field object_name string;
diff --git a/source/gameview/lightemitter.cpp b/source/gameview/lightemitter.cpp
new file mode 100644 (file)
index 0000000..0f78fe3
--- /dev/null
@@ -0,0 +1,41 @@
+#include "lightemitter.h"
+#include <msp/game/entity.h>
+#include <msp/game/transform.h>
+
+namespace Msp::GameView {
+
+LightEmitter::LightEmitter(Game::Handle<Game::Entity> e, Game::LightType t):
+       Component(e)
+{
+       if(t==Game::LightType::DIRECTIONAL)
+               light = &storage.emplace<GL::DirectionalLight>();
+       else
+               light = &storage.emplace<GL::PointLight>();
+}
+
+LightEmitter::~LightEmitter()
+{
+       if(lighting)
+               lighting->detach(*light);
+}
+
+void LightEmitter::set_lighting(GL::Lighting *l)
+{
+       if(lighting)
+               lighting->detach(*light);
+       lighting = l;
+       if(lighting)
+               lighting->attach(*light);
+}
+
+void LightEmitter::set_intensity(float i)
+{
+       light->set_color(GL::Color(i));
+}
+
+void LightEmitter::update_matrix()
+{
+       light->set_matrix(entity->get_transform()->get_world_matrix());
+}
+
+} // namespace Msp::GameView
diff --git a/source/gameview/lightemitter.h b/source/gameview/lightemitter.h
new file mode 100644 (file)
index 0000000..d3e2f83
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef MSP_GAMEVIEW_LIGHTEMITTER_H_
+#define MSP_GAMEVIEW_LIGHTEMITTER_H_
+
+#include <variant>
+#include <msp/game/component.h>
+#include <msp/game/light.h>
+#include <msp/gl/directionallight.h>
+#include <msp/gl/lighting.h>
+#include <msp/gl/pointlight.h>
+#include "mspgameview_api.h"
+
+namespace Msp::GameView {
+
+class MSPGAMEVIEW_API LightEmitter: public Game::Component
+{
+private:
+       std::variant<std::monostate, GL::DirectionalLight, GL::PointLight> storage;
+       GL::Light *light = nullptr;
+       GL::Lighting *lighting = nullptr;
+
+public:
+       LightEmitter(Game::Handle<Game::Entity>, Game::LightType);
+       ~LightEmitter();
+
+       void set_lighting(GL::Lighting *);
+
+       void set_intensity(float);
+
+       void update_matrix();
+};
+
+} // namespace Msp::GameView
+
+#endif
index a12333392108b42f8a00b380065abdf353d46d30..e6b2696c9755571f68cd550846923bc1c752b28f 100644 (file)
@@ -1,10 +1,14 @@
 #include "renderer.h"
 #include <msp/game/entity.h>
+#include <msp/game/light.h>
 #include <msp/game/meshsource.h>
 #include <msp/game/stage.h>
 #include <msp/game/transform.h>
 #include <msp/gl/sequencebuilder.h>
 #include <msp/gl/sequencetemplate.h>
+#include <msp/gl/directionallight.h>
+#include <msp/gl/pointlight.h>
+#include "lightemitter.h"
 #include "meshrenderer.h"
 
 using namespace std;
@@ -49,6 +53,18 @@ void Renderer::component_created(const Game::Events::ComponentCreated &event)
                        re.mesh_renderer->set_scene(&scene);
                }
        }
+
+       Game::Handle<Game::Light> light = dynamic_handle_cast<Game::Light>(event.component);
+       if(light)
+       {
+               RenderedEntity &re = get_rendered_entity(event.component->get_entity());
+               if(!re.light_emitter)
+               {
+                       re.light_emitter = Game::Owned<LightEmitter>(re.entity, light->get_type());
+                       re.light_emitter->set_intensity(light->get_intensity());
+                       re.light_emitter->set_lighting(&lighting);
+               }
+       }
 }
 
 Renderer::RenderedEntity &Renderer::get_rendered_entity(Game::Handle<Game::Entity> entity)
@@ -103,7 +119,12 @@ void Renderer::tick(Time::TimeDelta)
        }
 
        for(const RenderedEntity &e: entities)
-               e.mesh_renderer->update_matrix();
+       {
+               if(e.mesh_renderer)
+                       e.mesh_renderer->update_matrix();
+               if(e.light_emitter)
+                       e.light_emitter->update_matrix();
+       }
 
        view.render();
 }
index 484e07cb650c6494185c0fb02dd6c9abd95a56af..54d13653189f9349c01f270b3a074bcd22c1f117 100644 (file)
@@ -7,6 +7,8 @@
 #include <msp/game/owned.h>
 #include <msp/game/system.h>
 #include <msp/gl/camera.h>
+#include <msp/gl/light.h>
+#include <msp/gl/lighting.h>
 #include <msp/gl/sequence.h>
 #include <msp/gl/simplescene.h>
 #include <msp/gl/view.h>
@@ -14,6 +16,7 @@
 
 namespace Msp::GameView {
 
+class LightEmitter;
 class MeshRenderer;
 
 class MSPGAMEVIEW_API Renderer: public Game::System
@@ -23,6 +26,7 @@ private:
        {
                Game::Handle<Game::Entity> entity;
                Game::Owned<MeshRenderer> mesh_renderer;
+               Game::Owned<LightEmitter> light_emitter;
 
                RenderedEntity(Game::Handle<Game::Entity>);
        };
@@ -31,6 +35,7 @@ private:
        Game::EventObserver event_observer;
        std::vector<RenderedEntity> entities;
        GL::SimpleScene scene;
+       GL::Lighting lighting;
        Game::Handle<Game::Camera> active_camera;
        GL::Camera gl_camera;
        std::string current_seq_name;