def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; void main() { gl_Position = Mvp * vec4(in_vert, 1.0); } ''', fragment_shader=''' #version 330 out vec4 f_color; void main() { f_color = vec4(0.1, 0.1, 0.1, 1.0); } ''', ) self.vbo = self.ctx.buffer(grid(15, 10).astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
def __init__(self): self.ctx = mgl.create_context() self.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); } ''', ) width, height = self.wnd.size self.prog['scale'] = (height / width * 0.75, 0.75) vertices = np.array([ 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, 'vert', 'vert_color') self.blend = self.ctx.scope(enable_only=mgl.BLEND)
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.create_context() # import gltraces # mglprocs = mgl.glprocs(self.ctx) # gltraces.glprocs[:] = mglprocs # mglprocs[:] = gltraces.gltraces self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec3 in_text; out vec3 v_vert; out vec3 v_norm; out vec3 v_text; void main() { gl_Position = Mvp * vec4(in_vert, 1.0); v_vert = in_vert; v_norm = in_norm; v_text = in_text; } ''', fragment_shader=''' #version 330 uniform vec3 Light; uniform samplerCube Sampler; in vec3 v_vert; in vec3 v_norm; in vec3 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; f_color = vec4(texture(Sampler, normalize(v_text)).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('texture-test-cube.obj')) self.texture = self.ctx.texture_cube( (4, 4), 3, np.random.randint(128, 255, (4, 4, 3, 6), 'u1').tobytes()) self.sampler = self.ctx.sampler(self.texture, filter=mgl.LINEAR) self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz')) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text') self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program(vertex_shader=''' #version 330 in vec2 in_vert; out vec2 v_text; void main() { gl_Position = vec4(in_vert, 0.0, 1.0); v_text = in_vert; } ''', fragment_shader=''' #version 330 in vec2 v_text; out vec4 f_color; uniform sampler2D Texture; uniform vec2 Center; uniform float Scale; uniform float Ratio; uniform int Iter; void main() { vec2 c; int i; c.x = Ratio * v_text.x * Scale - Center.x; c.y = v_text.y * Scale - Center.y; vec2 z = c; 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; } f_color = texture(Texture, vec2((i == Iter ? 0.0 : float(i)) / 100.0, 0.0)); } ''') img = Image.open(data.find('pal.png')).convert('RGB') self.texture = self.ctx.texture(img.size, 3, img.tobytes()) self.sampler = self.ctx.sampler(self.texture) self.sampler.use() vertices = np.array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; void main() { gl_Position = vec4(in_vert, 0.0, 1.0); } ''', fragment_shader=''' #version 330 out vec4 f_color; void main() { f_color = vec4(0.3, 0.5, 1.0, 1.0); } ''', ) vertices = np.array([ 0.0, 0.8, -0.6, -0.8, 0.6, -0.8, ]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( 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; } ''', 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.objects = {} for name in ['ground', 'grass', 'billboard', 'billboard-holder', 'billboard-image']: obj = Obj.open(data.find('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(data.find('infographic-1.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB') self.texture = self.ctx.texture(img.size, 3, img.tobytes()) self.sampler = self.ctx.sampler(self.texture)
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; out vec2 v_text; void main() { gl_Position = vec4(in_vert, 0.0, 1.0); v_text = in_vert; } ''', fragment_shader=''' #version 330 in vec2 v_text; out vec4 f_color; uniform sampler2D Texture; uniform vec2 Seed; uniform int Iter; void main() { vec2 c = Seed; int i; vec2 z = vec2(3.0 * v_text.x, 2.0 * v_text.y); 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; } f_color = texture(Texture, vec2((i == Iter ? 0.0 : float(i)) / 100.0, 0.0)); } ''', ) img = Image.open(data.find('pal.png')).convert('RGB') self.texture = self.ctx.texture(img.size, 3, img.tobytes()) self.sampler = self.ctx.sampler(self.texture) self.sampler.use() vertices = np.array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; void main() { gl_Position = vec4(in_vert, 0.0, 1.0); } ''', fragment_shader=''' #version 330 out vec4 f_color; void main() { f_color = vec4(0.30, 0.50, 1.00, 1.0); } ''', ) self.transform = self.ctx.program(vertex_shader=''' #version 330 uniform vec2 Acc; in vec2 in_pos; in vec2 in_prev; out vec2 out_pos; out vec2 out_prev; void main() { out_pos = in_pos * 2.0 - in_prev + Acc; out_prev = in_pos; } ''', varyings=['out_pos', 'out_prev']) self.transform['Acc'] = (0.0, -0.0001) self.vbo1 = self.ctx.buffer(b''.join(particle() for i in range(1024))) self.vbo2 = self.ctx.buffer(reserve=self.vbo1.size) self.vao1 = self.ctx.simple_vertex_array(self.transform, self.vbo1, 'in_pos', 'in_prev') self.vao2 = self.ctx.simple_vertex_array(self.transform, self.vbo2, 'in_pos', 'in_prev') self.render_vao = self.ctx.vertex_array(self.prog, [ (self.vbo1, '2f 2x4', 'in_vert'), ]) self.idx = 0
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; void main() { gl_Position = vec4(in_vert, 0.0, 1.0); } ''', fragment_shader=''' #version 330 out vec4 f_color; void main() { f_color = vec4(0.30, 0.50, 1.00, 1.0); } ''', ) self.transform = self.ctx.program( vertex_shader=''' #version 330 uniform vec2 Acc; in vec2 in_pos; in vec2 in_prev; out vec2 out_pos; out vec2 out_prev; void main() { out_pos = in_pos * 2.0 - in_prev + Acc; out_prev = in_pos; } ''', varyings=['out_pos', 'out_prev'] ) self.transform['Acc'] = (0.0, -0.0001) self.vbo1 = self.ctx.buffer(b''.join(particle() for i in range(1024))) self.vbo2 = self.ctx.buffer(reserve=self.vbo1.size) self.vao1 = self.ctx.simple_vertex_array(self.transform, self.vbo1, 'in_pos', 'in_prev') self.vao2 = self.ctx.simple_vertex_array(self.transform, self.vbo2, 'in_pos', 'in_prev') self.render_vao = self.ctx.vertex_array(self.prog, [ (self.vbo1, '2f 2x4', 'in_vert'), ]) self.idx = 0
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.create_context() # import gltraces # mglprocs = mgl.glprocs(self.ctx) # gltraces.glprocs[:] = mglprocs # mglprocs[:] = gltraces.gltraces self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec3 in_text; out vec3 v_vert; out vec3 v_norm; out vec3 v_text; void main() { gl_Position = Mvp * vec4(in_vert, 1.0); v_vert = in_vert; v_norm = in_norm; v_text = in_text; } ''', fragment_shader=''' #version 330 uniform vec3 Light; uniform samplerCube Sampler; in vec3 v_vert; in vec3 v_norm; in vec3 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; f_color = vec4(texture(Sampler, normalize(v_text)).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('texture-test-cube.obj')) self.texture = self.ctx.texture_cube((4, 4), 3, np.random.randint(128, 255, (4, 4, 3, 6), 'u1').tobytes()) self.sampler = self.ctx.sampler(self.texture, filter=mgl.LINEAR) self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz')) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text') self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec3 in_text; out vec3 v_vert; out vec3 v_norm; out vec3 v_text; void main() { gl_Position = Mvp * vec4(in_vert, 1.0); v_vert = in_vert; v_norm = in_norm; v_text = in_text; } ''', fragment_shader=''' #version 330 uniform vec3 Light; uniform sampler3D Sampler; in vec3 v_vert; in vec3 v_norm; in vec3 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; f_color = vec4(texture(Sampler, v_text).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('texture-test-cube.obj')) self.texture = self.ctx.texture( (2, 2, 2), 3, np.random.randint(128, 255, (2, 2, 2, 3), 'u1')) self.sampler = self.ctx.sampler(self.texture, wrap=mgl.REPEAT_X | mgl.REPEAT_Y | mgl.REPEAT_Z) self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz')) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text') self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; in vec3 in_color; out vec3 v_color; // Goes to the fragment shader void main() { gl_Position = vec4(in_vert, 0.0, 1.0); v_color = in_color; } ''', fragment_shader=''' #version 330 in vec3 v_color; out vec4 f_color; void main() { // We're not interested in changing the alpha value f_color = vec4(v_color, 1.0); } ''', ) # Point coordinates are put followed by the vec3 color values vertices = np.array([ # x, y, red, green, blue 0.0, 0.8, 1.0, 0.0, 0.0, -0.6, -0.8, 0.0, 1.0, 0.0, 0.6, -0.8, 0.0, 0.0, 1.0, ]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) # We control the 'in_vert' and `in_color' variables self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_color')
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.create_context() self.prog = self.ctx.program( 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; } ''', fragment_shader=''' #version 330 uniform vec3 Light; 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; f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('crate.obj')) img = Image.open(data.find('crate.png')).transpose( Image.FLIP_TOP_BOTTOM).convert('RGB') self.texture = self.ctx.texture(img.size, 3, img.tobytes()) self.sampler = self.ctx.sampler(self.texture) self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty')) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text') self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
def ctx(): glhook = None if os.getenv('TRAVIS') == 'True': def glhook(glprocs, dtype): from gltraces import lookup, null glprocs[lookup['glBufferStorage']] = null glprocs[lookup['glClearBufferData']] = null context = mgl.create_context(standalone=True, glhook=glhook) return context
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; out vec2 v_text; void main() { gl_Position = vec4(in_vert, 0.0, 1.0); v_text = in_vert / 2.0 + 0.5; } ''', fragment_shader=''' #version 330 in vec2 v_text; out vec4 f_color; uniform vec2 Center; uniform int Iter; void main() { vec2 z = vec2(5.0 * (v_text.x - 0.5), 3.0 * (v_text.y - 0.5)); vec2 c = Center; 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); f_color = vec4( fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0 ); } ''', ) vertices = np.array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.create_context() self.prog = self.ctx.program( 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; } ''', fragment_shader=''' #version 330 uniform vec3 Light; 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; f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('crate.obj')) img = Image.open(data.find('crate.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB') self.texture = self.ctx.texture(img.size, 3, img.tobytes()) self.sampler = self.ctx.sampler(self.texture) self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty')) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text') self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec3 in_text; out vec3 v_vert; out vec3 v_norm; out vec3 v_text; void main() { gl_Position = Mvp * vec4(in_vert, 1.0); v_vert = in_vert; v_norm = in_norm; v_text = in_text; } ''', fragment_shader=''' #version 330 uniform vec3 Light; uniform sampler3D Sampler; in vec3 v_vert; in vec3 v_norm; in vec3 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; f_color = vec4(texture(Sampler, v_text).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('texture-test-cube.obj')) self.texture = self.ctx.texture((2, 2, 2), 3, np.random.randint(128, 255, (2, 2, 2, 3), 'u1')) self.sampler = self.ctx.sampler(self.texture, wrap=mgl.REPEAT_X | mgl.REPEAT_Y | mgl.REPEAT_Z) self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz')) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text') self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; void main() { gl_Position = vec4(in_vert, 0.0, 1.0); } ''', fragment_shader=''' #version 330 out vec4 f_color; void main() { f_color = vec4(0.3, 0.5, 1.0, 1.0); } ''', ) # 2 triangles sharing the head vertex (0,0) vertices = np.array([ 0.0, 0.0, -0.6, -0.8, 0.6, -0.8, 0.6, 0.8, -0.6, 0.8, ]) # Indecies are given to specify the order of drawing indecies = np.array([0, 1, 2, 0, 3, 4]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.ibo = self.ctx.buffer(indecies.astype('i4').tobytes()) vao_content = [ # 2 floats are assigned to the 'in' variable named 'in_vert' in the shader code (self.vbo, '2f', 'in_vert') ] self.vao = self.ctx.vertex_array(self.prog, vao_content, self.ibo)
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 vert; 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); } ''', fragment_shader=''' #version 330 out vec4 color; void main() { color = vec4(0.3, 0.5, 1.0, 1.0); } ''', ) width, height = self.wnd.size vertices = np.array([ 1.0, 0.0, -0.5, 0.86, -0.5, -0.86, ]) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'vert')
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.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( 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); } ''', 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(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 == 3) ? LIVING : DEAD; } else { out_vert = (neighbours == 3) ? LIVING : DEAD; } } ''', varyings=['out_vert']) self.transform['Width'] = width self.transform['Height'] = height self.texture = self.ctx.texture((width, height), 1, pixels.tobytes(), dtype='f4') self.texture.swizzle = 'RRR1' self.sampler = self.ctx.sampler(self.texture) # self.sampler.filter = (mgl.NEAREST, mgl.NEAREST) self.sampler.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)
Z = 1 consts = { "W": W, "H": H, "X": X + 1, "Y": Y, "Z": Z, } FRAMES = 50 OUTPUT_DIRPATH = "./output" if not os.path.isdir(OUTPUT_DIRPATH): os.makedirs(OUTPUT_DIRPATH) context = mgl.create_context(standalone=True) compute_shader = context.compute_shader(source('../gl/median_5x5.gl', consts)) # init buffers buffer_a_data = np.random.uniform(0.0, 1.0, (H, W, 4)).astype('f4') buffer_a = context.buffer(buffer_a_data) buffer_b_data = np.zeros((H, W, 4)).astype('f4') buffer_b = context.buffer(buffer_b_data) imgs = [] last_buffer = buffer_b for i in range(FRAMES): toggle = True if i % 2 else False buffer_a.bind_to_storage_buffer(1 if toggle else 0) buffer_b.bind_to_storage_buffer(0 if toggle else 1)
def __init__(self): self.ctx = mgl.create_context() # import gltraces # mglprocs = mgl.glprocs(self.ctx) # gltraces.glprocs[:] = mglprocs # mglprocs[:] = gltraces.gltraces self.prog = self.ctx.program( 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; } ''', fragment_shader=''' #version 330 uniform vec3 Light; 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; f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0); } ''', ) obj1 = Obj.open(data.find('crate_left.obj')) obj2 = Obj.open(data.find('crate.obj')) obj3 = Obj.open(data.find('crate_right.obj')) img1 = Image.open(data.find('crate.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB') img2 = Image.open(data.find('rock.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB') self.texture1 = self.ctx.texture(img1) self.texture2 = self.ctx.texture(img2) self.sampler1 = self.ctx.sampler(self.texture1) self.sampler2 = self.ctx.sampler(self.texture2) self.vbo1 = self.ctx.buffer(obj1.pack('vx vy vz nx ny nz tx ty')) self.vbo2 = self.ctx.buffer(obj2.pack('vx vy vz nx ny nz tx ty')) self.vbo3 = self.ctx.buffer(obj3.pack('vx vy vz nx ny nz tx ty')) self.vao1 = self.ctx.simple_vertex_array(self.prog, self.vbo1, 'in_vert', 'in_norm', 'in_text') self.vao1.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)]) self.vao2 = self.ctx.simple_vertex_array(self.prog, self.vbo2, 'in_vert', 'in_norm', 'in_text') self.vao2.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler1, 0)]) self.vao3 = self.ctx.simple_vertex_array(self.prog, self.vbo3, 'in_vert', 'in_norm', 'in_text') self.vao3.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)]) with self.ctx.recorder: self.ctx.clear(1.0, 1.0, 1.0) self.vao1.render() self.vao2.render() self.vao3.render() self.bytecode = self.ctx.recorder.dump() print(self.bytecode)
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( 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; } ''', 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.objects = {} for name in [ 'ground', 'grass', 'billboard', 'billboard-holder', 'billboard-image' ]: obj = Obj.open(data.find('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(data.find('infographic-1.jpg')).transpose( Image.FLIP_TOP_BOTTOM).convert('RGB') self.texture = self.ctx.texture(img.size, 3, img.tobytes()) self.sampler = self.ctx.sampler(self.texture)
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec3 in_color; in vec3 in_origin; in mat3 in_basis; out vec3 v_vert; out vec3 v_norm; out vec3 v_color; void main() { v_vert = in_origin + in_basis * in_vert; v_norm = in_basis * in_norm; v_color = in_color; gl_Position = Mvp * vec4(v_vert, 1.0); } ''', fragment_shader=''' #version 330 uniform vec3 Light; uniform sampler2D Texture; in vec3 v_vert; in vec3 v_norm; in vec3 v_color; 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; f_color = vec4(v_color * lum, 1.0); } ''', ) obj = Obj.open(data.find('lowpoly_toy_car.obj')) self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz')) self.vbo2 = self.ctx.buffer( struct.pack( '15f', 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, ) * len(cars)) self.vao = self.ctx.vertex_array(self.prog, [ (self.vbo1, '3f 3f', 'in_vert', 'in_norm'), (self.vbo2, '3f 3f 9f/i', 'in_color', 'in_origin', 'in_basis'), ])
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec3 in_color; in vec3 in_origin; in mat3 in_basis; out vec3 v_vert; out vec3 v_norm; out vec3 v_color; void main() { v_vert = in_origin + in_basis * in_vert; v_norm = in_basis * in_norm; v_color = in_color; gl_Position = Mvp * vec4(v_vert, 1.0); } ''', fragment_shader=''' #version 330 uniform vec3 Light; uniform sampler2D Texture; in vec3 v_vert; in vec3 v_norm; in vec3 v_color; 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; f_color = vec4(v_color * lum, 1.0); } ''', ) obj = Obj.open(data.find('lowpoly_toy_car.obj')) self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz')) self.vbo2 = self.ctx.buffer(struct.pack( '15f', 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, ) * len(cars)) self.vao = self.ctx.vertex_array(self.prog, [ (self.vbo1, '3f 3f', 'in_vert', 'in_norm'), (self.vbo2, '3f 3f 9f/i', 'in_color', 'in_origin', 'in_basis'), ])
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; in vec2 in_pos; in float in_scale; in vec3 in_color; out vec3 v_color; void main() { gl_Position = vec4(in_pos + (in_vert * in_scale), 0.0, 1.0); v_color = in_color; } ''', fragment_shader=''' #version 330 in vec3 v_color; out vec4 f_color; void main() { f_color = vec4(v_color, 1.0); } ''', ) # Vertex coordinates stored in position_vertex_buffer # # B------D # | | # A------C vertices = np.array([ -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, ]) self.position_vertex_buffer = self.ctx.buffer(vertices.astype('f4').tobytes()) # Vertex colors stored in color_buffer # # A, B are green # C, D are blue colors = np.array([ 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, ]) self.color_buffer = self.ctx.buffer(colors.astype('f4').tobytes()) # (Per instance) positions and scales stored in pos_scale_buffer # There are 8 (x_position, y_position, scale) pairs position_scale = np.array([ 0.5, 0.0, 0.3, 0.35, 0.35, 0.2, 0.0, 0.5, 0.3, -0.35, 0.35, 0.2, -0.5, 0.0, 0.3, -0.35, -0.35, 0.2, 0.0, -0.5, 0.3, 0.35, -0.35, 0.2, ]) self.pos_scale_buffer = self.ctx.buffer(position_scale.astype('f4').tobytes()) # Index buffer (also called element buffer) # There are 2 trianges to render # # A, B, C # B, C, D render_indicies = np.array([ 0, 1, 2, 1, 2, 3 ]) self.index_buffer = self.ctx.buffer(render_indicies.astype('i4').tobytes()) # The vao_content is a list of 3-tuples (buffer, format, attribs) # the format can have an empty or '/v', '/i', '/r' ending. # '/v' attributes are the default # '/i` attributes are per instance attributes # '/r' attributes are default values for the attributes (per render attributes) vao_content = [ (self.position_vertex_buffer, '2f', 'in_vert'), (self.color_buffer, '3f', 'in_color'), (self.pos_scale_buffer, '2f 1f/i', 'in_pos', 'in_scale'), ] self.vao = self.ctx.vertex_array(self.prog, vao_content, self.index_buffer)
def __init__(self): self.ctx = mgl.create_context() self.canvas_prog = self.ctx.program( vertex_shader=''' #version 330 in vec2 in_vert; out vec2 v_vert; void main() { gl_Position = vec4(in_vert * 2.0 - 1.0, 0.0, 1.0); v_vert = in_vert; } ''', fragment_shader=''' #version 330 uniform sampler2D Texture; in vec2 v_vert; out vec4 f_color; void main() { f_color = texture(Texture, v_vert); } ''', ) self.prog = self.ctx.program( 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; } ''', fragment_shader=''' #version 330 uniform vec3 Light; 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; vec3 base = vec3(0.5, 0.5, 0.5) * lum; vec3 spec = vec3(1.0, 1.0, 1.0) * pow(lum, 5.7); vec4 tex = texture(Texture, v_text); f_color = vec4(base * 0.1 + tex.rgb * lum + spec, tex.a); } ''', ) self.canvas_vbo = self.ctx.buffer( np.array([0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0], dtype='f4').tobytes()) self.canvas_vao = self.ctx.simple_vertex_array(self.canvas_prog, self.canvas_vbo, 'in_vert') bg_img = Image.open(data.find('mug-background.jpg')).transpose( Image.FLIP_TOP_BOTTOM).convert('RGB') self.bg_texture = self.ctx.texture(bg_img.size, 3, bg_img.tobytes()) self.bg_sampler = self.ctx.sampler(self.bg_texture) sticker_img = Image.open(data.find('mug-pymet-logo.png')).transpose( Image.FLIP_TOP_BOTTOM).convert('RGBA') self.sticker_texture = self.ctx.texture(sticker_img.size, 4, sticker_img.tobytes()) # self.sticker_texture.build_mipmaps(0, 2) self.sticker_sampler = self.ctx.sampler(self.sticker_texture) self.mug_texture = self.ctx.texture((1, 1), 3) self.mug_texture.write(struct.pack('3B', 10, 10, 10)) self.mug_sampler = self.ctx.sampler(self.mug_texture) obj = Obj.open(data.find('mug.obj')) self.mug_vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty')) self.mug_vao = self.ctx.simple_vertex_array(self.prog, self.mug_vbo, 'in_vert', 'in_norm', 'in_text') segs = 32 radius = 29.94 bottom = 6.601 top = 57.856 left = -163.12 * np.pi / 180.0 right = 11.25 * np.pi / 180.0 lin = np.linspace(left, right, segs) sticker_vertices = np.array([ np.repeat(np.cos(lin) * radius, 2), np.repeat(np.sin(lin) * radius, 2), np.tile([bottom, top], segs), np.repeat(np.cos(lin), 2), np.repeat(np.sin(lin), 2), np.tile([0.0, 0.0], segs), np.repeat(np.linspace(0.0, 1.0, segs), 2), np.tile([0.0, 1.0], segs), ]) self.sticker_vbo = self.ctx.buffer( sticker_vertices.T.astype('f4').tobytes()) self.sticker_vao = self.ctx.simple_vertex_array( self.prog, self.sticker_vbo, 'in_vert', 'in_norm', 'in_text')
import json import moderngl.next as mgl ctx = mgl.create_context(standalone=True, debug=True) print('Limits:', json.dumps(ctx.limits.json(), indent=2)) print('Extensions:', json.dumps(mgl.extensions(ctx), indent=2)) print('Hardware Info:', json.dumps(mgl.hwinfo(ctx), indent=2)) print('Version Code:', ctx.version_code)
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.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( 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); } ''', 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( 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 == 3) ? LIVING : DEAD; } else { out_vert = (neighbours == 3) ? LIVING : DEAD; } } ''', varyings=['out_vert'] ) self.transform['Width'] = width self.transform['Height'] = height self.texture = self.ctx.texture((width, height), 1, pixels.tobytes(), dtype='f4') self.texture.swizzle = 'RRR1' self.sampler = self.ctx.sampler(self.texture) # self.sampler.filter = (mgl.NEAREST, mgl.NEAREST) self.sampler.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): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 in vec3 vert; uniform float z_near; uniform float z_far; uniform float fovy; uniform float ratio; uniform vec3 center; uniform vec3 eye; uniform vec3 up; mat4 perspective() { float zmul = (-2.0 * z_near * z_far) / (z_far - z_near); float ymul = 1.0 / tan(fovy * 3.14159265 / 360); float xmul = ymul / ratio; return mat4( xmul, 0.0, 0.0, 0.0, 0.0, ymul, 0.0, 0.0, 0.0, 0.0, -1.0, -1.0, 0.0, 0.0, zmul, 0.0 ); } mat4 lookat() { vec3 forward = normalize(center - eye); vec3 side = normalize(cross(forward, up)); vec3 upward = cross(side, forward); return mat4( side.x, upward.x, -forward.x, 0, side.y, upward.y, -forward.y, 0, side.z, upward.z, -forward.z, 0, -dot(eye, side), -dot(eye, upward), dot(eye, forward), 1 ); } void main() { gl_Position = perspective() * lookat() * vec4(vert, 1.0); } ''', fragment_shader=''' #version 330 out vec4 color; void main() { color = vec4(0.04, 0.04, 0.04, 1.0); } ''', ) width, height = self.wnd.size self.prog['z_near'] = 0.1 self.prog['z_far'] = 1000.0 self.prog['ratio'] = width / height self.prog['fovy'] = 60 self.prog['eye'] = (3, 3, 3) self.prog['center'] = (0, 0, 0) self.prog['up'] = (0, 0, 1) grid = [] for i in range(65): grid.append([i - 32, -32.0, 0.0, i - 32, 32.0, 0.0]) grid.append([-32.0, i - 32, 0.0, 32.0, i - 32, 0.0]) grid = np.array(grid) self.vbo = self.ctx.buffer(grid.astype('f4').tobytes()) self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'vert')
def __init__(self): self.ctx = mgl.create_context() self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; uniform sampler2D Heightmap; in vec2 in_vert; out vec2 v_text; void main() { vec4 vertex = vec4(in_vert - 0.5, texture(Heightmap, in_vert).r * 0.2, 1.0); gl_Position = Mvp * vertex; v_text = in_vert; } ''', fragment_shader=''' #version 330 uniform sampler2D Heightmap; uniform sampler2D Color1; uniform sampler2D Color2; uniform sampler2D Cracks; uniform sampler2D Darken; in vec2 v_text; out vec4 f_color; void main() { float height = texture(Heightmap, v_text).r; float border = smoothstep(0.5, 0.7, height); vec3 color1 = texture(Color1, v_text * 7.0).rgb; vec3 color2 = texture(Color2, v_text * 6.0).rgb; vec3 color = color1 * (1.0 - border) + color2 * border; color *= 0.8 + 0.2 * texture(Darken, v_text * 3.0).r; color *= 0.5 + 0.5 * texture(Cracks, v_text * 5.0).r; color *= 0.5 + 0.5 * height; f_color = vec4(color, 1.0); } ''', ) vertices, index = terrain(32) self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes()) self.ibo = self.ctx.buffer(index.astype('i4').tobytes()) vao_content = [ (self.vbo, '2f', 'in_vert'), ] self.vao = self.ctx.vertex_array(self.prog, vao_content, self.ibo) img0 = Image.open(data.find('heightmap.jpg')).convert('L').transpose( Image.FLIP_TOP_BOTTOM) img1 = Image.open(data.find('grass.jpg')).convert('RGB').transpose( Image.FLIP_TOP_BOTTOM) img2 = Image.open(data.find('rock.jpg')).convert('RGB').transpose( Image.FLIP_TOP_BOTTOM) img3 = Image.open(data.find('cracks.jpg')).convert('L').transpose( Image.FLIP_TOP_BOTTOM) img4 = Image.open(data.find('checked.jpg')).convert('L').transpose( Image.FLIP_TOP_BOTTOM) tex0 = self.ctx.texture(img0.size, 1, img0.tobytes()) tex1 = self.ctx.texture(img1.size, 3, img1.tobytes()) tex2 = self.ctx.texture(img2.size, 3, img2.tobytes()) tex3 = self.ctx.texture(img3.size, 1, img3.tobytes()) tex4 = self.ctx.texture(img4.size, 1, img4.tobytes()) sampler0 = self.ctx.sampler(tex0) sampler1 = self.ctx.sampler(tex1) sampler2 = self.ctx.sampler(tex2) sampler3 = self.ctx.sampler(tex3) sampler4 = self.ctx.sampler(tex4) # tex0.build_mipmaps() # tex1.build_mipmaps() # tex2.build_mipmaps() # tex3.build_mipmaps() # tex4.build_mipmaps() sampler0.use(0) sampler1.use(1) sampler2.use(2) sampler3.use(3) sampler4.use(4) self.prog['Heightmap'] = 0 self.prog['Color1'] = 1 self.prog['Color2'] = 2 self.prog['Cracks'] = 3 self.prog['Darken'] = 4
def __init__(self, **kwargs): super().__init__(**kwargs) self.ctx = mgl.create_context() # import gltraces # mglprocs = mgl.glprocs(self.ctx) # gltraces.glprocs[:] = mglprocs # mglprocs[:] = gltraces.gltraces self.prog1 = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_norm; in vec3 in_text; out vec3 v_vert; out vec3 v_norm; out vec3 v_text; void main() { gl_Position = Mvp * vec4(in_vert, 1.0); v_vert = in_vert; v_norm = in_norm; v_text = in_text; } ''', fragment_shader=''' #version 330 uniform vec3 Light; uniform samplerCube Sampler; in vec3 v_vert; in vec3 v_norm; in vec3 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; f_color = vec4(texture(Sampler, normalize(v_text)).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('texture-test-cube.obj')) self.texture1 = self.ctx.texture_cube((512, 512), 3, np.random.randint(128, 255, (512, 512, 3, 6), 'u1').tobytes()) self.depth1 = self.ctx.texture_cube((512, 512), 1, None, dtype='d3') self.sampler1 = self.ctx.sampler(self.texture1, filter=mgl.LINEAR) self.fbo1 = self.ctx.framebuffer(self.texture1, self.depth1) # exit() self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz')) self.vao1 = self.ctx.simple_vertex_array(self.prog1, self.vbo1, 'in_vert', 'in_norm', 'in_text') self.vao1.scope = self.ctx.scope(mgl.DEPTH_TEST, self.ctx.screen, samplers=[(self.sampler1, 0)]) self.prog2 = self.ctx.program( vertex_shader=''' #version 330 in vec3 in_vert; in vec3 in_norm; in vec3 in_text; out vec3 v_vert; out vec3 v_norm; out vec3 v_text; void main() { v_vert = in_vert; v_norm = in_norm; v_text = in_text; } ''', geometry_shader=''' #version 330 layout(triangles) in; layout(triangle_strip, max_vertices=18) out; in vec3 v_vert[3]; in vec3 v_norm[3]; in vec3 v_text[3]; out vec3 g_vert; out vec3 g_norm; out vec3 g_text; uniform mat4 Mvp[6]; void main() { for (int layer = 0; layer < 6; ++layer) { gl_Layer = layer; for (int i = 0; i < 3; ++i) { g_vert = v_vert[i]; g_norm = v_norm[i]; g_text = v_text[i]; gl_Position = Mvp[layer] * vec4(g_vert, 1.0); EmitVertex(); } EndPrimitive(); } } ''', fragment_shader=''' #version 330 uniform sampler3D Sampler; uniform vec3 Light; in vec3 g_vert; in vec3 g_norm; in vec3 g_text; out vec4 f_color; void main() { float lum = clamp(dot(normalize(Light - g_vert), normalize(g_norm)), 0.0, 1.0) * 0.8 + 0.2; f_color = vec4(texture(Sampler, g_text).rgb * lum, 1.0); } ''', ) obj = Obj.open(data.find('test_scene.obj')) self.texture2 = self.ctx.texture((4, 4, 4), 3, np.repeat(np.random.randint(180, 220, (4, 4, 4), 'u1'), 3)) self.sampler2 = self.ctx.sampler(self.texture2, wrap=mgl.REPEAT_X | mgl.REPEAT_Y | mgl.REPEAT_Z, filter=mgl.LINEAR) self.vbo2 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz vx vy vz')) self.vao2 = self.ctx.simple_vertex_array(self.prog2, self.vbo2, 'in_vert', 'in_norm', 'in_text') self.vao2.scope = self.ctx.scope(mgl.DEPTH_TEST, self.fbo1, samplers=[(self.sampler2, 0)])