예제 #1
0
    def update(self):
        """
        called on every frame
        apply transformation matrix and project every polygon to 2d
        for color avg_z function is used
        polygons are sorted on avg_z value

        finally painting on surface is called
        """
        # Clock vector
        vector = Matrix3D.get_rot_z_matrix(self.angle).v_dot(self.vector)
        # projected = self.__project(self.vector, self.center)
        projected = self.__projectm(vector, self.center)
        pygame.draw.polygon(self.surface, pygame.Color(255,255,255,0), (self.center, projected), 1)
        # Cube
        mesh = self.model.transform(Matrix3D.get_rot_z_matrix(self.angle))
        #mesh = mesh.transform(Matrix3D.get_rot_x_matrix(self.angle))
        mesh = mesh.transform(Matrix3D.get_scale_matrix(SCALE, SCALE, SCALE))
        mesh = mesh.transform(Matrix3D.get_shift_matrix(X_SHIFT, Y_SHIFT, Z_SHIFT))
        for face in mesh:
            vertices = [self.__projectm(vertice, self.center) for vertice in face]
            pygame.draw.polygon(self.surface, pygame.Color(255,255,255,0), vertices, 1)
        self.angle += self.angle_step
        # axis vectors
        pygame.draw.polygon(self.surface, pygame.Color(255,0,0,0), (self.center, self.__projectm(self.x_axis, self.center)), 1)
        pygame.draw.polygon(self.surface, pygame.Color(0,255,0,0), (self.center, self.__projectm(self.y_axis, self.center)), 1)
        pygame.draw.polygon(self.surface, pygame.Color(0,0,255,0), (self.center, self.__projectm(self.z_axis, self.center)), 1)
