def __init__(self, ctx: moderngl.Context, size: Size, mesh: Mesh, projection: np.array, components=4): self.size = size self.projection = projection self.prog = ctx.program( vertex_shader=Path('shaders/simple.vert').read_text(), fragment_shader=Path('shaders/simple.frag').read_text()) #from ModernGL.ext.obj import Obj #mesh = Obj.open('meshes/cube.obj') #vertex_data = mesh.pack('vx vy vz nx ny nz') #self.vao = from_mesh(ctx, self.prog, mesh) vertex_data = pack(mesh).astype('f4').tobytes() self.vbo = ctx.buffer(vertex_data) self.vao = ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_normal') self.fbo = create_fbo(ctx, self.size, components) self.fbo.use() self.ctx = ctx
def load_program(name: str, ctx: mgl.Context) -> mgl.Program: """Create a program based on a shader name prefix. This function automatically load all shader type based on file existence, looking for _vertex, _fragment and _geometry suffices in the ``shaders`` directory. The same program instance is returned for identical combination of ``name``/``_ctx``. Args: name: shader file name prefix ctx: context Returns: the loaded program """ def _load_shader(path: str) -> Optional[str]: try: with open(path) as fp: return fp.read() except IOError: return None full_path = os.path.dirname( __file__) + os.path.sep + "shaders" + os.path.sep + name return ctx.program( vertex_shader=_load_shader(full_path + "_vertex.glsl"), fragment_shader=_load_shader(full_path + "_fragment.glsl"), geometry_shader=_load_shader(full_path + "_geometry.glsl"), )
def _compile(self, ctx: moderngl.Context): if self.program is None: self.program = ctx.program(vertex_shader=self.vertex_shader, fragment_shader=self.fragment_shader, geometry_shader=self.geometry_shader) for n in self.program: v = self.program[n] if isinstance(v, moderngl.Attribute): self.inputs[n] = v if isinstance(v, moderngl.Uniform): self.uniforms[n] = v
def __init__( self, ctx: mgl.Context, lc: vp.LineCollection, color: ColorType = (0, 0, 0, 0.25) ): super().__init__(ctx) vertex = """ #version 330 uniform mat4 projection; in vec2 position; void main() { gl_PointSize = 5.0; gl_Position = projection * vec4(position, 0.0, 1.0); } """ fragment = """ #version 330 uniform vec4 color; out vec4 out_color; void main() { out_color = color; } """ self._prog = ctx.program(vertex_shader=vertex, fragment_shader=fragment) self._color = color vertices, indices = self._build_buffers(lc) vbo = ctx.buffer(vertices.astype("f4").tobytes()) ibo = ctx.buffer(indices.astype("i4").tobytes()) self._vao = ctx.simple_vertex_array(self._prog, vbo, "position", index_buffer=ibo)
def __init__(self, context: mgl.Context): self.render = context.program(vertex_shader=particle_vert, fragment_shader=particle_frag) #trans_prog = res.read_text(shader_src, 'particle_transform.vert') self.transform = context.program(vertex_shader=particle_transform_vert, varyings=['out_pos_alpha', 'out_prev_pos_alpha'])
def VertexColorShader(context: mgl.Context): global vertex_color_shader if vertex_color_shader is None: vertex_color_shader = context.program(vertex_shader=vertex_color_vert, fragment_shader=vertex_color_frag) return vertex_color_shader
def TextShader(context: mgl.Context): global text_shader if text_shader is None: text_shader = context.program(vertex_shader=text_vert, fragment_shader=text_frag) return text_shader
def StippleShader(context: mgl.Context): global stipple_shader if stipple_shader is None: stipple_shader = context.program(vertex_shader=stipple_vert, fragment_shader=stipple_frag) return stipple_shader
def ImageShader(context: mgl.Context): global image_shader if image_shader is None: image_shader = context.program(vertex_shader=image_vert, fragment_shader=image_frag) return image_shader
def FlatShader(context: mgl.Context): global flat_shader if flat_shader is None: flat_shader = context.program(vertex_shader=flat_vert, fragment_shader=flat_frag) return flat_shader