def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; out vec2 tex; void main() { gl_Position = vec4(vert, 0.0, 1.0); tex = vert / 2.0 + vec2(0.5, 0.5); } ''') frag = GL.NewFragmentShader(''' #version 330 in vec2 tex; out vec4 color; uniform float scale; uniform vec2 center; uniform int iter; void main() { vec2 z = vec2(5.0 * (tex.x - 0.5), 3.0 * (tex.y - 0.5)); vec2 c = vec2(1.33 * (tex.x - 0.5) * scale - center.x, (tex.y - 0.5) * scale - center.y); int i; for(i = 0; i < iter; i++) { vec2 v = vec2((z.x * z.x - z.y * z.y) + c.x, (z.y * z.x + z.x * z.y) + c.y); if (dot(v, v) > 4.0) break; z = v; } float cm = fract((i == iter ? 0.0 : float(i)) * 10 / iter); color = vec4(fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['center'] = prog['center'] context['scale'] = prog['scale'] vbo = GL.NewVertexBuffer( struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['iter'], 100) GL.SetUniform(prog['scale'], 1.0) GL.SetUniform(prog['center'], 0.3, 0.2) except GL.Error as error: print(error) exit(1)
def paintGL(self): context['mx'] += context['dmx'] context['my'] += context['dmy'] context['s'] += context['ds'] GL.Clear(240, 240, 240) GL.SetUniform(context['center'], context['mx'], context['my']) GL.SetUniform(context['scale'], 0.5**context['s']) GL.RenderTriangleStrip(context['vao'], 4) self.update()
def paintGL(self): z = 0.5**context['s'] context['mx'] += context['dmx'] * z context['my'] += context['dmy'] * z context['s'] += context['ds'] GL.Clear(240, 240, 240) GL.SetUniform(context['pos'], context['mx'] / z, context['my'] / z) GL.SetUniform(context['zoom'], z) GL.RenderTriangleStrip(context['vao'], 4) self.update()
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 uniform vec2 pos; uniform float zoom; in vec2 vert; out vec2 textcoord; void main() { gl_Position = vec4(vert, 0.0, 1.0); textcoord = ((vert + pos) * zoom) / 2.0 + vec2(0.5, 0.5); } ''') frag = GL.NewFragmentShader(''' #version 330 in vec2 textcoord; out vec4 color; uniform int iter; void main() { vec2 z = vec2(3.0 * (textcoord.x - 0.5), 2.0 * (textcoord.y - 0.5)); vec2 c = vec2(0.0, 1.0); int i; for(i = 0; i < iter; i++) { float x = (z.x * z.x - z.y * z.y) + c.x; float y = (z.y * z.x + z.x * z.y) + c.y; if ((x * x + y * y) > 4.0) break; z.x = x; z.y = y; } float cm = fract((i == iter ? 0.0 : float(i)) * 10 / iter); color = vec4(fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['pos'] = prog['pos'] context['zoom'] = prog['zoom'] vbo = GL.NewVertexBuffer( struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['iter'], 100) except GL.Error as error: print(error) exit(1)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; out vec2 tex_coord; uniform vec2 scale; uniform float rotation; void main() { mat2 rot = mat2(cos(rotation), sin(rotation), -sin(rotation), cos(rotation)); gl_Position = vec4((rot * vert) * scale, 0.0, 1.0); tex_coord = vert; } ''') frag = GL.NewFragmentShader(''' #version 330 uniform sampler2D texture; in vec2 tex_coord; out vec4 color; void main() { color = vec4(texture2D(texture, tex_coord).rgb, 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['rotation'] = prog['rotation'] vbo = GL.NewVertexBuffer( struct.pack('6f', 1.0, 0.0, -0.5, 0.86, -0.5, -0.86)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['scale'], context['height'] / context['width'] * 0.75, 0.75) tex = GL.NewTexture(256, 256, Image.open('../DataFiles/Noise.jpg').tobytes()) GL.UseTexture(tex) except GL.Error as error: print(error) exit(1)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; in vec3 vert_color; out vec3 frag_color; uniform vec2 scale; uniform float rotation; void main() { frag_color = vert_color; mat2 rot = mat2(cos(rotation), sin(rotation), -sin(rotation), cos(rotation)); gl_Position = vec4((rot * vert) * scale, 0.0, 1.0); } ''') frag = GL.NewFragmentShader(''' #version 330 in vec3 frag_color; out vec4 color; void main() { color = vec4(frag_color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['rotation'] = prog['rotation'] vbo = GL.NewVertexBuffer( struct.pack('15f', 1.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.86, 0.0, 1.0, 0.0, -0.5, -0.86, 0.0, 0.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f3f', ['vert', 'vert_color']) GL.SetUniform(prog['scale'], context['height'] / context['width'] * 0.75, 0.75) except GL.Error as error: print(error) exit(1)
def on_draw(): GL.Clear(240, 240, 240) GL.SetUniform(prog['rotation'], elapsed) GL.RenderTriangles(vao, 3)
frag = GL.NewFragmentShader(''' #version 330 in vec3 frag_color; out vec4 color; void main() { color = vec4(frag_color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('15f', 1.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.86, 0.0, 1.0, 0.0, -0.5, -0.86, 0.0, 0.0, 1.0)) vao = GL.NewVertexArray(prog, vbo, '2f3f', ['vert', 'vert_color']) GL.SetUniform(prog['scale'], window.height / window.width * 0.75, 0.75) def update(dt): global elapsed elapsed = time.time() - start @window.event def on_draw(): GL.Clear(240, 240, 240) GL.SetUniform(prog['rotation'], elapsed) GL.RenderTriangles(vao, 3) pyglet.clock.schedule_interval(update, 1 / 60) pyglet.app.run()
vbo = GL.NewVertexBuffer(data) ibo = GL.NewIndexBuffer(idata) vao = GL.NewVertexArray(grass_prog, vbo, '3f3f3f1f1f', ['vert', 'direction', 'color', 'thickness', 'power'], ibo) ssao_vbo = GL.NewVertexBuffer( struct.pack('8f', 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0)) ssao_vao = GL.NewVertexArray(ssao_prog, ssao_vbo, '2f', ['vert']) ubo = GL.NewUniformBuffer(b''.join(struct.pack('2f', x, y) for x, y in kernel)) fbo, color, depth = GL.NewFramebuffer() GL.SetUniform(grass_prog['mat'], struct.pack('16f', *camera)) GL.SetUniform(ssao_prog['texture'], 0) GL.SetUniform(ssao_prog['depth'], 1) GL.UseUniformBuffer(ubo, ssao_prog['Kernel']) while WND.Update(): GL.UseFramebuffer(fbo) GL.EnableDepthTest() GL.Clear(0, 0, 0) GL.RenderTriangleStrip(vao, 1000 * 8) GL.UseFramebuffer(GL.SCREEN) GL.DisableDepthTest() GL.UseTexture(color, 0)
def display(): GL.Clear(240, 240, 240) GL.SetUniform(prog['rotation'], time.time() - start) GL.RenderTriangles(vao, 3) glutSwapBuffers()
} ''') frag = GL.NewFragmentShader(''' #version 330 in vec3 frag_color; out vec4 color; void main() { color = vec4(frag_color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('15f', 1.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.86, 0.0, 1.0, 0.0, -0.5, -0.86, 0.0, 0.0, 1.0)) vao = GL.NewVertexArray(prog, vbo, '2f3f', ['vert', 'vert_color']) GL.SetUniform(prog['scale'], height / width * 0.75, 0.75) def display(): GL.Clear(240, 240, 240) GL.SetUniform(prog['rotation'], time.time() - start) GL.RenderTriangles(vao, 3) glutSwapBuffers() glutDisplayFunc(display) glutIdleFunc(display) glutMainLoop()
out vec4 color; void main() { color = vec4(0.30, 0.50, 1.00, 1.0); } ''') prog = GL.NewProgram([vert, frag]) transform = GL.NewTransformProgram([tvert], ['out_pos', 'out_prev']) vbo1 = GL.NewVertexBuffer(b''.join( struct.pack('2f2f', 0.0, 0.0, random.uniform(-0.001, 0.001), random.uniform(-0.001, 0.0)) for i in range(1024))) vbo2 = GL.NewVertexBuffer(b''.join( struct.pack('2f2f', 0.0, 0.0, random.uniform(-0.001, 0.001), random.uniform(-0.001, 0.0)) for i in range(1024))) vao1 = GL.NewVertexArray(transform, vbo1, '2f2f', ['in_pos', 'in_prev']) vao2 = GL.NewVertexArray(transform, vbo2, '2f2f', ['in_pos', 'in_prev']) render_vao = GL.NewVertexArray(prog, vbo1, '2f8x', ['vert']) GL.SetUniform(transform['acc'], 0, -0.00001) while WND.Update(): GL.Clear(240, 240, 240) GL.RenderPoints(render_vao, 1024) GL.TransformPoints(vao1, vbo2, 1024) GL.TransformPoints(vao2, vbo1, 1024)
} ''') frag = GL.NewFragmentShader(''' #version 330 out vec4 color; void main() { color = vec4(0.04, 0.04, 0.04, 1.0); } ''') prog = GL.NewProgram([vert, frag]) GL.SetUniform(prog['znear'], 0.1) GL.SetUniform(prog['zfar'], 1000.0) GL.SetUniform(prog['ratio'], 16 / 9) GL.SetUniform(prog['fovy'], 60) GL.SetUniform(prog['eye'], 3, 3, 3) GL.SetUniform(prog['center'], 0, 0, 0) GL.SetUniform(prog['up'], 0, 0, 1) grid = bytes() for i in range(0, 65): grid += struct.pack('6f', i - 32, -32.0, 0.0, i - 32, 32.0, 0.0) grid += struct.pack('6f', -32.0, i - 32, 0.0, 32.0, i - 32, 0.0) vbo = GL.NewVertexBuffer(grid)
def display(): GL.Clear(240, 240, 240) GL.EnableOnly(GL.ENABLE_BLEND + GL.ENABLE_MULTISAMPLE) GL.SetUniform(prog['rotation'], time.time() - start) GL.RenderTriangles(vao, 3, instances=10) glutSwapBuffers()
def on_draw(): GL.Clear(240, 240, 240) GL.SetUniform(prog['rotation'], time.time() - start) GL.RenderTriangles(vao, 3)
} '''), GL.NewFragmentShader(''' #version 330 core out vec4 color; void main() { color = vec4(0.05, 0.05, 0.05, 1.0); } '''), ]) width, height = WND.GetSize() GL.SetUniform(points['scale'], 10, 10 * height / width) def vec(a, r=1): return [math.cos(a) * r, math.sin(a) * r] def rvec(a, b): return vec(random.uniform(0.0, math.pi * 2.0), random.uniform(a, b)) P = numpy.array([[0.0, 0.0] for i in range(100)]) V = numpy.array([rvec(0.01, 0.05) for i in range(100)]) vbo = GL.NewVertexBuffer(P, dynamic=True) vao = GL.NewVertexArray(points, vbo, '2d', ['vert'])
z.x = x; z.y = y; } float cm = fract((i == iter ? 0.0 : float(i)) * 10 / iter); color = vec4(fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer( struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) vao = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['iter'], 100) tx, ty = 0, 0 z = 1 while WND.Update(): GL.Clear(240, 240, 240) dx, dy, mw = WND.GetMouse() z *= 1.1**(mw / 150) tx -= dx / 100 * z ty += dy / 100 * z GL.SetUniform(prog['pos'], tx / z, ty / z) GL.SetUniform(prog['zoom'], z) GL.RenderTriangleStrip(vao, 4)
#version 330 in vec3 frag_color; out vec4 color; void main() { color = vec4(frag_color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('15f', 1.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.86, 0.0, 1.0, 0.0, -0.5, -0.86, 0.0, 0.0, 1.0)) vao = GL.NewVertexArray(prog, vbo, '2f3f', ['vert', 'vert_color']) GL.SetUniform(prog['scale'], height / width * 0.75, 0.75) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False GL.Clear(240, 240, 240) GL.SetUniform(prog['rotation'], time.time() - start) GL.RenderTriangles(vao, 3) pygame.display.flip() pygame.time.wait(10)
0.0, 0.0, # B -0.5, 0.86, 0.0, 1.0, 0.0, # C -0.5, -0.86, 0.0, 0.0, 1.0, ] packed_triangle = struct.pack('15f', *triangle) vbo = GL.NewVertexBuffer(packed_triangle) vao = GL.NewVertexArray(prog, vbo, '2f3f', ['vert', 'vert_color']) width, height = WND.GetSize() while WND.Update(): GL.Clear(240, 240, 240) GL.SetUniform(prog['rotate'], WND.GetTime()) GL.SetUniform(prog['scale'], height / width * 0.75, 0.75) GL.RenderTriangles(vao, 3, instances=10)
def paintGL(self): GL.Clear(240, 240, 240) GL.SetUniform(context['rotation'], time.time() - context['start']) GL.RenderTriangles(context['vao'], 3) self.update()
GL.RenderLineStrip() GL.RenderLines() GL.RenderPoints() GL.RenderTriangleFan() GL.RenderTriangleStrip() GL.RenderTriangles() GL.RenderLinesAdjacency() GL.RenderLineStripAdjacency() GL.RenderTrianglesAdjacency() GL.RenderTriangleStripAdjacency() GL.SetTextureFiltered() GL.SetTextureMipmapped() GL.SetTexturePixelated() GL.SetUniform() GL.SetUniformMatrix() # GL.UpdateIndexBuffer() # GL.UpdateTexture() # GL.UpdateUniformBuffer() # GL.UpdateVertexBuffer() GL.UseDefaultFramebuffer() GL.UseFramebuffer() GL.UseTexture() GL.UseTextureAsImage() GL.UseUniformBuffer() GL.Viewport()
frag = vec4(color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer( struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) vao = GL.NewVertexArray(prog, vbo, '2f', ['vert']) ssbo = GL.NewStorageBuffer( open('../DataFiles/Raytrace-scene.dat', 'rb').read()) GL.UseStorageBuffer(ssbo, 1) GL.SetUniform(prog['ratio'], 16 / 9) x, y = 0.0, 0.0 while WND.Update(): GL.Clear(240, 240, 240) if WND.KeyDown('A'): x -= 0.04 if WND.KeyDown('D'): x += 0.04 if WND.KeyDown('W'): y += 0.04 if WND.KeyDown('S'): y -= 0.04
def paintGL(self): GL.Clear(240, 240, 240) GL.EnableOnly(GL.ENABLE_BLEND + GL.ENABLE_MULTISAMPLE) GL.SetUniform(context['rotation'], time.time() - context['start']) GL.RenderTriangles(context['vao'], 3, instances=10) self.update()
if (dot(sub, sub) < r * r) { vec2 dir = p.pos - p.prev; p.pos = circle[i].pos + normalize(sub) * r; p.prev = p.pos - reflect(dir, normalize(sub)) * 0.7; } } } void main() { update(particle[gl_GlobalInvocationID.x]); } ''') circles_prog = GL.NewProgram([circles_vert, circles_frag]) GL.SetUniform(circles_prog['scale'], 0.002 * height / width, 0.002) circle_vbo = GL.NewVertexBuffer(b''.join(struct.pack('2f', cos(i * 2 * pi / 128), sin(i * 2 * pi / 128)) for i in range(128))) circle_vao = GL.NewVertexArray(circles_prog, circle_vbo, '2f', ['vert']) circles = [ (-400, -200, 100), (-250, -350, 100), (0, -300, 100), (250, -350, 100), (400, -200, 100), ] circles_ubo = GL.NewUniformBuffer(struct.pack('i4x', len(circles)) + b''.join(struct.pack('2f1f4x', *c) for c in circles)) GL.UseUniformBuffer(circles_ubo, circles_prog['Circles'])
if (dot(v, v) > 4.0) break; z = v; } float cm = fract((i == iter ? 0.0 : float(i)) * 10 / iter); color = vec4(fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer( struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) vao = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['iter'], 100) GL.SetUniform(prog['scale'], 1.0) GL.SetUniform(prog['center'], 0.3, 0.2) x, y, scale = 0, 0, 1 while WND.Update(): GL.Clear(240, 240, 240) mx, my, mw = WND.GetMouse() scale *= 1.1**(mw / 150) x += mx / 100 y += my / 100 GL.SetUniform(prog['center'], x, y) GL.SetUniform(prog['scale'], scale) GL.RenderTriangleStrip(vao, 4)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 430 in vec2 vert; out vec3 frag_vert; uniform float ratio; uniform vec3 position; uniform vec3 target; void main() { vec3 X = normalize(position - target); vec3 Y = normalize(cross(X, vec3(0.0, 0.0, 1.0))); vec3 Z = normalize(cross(X, Y)); mat3 M = mat3(X, Y, Z); gl_Position = vec4(vert, 0.0, 1.0); frag_vert = M * vec3(1.0, vert.x * ratio, vert.y); } ''') frag = GL.NewFragmentShader(''' #version 430 struct Face { vec4 color; vec3 T1, T2_T1, T3_T1, T2_T1_C_T3_T1, N; }; in vec3 frag_vert; out vec4 frag; uniform vec3 position; layout (binding = 1) buffer Input { int faces; Face face[]; }; void main() { float factor = 1.0; vec3 R1 = position; vec3 R1_R2 = normalize(frag_vert); vec3 color = vec3(0.0, 0.0, 0.0); for (int h = 0; h < 5; ++h) { int hit = 0; float dist = -1; for (int i = 0; i < faces; ++i) { vec3 T1 = face[i].T1; float f = dot(R1_R2, face[i].N); if (f <= 0.0) continue; float D = dot(R1_R2, face[i].T2_T1_C_T3_T1); if (D == 0.0) continue; vec3 R1_T1 = R1 - T1; float u = dot(R1_T1, face[i].T2_T1_C_T3_T1) / D; if (u < 0.0 || (dist > 0.0 && dist < u)) continue; vec3 R1_R2_C_R1_T1 = cross(R1_R2, R1_T1); float v = dot(face[i].T3_T1, R1_R2_C_R1_T1) / D; if (v < 0.0 || v > 1.0) continue; float w = -dot(face[i].T2_T1, R1_R2_C_R1_T1) / D; if (w < 0.0 || w > 1.0) continue; dist = u; hit = i; } if (dist > 0) { float g = dot(R1_R2, face[hit].N); color += face[hit].color.rgb * factor * g; R1 = R1 - R1_R2 * dist; R1_R2 = reflect(R1_R2, face[hit].N); factor *= 1.0 - face[hit].color.a; if (factor < 0.01) break; } else { break; } } frag = vec4(color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) ssbo = GL.NewStorageBuffer(open('../DataFiles/Raytrace-scene.dat', 'rb').read()) GL.UseStorageBuffer(ssbo, 1) GL.SetUniform(prog['ratio'], context['width'] / context['height']) GL.SetUniform(prog['position'], 0.7, 0.7, 0.0) GL.SetUniform(prog['target'], 0, 0, 0) except GL.Error as error: print(error) exit(1)
def on_draw(): GL.Clear(240, 240, 240) GL.EnableOnly(GL.ENABLE_BLEND + GL.ENABLE_MULTISAMPLE) GL.SetUniform(prog['rotation'], elapsed) GL.RenderTriangles(vao, 3, instances=10)