예제 #2
0
 def test_rot_matrices(self):
     m = Matrix3D.get_rot_x_matrix(100)
     assert m.dot(Matrix3D.identity()) == m
     m = Matrix3D.get_rot_y_matrix(100)
     assert m.dot(Matrix3D.identity()) == m
     m = Matrix3D.get_rot_z_matrix(100)
     assert m.dot(Matrix3D.identity()) == m
     # rotate vector only in x-axis around x - nothing should happen
     v1 = Vector3D(1, 0, 0, 1)
     assert Matrix3D.get_rot_x_matrix(100).v_dot(v1) == v1
     v1 = Vector3D(0, 1, 0, 1)
     assert Matrix3D.get_rot_y_matrix(100).v_dot(v1) == v1
     v1 = Vector3D(0, 0, 1, 1)
     assert Matrix3D.get_rot_z_matrix(100).v_dot(v1) == v1
     # rotate vectors really
     v1 = Vector3D(1.0, 0.0, 0.0, 1.0)
     # 90 degrees or pi/2
     real_v = Matrix3D.get_rot_z_matrix(math.pi / 2).v_dot(v1)
     test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # 180 degrees
     real_v = Matrix3D.get_rot_z_matrix(math.pi).v_dot(v1)
     test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # 270 degrees
     real_v = Matrix3D.get_rot_z_matrix(math.pi + math.pi / 2).v_dot(v1)
     test_v = Vector3D.from_list([0.000000, -1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # 360 degrees
     real_v = Matrix3D.get_rot_z_matrix(2 * math.pi).v_dot(v1)
     test_v = Vector3D.from_list([1.000000, 0.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # rotate around Y-Axis about 180 degrees
     real_v = Matrix3D.get_rot_y_matrix(math.pi).v_dot(v1)
     test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # rotate y:90 and x:90 -> (0, 1, 0, 1)
     real_v = Matrix3D.get_rot_y_matrix(math.pi / 2).v_dot(v1)
     test_v = Vector3D.from_list([0.000000, 0.000000, -1.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     real_v = Matrix3D.get_rot_x_matrix(math.pi / 2).v_dot(real_v)
     test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # and this is the combined version
     rot_y = Matrix3D.get_rot_y_matrix(math.pi / 2)
     print "rotation around y:\n", rot_y
     rot_x = Matrix3D.get_rot_x_matrix(math.pi / 2)
     print "rotation around x:\n", rot_x
     rot_z = Matrix3D.get_rot_z_matrix(math.pi / 2)
     print "rotation around z:\n", rot_z
     rot_m = rot_x.dot(rot_y.dot(rot_z))
     print "combined rotation matrix:\n", rot_m
     real_v = rot_m.v_dot(v1)
     print "resulting vector:", real_v
     test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
예제 #3
0
 def test_rot_matrices(self):
     m = Matrix3D.get_rot_x_matrix(100)
     assert m.dot(Matrix3D.identity()) == m
     m = Matrix3D.get_rot_y_matrix(100)
     assert m.dot(Matrix3D.identity()) == m
     m = Matrix3D.get_rot_z_matrix(100)
     assert m.dot(Matrix3D.identity()) == m
     # rotate vector only in x-axis around x - nothing should happen
     v1 = Vector3D(1, 0, 0, 1)
     assert Matrix3D.get_rot_x_matrix(100).v_dot(v1) == v1
     v1 = Vector3D(0, 1, 0, 1)
     assert Matrix3D.get_rot_y_matrix(100).v_dot(v1) == v1
     v1 = Vector3D(0, 0, 1, 1)
     assert Matrix3D.get_rot_z_matrix(100).v_dot(v1) == v1
     # rotate vectors really
     v1 = Vector3D(1.0, 0.0, 0.0, 1.0)
     # 90 degrees or pi/2
     real_v = Matrix3D.get_rot_z_matrix(math.pi/2).v_dot(v1)
     test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # 180 degrees
     real_v = Matrix3D.get_rot_z_matrix(math.pi).v_dot(v1)
     test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # 270 degrees
     real_v = Matrix3D.get_rot_z_matrix(math.pi + math.pi/2).v_dot(v1)
     test_v = Vector3D.from_list([0.000000, -1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # 360 degrees
     real_v = Matrix3D.get_rot_z_matrix(2 * math.pi).v_dot(v1)
     test_v = Vector3D.from_list([1.000000, 0.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # rotate around Y-Axis about 180 degrees
     real_v = Matrix3D.get_rot_y_matrix(math.pi).v_dot(v1)
     test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # rotate y:90 and x:90 -> (0, 1, 0, 1)
     real_v = Matrix3D.get_rot_y_matrix(math.pi/2).v_dot(v1)
     test_v = Vector3D.from_list([0.000000, 0.000000, -1.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     real_v = Matrix3D.get_rot_x_matrix(math.pi/2).v_dot(real_v)
     test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
     # and this is the combined version
     rot_y = Matrix3D.get_rot_y_matrix(math.pi/2)
     print "rotation around y:\n", rot_y
     rot_x = Matrix3D.get_rot_x_matrix(math.pi/2)
     print "rotation around x:\n", rot_x
     rot_z = Matrix3D.get_rot_z_matrix(math.pi/2)
     print "rotation around z:\n", rot_z
     rot_m = rot_x.dot(rot_y.dot(rot_z))
     print "combined rotation matrix:\n", rot_m
     real_v = rot_m.v_dot(v1)
     print "resulting vector:", real_v
     test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000])
     assert real_v.nearly_equal(test_v)
예제 #4
0
    def update(self):
        """
        called on every frame
        apply transformation matrix and project every polygon to 2d
        for color avg_z function is used
        polygons are sorted on avg_z value

        finally painting on surface is called
        """
        # Clock vector
        vector = Matrix3D.get_rot_z_matrix(self.angle).v_dot(self.vector)
        # projected = self.__project(self.vector, self.center)
        projected = self.__projectm(vector, self.center)
        pygame.draw.polygon(self.surface, pygame.Color(255, 255, 255, 0),
                            (self.center, projected), 1)
        # Cube
        mesh = self.model.transform(Matrix3D.get_rot_z_matrix(self.angle))
        #mesh = mesh.transform(Matrix3D.get_rot_x_matrix(self.angle))
        mesh = mesh.transform(Matrix3D.get_scale_matrix(SCALE, SCALE, SCALE))
        mesh = mesh.transform(
            Matrix3D.get_shift_matrix(X_SHIFT, Y_SHIFT, Z_SHIFT))
        for face in mesh:
            vertices = [
                self.__projectm(vertice, self.center) for vertice in face
            ]
            pygame.draw.polygon(self.surface, pygame.Color(255, 255, 255, 0),
                                vertices, 1)
        self.angle += self.angle_step
        # axis vectors
        pygame.draw.polygon(
            self.surface, pygame.Color(255, 0, 0, 0),
            (self.center, self.__projectm(self.x_axis, self.center)), 1)
        pygame.draw.polygon(
            self.surface, pygame.Color(0, 255, 0, 0),
            (self.center, self.__projectm(self.y_axis, self.center)), 1)
        pygame.draw.polygon(
            self.surface, pygame.Color(0, 0, 255, 0),
            (self.center, self.__projectm(self.z_axis, self.center)), 1)