示例#1
0
 def test_matrix(self):
     """basic matrix functions"""
     result = Utils3d.get_identity_matrix()
     identity = Utils3d.get_identity_matrix()
     result *= 2
     print("__imul__(2):\n", result)
     result /= 2
     print("__idiv__(2):\n", result) 
     result += identity
     print("__iadd__(eye):\n", result)
     print("__mul__(2):\n", result * 2)
     print("__div__(2):\n", result / 2)
     print("__add__(eye):\n", result + identity)
示例#2
0
 def test_matrix(self):
     """basic matrix functions"""
     result = Utils3d.get_identity_matrix()
     identity = Utils3d.get_identity_matrix()
     result *= 2
     print "__imul__(2):\n", result
     result /= 2
     print "__idiv__(2):\n", result 
     result += identity
     print "__iadd__(eye):\n", result
     print "__mul__(2):\n", result * 2
     print "__div__(2):\n", result / 2
     print "__add__(eye):\n", result + identity
示例#3
0
 def test_change_of_basis(self):
     vector = Vector.from_tuple(16, 9, 0)
     identiy = Utils3d.get_identity_matrix()
     rot_z = Utils3d.get_rot_z_matrix(1)
     y_ratio = 16.0 / 9.0
     alt_basis = Matrix3d.from_row_vectors(Vector.from_tuple(1, 0, 0),
                                           Vector.from_tuple(0, y_ratio, 0),
                                           Vector.from_tuple(0, 0, 1))
     alt_basis_inv = alt_basis.inverse()
     # these two vectors should be the same
     #print "v = ", vector
     result_1 = alt_basis_inv.mul_vec(vector)
     #print "v1 = C⁻¹(v): ", result_1
     result_2 = alt_basis.mul_vec(result_1)
     #print "v = C(v1)): ", result_2
     self.assertEqual(vector, result_2)
    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
        """
        # light from above
        light_position = np.array((0.0, 0.0, 10.0, 1.0), dtype=DTYPE)
        # apply linear transformations to vetices
        # daw faces fom lowe z to higher
        transformation = self.transformations[self.frames % self.len_transformations]
        color = pygame.Color(200, 200, 200, 255)
        for polygon in self.polygons:
            # apply transformation to every vertice in polygon
            newpolygon = polygon.transform(transformation)
            # get new position vector
            pos_vec = newpolygon.get_position_vector()
            # calculate vector from face to lightsource
            v_light = pos_vec - light_position
            # get the normal of the face
            normal = newpolygon.get_normal_faster()
            # calculate angle between face normal and vector to light source
            light_angle = Utils3d.angle_to(normal, v_light)
            # angle to light source in radians, between 0 and math.pi
            normal_color = int(light_angle * 255 / math.pi)
            #avg_z = max(min(abs(int(newface.get_avg_z() * 10)), 255), 0) 
            color = pygame.Color(normal_color, normal_color, normal_color, 255)
            pygame.draw.polygon(self.surface, color, newpolygon.projected(self.origin_x, self.origin_y), 1)
        self.frames += 1
    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
        """
        # light from above
        light_position = np.array((0.0, 0.0, 10.0, 1.0), dtype=DTYPE)
        # apply linear transformations to vetices
        # daw faces fom lowe z to higher
        transformation = self.transformations[self.frames %
                                              self.len_transformations]
        color = pygame.Color(200, 200, 200, 255)
        for polygon in self.polygons:
            # apply transformation to every vertice in polygon
            newpolygon = polygon.transform(transformation)
            # get new position vector
            pos_vec = newpolygon.get_position_vector()
            # calculate vector from face to lightsource
            v_light = pos_vec - light_position
            # get the normal of the face
            normal = newpolygon.get_normal_faster()
            # calculate angle between face normal and vector to light source
            light_angle = Utils3d.angle_to(normal, v_light)
            # angle to light source in radians, between 0 and math.pi
            normal_color = int(light_angle * 255 / math.pi)
            #avg_z = max(min(abs(int(newface.get_avg_z() * 10)), 255), 0)
            color = pygame.Color(normal_color, normal_color, normal_color, 255)
            pygame.draw.polygon(
                self.surface, color,
                newpolygon.projected(self.origin_x, self.origin_y), 1)
        self.frames += 1
