Skip to content

Commit e672a02

Browse files
authored
Add FPS and boid counters to GPU flocking example (#205)
1 parent 9f34b8b commit e672a02

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

crates/processing_pyo3/examples/flocking_duck.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
center = None
162162
extent = 0.0
163163
max_speed = 0.0
164+
boid_count = 0
165+
title_last_time = 0.0
166+
title_last_frame = 0
164167

165168

166169
# Two triangles folded slightly along the nose-tail spine, like a paper
@@ -185,7 +188,7 @@ def boid_geometry(half_width, length, droop):
185188

186189

187190
def setup():
188-
global p, boid, mat, flock_pass, integrate_pass, center, extent, max_speed
191+
global p, boid, mat, flock_pass, integrate_pass, center, extent, max_speed, boid_count
189192

190193
size(900, 700)
191194
mode_3d()
@@ -215,6 +218,8 @@ def setup():
215218
# derived from the mesh's bounding box, so the sketch doesn't care what
216219
# units the model was authored in.
217220
homes = p.buffer(Attribute.position()).read()
221+
boid_count = len(homes)
222+
window_title(f"GPU Flocking Duck — {boid_count:,} boids")
218223
lo = [min(v[i] for v in homes) for i in range(3)]
219224
hi = [max(v[i] for v in homes) for i in range(3)]
220225
center = [(lo[i] + hi[i]) * 0.5 for i in range(3)]
@@ -246,6 +251,15 @@ def setup():
246251

247252

248253
def draw():
254+
global title_last_time, title_last_frame
255+
256+
title_elapsed = elapsed_time - title_last_time
257+
if title_elapsed >= 0.5:
258+
fps = (frame_count - title_last_frame) / title_elapsed
259+
window_title(f"GPU Flocking Duck — {boid_count:,} boids — {fps:.0f} FPS")
260+
title_last_time = elapsed_time
261+
title_last_frame = frame_count
262+
249263
t = elapsed_time * 0.2
250264
r = extent * 1.1
251265
camera_position(center[0] + cos(t) * r, center[1] + extent * 0.35, center[2] + sin(t) * r)

crates/processing_pyo3/examples/flocking_gpu.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@
156156
mat = None
157157
flock_pass = None
158158
integrate_pass = None
159+
title_last_time = 0.0
160+
title_last_frame = 0
159161

160162

161163
# Two triangles folded slightly along the nose-tail spine, like a paper
@@ -183,6 +185,7 @@ def setup():
183185
global p, boid, mat, flock_pass, integrate_pass
184186

185187
size(900, 700)
188+
window_title(f"GPU Flocking — {BOID_COUNT:,} boids")
186189
mode_3d()
187190

188191
directional_light((0.95, 0.9, 0.85), 800.0)
@@ -226,6 +229,15 @@ def setup():
226229

227230

228231
def draw():
232+
global title_last_time, title_last_frame
233+
234+
title_elapsed = elapsed_time - title_last_time
235+
if title_elapsed >= 0.5:
236+
fps = (frame_count - title_last_frame) / title_elapsed
237+
window_title(f"GPU Flocking — {BOID_COUNT:,} boids — {fps:.0f} FPS")
238+
title_last_time = elapsed_time
239+
title_last_frame = frame_count
240+
229241
t = elapsed_time * 0.1
230242
r = BOUND * 2.6
231243
camera_position(cos(t) * r, BOUND * 0.8, sin(t) * r)

0 commit comments

Comments
 (0)