Skip to content
Snippets Groups Projects
Commit 8ac57685 authored by Sebastian Gaida's avatar Sebastian Gaida
Browse files

[#69] add moving and respawning particles

parent f3261631
No related branches found
No related tags found
1 merge request!56Resolve "Partikelsystem"
Pipeline #25691 failed
...@@ -11,7 +11,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) ...@@ -11,7 +11,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
# adding source files to the project # adding source files to the project
add_executable(particle_simulation src/main.cpp add_executable(particle_simulation src/main.cpp
src/ParticleSystem.hpp src/ParticleSystem.cpp src/ParticleSystem.hpp src/ParticleSystem.cpp
src/Particle.hpp) src/Particle.hpp src/Particle.cpp)
# this should fix the execution path to load local files from the project (for MSVC) # this should fix the execution path to load local files from the project (for MSVC)
if(MSVC) if(MSVC)
......
No preview for this file type
#include "Particle.hpp"
Particle::Particle(glm::vec3 position, glm::vec3 velocity, float lifeTime)
: m_position(position),
m_velocity(velocity),
m_lifeTime(lifeTime)
{}
const glm::vec3& Particle::getPosition()const{
return m_position;
}
const bool Particle::isAlive()const{
return m_lifeTime > 0.f;
}
void Particle::setPosition( const glm::vec3 pos ){
m_position = pos;
}
void Particle::update( const float delta ){
m_position += m_velocity * delta;
}
void Particle::setLifeTime( const float lifeTime ){
m_lifeTime = lifeTime;
}
const float& Particle::getLifeTime()const{
return m_lifeTime;
}
\ No newline at end of file
#pragma once #pragma once
#include <glm/glm.hpp>
class Particle { class Particle {
public: public:
Particle(glm::vec3 position, glm::vec3 velocity) Particle(glm::vec3 position, glm::vec3 velocity, float lifeTime = 1.f);
noexcept
: m_position(position), const glm::vec3& getPosition()const;
m_velocity(velocity) {}
void setPosition( const glm::vec3 pos );
const glm::vec3& getPosition()const{
return m_position; void update( const float delta );
};
const bool isAlive()const;
void setLifeTime( const float lifeTime );
const float& getLifeTime()const;
private: private:
// all properties of the Particle // all properties of the Particle
glm::vec3 m_position; glm::vec3 m_position;
float padding = 0.f; float m_lifeTime;
glm::vec3 m_velocity; glm::vec3 m_velocity;
float padding_2 = 0.f; float padding_2 = 0.f;
}; };
\ No newline at end of file
#include "ParticleSystem.hpp" #include "ParticleSystem.hpp"
const std::vector<Particle>& ParticleSystem::getParticles() const { const std::vector<Particle>& ParticleSystem::getParticles() const{
return m_particles; return m_particles;
} }
...@@ -9,4 +9,20 @@ void ParticleSystem::addParticle( const Particle particle ){ ...@@ -9,4 +9,20 @@ void ParticleSystem::addParticle( const Particle particle ){
} }
void ParticleSystem::addParticles( const std::vector<Particle> particles ){ void ParticleSystem::addParticles( const std::vector<Particle> particles ){
m_particles.insert(m_particles.end(), particles.begin(), particles.end()); m_particles.insert(m_particles.end(), particles.begin(), particles.end());
} }
\ No newline at end of file
void ParticleSystem::updateParticles( const float deltaTime ){
for(Particle& particle :m_particles){
bool alive = particle.isAlive();
particle.setPosition( particle.getPosition() * static_cast<float>(alive) + static_cast<float>(!alive) * m_respawnPos );
particle.setLifeTime( (particle.getLifeTime() * alive + !alive * m_maxLifeTime ) - deltaTime );
particle.update(deltaTime);
}
}
void ParticleSystem::setRespawnPos( const glm::vec3 respawnPos){
m_respawnPos = respawnPos;
}
void ParticleSystem::setMaxLifeTime( const float respawnTime ){
m_maxLifeTime = respawnTime;
}
#pragma once #pragma once
#include <glm/glm.hpp>
#include <vector> #include <vector>
#include "Particle.hpp" #include "Particle.hpp"
...@@ -10,7 +9,12 @@ public: ...@@ -10,7 +9,12 @@ public:
const std::vector<Particle> &getParticles() const; const std::vector<Particle> &getParticles() const;
void addParticle( const Particle particle ); void addParticle( const Particle particle );
void addParticles( const std::vector<Particle> particles ); void addParticles( const std::vector<Particle> particles );
void updateParticles( const float deltaTime );
void setRespawnPos( const glm::vec3 respawnPos );
void setMaxLifeTime( const float respawnTime );
private: private:
std::vector<Particle> m_particles; std::vector<Particle> m_particles;
glm::vec3 m_respawnPos = glm::vec3(0.f);
float m_maxLifeTime = 1.f;
}; };
\ No newline at end of file
...@@ -57,11 +57,7 @@ int main(int argc, const char** argv) { ...@@ -57,11 +57,7 @@ int main(int argc, const char** argv) {
particleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX); particleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX);
particleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT); particleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT);
std::vector<vkcv::DescriptorBinding> descriptorBindings = { vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(particleShaderProgram.getReflectedDescriptors()[0]);
vkcv::DescriptorBinding(0, vkcv::DescriptorType::UNIFORM_BUFFER, 1, vkcv::ShaderStage::FRAGMENT),
vkcv::DescriptorBinding(1, vkcv::DescriptorType::UNIFORM_BUFFER, 1, vkcv::ShaderStage::FRAGMENT)};
vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorBindings);
vkcv::Buffer<glm::vec3> vertexBuffer = core.createBuffer<glm::vec3>( vkcv::Buffer<glm::vec3> vertexBuffer = core.createBuffer<glm::vec3>(
vkcv::BufferType::VERTEX, vkcv::BufferType::VERTEX,
...@@ -124,22 +120,19 @@ int main(int argc, const char** argv) { ...@@ -124,22 +120,19 @@ int main(int argc, const char** argv) {
vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle); vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle);
//vkcv::DrawcallInfo drawcalls(renderMesh, {vkcv::DescriptorSetUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle)}); //vkcv::DrawcallInfo drawcalls(renderMesh, {vkcv::DescriptorSetUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle)});
glm::vec3 instancePosition;
glm::vec2 pos = glm::vec2(1.f); glm::vec2 pos = glm::vec2(1.f);
window.e_mouseMove.add([&]( double offsetX, double offsetY) { window.e_mouseMove.add([&]( double offsetX, double offsetY) {
pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY)); pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY));
instancePosition.x = static_cast<float>(offsetX);
instancePosition.y = static_cast<float>(offsetY);
instancePosition.z = -1.f;
}); });
ParticleSystem particleSystem; ParticleSystem particleSystem;
particleSystem.setMaxLifeTime(3.f);
glm::vec3 vel = glm::vec3(0.f , 0.1f, 0.0f);
particleSystem.addParticles({ particleSystem.addParticles({
Particle(instancePosition, glm::vec3(0.f)), Particle(glm::vec3(0.f, 1.f, 0.f), vel, 1.f),
Particle(glm::vec3( 0.2f, 0.1f, 0.f), glm::vec3(0.f)), Particle(glm::vec3( 0.2f, 0.1f, 0.f), vel, 2.f),
Particle(glm::vec3(0.15f, 0.f, 0.1f), glm::vec3(0.f))}); Particle(glm::vec3(0.15f, 0.f, 0.1f), vel, 3.f)});
std::vector<glm::mat4> modelMatrices; std::vector<glm::mat4> modelMatrices;
std::vector<vkcv::DrawcallInfo> drawcalls; std::vector<vkcv::DrawcallInfo> drawcalls;
...@@ -152,8 +145,6 @@ int main(int argc, const char** argv) { ...@@ -152,8 +145,6 @@ int main(int argc, const char** argv) {
glm::vec4 colorData = glm::vec4(1.0f,1.0f,0.0f,1.0f); glm::vec4 colorData = glm::vec4(1.0f,1.0f,0.0f,1.0f);
while (window.isWindowOpen()) while (window.isWindowOpen())
{ {
window.pollEvents(); window.pollEvents();
...@@ -169,6 +160,12 @@ int main(int argc, const char** argv) { ...@@ -169,6 +160,12 @@ int main(int argc, const char** argv) {
auto end = std::chrono::system_clock::now(); auto end = std::chrono::system_clock::now();
float deltatime = std::chrono::duration<float>(end - start).count(); float deltatime = std::chrono::duration<float>(end - start).count();
start = end; start = end;
particleSystem.updateParticles( deltatime );
modelMatrices.clear();
for(Particle particle : particleSystem.getParticles()) {
modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition()));
}
//modelmatrix = glm::rotate(modelmatrix, angle, glm::vec3(0,0,1)); //modelmatrix = glm::rotate(modelmatrix, angle, glm::vec3(0,0,1));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment