Example #1
0
    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
Example #2
0
    def __init__(
        self,
        ctx: mgl.Context,
        paper_size: Tuple[float, float],
        color: ColorType = (0, 0, 0, 0.45),
        shadow_size: float = 7.0,
    ):
        super().__init__(ctx)

        data = np.array(
            [
                (0, 0),
                (paper_size[0], 0),
                (paper_size[0], paper_size[1]),
                (0, paper_size[1]),
                (paper_size[0], shadow_size),
                (paper_size[0] + shadow_size, shadow_size),
                (paper_size[0] + shadow_size, paper_size[1] + shadow_size),
                (shadow_size, paper_size[1] + shadow_size),
                (shadow_size, paper_size[1]),
            ],
            dtype="f4",
        )
        line_idx = np.array([0, 1, 2, 3], dtype="i4")
        triangle_idx = np.array(
            [
                (0, 3, 1),  # page background
                (1, 3, 2),
                (4, 2, 5),  # shadow
                (2, 6, 5),
                (7, 6, 2),
                (8, 7, 2),
            ],
            dtype="i4",
        ).reshape(-1)

        self._color = color
        self._prog = load_program("fast_line_mono", ctx)

        vbo = ctx.buffer(data.tobytes())
        self._bounds_vao = ctx.simple_vertex_array(
            self._prog, vbo, "in_vert", index_buffer=ctx.buffer(line_idx.tobytes())
        )
        self._shading_vao = ctx.simple_vertex_array(
            self._prog, vbo, "in_vert", index_buffer=ctx.buffer(triangle_idx.tobytes())
        )
Example #3
0
    def __init__(self, ctx: mgl.Context, lc: vp.LineCollection, color: ColorType):
        super().__init__(ctx)

        self._prog = load_program("fast_line", ctx)

        vertices, indices = self._build_buffers(lc, color=color)
        vbo = ctx.buffer(vertices.astype("f4").tobytes())
        ibo = ctx.buffer(indices.astype("i4").tobytes())
        self._vao = ctx.simple_vertex_array(
            self._prog, vbo, "in_vert", "in_color", index_buffer=ibo
        )
Example #4
0
    def __init__(
        self, ctx: mgl.Context, lc: vp.LineCollection, pen_width: float, color: ColorType
    ):
        super().__init__(ctx)

        self._color = color
        self._pen_width = pen_width
        self._prog = load_program("preview_line", ctx)

        vertices, indices = self._build_buffers(lc)
        vbo = ctx.buffer(vertices.tobytes())
        ibo = ctx.buffer(indices.tobytes())
        self._vao = ctx.simple_vertex_array(self._prog, vbo, "position", index_buffer=ibo)
Example #5
0
    def __init__(
        self, ctx: mgl.Context, lc: vp.LineCollection, color: ColorType = (0, 0, 0, 0.5)
    ):
        super().__init__(ctx)

        self._color = color
        self._prog = load_program("fast_line_mono", ctx)

        # build vertices
        vertices: List[Tuple[float, float]] = []
        for i in range(len(lc) - 1):
            vertices.extend(
                ((lc[i][-1].real, lc[i][-1].imag), (lc[i + 1][0].real, lc[i + 1][0].imag))
            )

        if len(vertices) > 0:
            vbo = ctx.buffer(np.array(vertices, dtype="f4").tobytes())
            self._vao = ctx.simple_vertex_array(self._prog, vbo, "in_vert")
        else:
            self._vao = None
Example #6
0
    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)
Example #7
0
def from_mesh(ctx: moderngl.Context, program: moderngl.Program, mesh: Mesh):
    buffer = mesh.vertices.astype('f4').copy('C')
    vbo = ctx.buffer(buffer)
    vao = ctx.simple_vertex_array(program, vbo, 'in_position', 'in_normal')
    return vao