Пример #1
0
    def __init__(self):
        w, h = 512, 512
        super().__init__('Demo #3', w, h)

        # Ensure PyTorch CUDA is initialised (it is important that this happens _after_ PyCUDA
        # initialises its context, which is currently done via autoinit).
        assert torch.cuda.is_available()
        torch.empty(1, device='cuda')

        # Create a texture buffer with OpenGL and CUDA views
        self.tex = MappedTexture(h, w)

        vertex_code = resources.read_text(glupy.examples.demo03, 'demo03.vert')
        fragment_code = resources.read_text(glupy.examples.demo03,
                                            'demo03.frag')
        self.program = ShaderProgram(vertex_code, fragment_code)

        vertex_data = np.empty(4, [
            ('position', np.float32, 2),
            ('texcoord', np.float32, 2),
        ])

        vertex_data['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
        vertex_data['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]

        self.vao = VAO(vbo=VBO(vertex_data), connect_to=self.program)

        self.theta = 0
Пример #2
0
    def __init__(self):
        super().__init__('Demo #1', 1600, 900)

        vertex_code = resources.read_text(glupy.examples.demo01, 'demo01.vert')
        fragment_code = resources.read_text(glupy.examples.demo01, 'demo01.frag')
        self.program = ShaderProgram(vertex_code, fragment_code)

        # # Option 1: Interleaved memory layout.
        # layout = np.dtype(([
        #     ('position', (np.float32, 3)),
        #     ('color', (np.float32, 4)),
        # ], 4), align=True)

        # Option 2: "Struct of arrays" memory layout.
        layout = np.dtype(([
            ('position', (np.float32, 3), 4),
            ('color', (np.float32, 4), 4),
        ]), align=True)

        vertex_data = np.empty(layout.shape, layout.base)
        vertex_data['position'] = [[-1, +1, 0], [+1, +1, 0], [-1, -1, 0], [+1, -1, 0]]
        vertex_data['color'] = [[0, 1, 0, 1], [1, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1]]

        index_data = np.asarray([
            0, 1, 2,
            3, 1, 2,
        ], dtype=np.uint32)

        self.vao = VAO(VBO(vertex_data), EBO(index_data), connect_to=self.program)
Пример #3
0
    def __init__(self, width, height):
        self.shader = create_image_shader()

        vertex_data = np.empty(4, [
            ('position', np.float32, 2),
            ('texcoord', np.float32, 2),
        ])

        vertex_data['position'] = [(0, 0), (0, 1), (1, 0), (1, 1)]
        vertex_data['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]

        self.vao = VAO(vbo=VBO(vertex_data), connect_to=self.shader)

        self.image = None
        self.tex = MappedTexture(height, width)
        self.aspect_ratio = width / height
Пример #4
0
    def __init__(self):
        self.height_px = 10  # Seek bar height (in pixels)

        self.shader = create_seekbar_shader()

        vertex_data = np.empty(4, [
            ('position', np.float32, 2),
            ('texcoord', np.float32, 2),
        ])

        vertex_data['position'] = [(0, 0), (0, 1), (1, 0), (1, 1)]
        vertex_data['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]

        with self.shader:
            self.shader.set_uniform_float('depth', 0.0)

        self.vao = VAO(vbo=VBO(vertex_data), connect_to=self.shader)
Пример #5
0
    def __init__(self, shader, centre, normal, forwards, size=16000.0):
        self.shader = shader

        vertex_data_fields = [('position', np.float32, 3),
                              ('texcoord', np.float32, 2)]

        vertex_data = np.asarray([
            ((-1, 0, -1), (0, 0)),
            ((1, 0, -1), (1, 0)),
            ((1, 0, 1), (1, 1)),
            ((-1, 0, 1), (0, 1)),
        ],
                                 dtype=vertex_data_fields)

        # Calculate rotation matrix for the plane.
        up = forwards / np.linalg.norm(forwards, ord=2)
        c = normal / np.linalg.norm(normal, ord=2)
        a = np.cross(up, c)
        a = a / np.linalg.norm(a, ord=2)
        b = np.cross(c, a)
        rot = np.eye(4)
        rot[0, :3] = a
        rot[1, :3] = c
        rot[2, :3] = b

        model_matrix = mat4.identity()
        model_matrix = mat4.scale(size) @ model_matrix
        model_matrix = rot @ model_matrix
        model_matrix = mat4.translate(*centre) @ model_matrix
        with self.shader:
            self.shader.set_uniform_mat4('modelMatrix', model_matrix)

        index_data = np.asarray([
            0,
            1,
            2,
            0,
            2,
            3,
        ], dtype=np.uint32)

        self.vao = VAO(vbo=VBO(vertex_data),
                       ebo=EBO(index_data),
                       connect_to=self.shader)
Пример #6
0
    def __init__(self):
        super().__init__('Demo #2', 1600, 900)

        gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)

        vertex_code = resources.read_text(glupy.examples.demo02, 'demo02.vert')
        fragment_code = resources.read_text(glupy.examples.demo02, 'demo02.frag')
        self.program = ShaderProgram(vertex_code, fragment_code)

        vertex_data = np.empty(8, [
            ('position', np.float32, 3),
            ('color', np.float32, 4),
        ])
        index_data = np.empty(12 * 3, dtype=np.uint32)

        vertex_data['position'] = [
            (-1, -1, -1),
            (+1, -1, -1),
            (+1, +1, -1),
            (-1, +1, -1),
            (-1, -1, +1),
            (+1, -1, +1),
            (+1, +1, +1),
            (-1, +1, +1),
        ]
        vertex_data['color'] = [
            (0, 1, 0, 1),
            (1, 1, 0, 1),
            (1, 0, 0, 1),
            (0, 0, 1, 1),
            (1, 1, 1, 1),
            (1, 1, 1, 1),
            (1, 1, 1, 1),
            (1, 1, 1, 1),
        ]

        index_data[:] = [
            0, 1, 2,
            2, 3, 0,
            1, 5, 6,
            6, 2, 1,
            7, 6, 5,
            5, 4, 7,
            4, 0, 3,
            3, 7, 4,
            4, 5, 1,
            1, 0, 4,
            3, 2, 6,
            6, 7, 3
        ]

        self.vao = VAO(vbo=VBO(vertex_data), ebo=EBO(index_data), connect_to=self.program)

        trans_model = mat4.scale(0.5) #@ mat4.rotate_axis_angle(0, 1/np.sqrt(2), 1/np.sqrt(2), np.pi / 4)
        trans_view = mat4.translate(2.0, 0, 5.0)
        trans_proj = mat4.perspective(np.pi / 3, 16 / 9, 0.1, 100)

        with self.program:
            self.program.set_uniform_mat4('transModel', trans_model)
            self.program.set_uniform_mat4('transView', trans_view)
            self.program.set_uniform_mat4('transProj', trans_proj)
Пример #7
0
 def test_pattern4(self, gl_context):
     vbo1 = VBO(np.dtype((np.float32, 4)))
     vbo2 = VBO(np.dtype((np.float32, 4)))
     vbo3 = VBO(np.dtype((np.float32, 4)))
     vao1 = VAO()
     vao2 = VAO()
     with vao2:
         vao2.bind_vbo(vbo2)
         assert vbo2.is_currently_bound()
     assert vbo2.is_currently_bound()
     with vbo3:
         assert vbo3.is_currently_bound()
         with vao1:
             vao1.bind_vbo(vbo1)
             assert vbo1.is_currently_bound()
         assert vbo1.is_currently_bound()
         with vao2:
             assert vbo1.is_currently_bound()
         assert vbo1.is_currently_bound()
Пример #8
0
    def __init__(self,
                 shader,
                 start_pos,
                 end_pos,
                 colour=np.asarray([1.0, 1.0, 1.0, 1.0])):
        self.shader = shader
        self.colour = colour

        self.start_pos = start_pos
        self.end_pos = end_pos

        diff = self.end_pos - self.start_pos
        dist = np.linalg.norm(diff, 2)
        u = diff / (dist + 1e-6)

        axis = np.zeros(3)
        axis[np.argmin(np.abs(u))] = 1.0
        perp1 = np.cross(u, axis)
        perp2 = np.cross(u, perp1)

        vertex_data_fields = [('position', np.float32, 3)]

        a = 0.1 * diff + self.start_pos  # Mid-band location
        b = 0.1 * dist  # Thickness
        vertex_data = np.asarray([
            (tuple(self.start_pos), ),
            (tuple(self.end_pos), ),
            (tuple(a + b * perp1), ),
            (tuple(a + b * perp2), ),
            (tuple(a - b * perp1), ),
            (tuple(a - b * perp2), ),
        ],
                                 dtype=vertex_data_fields)

        index_data = np.asarray([
            3,
            2,
            0,
            1,
            2,
            3,
            4,
            3,
            0,
            1,
            3,
            4,
            5,
            4,
            0,
            1,
            4,
            5,
            2,
            5,
            0,
            1,
            5,
            2,
        ],
                                dtype=np.uint32)

        self.vao = VAO(vbo=VBO(vertex_data),
                       ebo=EBO(index_data),
                       connect_to=self.shader)