From f32616313ff1e396c45649e650680221df6de4c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Katharina=20Kr=C3=A4mer?= <kkraemer4@uni-koblenz.de>
Date: Mon, 14 Jun 2021 15:27:31 +0200
Subject: [PATCH] [#69] added multiple drawcalls for particles

---
 projects/particle_simulation/CMakeLists.txt   |  3 +-
 .../particle_simulation/shaders/shader.frag   |  6 +-
 projects/particle_simulation/src/Particle.hpp | 20 +++++
 .../src/ParticleSystem.hpp                    | 18 +---
 projects/particle_simulation/src/main.cpp     | 83 +++++++------------
 5 files changed, 58 insertions(+), 72 deletions(-)
 create mode 100644 projects/particle_simulation/src/Particle.hpp

diff --git a/projects/particle_simulation/CMakeLists.txt b/projects/particle_simulation/CMakeLists.txt
index 935a5086..43dfb169 100644
--- a/projects/particle_simulation/CMakeLists.txt
+++ b/projects/particle_simulation/CMakeLists.txt
@@ -10,7 +10,8 @@ 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/ParticleSystem.hpp src/ParticleSystem.cpp
+		src/Particle.hpp)
 
 # this should fix the execution path to load local files from the project (for MSVC)
 if(MSVC)
diff --git a/projects/particle_simulation/shaders/shader.frag b/projects/particle_simulation/shaders/shader.frag
index 540281d9..f7a6c9bc 100644
--- a/projects/particle_simulation/shaders/shader.frag
+++ b/projects/particle_simulation/shaders/shader.frag
@@ -14,7 +14,7 @@ layout(set=0,binding=1) uniform uPosition{
 void main()
 {
 	vec2 mouse = vec2(Position.position.x, Position.position.y);
-
-		outColor = float(distance(gl_FragCoord.xy, mouse) < 100) * vec4(0,0,1,0) +
-		 		   float(distance(gl_FragCoord.xy, mouse) >= 100) * Color.color;
+		outColor = vec4(0,1,0,0);
+		//outColor = float(distance(gl_FragCoord.xy, mouse) < 100) * vec4(0,0,1,0) +
+		// 		   float(distance(gl_FragCoord.xy, mouse) >= 100) * Color.color;
 }
\ No newline at end of file
diff --git a/projects/particle_simulation/src/Particle.hpp b/projects/particle_simulation/src/Particle.hpp
new file mode 100644
index 00000000..b392d543
--- /dev/null
+++ b/projects/particle_simulation/src/Particle.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+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;
+    };
+private:
+    // all properties of the Particle
+    glm::vec3 m_position;
+    float padding = 0.f;
+    glm::vec3 m_velocity;
+    float padding_2 = 0.f;
+};
\ No newline at end of file
diff --git a/projects/particle_simulation/src/ParticleSystem.hpp b/projects/particle_simulation/src/ParticleSystem.hpp
index 709a175c..c7e1a2fd 100644
--- a/projects/particle_simulation/src/ParticleSystem.hpp
+++ b/projects/particle_simulation/src/ParticleSystem.hpp
@@ -2,20 +2,7 @@
 
 #include <glm/glm.hpp>
 #include <vector>
-
-struct Particle{
-    Particle(glm::vec3 position, glm::vec3 velocity)
-    noexcept
-    : m_position(position),
-    m_velocity(velocity)
-    {}
-
-    // all properties of the Particle
-    glm::vec3 m_position;
-    float padding = 0.f;
-    glm::vec3 m_velocity;
-    float padding_2 = 0.f;
-};
+#include "Particle.hpp"
 
 class ParticleSystem {
 
@@ -26,5 +13,4 @@ public:
 
 private:
     std::vector<Particle> m_particles;
-
-};
+};
\ No newline at end of file
diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp
index 2767c23c..a0db6620 100644
--- a/projects/particle_simulation/src/main.cpp
+++ b/projects/particle_simulation/src/main.cpp
@@ -68,9 +68,9 @@ int main(int argc, const char** argv) {
             3
     );
 
