def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; void main() { gl_Position = vec4(vert, 0.0, 1.0); } ''') frag = GL.NewFragmentShader(''' #version 330 out vec4 color; void main() { color = vec4(0.30, 0.50, 1.00, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('6f', 0.0, 0.8, -0.6, -0.8, 0.6, -0.8)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) 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 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; 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 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 __init__(self, **kwargs): super(CustomWidget, self).__init__(**kwargs) with self.canvas: GL.Init() self.vert = GL.NewVertexShader(''' #version 330 in vec2 vert; void main() { gl_Position = vec4(vert, 0.0, 1.0); } ''') self.frag = GL.NewFragmentShader(''' #version 330 out vec4 color; void main() { color = vec4(0.30, 0.50, 1.00, 1.0); } ''') self.prog = GL.NewProgram([self.vert, self.frag]) self.vbo = GL.NewVertexBuffer( struct.pack('6f', 0.0, 0.8, -0.6, -0.8, 0.6, -0.8)) self.vao = GL.NewVertexArray(self.prog, self.vbo, '2f', ['vert']) self.draw() Callback(self.draw)
triangle = [ 0.0, 0.8, # A -0.6, -0.8, # B 0.6, -0.8 # C ] # We have to pack the floats packed_triangle = struct.pack('6f', *triangle) # Now lets create a vertex buffer "containing" our triangle vbo = GL.NewVertexBuffer(packed_triangle) # Now we will create a VertexArray and specify what vertex attributes will be used during the rendering # The first parameter is a program object used for rendering # The second parameter is a VertexBuffer holding information about the triangle # The third parameter is the format of a single vertex # Now we only define the position as 2 float x and y coordinate # Later we can define color normals or any other data used in the vertex shader. # The fourth parameter must be a list containing a vertex attribute name in the vertex shader vao = GL.NewVertexArray(prog, vbo, '2f', ['vert']) while WND.Update(): GL.Clear(240, 240, 240) # Render a triangle using 3 vertices (remember we have 6 floats 3 * 2f) # RenderTriangles will use the program assigned to vao # By default the specified attribues are enabled (for now we have a single "vert" attribute) GL.RenderTriangles(vao, 3)
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)
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)
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']) particles_prog = GL.NewProgram([particles_vert, particles_frag]) GL.SetUniform(particles_prog['scale'], 0.002 * height / width, 0.002)
data += struct.pack('3f', r, g, b) data += struct.pack('1f', 0) data += struct.pack('1f', d) idata = b'' for k in range(1000): idata += struct.pack( '%di' % (step + 3), *([k * (step + 2) + i for i in range(step + 2)] + [-1])) 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'])
]) 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']) while WND.Update(): P += V GL.Clear(240, 240, 240) GL.RenderPoints(vao, 100) GL.UpdateVertexBuffer(vbo, 0, P) if WND.KeyPressed(' '): 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)])
frag = GL.NewFragmentShader(''' #version 330 in float d; out vec4 color; void main() { color = vec4(0.30 * d, 0.50 * d, 1.00 * d, 0.5); } ''') prog, iface = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('72f', *verts)) vao = GL.NewVertexArray(prog, vbo, '3f1f', ['vert', 'diffuse']) nodes = [GL.NewFramebuffer(256, 256) for i in range(6)] enables = [ GL.ENABLE_BLEND, GL.ENABLE_CULL_FACE, GL.ENABLE_DEPTH_TEST, GL.ENABLE_BLEND + GL.ENABLE_DEPTH_TEST, GL.ENABLE_CULL_FACE + GL.ENABLE_BLEND, GL.ENABLE_BLEND + GL.ENABLE_DEPTH_TEST + GL.ENABLE_CULL_FACE, ] GL.Viewport(0, 0, 256, 256) for i, (fbo, color, depth) in enumerate(nodes): GL.UseFramebuffer(fbo)
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)