|
| 1 | +import moderngl |
| 2 | +import pygame |
| 3 | +import numpy as np |
| 4 | +import time |
| 5 | +import pyrr # For matrix transformations |
| 6 | +import pmma |
| 7 | + |
| 8 | +# Initialize Pygame |
| 9 | +pmma.init() |
| 10 | +pygame.init() |
| 11 | +screen = pygame.display.set_mode((0, 0), pygame.OPENGL | pygame.DOUBLEBUF | pygame.FULLSCREEN) |
| 12 | + |
| 13 | +# Initialize ModernGL Context |
| 14 | +ctx = moderngl.create_context() |
| 15 | + |
| 16 | +# Mesh Parameters |
| 17 | +n = 500 # Grid resolution |
| 18 | +size = 2.0 # World-space size of the mesh |
| 19 | +spacing = size / (n - 1) |
| 20 | + |
| 21 | +# Generate Mesh Vertices |
| 22 | +vertices = [] |
| 23 | +indices = [] |
| 24 | +for i in range(n): |
| 25 | + for j in range(n): |
| 26 | + x = -1.0 + j * spacing |
| 27 | + z = -1.0 + i * spacing |
| 28 | + y = 0.0 # Initial height |
| 29 | + vertices.extend([x, y, z]) |
| 30 | + |
| 31 | +# Generate Indices for Triangle Strip |
| 32 | +for i in range(n - 1): |
| 33 | + for j in range(n): |
| 34 | + indices.append(i * n + j) |
| 35 | + indices.append((i + 1) * n + j) |
| 36 | +indices = np.array(indices, dtype=np.uint32) |
| 37 | + |
| 38 | +# Convert to Numpy Array |
| 39 | +vertices = np.array(vertices, dtype=np.float32) |
| 40 | + |
| 41 | +# Create Buffers |
| 42 | +vbo = ctx.buffer(vertices) |
| 43 | +ibo = ctx.buffer(indices) |
| 44 | + |
| 45 | +# Camera and Projection Matrices |
| 46 | +proj_matrix = pyrr.matrix44.create_perspective_projection(45.0, screen.get_width() / screen.get_height(), 0.1, 10.0) |
| 47 | +view_matrix = pyrr.matrix44.create_look_at( |
| 48 | + eye=[0, 1.5 / 2, 3 / 2.3], # Camera position |
| 49 | + target=[0, 0, 0], # Looking at the center |
| 50 | + up=[0, 1, 0] # Up direction |
| 51 | +) |
| 52 | + |
| 53 | +# Shader Program |
| 54 | +prog = ctx.program( |
| 55 | + vertex_shader=""" |
| 56 | + #version 330 |
| 57 | + uniform float time; |
| 58 | + uniform mat4 proj; |
| 59 | + uniform mat4 view; |
| 60 | + uniform float wave_strength; |
| 61 | + uniform vec3 color1; |
| 62 | + uniform vec3 color2; |
| 63 | +
|
| 64 | + in vec3 in_vert; |
| 65 | + out vec3 v_color; |
| 66 | +
|
| 67 | + void main() { |
| 68 | + float dist = length(in_vert.xz); |
| 69 | + float scale = 1.0 - smoothstep(0.0, 1.2, dist); // Stronger waves near center |
| 70 | + float wave = sin(6.0 * dist - time * 3.0) * wave_strength * scale; |
| 71 | +
|
| 72 | + vec3 pos = vec3(in_vert.x, in_vert.y + wave, in_vert.z); |
| 73 | +
|
| 74 | + gl_Position = proj * view * vec4(pos, 1.0); |
| 75 | +
|
| 76 | + float t = 0.5 + 0.5 * (wave / wave_strength); |
| 77 | + v_color = mix(color1, color2, t); |
| 78 | + } |
| 79 | + """, |
| 80 | + fragment_shader=""" |
| 81 | + #version 330 |
| 82 | + in vec3 v_color; |
| 83 | + out vec4 fragColor; |
| 84 | +
|
| 85 | + void main() { |
| 86 | + fragColor = vec4(v_color, 1.0); |
| 87 | + } |
| 88 | + """ |
| 89 | +) |
| 90 | + |
| 91 | +# Pass Projection & View Matrices |
| 92 | +prog["proj"].write(proj_matrix.astype("f4").tobytes()) |
| 93 | +prog["view"].write(view_matrix.astype("f4").tobytes()) |
| 94 | + |
| 95 | +# Set Default Uniform Values |
| 96 | +prog["wave_strength"].value = 0.15 # Control displacement amount |
| 97 | +color_one = pmma.ColorConverter() |
| 98 | +color_two = pmma.ColorConverter() |
| 99 | +prog["color1"].value = color_one.generate_color_from_perlin_noise(format=pmma.Constants.SMALL_RGB) |
| 100 | +prog["color2"].value = color_two.generate_color_from_perlin_noise(format=pmma.Constants.SMALL_RGB) |
| 101 | + |
| 102 | +# Vertex Array Object |
| 103 | +vao = ctx.vertex_array(prog, [(vbo, "3f", "in_vert")], index_buffer=ibo) |
| 104 | + |
| 105 | +# Time Tracking |
| 106 | +start_time = time.time() |
| 107 | + |
| 108 | +# Main Loop |
| 109 | +running = True |
| 110 | +while running: |
| 111 | + for event in pygame.event.get(): |
| 112 | + if event.type == pygame.QUIT: |
| 113 | + running = False |
| 114 | + elif event.type == pygame.KEYDOWN: |
| 115 | + if event.key == pygame.K_UP: |
| 116 | + prog["wave_strength"].value += 0.05 # Increase wave effect |
| 117 | + elif event.key == pygame.K_DOWN: |
| 118 | + prog["wave_strength"].value -= 0.05 # Decrease wave effect |
| 119 | + |
| 120 | + prog["color1"].value = color_one.generate_color_from_perlin_noise(value=(time.time() - start_time)/7, format=pmma.Constants.SMALL_RGB) |
| 121 | + prog["color2"].value = color_two.generate_color_from_perlin_noise(value=(time.time() - start_time)/7, format=pmma.Constants.SMALL_RGB) |
| 122 | + prog["time"].value = time.time() - start_time |
| 123 | + |
| 124 | + # Render |
| 125 | + ctx.clear(0.1, 0.1, 0.1) |
| 126 | + vao.render(moderngl.TRIANGLE_STRIP) |
| 127 | + pygame.display.flip() |
| 128 | + |
| 129 | + pmma.compute() |
| 130 | + |
| 131 | +pygame.quit() |
0 commit comments