示例#1
0
Viewport = viewport(width/8, height/8,
                    width * 3/4, height * 3/4,
                    depth)
ModelView = lookat(eye, center, up)

ViewProjection = np.dot(np.dot(Viewport, Projection), ModelView)


def map_to_screen(v):
    v = np.dot(ViewProjection, np.append(v, 1))
    return v[:3] / v[3]


if __name__ == "__main__":
    timestart = time.perf_counter()
    image = utils.createImage(width, height, 3, np.uint8)
    m = Model("obj/african_head.obj")

    texture = utils.readImage("obj/african_head_diffuse.tga")
    texture = np.flipud(texture)
    texture = texture.transpose(1, 0, 2)
    print("texture.shape = {}, dtype = {}".format(texture.shape, texture.dtype))

    timeend = time.perf_counter()
    print("Read model :: ", (timeend - timestart), "s")

    light_dir = np.array([0, 0, -1])

    timestart = time.perf_counter()
    z_buffer = np.zeros((width, height))
    for f in m.faces:
示例#2
0
import utils
from gui import MainWindow
import time

width = 800
height = 800
padding = 10


def map_to_screen(v):
    return [(v[0] + 1) * (width - padding) / 2, (v[1] + 1) * (height - padding)/2, v[2] * 255]


if __name__ == "__main__":
    timestart = time.perf_counter()
    image = utils.createImage(width, height)
    m = Model("obj/african_head.obj")
    timeend = time.perf_counter()
    print("Read model :: ", (timeend - timestart), "s")

    light_dir = np.array([0, 0, -1])

    timestart = time.perf_counter()
    for i, f in enumerate(m.faces):
        v = np.array([map_to_screen(m.vertices[f[i][0]]) for i in range(3)])

        normal = np.cross(v[2] - v[0], v[1] - v[0])
        normal /= np.linalg.norm(normal)
        intensity = abs(np.dot(normal, light_dir))

        # triangle_line_sweep(v, image, utils.WHITE * intensity)
示例#3
0
from minigl import *
import utils

image = utils.createImage(800, 800)

pixel((5, 5), image, (1, 1, 1))

aligned_rectangle((20, 20), (400, 40), image, (1, 1, 0))

line((20, 25), (60, 60), image, (1, 0, 1))
line((20, 25), (60, 100), image, (0, 1, 1))
line((20, 25), (100, 60), image, (0, 0, 1))
line((100, 25), (20, 60), image, (0, 0, 1))

utils.saveImage("out/1_primitives.jpg", image)

# Versions of line renderer.


def line_v1(a, b, image, color):
    # First attempt
    a = np.asarray(a)
    b = np.asarray(b)
    d = b - a
    for t in np.arange(0, 1.01, 0.01):
        p = (a + d * t).astype(int)
        pixel(p, image, color)


def line_v2(a, b, image, color):
    # Second  attempt