Esempio n. 1
0
 def test_translation_matrix(self):
     T = matrix.translation_matrix(5, 6, 7)
     R = np.array([
         [1, 0, 0, 5],
         [0, 1, 0, 6],
         [0, 0, 1, 7],
         [0, 0, 0, 1]
         ])
     self.assertTrue(np.array_equal(T, R))
Esempio n. 2
0
    def reset_view(self):
        self.viewport = (
            0,
            0,
            int(builtins.width * builtins.pixel_x_density),
            int(builtins.height * builtins.pixel_y_density),
        )
        self.texture_viewport = (
            0,
            0,
            builtins.width,
            builtins.height,
        )

        gloo.set_viewport(*self.viewport)  # pylint: disable=no-member

        cz = (builtins.height / 2) / math.tan(math.radians(30))
        self.projection_matrix = matrix.perspective_matrix(
            math.radians(60),
            builtins.width / builtins.height,
            0.1 * cz,
            10 * cz
        )
        self.modelview_matrix = matrix.translation_matrix(-builtins.width / 2,
                                                          builtins.height / 2,
                                                          -cz)
        self.modelview_matrix = self.modelview_matrix.dot(
            matrix.scale_transform(1, -1, 1))

        self.transform_matrix = np.identity(4)

        self.default_prog['modelview'] = self.modelview_matrix.T.flatten()
        self.default_prog['projection'] = self.projection_matrix.T.flatten()

        self.texture_prog['modelview'] = self.modelview_matrix.T.flatten()
        self.texture_prog['projection'] = self.projection_matrix.T.flatten()

        self.line_prog = Program(src_line.vert, src_line.frag)

        self.line_prog['modelview'] = self.modelview_matrix.T.flatten()
        self.line_prog['projection'] = self.projection_matrix.T.flatten()
        self.line_prog["height"] = builtins.height

        self.fbuffer_tex_front = Texture2D(
            (builtins.height, builtins.width, 3))
        self.fbuffer_tex_back = Texture2D((builtins.height, builtins.width, 3))

        for buf in [self.fbuffer_tex_front, self.fbuffer_tex_back]:
            self.fbuffer.color_buffer = buf
            with self.fbuffer:
                self.clear()
Esempio n. 3
0
    def flush_geometry(self):
        """Flush all the shape geometry from the draw queue to the GPU.
        """
        for index, shape in enumerate(self.draw_queue):
            current_shape, current_obj = self.draw_queue[index][
                0], self.draw_queue[index][1]
            # If current_shape is lines, bring it to the front by epsilon
            # to resolve z-fighting
            if current_shape == 'lines':
                # line_transform is used whenever we render lines to break ties in depth
                # We transform the points to camera space, move them by
                # Z_EPSILON, and them move them back to world space
                line_transform = inv(self.lookat_matrix).dot(
                    translation_matrix(0, 0,
                                       Z_EPSILON).dot(self.lookat_matrix))
                vertices = current_obj[0]
                current_obj = (np.hstack([
                    vertices, np.ones((vertices.shape[0], 1))
                ]).dot(line_transform.T)[:, :3], *current_obj[1:])
            self.render_with_shaders(current_shape, current_obj)

        self.draw_queue = []
Esempio n. 4
0
    def translate(self, x, y, z=0):
        """Translate the shape origin to the given location.

        :param x: The displacement amount in the x-direction (controls
            the left/right displacement)
        :type x: int

        :param y: The displacement amount in the y-direction (controls
            the up/down displacement)
        :type y: int

        :param z: The displacement amount in the z-direction (0 by
            default). This controls the displacement away-from/towards
            the screen.
        :type z: int

        :returns: The translation matrix applied to the transform
            matrix.
        :rtype: np.ndarray

        """
        tmat = matrix.translation_matrix(x, y, z)
        return tmat