示例#6
0
 def test_rotation(self):
     """test rotation transformation"""
     # original vector point to 0 degree in X-Y coordinates
     vector = Vector.from_tuple(1, 0, 0)
     # print "Original Vector.from_tuple", vector
     identity = Utils3d.get_identity_matrix()
     # should rotate about math.pi = 180 degrees on X-Y Plane counter-clockwise
     # so we need Rotation Matrix around Z-axis
     rot_x = Utils3d.get_rot_z_matrix(math.pi)
     # print "Rotation matrix: \n", rot_x
     t = rot_x.mul_vec(vector)
     # print "Rotated vector: ", t
     self.assertEqual(t.length(), 1)
     # this is maybe not exactly equal
     self.assertTrue(-1.0-1e10 < t[0] < -1+1e10)
     self.assertTrue(0.0-1e10 < t[1] < 0+1e10)
     self.assertTrue(0.0-1e10 < t[2] < 0+1e10)
示例#7
0
 def test_rot_align(self):
     obj1 = Vector.from_tuple(1, 0, 0)
     obj2 = Vector.from_tuple(0, 1, 0)
     transformation = Utils3d.get_rot_align(obj1, obj2)
     #print transformation
     result = transformation.mul_vec(obj1)
     #print result
     self.assertEqual(result, obj2)
示例#8
0
 def test_change_of_basis(self):
     vector = Vector.from_tuple(16, 9, 0)
     identiy = Utils3d.get_identity_matrix()
     rot_z = Utils3d.get_rot_z_matrix(1)
     y_ratio = 16.0/9.0
     alt_basis = Matrix3d.from_row_vectors(
         Vector.from_tuple(1, 0, 0),
         Vector.from_tuple(0, y_ratio, 0),
         Vector.from_tuple(0, 0, 1))
     alt_basis_inv = alt_basis.inverse()
     # these two vectors should be the same
     #print "v = ", vector
     result_1 = alt_basis_inv.mul_vec(vector)
     #print "v1 = C⁻¹(v): ", result_1
     result_2 = alt_basis.mul_vec(result_1)
     #print "v = C(v1)): ", result_2
     self.assertEqual(vector, result_2)
示例#9
0
 def test_rot_align(self):
     obj1 = Vector.from_tuple(1, 0, 0)
     obj2 = Vector.from_tuple(0, 1, 0)
     transformation = Utils3d.get_rot_align(obj1, obj2)
     #print transformation
     result = transformation.mul_vec(obj1)
     #print result
     self.assertEqual(result, obj2)
示例#10
0
 def test_rotation(self):
     """test rotation transformation"""
     # original vector point to 0 degree in X-Y coordinates
     vector = Vector.from_tuple(1, 0, 0)
     # print "Original Vector.from_tuple", vector
     identity = Utils3d.get_identity_matrix()
     # should rotate about math.pi = 180 degrees on X-Y Plane counter-clockwise
     # so we need Rotation Matrix around Z-axis
     rot_x = Utils3d.get_rot_z_matrix(math.pi)
     # print "Rotation matrix: \n", rot_x
     t = rot_x.mul_vec(vector)
     # print "Rotated vector: ", t
     self.assertEqual(t.length(), 1)
     # this is maybe not exactly equal
     self.assertTrue(-1.0-1e10 < t[0] < -1+1e10)
     self.assertTrue(0.0-1e10 < t[1] < 0+1e10)
     self.assertTrue(0.0-1e10 < t[2] < 0+1e10)
