Exemplo n.º 1
0
 def read(self, buffer, offset):
     d = struct.unpack_from('9d', buffer, offset)
     return Matrix(
         d[0:3],
         d[3:6],
         d[6:9],
     )
Exemplo n.º 2
0
 def read(self, buffer, offset):
     d = struct.unpack_from('12d', buffer, offset)
     return Matrix(
         d[0:4],
         d[4:8],
         d[8:12],
     )
Exemplo n.º 3
0
    def update2(self, dt):
        self.game_time += dt
        draw = self.app.draw
        draw.draw_axis(Matrix(), 2.0)
        draw.draw_grid(1.0, 10, toy.coloring.GRAY)

        angle = self.game_time
        q = Quaternion.from_euler_angles(Vector(0.0, angle, 0.0))
        R = Matrix.from_rotation(q)
        S = Matrix.from_scale(Vector(4.0, 1.0, 1.0))
        draw.draw_box(R * S, toy.coloring.GREEN)
        draw.draw_box(S * R, toy.coloring.YELLOW)
Exemplo n.º 4
0
    def __init__(self):
        self.width = 512
        self.height = 512
        self.aspect = 1.0
        self.view = Matrix()

        self.perspective_fov = math.radians(60.0)
        self.ortho_extent = 10.0

        self.view_eye = Vector(0.0, 4.0, -10.0)
        self.view_at = Vector(0.0, 0.0, 0.0)
        self.view_up = Vector(0.0, 1.0, 0.0)

        self.set_look_at(self.view_eye, self.view_at, self.view_up)
        self.set_perspective(self.perspective_fov)
        self.set_ortho(self.ortho_extent)

        self.mode = self.MODE_PERSPECTIVE
Exemplo n.º 5
0
    def _readSmoothMtxs(self, fres):
        """Read the smooth matrices."""

        self.smooth_mtxs = []
        for i in range(max(self.smooth_idxs)):
            mtx = fres.read('3f',
                            count=4,
                            pos=self.smooth_mtx_offs + (i * 16 * 3))

            # warn about invalid values
            for y in range(4):
                for x in range(3):
                    n = mtx[y][x]
                    if math.isnan(n) or math.isinf(n):
                        log.warning(
                            "Skeleton smooth mtx %d element [%d,%d] is %s", i,
                            x, y, n)

            # replace all invalid values with zeros
            flt = lambda e: \
                0 if (math.isnan(e) or math.isinf(e)) else e
            mtx = list(map(lambda row: list(map(flt, row)), mtx))
            #mtx[3][3] = 1 # debug
            mtx = Matrix(*mtx)

            # transpose
            #m = [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]]
            #for y in range(4):
            #    for x in range(4):
            #        m[x][y] = mtx[y][x]
            #mtx = m

            # log values to debug
            #log.debug("Inverse mtx %d:", i)
            #for y in range(4):
            #    log.debug("  %s", ' '.join(map(
            #        lambda v: '%+3.2f' % v, mtx[y])))
            self.smooth_mtxs.append(mtx)
Exemplo n.º 6
0
    def update3(self, dt):
        # print('game_time', id(self), self.game_time)
        self.game_time += dt
        draw = self.app.draw
        draw.draw_axis(Matrix(), 10.0)
        draw.draw_grid(10.0, 5, toy.coloring.LIGHT_GRAY)
        draw.draw_grid(1.0, 10, toy.coloring.GRAY)

        camera = self.app.camera
        p = camera.top_down_screen_to_world(
            Vector(camera.width / 2.0, camera.height / 2.0, 0.0))
        draw.draw_sphere(p, 1.0, toy.coloring.RED)

        p = camera.top_down_screen_to_world(Vector(0.0, 0.0, 0.0))
        draw.draw_sphere(p, 1.0, toy.coloring.GREEN)

        p = camera.top_down_screen_to_world(
            Vector(camera.width, camera.height, 0.0))
        draw.draw_sphere(p, 1.0, toy.coloring.BLUE)

        model_matrix = Transform(
            # Vector(5.0*math.cos(self.game_time), 0.0, 5.0*math.sin(self.game_time)),
            # Quaternion.from_euler_angles(Vector(self.game_time * 3.0, self.game_time, 0.0))
        ).to_matrix()

        sub_model_matrix = Transform(Vector(0.0, 2.0 * self.game_time,
                                            0.0)).to_matrix()

        draw = toy.draw.LocalDraw(self.app.batch, model_matrix)
        sub_draw = toy.draw.LocalDraw(self.app.batch,
                                      model_matrix * sub_model_matrix)
        sub_draw.draw_sphere(Vector(0.0, 0.0, 0.0), 5.0, toy.coloring.MAGENTA)

        draw.draw_point(Vector(0.0, 2.0, 0.0))
        draw.draw_line(Vector(-5.0, 1.0, 0.0), Vector(5.0, 1.0, 0.0))
        draw.draw_sphere(Vector(0.0, 3.0, 0.0), 2.0, toy.coloring.BLUE)
        transform = Transform(
            Vector(1.0, 2.0, 4.0),
            Quaternion.from_euler_angles(Vector(0.8, 0.2, 0.0)))
        matrix = transform.to_matrix()
        draw.draw_cone(matrix, 1.0, 2.0)
        draw.draw_axis(matrix, 1.0)
        draw.draw_cube(Vector(), 1.0)
        draw.draw_box(matrix, toy.coloring.GREEN)

        p0 = Vector(0.0, 100.0, 0.0)
        p1 = Vector(3.0, 6.0, 9.0)
        draw.draw_line(p0, p1, toy.coloring.CYAN)
        draw.draw_cylinder(p0, p1, 1.0)

        p0 = Vector(0.0, 10.0, 0.0)
        p1 = 30.0 * Vector(math.cos(self.game_time), math.sin(self.game_time),
                           1.0)
        draw.draw_cylinder(p0, p1, 3.0)
        draw.draw_sphere(p1, 3.0, toy.coloring.CYAN)

        points = []
        for i in range(50):
            r = i * 0.5
            a = r + self.game_time
            points.append(Vector(math.cos(a), r, math.sin(a)))
        draw.draw_polyline(points, toy.coloring.CYAN)

        text_batch = self.app.text_batch
        text_batch.draw_text(Vector(0.0, 0.0, 0.0), 'abcabfdsfdc')
        text_batch.draw_text(Vector(0.0, 20.0, 0.0), 'defdef',
                             toy.coloring.MAGENTA)
        text_batch.draw_text(Vector(0.0, 200.0, 0.0), 'ABCABC\nDEFDEF')
        text_batch.draw_text(Vector(0.0, 500.0, 0.0), '1234567890',
                             toy.coloring.BLUE, 2.0)
        view_eye = self.app.camera.view_eye
        view_at = self.app.camera.view_at
        view_info = 'eye {}, dir {}'.format(view_eye, view_at - view_eye)
        text_batch.draw_text(Vector(10.0, 400.0, 0.0), view_info,
                             toy.coloring.BLACK, 0.4)

        world_p = (model_matrix * sub_model_matrix).transform_point(
            Vector(0.0, 0.0, 0.0))
        # world_p = Vector()
        screen_p = camera.world_to_screen(world_p)

        text_batch.draw_text(screen_p, 'Lklm world{}'.format(world_p),
                             toy.coloring.BLACK, 0.5)

        model_matrix = Transform(
            Vector(),
            Quaternion.from_euler_angles(Vector(0.0, 0.0 * self.game_time,
                                                0.0)),
            Vector(1.0, 1.0, 1.0) * 1.0).to_matrix()