Example #1
0
 def shadow_matrix(eye):
     up = Vector(0, 1, 0).normalize()
     z = Vector(-eye.x, -eye.y, eye.z)
     x = up.cross(z).normalize()
     y = z.cross(x).normalize()
     minv = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
     for i in range(3):
         minv[0][i] = x[i]
         minv[1][i] = y[i]
         minv[2][i] = z[i]
     return lambda matrix: (matrix * Matrix(minv)).data[0]
Example #2
0
def GetRandomVector(minX, maxX, minY, maxY):
    return Vector(random.randint(minX, maxX), random.randint(minY, maxY))
Example #3
0
    def __init__(self, polygons, pixel_mode, eye_vector, light_vector, transform=None,
                 texture=None, normals_map=None, shadow=None):
        new_polygons = []
        xx, yy, zz = [], [], []
        self.transform = transform
        for polygon in polygons:
            new_polygon = []
            for v in polygon:
                x, y, z = transform(Matrix([v['xyz']])) if transform else v['xyz']
                new_polygon.append({
                    'xyz': (x, y, z),
                    'texture': v['texture'],
                    'normals': v['normals'],
                })
                xx.append(x)
                yy.append(y)
                zz.append(z)
            new_polygons.append(new_polygon)
        self.width = width = Scr_x
        self.min_x = min_x = min(xx)
        self.min_y = min_y = min(yy)
        self.min_z = min_z = min(zz)
        self.scale_x = scale_x = (width - 1) // (max(xx) - min(xx))
        self.scale_y = scale_y = (width - 1) // (max(xx) - min(xx))
        self.scale_z = scale_z = Scr_z // (max(zz) - min(zz))
        self.height = height = int((max(yy) - min(yy)) * scale_y) + 1
        self.pixels = [[0]] * width * (height + 1)
        self.normals_map = normals_map
        self.eye_vector = eye_vector
        self.light_vector = light_vector
        self.shadow = shadow

        if texture:
            self.texture_pixels = texture.load()
            self.texture_width = texture.width - 1
            self.texture_height = texture.height - 1
        if pixel_mode == 0:
            self.set_pixel_now = self.set_pixel0
        elif pixel_mode == 1:
            self.set_pixel_now = self.set_pixel1
        elif pixel_mode == 2:
            self.set_pixel_now = self.set_pixel2
        elif pixel_mode == 3:
            self.set_pixel_now = self.set_pixel3
        elif pixel_mode == 4:
            self.set_pixel_now = self.set_pixel4
        elif pixel_mode == 5:
            self.set_pixel_now = self.set_pixel5
        elif pixel_mode == 6:
            self.set_pixel_now = self.set_pixel6
        elif pixel_mode == 7:
            self.set_pixel_now = self.set_pixel7

        tr_num = 0
        triangles = self.triangles = []
        for polygon in new_polygons:
            vectors3 = []
            for v in polygon:
                x, y, z = v['xyz']
                vector = Vector(
                    round((x - min_x) * scale_x),
                    round((y - min_y) * scale_y),
                    round((z - min_z) * scale_z)
                )
                vector.u, vector.v = v['texture'] or (0, 0)
                x, y, z = v['normals']
                vector.light = int((x * light_vector.x + y * light_vector.y + z * light_vector.z) * 256)
                vectors3.append(vector)
            self.triangle(tr_num, *vectors3)
            for vector in vectors3:
                vector.x *= 2
                vector.y *= 2
            triangles.append(vectors3)
            tr_num += 1
Example #4
0
                    new_line.append(p)
                    new_line.append(p)
                layer2.extend(new_line)
                layer2.extend(new_line)
            view.width *= 2
            view.height *= 2
            view.scale_x *= 2
            view.scale_y *= 2
            view.pixels = [[0]] * view.width * (view.height + 1)
            for tr_num, vectors3 in enumerate(view.triangles):
                if tr_num in visible_triangles:
                    view.triangle(tr_num, *vectors3, multisampling=True)

        canvas = Canvas(canvas_width, canvas_height, view.pixels, multisampling=Multisampling)
        canvas.img.show()

dt = datetime.datetime.now()
face = Render(
    obj_file='face.obj',
    texture_file='african_head_diffuse.tga',
    normals_file='african_head_nm.png'
)
face.show(
    light_vector=Vector(1, 1, 1).normalize(),
    eye_vector=Vector(0, 0, 1).normalize(),
    x_rotate=-0.4*0,
    y_rotate=0.2*0,
    z_rotate=-0.2*0,
)
print datetime.datetime.now() - dt