gpu_particle_burst_03.py Diff#

gpu_particle_burst_03.py#
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/development/doc/tutorials/gpu_particle_burst/gpu_particle_burst_02.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/development/doc/tutorials/gpu_particle_burst/gpu_particle_burst_03.py
@@ -1,6 +1,8 @@
 """
 Example showing how to create particle explosions via the GPU.
 """
+import random
+import time
 from array import array
 from dataclasses import dataclass
 
@@ -11,12 +13,15 @@
 SCREEN_HEIGHT = 768
 SCREEN_TITLE = "GPU Particle Explosion"
 
+PARTICLE_COUNT = 300
+
 
 @dataclass
 class Burst:
     """ Track for each burst. """
     buffer: arcade.gl.Buffer
     vao: arcade.gl.Geometry
+    start_time: float
 
 
 class MyWindow(arcade.Window):
@@ -27,7 +32,7 @@
 
         # Program to visualize the points
         self.program = self.ctx.load_program(
-            vertex_shader="vertex_shader_v1.glsl",
+            vertex_shader="vertex_shader_v2.glsl",
             fragment_shader="fragment_shader.glsl",
         )
 
@@ -43,6 +48,9 @@
         # Loop through each burst
         for burst in self.burst_list:
 
+            # Set the uniform data
+            self.program['time'] = time.time() - burst.start_time
+
             # Render the burst
             burst.vao.render(self.program, mode=self.ctx.POINTS)
 
@@ -55,8 +63,13 @@
 
         def _gen_initial_data(initial_x, initial_y):
             """ Generate data for each particle """
-            yield initial_x
-            yield initial_y
+            for i in range(PARTICLE_COUNT):
+                dx = random.uniform(-.2, .2)
+                dy = random.uniform(-.2, .2)
+                yield initial_x
+                yield initial_y
+                yield dx
+                yield dy
 
         # Recalculate the coordinates from pixels to the OpenGL system with
         # 0, 0 at the center.
@@ -72,14 +85,14 @@
         # Create a buffer description specifying the buffer's data format
         buffer_description = arcade.gl.BufferDescription(
             buffer,
-            '2f',
-            ['in_pos'])
+            '2f 2f',
+            ['in_pos', 'in_vel'])
 
         # Create our Vertex Attribute Object
         vao = self.ctx.geometry([buffer_description])
 
         # Create the Burst object and add it to the list of bursts
-        burst = Burst(buffer=buffer, vao=vao)
+        burst = Burst(buffer=buffer, vao=vao, start_time=time.time())
         self.burst_list.append(burst)