示例#11
0
 def test_shift(self):
     """shift vector with transformation matrix"""
     vector = Vector.from_tuple(1, 0, 0)
     # this should shift X Axis about 2
     shift_vector = Utils3d.get_shift_vector(2, 0, 0)
     # calculate linear transformation A*v
     t = shift_vector + vector
     # result should be shifted about 2 on x-axis
     self.assertEqual(t, Vector.from_tuple(3, 0, 0))
示例#12
0
 def test_shift(self):
     """shift vector with transformation matrix"""
     vector = Vector.from_tuple(1, 0, 0)
     # this should shift X Axis about 2
     shift_vector = Utils3d.get_shift_vector(2, 0, 0)
     # calculate linear transformation A*v
     t = shift_vector + vector
     # result should be shifted about 2 on x-axis
     self.assertEqual(t, Vector.from_tuple(3, 0, 0))
示例#13
0
 def test_determinant(self):
     identity = Utils3d.get_identity_matrix()
     det = identity.determinant()
     #print "Determinant of Identity Matrix: ", det
     self.assertEqual(det, 1.0)
     matrix = Matrix3d.from_row_vectors(Vector.from_tuple(1, 0, 0),
                                        Vector.from_tuple(0, 2, 0),
                                        Vector.from_tuple(0, 0, 3))
     det = matrix.determinant()
     #print "Determinant of test matrix: ", det
     self.assertEqual(det, 6.0)
     inv = matrix.inverse()
示例#14
0
 def test_determinant(self):
     identity = Utils3d.get_identity_matrix()
     det = identity.determinant()
     #print "Determinant of Identity Matrix: ", det
     self.assertEqual(det, 1.0)
     matrix = Matrix3d.from_row_vectors(
         Vector.from_tuple(1, 0, 0),
         Vector.from_tuple(0, 2, 0),
         Vector.from_tuple(0, 0, 3))
     det = matrix.determinant()
     #print "Determinant of test matrix: ", det
     self.assertEqual(det, 6.0)
     inv = matrix.inverse()
示例#15
0
 def test_projection(self):
     obj = Vector.from_tuple(1, 2, 3)
     result = Utils3d.project(obj, 800, 600, 1, 1)
示例#16
0
def test():
    try:
        fps = 50
        surface = pygame.display.set_mode((800, 600))
        print pygame.display.Info()
        pygame.init()
        things = (
                {"start" : 0, "stop" : 10,
                    "class" : SinusText(surface, "SimpleDemo by GunnerySergeant", 200, 20, 1, pygame.Color(0,255,255))},
                {"start" : 10, "stop" : 20,
                    "class" : SinusText(surface, "Start with some Plasma Effect", 200, 30, 2, pygame.Color(0,255,255)),},
                {"start" : 20, "stop": 30,
                    "class" : Plasma(surface, scale=4),},
                {"start" : 30, "stop" : 40,
                    "class" : SinusText(surface, "a nice PlasmaFractal Effect", 200, 30, 2, pygame.Color(0,255,255)),},
                {"start" : 40, "stop": 50,
                    "class" : PlasmaFractal(surface, scale=4),},
                {"start" : 50, "stop" : 60,
                    "class" : SinusText(surface, "some sice coffeebean graphics, don't know why its so called, do you?", 200, 30, 2, pygame.Color(0,255,255)),},
                {"start" : 70, "stop" : 80,
                    "class" : CoffeeDraw(surface),},
                {"start" : 90, "stop" : 100,
                    "class" : SinusText(surface, "no demo without rotating cubes ...", 200, 20, 2, pygame.Color(0,128,255)),},
                {"start" : 90, "stop" : 100,
                    "class" : SinusText(surface, "no demo without rotating cubes ...", 190, 30, 4, pygame.Color(0,128,255)),},
                {"start" : 100, "stop": 110,
                    "class" :
                    Mesh(
                        surface,
                        origin=(300, 300), 
                        transformations=
                            Utils3d.get_rot_matrix(
                                Utils3d.get_scale_rot_matrix(
                                    scale_tuple=(600,600,1), 
                                    aspect_tuple=(16, 9),
                                    shift_tuple=(0, 0, -10)),
                                degrees=(1, 2, 3),
                                steps=360),
                        polygons = Utils3d.get_cube_polygons())},
                {"start" : 109, "stop" : 120,
                "class" : SinusText(surface, "greetings to all, who are better demomakers than i", 200, 30, 2, pygame.Color(0,255,255)),},
         
            )
        clock = pygame.time.Clock()       
        # mark pause state 
        pause = False
        # fill background
        surface.fill((0, 0, 0, 255))
        running = True
        frames = 0
        starttime = time.time()
        while running:
            # limit to FPS
            clock.tick(fps)
            # Event Handling
            events = pygame.event.get()  
            for event in events:  
                if event.type == pygame.QUIT:  
                    running = False
            keyinput = pygame.key.get_pressed()
            if keyinput is not None:
                # print keyinput
                if keyinput[pygame.K_ESCAPE]:
                    running = False
            runtime = int(time.time() - starttime)
            # Update Graphics
            if pause is not True:
                surface.fill((0, 0, 0, 255))
                for thing in things:
                    if thing["start"] < runtime < thing["stop"]:
                        thing["class"].update()
                pygame.display.update()
                # pygame.display.flip()
            frames += 1
        duration = time.time() - starttime
        print "Done %s frames in %s seconds, %s frames/s" % (frames, duration, frames/duration)
    except KeyboardInterrupt:
        print 'shutting down'
