def initializeGL(self): self.ctx = ModernGL.create_context() prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 vert; in vec4 vert_color; out vec4 frag_color; uniform vec2 scale; uniform float rotation; void main() { frag_color = vert_color; float r = rotation * (0.5 + gl_InstanceID * 0.05); mat2 rot = mat2(cos(r), sin(r), -sin(r), cos(r)); gl_Position = vec4((rot * vert) * scale, 0.0, 1.0); } '''), fragment_shader=''' #version 330 in vec4 frag_color; out vec4 color; void main() { color = vec4(frag_color); } '''),
def __init__(self, **kwargs): super(CustomWidget, self).__init__(**kwargs) with self.canvas: self.ctx = ModernGL.create_context() self.vert = vertex_shader=''' #version 330 in vec2 vert; void main() { gl_Position = vec4(vert, 0.0, 1.0); } ''') self.frag = fragment_shader=''' #version 330 out vec4 color; void main() { color = vec4(0.30, 0.50, 1.00, 1.0); } ''') self.prog = self.ctx.program(self.vert, self.frag]) self.vbo = self.ctx.buffer(struct.pack('6f', 0.0, 0.8, -0.6, -0.8, 0.6, -0.8)) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, ['vert']) self.draw() Callback(self.draw)
def main(): ''' Sample program to test GLWindow. ''' print('GLWindow:', GLWindow.__version__) print('Python:', sys.version) print('Platform:', sys.platform) wnd = GLWindow.create_window((480, 480), title='GLWindow Sample') wnd.vsync = False ctx = ModernGL.create_context() prog = ctx.program([ ctx.vertex_shader(''' #version 330 in vec2 vert; in vec4 vert_color; out vec4 frag_color; uniform vec2 scale; uniform float rotation; void main() { frag_color = vert_color; float r = rotation * (0.5 + gl_InstanceID * 0.05); mat2 rot = mat2(cos(r), sin(r), -sin(r), cos(r)); gl_Position = vec4((rot * vert) * scale, 0.0, 1.0); } '''), ctx.fragment_shader(''' #version 330 in vec4 frag_color; out vec4 color; void main() { color = vec4(frag_color); } '''), ]) scale = prog.uniforms['scale'] rotation = prog.uniforms['rotation'] vbo = ctx.buffer(struct.pack( '18f', 1.0, 0.0, 1.0, 0.0, 0.0, 0.5, -0.5, 0.86, 0.0, 1.0, 0.0, 0.5, -0.5, -0.86, 0.0, 0.0, 1.0, 0.5, )) vao = ctx.simple_vertex_array(prog, vbo, ['vert', 'vert_color']) while wnd.update(): wnd.clear(0.95, 0.95, 0.95) width, height = wnd.size scale.value = (height / width * 0.75, 0.75) ctx.viewport = wnd.viewport ctx.enable(ModernGL.BLEND) rotation.value = wnd.time vao.render(instances=10)
def __init__(self, **kwargs): super(CustomWidget, self).__init__(**kwargs) with self.canvas: self.ctx = ModernGL.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_color; out vec3 v_color; void main() { v_color = in_color; gl_Position = Mvp * vec4(in_vert, 1.0); } '''), fragment_shader=''' #version 330 in vec3 v_color; out vec4 f_color; void main() { f_color = vec4(v_color, 1.0); } '''), ])
def run(self): glfw.make_context_current(self._wnd) self._ctx = ModernGL.create_context() self.initialize(self._ctx) while not glfw.window_should_close(self._wnd): width, height = self.size() self._ctx.viewport = (0, 0, width, height) self.render(self._ctx) glfw.swap_buffers(self._wnd) glfw.wait_events()
def initializeGL(self): self.ctx = ModernGL.create_context() prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 vert; void main() { gl_Position = vec4(vert, 0.0, 1.0); } '''), fragment_shader=''' #version 330 out vec4 color; void main() { color = vec4(0.30, 0.50, 1.00, 1.0); } '''),
def initializeGL(self): self.ctx = ModernGL.create_context() img = Image.open(os.path.join(os.path.dirname(__file__), '..', 'data', 'noise.jpg')) texture = self.ctx.texture(img.size, 3, img.tobytes()) texture.use() prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 vert; in vec2 tex_coord; out vec2 v_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); v_tex_coord = tex_coord; } '''), fragment_shader=''' #version 330 uniform sampler2D texture; in vec2 v_tex_coord; out vec4 color; void main() { color = vec4(texture2D(texture, v_tex_coord).rgb, 1.0); } '''),
import struct import GLWindow import ModernGL from pyrr import Matrix44 wnd = GLWindow.create_window() ctx = ModernGL.create_context() prog = ctx.program( ctx.vertex_shader(''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_color; out vec3 v_color; void main() { v_color = in_color; gl_Position = Mvp * vec4(in_vert, 1.0); } '''), ctx.fragment_shader(''' #version 330 in vec3 v_color; out vec4 f_color;
import os import struct import GLWindow import ModernGL from PIL import Image # Window & Context wnd = GLWindow.create_window() ctx = ModernGL.create_context() # Shaders & Program prog = ctx.program([ ctx.vertex_shader(''' #version 330 in vec2 vert; in vec2 tex_coord; out vec2 v_tex_coord; uniform vec2 scale; uniform float rotation; void main() { mat2 rot = mat2( cos(rotation), sin(rotation), -sin(rotation), cos(rotation) );
def __init__(self, wnd): self.wnd = wnd self.ctx = ModernGL.create_context() self.prog = self.ctx.program([ self.ctx.vertex_shader(''' #version 330 uniform vec4 Camera; // Per vertex in vec2 in_vert; in vec2 in_texture; // Per instance in vec3 in_pos; in vec2 in_size; in vec4 in_tint; out vec2 v_vert; out vec2 v_texture; out vec4 v_tint; void main() { mat2 rotate = mat2( cos(in_pos.z), sin(in_pos.z), -sin(in_pos.z), cos(in_pos.z) ); v_vert = rotate * (in_vert * in_size) + in_pos.xy; gl_Position = vec4((v_vert - Camera.xy) / Camera.zw, 0.0, 1.0); v_texture = in_texture; v_tint = in_tint; } '''), self.ctx.fragment_shader(''' #version 330 uniform sampler2D Texture; in vec2 v_vert; in vec2 v_texture; in vec4 v_tint; out vec4 f_color; void main() { vec4 tex = texture(Texture, v_texture); vec3 color = tex.rgb * (1.0 - v_tint.a) + v_tint.rgb * v_tint.a; f_color = vec4(color, tex.a); } '''), ]) img = Image.open(local('data', 'crate.png')).convert('RGBA') self.tex1 = self.ctx.texture(img.size, 4, img.tobytes()) self.tex1.use(0) img = Image.open(local('data', 'ball.png')).convert('RGBA') self.tex2 = self.ctx.texture(img.size, 4, img.tobytes()) self.tex2.use(1) vertices = np.array([ -1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, ]) vbo1 = self.ctx.buffer(vertices.astype('f4').tobytes()) self.vbo2 = self.ctx.buffer(reserve=1024 * 1024) vao_content = [ (vbo1, '2f2f', ['in_vert', 'in_texture']), (self.vbo2, '3f2f4f/i', ['in_pos', 'in_size', 'in_tint']), ] self.vao = self.ctx.vertex_array(self.prog, vao_content) self.space = pymunk.Space() self.space.gravity = (0.0, -900.0) shape = pymunk.Segment(self.space.static_body, (5, 100), (595, 100), 1.0) shape.friction = 1.0 self.space.add(shape) self.bodies = [] self.balls = [] for x in range(5): for y in range(10): size = 20 mass = 10.0 moment = pymunk.moment_for_box(mass, (size, size)) body = pymunk.Body(mass, moment) body.position = Vec2d(300 + x * 50, 105 + y * (size + 0.1)) shape = pymunk.Poly.create_box(body, (size, size)) shape.friction = 0.3 self.space.add(body,shape) self.bodies.append(body)
def initializeGL(self): self.ctx = ModernGL.create_context() img = Image.open( os.path.join(os.path.dirname(__file__), '..', 'data', 'noise.jpg')) texture = self.ctx.texture(img.size, 3, img.tobytes()) texture.use() prog = self.ctx.program([ self.ctx.vertex_shader(''' #version 330 in vec2 vert; in vec2 tex_coord; out vec2 v_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); v_tex_coord = tex_coord; } '''), self.ctx.fragment_shader(''' #version 330 uniform sampler2D texture; in vec2 v_tex_coord; out vec4 color; void main() { color = vec4(texture2D(texture, v_tex_coord).rgb, 1.0); } '''), ]) self.scale = prog.uniforms['scale'] self.rotation = prog.uniforms['rotation'] vbo = self.ctx.buffer( struct.pack( '12f', 1.0, 0.0, 0.5, 1.0, -0.5, 0.86, 1.0, 0.0, -0.5, -0.86, 0.0, 0.0, )) self.vao = self.ctx.simple_vertex_array(prog, vbo, ['vert', 'tex_coord'])
def __init__(self, wnd): self.wnd = wnd self.ctx = ModernGL.create_context() self.prog = self.ctx.program([ self.ctx.vertex_shader(''' #version 330 in vec2 in_vert; in vec4 in_color; out vec4 v_color; uniform float Rotation; uniform vec2 Scale; void main() { v_color = in_color; float r = Rotation * (0.5 + gl_InstanceID * 0.05); mat2 rot = mat2(cos(r), sin(r), -sin(r), cos(r)); gl_Position = vec4((rot * in_vert) * Scale, 0.0, 1.0); } '''), self.ctx.fragment_shader(''' #version 330 in vec4 v_color; out vec4 f_color; void main() { f_color = v_color; } '''), ]) self.scale = self.prog.uniforms['Scale'] self.rotation = self.prog.uniforms['Rotation'] vertices = np.array([ # x, y, red, green, blue, alpha 1.0, 0.0, 1.0, 0.0, 0.0, 0.5, -0.5, 0.86, 0.0, 1.0, 0.0, 0.5, -0.5, -0.86, 0.0, 0.0, 1.0, 0.5, ]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, ['in_vert', 'in_color'])
def __init__(self, wnd): self.wnd = wnd self.ctx = ModernGL.create_context() width, height = self.wnd.size canvas = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0]).astype('f4') pixels = np.round(np.random.rand(width, height)).astype('f4') grid = np.dstack(np.mgrid[0:height, 0:width][::-1]).astype('i4') self.prog = self.ctx.program([ self.ctx.vertex_shader(''' #version 330 in vec2 in_vert; out vec2 v_text; void main() { v_text = in_vert; gl_Position = vec4(in_vert * 2.0 - 1.0, 0.0, 1.0); } '''), self.ctx.fragment_shader(''' #version 330 uniform sampler2D Texture; in vec2 v_text; out vec4 f_color; void main() { f_color = texture(Texture, v_text); } '''), ]) self.transform = self.ctx.program( self.ctx.vertex_shader(''' #version 330 uniform sampler2D Texture; uniform int Width; uniform int Height; in ivec2 in_text; out float out_vert; #define LIVING 0.0 #define DEAD 1.0 bool cell(int x, int y) { return texelFetch(Texture, ivec2((x + Width) % Width, (y + Height) % Height), 0).r < 0.5; } void main() { bool living = cell(in_text.x, in_text.y); int neighbours = 0; if (cell(in_text.x - 1, in_text.y - 1)) neighbours++; if (cell(in_text.x - 1, in_text.y + 0)) neighbours++; if (cell(in_text.x - 1, in_text.y + 1)) neighbours++; if (cell(in_text.x + 1, in_text.y - 1)) neighbours++; if (cell(in_text.x + 1, in_text.y + 0)) neighbours++; if (cell(in_text.x + 1, in_text.y + 1)) neighbours++; if (cell(in_text.x + 0, in_text.y + 1)) neighbours++; if (cell(in_text.x + 0, in_text.y - 1)) neighbours++; if (living) { out_vert = (neighbours >= 2 && neighbours <= 4) ? LIVING : DEAD; } else { out_vert = (neighbours == 3) ? LIVING : DEAD; } } '''), varyings=['out_vert'] ) self.transform.uniforms['Width'].value = width self.transform.uniforms['Height'].value = height self.texture = self.ctx.texture((width, height), 1, pixels.tobytes(), floats=True) self.texture.filter = ModernGL.NEAREST self.texture.swizzle = 'RRR1' self.texture.use() self.vbo = self.ctx.buffer(canvas.tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, ['in_vert']) self.text = self.ctx.buffer(grid.tobytes()) self.tao = self.ctx.simple_vertex_array(self.transform, self.text, ['in_text']) self.pbo = self.ctx.buffer(reserve=pixels.nbytes)
def __init__(self, wnd): self.wnd = wnd self.ctx = ModernGL.create_context() self.prog = self.ctx.program([ self.ctx.vertex_shader(''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec2 in_text; out vec3 v_vert; out vec3 v_norm; out vec2 v_text; void main() { gl_Position = Mvp * vec4(in_vert, 1.0); v_vert = in_vert; v_norm = in_norm; v_text = in_text; } '''), self.ctx.fragment_shader(''' #version 330 uniform vec3 Light; uniform vec3 Color; uniform bool UseTexture; uniform sampler2D Texture; in vec3 v_vert; in vec3 v_norm; in vec2 v_text; out vec4 f_color; void main() { float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2; if (UseTexture) { f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0); } else { f_color = vec4(Color * lum, 1.0); } } '''), ]) self.mvp = self.prog.uniforms['Mvp'] self.light = self.prog.uniforms['Light'] self.color = self.prog.uniforms['Color'] self.use_texture = self.prog.uniforms['UseTexture'] self.objects = {} for name in ['ground', 'grass', 'billboard', 'billboard-holder', 'billboard-image']: obj = Obj.open(local('data', 'scene-1-%s.obj' % name)) vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty')) vao = self.ctx.simple_vertex_array(self.prog, vbo, ['in_vert', 'in_norm', 'in_text']) self.objects[name] = vao img = Image.open(local('data', 'infographic-1.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB') self.texture = self.ctx.texture(img.size, 3, img.tobytes()) self.texture.build_mipmaps()