From e422ff5c3892c55d6aacf5d860c2e59546851d16 Mon Sep 17 00:00:00 2001 From: MichalSt Date: Wed, 11 Oct 2023 22:57:53 +0100 Subject: [PATCH] Added the ParticleEmitter component. #60 Added one to the default scene. --- CMakeLists.txt | 2 ++ source/Component/ParticleEmitter.cpp | 19 +++++++++++++++ source/Component/ParticleEmitter.hpp | 35 ++++++++++++++++++++++++++++ source/System/SceneSystem.cpp | 7 ++++++ 4 files changed, 63 insertions(+) create mode 100644 source/Component/ParticleEmitter.cpp create mode 100644 source/Component/ParticleEmitter.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1508450..bceb418 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,8 @@ source/Component/Input.hpp source/Component/Label.hpp source/Component/Mesh.cpp source/Component/Mesh.hpp +source/Component/ParticleEmitter.hpp +source/Component/ParticleEmitter.cpp source/Component/RigidBody.cpp source/Component/RigidBody.hpp source/Component/Lights.cpp diff --git a/source/Component/ParticleEmitter.cpp b/source/Component/ParticleEmitter.cpp new file mode 100644 index 0000000..796a777 --- /dev/null +++ b/source/Component/ParticleEmitter.cpp @@ -0,0 +1,19 @@ +#include "ParticleEmitter.hpp" + +namespace Component +{ + ParticleEmitter::ParticleEmitter(const TextureRef& p_texture) + : diffuse{p_texture} + , emit_position{glm::vec3(0.f)} + , emit_velocity_min{glm::vec3(-0.5f, 1.f, -0.5f)} + , emit_velocity_max{glm::vec3( 0.5f, 1.f, 0.5f)} + , spawn_period{1.f} + , time_to_next_spawn{0.f} // Spawn on creation. + , spawn_count{3u} + , lifetime{7.f} + , max_particle_count{1000} + , particles{} + { + particles.reserve(max_particle_count); + } +} \ No newline at end of file diff --git a/source/Component/ParticleEmitter.hpp b/source/Component/ParticleEmitter.hpp new file mode 100644 index 0000000..89b0d47 --- /dev/null +++ b/source/Component/ParticleEmitter.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "Component/Texture.hpp" +#include "glm/vec3.hpp" +#include "glm/vec4.hpp" + +#include "Utility/Config.hpp" + +namespace Component +{ + struct Particle + { + glm::vec4 position; + glm::vec4 velocity; + DeltaTime lifetime; + }; + + class ParticleEmitter + { + public: + ParticleEmitter(const TextureRef& p_texture); + + TextureRef diffuse; // Texture applied to all the particles. + glm::vec3 emit_position; // Position particles emit from. + glm::vec3 emit_velocity_min; // Minimum starting velocity in m/s. + glm::vec3 emit_velocity_max; // Maximum starting velocity in m/s. + DeltaTime spawn_period; // Duration between particle spawn attempts. + DeltaTime time_to_next_spawn; // Duration remaining to next spawn attempt. + size_t spawn_count; // How many particles to spawn every spawn_period. + DeltaTime lifetime; // Duration in seconds a particle stays alive before being removed. + size_t max_particle_count; // Max number of particles that can be alive concurrently. + + std::vector particles; + }; +} \ No newline at end of file diff --git a/source/System/SceneSystem.cpp b/source/System/SceneSystem.cpp index 1576897..a10191d 100644 --- a/source/System/SceneSystem.cpp +++ b/source/System/SceneSystem.cpp @@ -14,6 +14,7 @@ #include "RigidBody.hpp" #include "Texture.hpp" #include "Transform.hpp" +#include "Component/ParticleEmitter.hpp" // Geometry #include "Geometry.hpp" @@ -200,6 +201,12 @@ namespace System m_scene.m_entities.addEntity(Component::Label{"Spotlight 1"}, spotlight); } } + + { // Particle + m_scene.m_entities.addEntity( + Component::Label{"Particle emitter"}, + Component::ParticleEmitter{mTextureSystem.getTexture(Config::Texture_Directory / "marcy.jpeg")}); + } } void SceneSystem::constructBoxScene()