From 04eaac5b1222fa57aa85c43fb990b9209d01f931 Mon Sep 17 00:00:00 2001 From: Sebastian Gaida <gaida@ca-digit.com> Date: Mon, 14 Jun 2021 12:37:45 +0200 Subject: [PATCH] [#69] add ParticleSystem basics --- projects/particle_simulation/CMakeLists.txt | 3 +- .../particle_simulation/shaders/shader.vert | 4 +-- projects/particle_simulation/shaders/vert.spv | Bin 1184 -> 1184 bytes .../src/ParticleSystem.cpp | 12 +++++++ .../src/ParticleSystem.hpp | 30 ++++++++++++++++++ projects/particle_simulation/src/main.cpp | 25 ++++++++------- 6 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 projects/particle_simulation/src/ParticleSystem.cpp create mode 100644 projects/particle_simulation/src/ParticleSystem.hpp diff --git a/projects/particle_simulation/CMakeLists.txt b/projects/particle_simulation/CMakeLists.txt index 7fd6399f..935a5086 100644 --- a/projects/particle_simulation/CMakeLists.txt +++ b/projects/particle_simulation/CMakeLists.txt @@ -9,7 +9,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) # 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) # 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.vert b/projects/particle_simulation/shaders/shader.vert index 97376d3d..7cce3a08 100644 --- a/projects/particle_simulation/shaders/shader.vert +++ b/projects/particle_simulation/shaders/shader.vert @@ -1,7 +1,7 @@ #version 450 core #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 position; +layout(location = 0) in vec3 particle; layout( push_constant ) uniform constants{ mat4 mvp; @@ -9,5 +9,5 @@ layout( push_constant ) uniform constants{ void main() { - gl_Position = mvp * vec4(position, 1.0); + gl_Position = mvp * vec4(particle, 1.0); } \ No newline at end of file diff --git a/projects/particle_simulation/shaders/vert.spv b/projects/particle_simulation/shaders/vert.spv index 1d8d780bcb1a3dff229389902f9d624e83500380..e77272b23e4965f386db65c89cdb96aaa92b0964 100644 GIT binary patch delta 20 ccmZ3$xqx%ST1NK7qLR$yoYc)*7~e1f08r%z@&Et; delta 20 ccmZ3$xqx%ST1NK#;>?oF{JhOu7~e1f08$7D6951J diff --git a/projects/particle_simulation/src/ParticleSystem.cpp b/projects/particle_simulation/src/ParticleSystem.cpp new file mode 100644 index 00000000..5428b02b --- /dev/null +++ b/projects/particle_simulation/src/ParticleSystem.cpp @@ -0,0 +1,12 @@ +#include "ParticleSystem.hpp" + +const std::vector<Particle>& ParticleSystem::getParticles() const { + return m_particles; +} + +void ParticleSystem::addParticle( const Particle particle ){ + m_particles.push_back(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 diff --git a/projects/particle_simulation/src/ParticleSystem.hpp b/projects/particle_simulation/src/ParticleSystem.hpp new file mode 100644 index 00000000..709a175c --- /dev/null +++ b/projects/particle_simulation/src/ParticleSystem.hpp @@ -0,0 +1,30 @@ +#pragma once + +#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; +}; + +class ParticleSystem { + +public: + const std::vector<Particle> &getParticles() const; + void addParticle( const Particle particle ); + void addParticles( const std::vector<Particle> particles ); + +private: + std::vector<Particle> m_particles; + +}; diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp index 98c597db..7c7356d4 100644 --- a/projects/particle_simulation/src/main.cpp +++ b/projects/particle_simulation/src/main.cpp @@ -3,6 +3,7 @@ #include <GLFW/glfw3.h> #include <vkcv/camera/CameraManager.hpp> #include <chrono> +#include "ParticleSystem.hpp" int main(int argc, const char** argv) { const char* applicationName = "Particlesystem"; @@ -62,25 +63,27 @@ int main(int argc, const char** argv) { vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorBindings); - vkcv::Buffer<glm::vec3> vertexbuffer = core.createBuffer<glm::vec3>( + vkcv::Buffer<Particle> vertexBuffer = core.createBuffer<Particle>( vkcv::BufferType::VERTEX, 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)}; - - vertexbuffer.fill(vertices); + ParticleSystem particleSystem; + particleSystem.addParticles({ + Particle(glm::vec3(-0.5, 0.5, -1), glm::vec3(0.f)), + Particle(glm::vec3(0.5, 0.5, -1), glm::vec3(0.f)), + Particle(glm::vec3(0, -0.5, -1), glm::vec3(0.f))}); + vertexBuffer.fill(particleSystem.getParticles()); vkcv::VertexAttribute attrib = vkcv::VertexAttribute{ vkcv::PrimitiveType::POSITION, - 0, - sizeof(glm::vec3) * vertices.size(), - 0, + offsetof(Particle, m_position), + sizeof(Particle::m_position) * particleSystem.getParticles().size(), + sizeof(Particle) - sizeof(Particle::m_position), // why is this the difference and not the total size? maybe calced after the last element not from the first like OpenGL 5126, - 3}; + sizeof(Particle::m_position) / sizeof(float)// number of elements, like vec3 = 3 + }; const vkcv::PipelineConfig particlePipelineDefinition( particleShaderProgram, @@ -103,7 +106,7 @@ int main(int argc, const char** argv) { ); const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { - vkcv::VertexBufferBinding(0, vertexbuffer.getVulkanHandle()) + vkcv::VertexBufferBinding(0, vertexBuffer.getVulkanHandle()) }; vkcv::DescriptorWrites setWrites; -- GitLab