diff --git a/projects/particle_simulation/CMakeLists.txt b/projects/particle_simulation/CMakeLists.txt index 7fd6399f1d3d6c9aa9c01238c55658c2cbc4600a..935a50864911d219ed1a19ca78194c0b520a0ca8 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 97376d3df46525e49aa5fd0f0f1b6f84b15665c1..7cce3a089413d16e1032bcf8d3e4d149c129d8e0 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 Binary files a/projects/particle_simulation/shaders/vert.spv and b/projects/particle_simulation/shaders/vert.spv differ diff --git a/projects/particle_simulation/src/ParticleSystem.cpp b/projects/particle_simulation/src/ParticleSystem.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5428b02b46988917bcad53c19de1eaa051aaef38 --- /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 0000000000000000000000000000000000000000..709a175cfd69da7d0635d269655eebc5c559b0ce --- /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 98c597dbe6fcce40d8bfd9b3a23a5a9d0eb6ca56..7c7356d493b00dbfd73158aecd62e6394252fcb7 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;