Skip to content

Commit

Permalink
Added the ParticleEmitter component. #60
Browse files Browse the repository at this point in the history
Added one to the default scene.
  • Loading branch information
MStachowicz committed Oct 11, 2023
1 parent aac020b commit e422ff5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions source/Component/ParticleEmitter.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 35 additions & 0 deletions source/Component/ParticleEmitter.hpp
Original file line number Diff line number Diff line change
@@ -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<Particle> particles;
};
}
7 changes: 7 additions & 0 deletions source/System/SceneSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "RigidBody.hpp"
#include "Texture.hpp"
#include "Transform.hpp"
#include "Component/ParticleEmitter.hpp"

// Geometry
#include "Geometry.hpp"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e422ff5

Please sign in to comment.