示例#17
0
def test():
    """test"""
    try:
        total_starttime = time.time()
        #fps = 150
        surface = pygame.display.set_mode((600, 600))
        pygame.init()
        cube = Utils3d.get_cube_polygons()
        objects = []
        for y in range(100, 500, 50):
            for x in range(100, 500, 50):
                objects.append(
                    Mesh(surface,
                         origin=(300, 300),
                         transformations=Transformer.flying_cubes_t(x, y),
                         polygons=cube))
        clock = pygame.time.Clock()
        pause = False
        color = pygame.Color(255, 255, 255, 255)
        print("Matrix precalculations done in %s seconds" %
              (time.time() - total_starttime))
        anim_starttime = time.time()
        frames = 100
        while frames > 0:
            #clock.tick(fps)
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    sys.exit(0)
            keyinput = pygame.key.get_pressed()
            if keyinput is not None:
                # print keyinput
                if keyinput[pygame.K_ESCAPE]:
                    sys.exit(1)
                if keyinput[pygame.K_UP]:
                    viewer_distance += 1
                if keyinput[pygame.K_DOWN]:
                    viewer_distance -= 1
                if keyinput[pygame.K_PLUS]:
                    fov += .1
                if keyinput[pygame.K_MINUS]:
                    fov -= .1
                if keyinput[pygame.K_p]:
                    pause = not pause
                if keyinput[pygame.K_r]:
                    viewer_distance = 256
                    fov = 2
            if pause is not True:
                # clear screen
                surface.fill((0, 0, 0, 255))
                for thing in objects:
                    thing.update()
                pygame.display.flip()
            frames -= 1
        duration = time.time() - anim_starttime
        print("Done 100 Frames in %f seonds, average %f fps" %
              (duration, 100 / duration))
        print("Whole program duration %f seconds" %
              (time.time() - total_starttime))
    except KeyboardInterrupt:
        print('shutting down')
示例#18
0
 def test_projection(self):
     obj = Vector.from_tuple(1, 2, 3)
     result = Utils3d.project(obj, 800, 600, 1, 1)