-    const std::vector<glm::vec3> vertices = {glm::vec3(-0.5, 0.5, -1),
-                                             glm::vec3( 0.5, 0.5, -1),
-                                             glm::vec3(0, -0.5, -1)};
+    const std::vector<glm::vec3> vertices = {glm::vec3(-0.1, 0.1, 0),
+                                             glm::vec3( 0.1, 0.1, 0),
+                                             glm::vec3(0, -0.1, 0)};
 
     vertexBuffer.fill(vertices);
 
@@ -82,12 +82,6 @@ int main(int argc, const char** argv) {
             5126,
             3};
 
-    ParticleSystem particleSystem;
-    particleSystem.addParticles({
-                                        Particle(glm::vec3(0.f), glm::vec3(0.f)),
-                                        Particle(glm::vec3(0.f), glm::vec3(0.f)),
-                                        Particle(glm::vec3(0.f), glm::vec3(0.f))});
-
     const vkcv::PipelineConfig particlePipelineDefinition(
             particleShaderProgram,
             UINT32_MAX,
@@ -112,6 +106,7 @@ int main(int argc, const char** argv) {
             vkcv::VertexBufferBinding(0, vertexBuffer.getVulkanHandle())
     };
 
+
     vkcv::DescriptorWrites setWrites;
     setWrites.uniformBufferWrites = {vkcv::UniformBufferDescriptorWrite(0,color.getHandle()),
                                      vkcv::UniformBufferDescriptorWrite(1,position.getHandle())};
@@ -125,46 +120,39 @@ int main(int argc, const char** argv) {
 
     const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle();
 
-    const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), 3);
+    const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), particleIndexBuffer.getCount());
     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)});
 
-    auto start = std::chrono::system_clock::now();
-
-    glm::vec4 colorData = glm::vec4(1.0f,1.0f,0.0f,1.0f);
+    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.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))});
+
+    std::vector<glm::mat4> modelMatrices;
+    std::vector<vkcv::DrawcallInfo> drawcalls;
+    for(auto particle :  particleSystem.getParticles()) {
+        modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition()));
+        drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage}));
+    }
 
-    struct Particle{
-        glm::vec2 Position;
-        glm::vec2 Velocity;
-        float Rotation = 0.0f;
-        float SizeBegin, SizeEnd;
-
-        float LifeTime = 1.0f;
-        float LifeRemaining = 0.0f;
-
-        bool Active = true;
-    };
-
-    std::vector<Particle> m_ParticlePool;
-    uint32_t poolIndex = 999;
+    auto start = std::chrono::system_clock::now();
 
-    m_ParticlePool.resize(1000);
+    glm::vec4 colorData = glm::vec4(1.0f,1.0f,0.0f,1.0f);
 
-    //float angle = 0.0005;
-    glm::mat4 modelmatrix = glm::mat4(1.0);
 
-    for(auto& particle : m_ParticlePool){
-        if(!particle.Active){
-            continue;
-        }
-    }
 
     while (window.isWindowOpen())
     {
@@ -183,25 +171,16 @@ int main(int argc, const char** argv) {
         start = end;
 
         //modelmatrix = glm::rotate(modelmatrix, angle, glm::vec3(0,0,1));
-        for(auto& particle : m_ParticlePool){
-            if (!particle.Active){
-                continue;
-            }
-            if (particle.LifeRemaining <= 0.0f){
-                particle.Active = false;
-                continue;
-            }
-
-            particle.LifeRemaining -= deltatime;
-            particle.Position += particle.Velocity * deltatime;
-            particle.Rotation += 0.01f * deltatime;
-
-        }
 
         cameraManager.getCamera().updateView(deltatime);
-        const glm::mat4 mvp = modelmatrix * cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView();
+        //const glm::mat4 mvp = modelmatrix * cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView();
+        std::vector<glm::mat4> mvp;
+        mvp.clear();
+        for(const auto& m : modelMatrices){
+            mvp.push_back(m * cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView());
+        }
 
-        vkcv::PushConstantData pushConstantData((void*)&mvp, sizeof(glm::mat4));
+        vkcv::PushConstantData pushConstantData((void*)mvp.data(), sizeof(glm::mat4));
         auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics);
 
         core.recordDrawcallsToCmdStream(
-- 
GitLab