diff --git a/projects/particle_simulation/shaders/shader_gravity.comp b/projects/particle_simulation/shaders/shader_gravity.comp new file mode 100644 index 0000000000000000000000000000000000000000..77954958c694a3c6c620818dd3b5d999e51b4a42 --- /dev/null +++ b/projects/particle_simulation/shaders/shader_gravity.comp @@ -0,0 +1,80 @@ +#version 450 core +#extension GL_ARB_separate_shader_objects : enable + +layout(local_size_x = 256) in; + +struct Particle +{ + vec3 position; + float lifeTime; + vec3 velocity; + float mass; + vec3 reset_velocity; + float _padding; +}; + +layout(std430, binding = 0) coherent buffer buffer_inParticle +{ + Particle inParticle[]; +}; + +layout( push_constant ) uniform constants{ + float deltaTime; + float rand; +}; + +const int n = 4; +vec4 gravityPoint[n] = vec4[n]( + vec4(-0.8, -0.5, 0.0, 3), + vec4(-0.4, 0.5, 0.8, 2), + vec4( 0.8, 0.8, -0.3, 4), + vec4( 0.5, -0.7, -0.5, 1) +); + +const float G = 6.6743015e-11; +const float sim_d_factor = 10e11; +const float sim_g_factor = 10e30; +const float sim_t_factor = 5; +const float c = 299792458; + +void main() { + uint id = gl_GlobalInvocationID.x; + inParticle[id].lifeTime -= deltaTime; + vec3 pos = inParticle[id].position; + vec3 vel = inParticle[id].velocity; + float mass = inParticle[id].mass; + + if(inParticle[id].lifeTime < 0.f) + { + inParticle[id].lifeTime = 5.f * rand; + inParticle[id].mass *= rand; + + pos = vec3(0); + vel *= rand; + } + + for(int i = 0; i < n; i++) + { + vec3 d = (gravityPoint[i].xyz - pos) * sim_d_factor; + float r = length(d); + float g = G * (gravityPoint[i].w * sim_g_factor) / (r * r); + + if (r > 0) { + vec3 dvel = (deltaTime * sim_t_factor) * g * (d / r); + + vel = (vel + dvel) / (1.0 + dot(vel, dvel) / (c*c)); + } + } + + pos += vel * (deltaTime * sim_t_factor); + + vec3 a_pos = abs(pos); + + if ((a_pos.x > 2.0) || (a_pos.y > 2.0) || (a_pos.z > 2.0)) + { + inParticle[id].lifeTime *= 0.9; + } + + inParticle[id].position = pos; + inParticle[id].velocity = vel; +} diff --git a/projects/particle_simulation/src/Particle.hpp b/projects/particle_simulation/src/Particle.hpp index 268e4bcf5bba34720e66a5424bd817499ba294a6..f374218fd8a08f1e1bf367bdc899a71c55ea1b78 100644 --- a/projects/particle_simulation/src/Particle.hpp +++ b/projects/particle_simulation/src/Particle.hpp @@ -28,7 +28,7 @@ private: glm::vec3 m_position; float m_lifeTime; glm::vec3 m_velocity; - float padding_2 = 0.f; + float mass = 1.f; glm::vec3 m_reset_velocity; float padding_3 = 0.f; };