summaryrefslogtreecommitdiffstats
path: root/examples/qt3d/compute-particles/particles.comp
diff options
context:
space:
mode:
authorPaul Lemire <paul.lemire@kdab.com>2015-12-08 09:01:15 +0100
committerSean Harmer <sean.harmer@kdab.com>2016-01-14 21:36:00 +0000
commit4a44b6520ff8add3dd65be6224fd7410e5405939 (patch)
tree13a5a4a3df1cc3f7059afc7f8f55286f434f26e6 /examples/qt3d/compute-particles/particles.comp
parent08ad89dde168476c8e7045b84785f26eb8c8bc7a (diff)
Add a compute shader example
Change-Id: I956b647e2218ad3e604bd9e8260b3ea0a90dc84e Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'examples/qt3d/compute-particles/particles.comp')
-rw-r--r--examples/qt3d/compute-particles/particles.comp41
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/qt3d/compute-particles/particles.comp b/examples/qt3d/compute-particles/particles.comp
new file mode 100644
index 000000000..7feaf5fdd
--- /dev/null
+++ b/examples/qt3d/compute-particles/particles.comp
@@ -0,0 +1,41 @@
+#version 430 core
+
+uniform float particleStep;
+uniform float finalCollisionFactor;
+
+layout (local_size_x = 1024) in;
+
+struct ParticleData
+{
+ vec3 position;
+ vec3 direction;
+ vec3 color;
+};
+
+// Particles from previouse frame
+layout (std430, binding = 0) coherent buffer Particles
+{
+ ParticleData particles[];
+} data;
+
+void main(void)
+{
+ uint globalId = gl_GlobalInvocationID.x;
+
+ // Retrieve current particle from previous frame
+ ParticleData currentParticle = data.particles[globalId];
+
+ // New position = old position + distance traveled over step duration
+ currentParticle.position = currentParticle.position + currentParticle.direction * particleStep;
+
+ // Make acceleration more or less point toward the center of the scene
+ vec3 acceleration = normalize(vec3(0.0) - currentParticle.position) * finalCollisionFactor;
+
+ // New velocity = old velocity + acceleration over step duration
+ currentParticle.direction = currentParticle.direction + acceleration * particleStep;
+
+
+
+ // Save updated particle
+ data.particles[globalId] = currentParticle;
+}