diff --git a/projects/particle_simulation/CMakeLists.txt b/projects/particle_simulation/CMakeLists.txt index 43dfb16922831979fcdbc68f8a4d9ef7f91793e3..2e62c66e72f4eea9434acf4b1c4f26f739b5d989 100644 --- a/projects/particle_simulation/CMakeLists.txt +++ b/projects/particle_simulation/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) # adding source files to the project add_executable(particle_simulation src/main.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) if(MSVC) diff --git a/projects/particle_simulation/shaders/frag.spv b/projects/particle_simulation/shaders/frag.spv index fdf98514955a63d0e54791970d2f1867ca3c5375..1b3a6f369887d3cad66e2f93316261c9c94202ac 100644 Binary files a/projects/particle_simulation/shaders/frag.spv and b/projects/particle_simulation/shaders/frag.spv differ diff --git a/projects/particle_simulation/src/Particle.cpp b/projects/particle_simulation/src/Particle.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdb69f7b49f0a9e0dae2b6c49766ef1253db9e78 --- /dev/null +++ b/projects/particle_simulation/src/Particle.cpp @@ -0,0 +1,32 @@ + +#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 diff --git a/projects/particle_simulation/src/Particle.hpp b/projects/particle_simulation/src/Particle.hpp index b392d5434f2e1183fd9f0012e2b17839ae0ee94f..9c08631e3679d53f0ddb154f9f99c834db9d5b85 100644 --- a/projects/particle_simulation/src/Particle.hpp +++ b/projects/particle_simulation/src/Particle.hpp @@ -1,20 +1,28 @@ #pragma once +#include <glm/glm.hpp> + class Particle { public: - Particle(glm::vec3 position, glm::vec3 velocity) - noexcept - : m_position(position), - m_velocity(velocity) {} - - const glm::vec3& getPosition()const{ - return m_position; - }; + Particle(glm::vec3 position, glm::vec3 velocity, float lifeTime = 1.f); + + const glm::vec3& getPosition()const; + + void setPosition( const glm::vec3 pos ); + + void update( const float delta ); + + const bool isAlive()const; + + void setLifeTime( const float lifeTime ); + + const float& getLifeTime()const; + private: // all properties of the Particle glm::vec3 m_position; - float padding = 0.f; + float m_lifeTime; glm::vec3 m_velocity; float padding_2 = 0.f; }; \ No newline at end of file diff --git a/projects/particle_simulation/src/ParticleSystem.cpp b/projects/particle_simulation/src/ParticleSystem.cpp index 5428b02b46988917bcad53c19de1eaa051aaef38..4c3a92863098e678b2c178565e268eb20b3839a9 100644 --- a/projects/particle_simulation/src/ParticleSystem.cpp +++ b/projects/particle_simulation/src/ParticleSystem.cpp @@ -1,6 +1,6 @@ #include "ParticleSystem.hpp" -const std::vector<Particle>& ParticleSystem::getParticles() const { +const std::vector<Particle>& ParticleSystem::getParticles() const{ return m_particles; } @@ -9,4 +9,20 @@ void ParticleSystem::addParticle( const Particle particle ){ } void ParticleSystem::addParticles( const std::vector<Particle> particles ){ 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; +} diff --git a/projects/particle_simulation/src/ParticleSystem.hpp b/projects/particle_simulation/src/ParticleSystem.hpp index c7e1a2fd55a00dd97ec67c762b88f53748bb872b..e4ec3b3687824771deef573aa4328776cbe5212a 100644 --- a/projects/particle_simulation/src/ParticleSystem.hpp +++ b/projects/particle_simulation/src/ParticleSystem.hpp @@ -1,6 +1,5 @@ #pragma once -#include <glm/glm.hpp> #include <vector> #include "Particle.hpp" @@ -10,7 +9,12 @@ public: const std::vector<Particle> &getParticles() const; void addParticle( const Particle particle ); void addParticles( const std::vector<Particle> particles ); + void updateParticles( const float deltaTime ); + void setRespawnPos( const glm::vec3 respawnPos ); + void setMaxLifeTime( const float respawnTime ); private: std::vector<Particle> m_particles; + glm::vec3 m_respawnPos = glm::vec3(0.f); + float m_maxLifeTime = 1.f; }; \ No newline at end of file diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp index a0db6620d5a4c987f8983d5ee8132e6692b04ef0..ef718ceba005333626cd42f9ce01fd06db28dad1 100644 --- a/projects/particle_simulation/src/main.cpp +++ b/projects/particle_simulation/src/main.cpp @@ -57,11 +57,7 @@ int main(int argc, const char** argv) { particleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX); particleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT); - std::vector<vkcv::DescriptorBinding> descriptorBindings = { - 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::DescriptorSetHandle descriptorSet = core.createDescriptorSet(particleShaderProgram.getReflectedDescriptors()[0]); vkcv::Buffer<glm::vec3> vertexBuffer = core.createBuffer<glm::vec3>( vkcv::BufferType::VERTEX, @@ -124,22 +120,19 @@ int main(int argc, const char** argv) { vkcv::DescriptorSetUsage descriptorUsage(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); window.e_mouseMove.add([&]( double offsetX, double 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.setMaxLifeTime(3.f); + glm::vec3 vel = glm::vec3(0.f , 0.1f, 0.0f); particleSystem.addParticles({ - Particle(instancePosition, glm::vec3(0.f)), - Particle(glm::vec3( 0.2f, 0.1f, 0.f), glm::vec3(0.f)), - Particle(glm::vec3(0.15f, 0.f, 0.1f), 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), vel, 2.f), + Particle(glm::vec3(0.15f, 0.f, 0.1f), vel, 3.f)}); std::vector<glm::mat4> modelMatrices; std::vector<vkcv::DrawcallInfo> drawcalls; @@ -152,8 +145,6 @@ int main(int argc, const char** argv) { glm::vec4 colorData = glm::vec4(1.0f,1.0f,0.0f,1.0f); - - while (window.isWindowOpen()) { window.pollEvents(); @@ -169,6 +160,12 @@ int main(int argc, const char** argv) { auto end = std::chrono::system_clock::now(); float deltatime = std::chrono::duration<float>(end - start).count(); 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));