示例#19
0
def test():
    """test"""
    try:
        total_starttime = time.time()
        #fps = 150
        surface = pygame.display.set_mode((600, 600))
        pygame.init()
        cube = [Polygon(Utils3d.get_rectangle_points()), ]
        objects = []
        stepper = -0.1
        for x in range(100):
            objects.append(
                Mesh(
                    surface,
                    origin=(300, 300), 
                    transformations=(
                        Utils3d.get_shift_matrix(0, 0, stepper).dot(
                            Utils3d.get_scale_matrix(100,100,1).dot(
                                Utils3d.get_rot_z_matrix(x*math.pi/180))),),
                    polygons = cube)
            )
            stepper -= 0.1
        clock = pygame.time.Clock()       
        pause = False
        color = pygame.Color(255, 255, 255, 255)
        print "Matrix precalculations done in %s seconds" % (time.time()-total_starttime)
        anim_starttime = time.time()
        frames = 0
        while True:
            #clock.tick(fps)
            events = pygame.event.get()  
            for event in events:  
                if event.type == pygame.QUIT:  
                    sys.exit(0)
            keyinput = pygame.key.get_pressed()
            if keyinput is not None:
                # print keyinput
                if keyinput[pygame.K_ESCAPE]:
                    sys.exit(1)
                if keyinput[pygame.K_UP]:
                    viewer_distance += 1
                if keyinput[pygame.K_DOWN]:
                    viewer_distance -= 1
                if keyinput[pygame.K_PLUS]:
                    fov += .1
                if keyinput[pygame.K_MINUS]:
                    fov -= .1
                if keyinput[pygame.K_p]:
                    pause = not pause
                if keyinput[pygame.K_r]:
                    viewer_distance = 256
                    fov = 2
            if pause is not True:
                # clear screen
                surface.fill((0, 0, 0, 255))
                for thing in objects:
                    thing.update()
                pygame.display.flip()
            frames += 1 
        duration = time.time() - anim_starttime
        print "Done 100 Frames in %f seonds, average %f fps" % (duration, 100/duration)
        print "Whole program duration %f seconds" % (time.time()-total_starttime)
    except KeyboardInterrupt:
        print 'shutting down'
示例#20
0
def test():
    try:
        fps = 50
        surface = pygame.display.set_mode((800, 600))
        print(pygame.display.Info())
        pygame.init()
        things = (
            {
                "start":
                0,
                "stop":
                10,
                "class":
                SinusText(surface, "SimpleDemo by GunnerySergeant", 200, 20, 1,
                          pygame.Color(0, 255, 255))
            },
            {
                "start":
                10,
                "stop":
                20,
                "class":
                SinusText(surface, "Start with some Plasma Effect", 200, 30, 2,
                          pygame.Color(0, 255, 255)),
            },
            {
                "start": 20,
                "stop": 30,
                "class": Plasma(surface, scale=4),
            },
            {
                "start":
                30,
                "stop":
                40,
                "class":
                SinusText(surface, "a nice PlasmaFractal Effect", 200, 30, 2,
                          pygame.Color(0, 255, 255)),
            },
            {
                "start": 40,
                "stop": 50,
                "class": PlasmaFractal(surface, scale=4),
            },
            {
                "start":
                50,
                "stop":
                60,
                "class":
                SinusText(
                    surface,
                    "some sice coffeebean graphics, don't know why its so called, do you?",
                    200, 30, 2, pygame.Color(0, 255, 255)),
            },
            {
                "start": 70,
                "stop": 80,
                "class": CoffeeDraw(surface),
            },
            {
                "start":
                90,
                "stop":
                100,
                "class":
                SinusText(surface, "no demo without rotating cubes ...", 200,
                          20, 2, pygame.Color(0, 128, 255)),
            },
            {
                "start":
                90,
                "stop":
                100,
                "class":
                SinusText(surface, "no demo without rotating cubes ...", 190,
                          30, 4, pygame.Color(0, 128, 255)),
            },
            {
                "start":
                100,
                "stop":
                110,
                "class":
                Mesh(surface,
                     origin=(300, 300),
                     transformations=Utils3d.get_rot_matrix(
                         Utils3d.get_scale_rot_matrix(scale_tuple=(600, 600,
                                                                   1),
                                                      aspect_tuple=(16, 9),
                                                      shift_tuple=(0, 0, -10)),
                         degrees=(1, 2, 3),
                         steps=360),
                     polygons=Utils3d.get_cube_polygons())
            },
            {
                "start":
                109,
                "stop":
                120,
                "class":
                SinusText(
                    surface,
                    "greetings to all, who are better demomakers than i", 200,
                    30, 2, pygame.Color(0, 255, 255)),
            },
        )
        clock = pygame.time.Clock()
        # mark pause state
        pause = False
        # fill background
        surface.fill((0, 0, 0, 255))
        running = True
        frames = 0
        starttime = time.time()
        while running:
            # limit to FPS
            clock.tick(fps)
            # Event Handling
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    running = False
            keyinput = pygame.key.get_pressed()
            if keyinput is not None:
                # print keyinput
                if keyinput[pygame.K_ESCAPE]:
                    running = False
            runtime = int(time.time() - starttime)
            # Update Graphics
            if pause is not True:
                surface.fill((0, 0, 0, 255))
                for thing in things:
                    if thing["start"] < runtime < thing["stop"]:
                        thing["class"].update()
                pygame.display.update()
                # pygame.display.flip()
            frames += 1
        duration = time.time() - starttime
        print("Done %s frames in %s seconds, %s frames/s" %
              (frames, duration, frames / duration))
    except KeyboardInterrupt:
        print('shutting down')
