|
| 1 | +import math |
| 2 | +import random |
| 3 | + |
| 4 | +import numpy |
| 5 | + |
| 6 | +import moderngl as mgl |
| 7 | +from demosys.effects import effect |
| 8 | +from demosys.opengl import VAO |
| 9 | +from pyrr import matrix44, vector3 |
| 10 | + |
| 11 | + |
| 12 | +class FeedbackEffect(effect.Effect): |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + self.feedback = self.get_shader("feedback/transform.glsl") |
| 16 | + self.shader = self.get_shader("feedback/billboards.glsl") |
| 17 | + self.texture = self.get_texture("feedback/particle.png") |
| 18 | + |
| 19 | + # VAOs representing the two different buffer bindings |
| 20 | + self.particles1 = None |
| 21 | + self.particles2 = None |
| 22 | + self.particles = None |
| 23 | + |
| 24 | + # VBOs for each position buffer |
| 25 | + self.pos1 = None |
| 26 | + self.pos2 = None |
| 27 | + self.pos = None |
| 28 | + self.init_particles() |
| 29 | + |
| 30 | + @effect.bind_target |
| 31 | + def draw(self, time, frametime, target): |
| 32 | + self.ctx.disable(mgl.DEPTH_TEST) |
| 33 | + self.ctx.enable(mgl.BLEND) |
| 34 | + self.ctx.blend_func = mgl.SRC_ALPHA, mgl.ONE_MINUS_SRC_ALPHA |
| 35 | + |
| 36 | + m_proj = self.create_projection(90.0, 1.0, 1000.0) |
| 37 | + |
| 38 | + # Rotate and translate |
| 39 | + m_mv = self.create_transformation(rotation=(time * 0.0, time * 0, time * 0), |
| 40 | + translation=(0.0, 0.0, -40.0)) |
| 41 | + |
| 42 | + # Apply the rotation and translation from the system camera |
| 43 | + m_mv = matrix44.multiply(m_mv, self.sys_camera.view_matrix) |
| 44 | + |
| 45 | + gravity_pos = vector3.create(math.sin(time) * 5, |
| 46 | + math.cos(time) * 5, |
| 47 | + math.sin(time / 3) * 5) |
| 48 | + gravity_force = math.cos(time / 2) * 3.0 + 3.0 |
| 49 | + # gravity_force = 2.0 |
| 50 | + |
| 51 | + # Transform positions |
| 52 | + self.feedback.uniform("gravity_pos", gravity_pos.astype('f4').tobytes()) |
| 53 | + self.feedback.uniform("gravity_force", gravity_force) |
| 54 | + self.feedback.uniform("timedelta", frametime) |
| 55 | + self.particles.transform(self.feedback, self.pos) |
| 56 | + |
| 57 | + # Draw particles |
| 58 | + self.shader.uniform("m_proj", m_proj.astype('f4').tobytes()) |
| 59 | + self.shader.uniform("m_mv", m_mv.astype('f4').tobytes()) |
| 60 | + self.texture.use(location=0) |
| 61 | + self.shader.uniform("texture0", 0) |
| 62 | + self.particles.draw(self.shader) |
| 63 | + |
| 64 | + # Swap buffers |
| 65 | + self.pos = self.pos1 if self.pos == self.pos2 else self.pos2 |
| 66 | + self.particles = self.particles1 if self.particles == self.particles2 else self.particles2 |
| 67 | + |
| 68 | + def init_particles(self): |
| 69 | + count = 50000 |
| 70 | + area = 100.0 |
| 71 | + speed = 5.0 |
| 72 | + |
| 73 | + def gen(): |
| 74 | + for _ in range(count): |
| 75 | + # Position |
| 76 | + yield random.uniform(-area, area) |
| 77 | + yield random.uniform(-area, area) |
| 78 | + yield random.uniform(-area, area) |
| 79 | + # Velocity |
| 80 | + yield random.uniform(-speed, speed) |
| 81 | + yield random.uniform(-speed, speed) |
| 82 | + yield random.uniform(-speed, speed) |
| 83 | + |
| 84 | + data1 = numpy.fromiter(gen(), count=count * 6, dtype=numpy.float32) |
| 85 | + data2 = numpy.fromiter(gen(), count=count * 6, dtype=numpy.float32) |
| 86 | + |
| 87 | + self.pos1 = self.ctx.buffer(data1.tobytes()) |
| 88 | + self.particles1 = VAO("particles1", mode=mgl.POINTS) |
| 89 | + self.particles1.buffer(self.pos1, '3f 3f', ['in_position', 'in_velocity']) |
| 90 | + |
| 91 | + self.pos2 = self.ctx.buffer(data2.tobytes()) |
| 92 | + self.particles2 = VAO("particles2", mode=mgl.POINTS) |
| 93 | + self.particles2.buffer(self.pos2, '3f 3f', ['in_position', 'in_velocity']) |
| 94 | + |
| 95 | + # Set initial start buffers |
| 96 | + self.particles = self.particles1 |
| 97 | + self.pos = self.pos2 |
0 commit comments