示例#21
0
def test():
    """test"""
    try:
        total_starttime = time.time()
        #fps = 150
        surface = pygame.display.set_mode((600, 600))
        pygame.init()
        cube = Utils3d.get_cube_polygons()
        objects = []
        for y in range(100, 500, 50):
            for x in range(100, 500, 50):
                objects.append(
                    Mesh(
                        surface,
                        origin=(x, y), 
                        transformations=
                            Utils3d.get_rot_matrix(
                                Utils3d.get_scale_rot_matrix(
                                    scale_tuple=(100,100,1), 
                                    aspect_tuple=(16, 9),
                                    shift_tuple=(0, 0, -10)),
                                degrees=((x-y+20)/50, (y-x+40)/50, 3),
                                steps=360),
                        polygons = cube)
                )
        clock = pygame.time.Clock()       
        pause = False
        color = pygame.Color(255, 255, 255, 255)
        print "Matrix precalculations done in %s seconds" % (time.time()-total_starttime)
        anim_starttime = time.time()
        frames = 100
        while frames > 0:
            #clock.tick(fps)
            events = pygame.event.get()  
            for event in events:  
                if event.type == pygame.QUIT:  
                    sys.exit(0)
            keyinput = pygame.key.get_pressed()
            if keyinput is not None:
                # print keyinput
                if keyinput[pygame.K_ESCAPE]:
                    sys.exit(1)
                if keyinput[pygame.K_UP]:
                    viewer_distance += 1
                if keyinput[pygame.K_DOWN]:
                    viewer_distance -= 1
                if keyinput[pygame.K_PLUS]:
                    fov += .1
                if keyinput[pygame.K_MINUS]:
                    fov -= .1
                if keyinput[pygame.K_p]:
                    pause = not pause
                if keyinput[pygame.K_r]:
                    viewer_distance = 256
                    fov = 2
            if pause is not True:
                # clear screen
                surface.fill((0, 0, 0, 255))
                for thing in objects:
                    thing.update()
                pygame.display.flip()
            frames -= 1
        duration = time.time() - anim_starttime
        print "Done 100 Frames in %f seonds, average %f fps" % (duration, 100/duration)
        print "Whole program duration %f seconds" % (time.time()-total_starttime)
    except KeyboardInterrupt:
        print 'shutting down'