Beispiel #1
0
def sphinput():
    lx, ly, lz = tuple(input('light position x,y,z: '))
    vz = int(input('viewer position z: '))
    vx = vy = 250
    Ia = tuple(input('ambient color r,g,b: '))
    Id = tuple(input('light color r,g,b: '))
    Is = tuple(input('spectral color r,g,b: '))
    Ka = Kd = tuple(input('ball color r,g,b: '))
    Ks = 255, 255, 255
    a = int(input('shininess a: '))
    n = 1. / float(input('sphere steps: '))
    r = float(input('radius: '))
    cx, cy, cz = tuple(input('center position x,y,z: '))
    tris = transform.T(cx, cy, cz) * edgeMtx.sphere(r, n)
    zbuf = [[None] * 500 for i in xrange(500)]
    zbuf[250][250] = 1
    print zbuf[251][250]
    img = Image(500,500)
    triList = []
    for i in range(0, len(tris[0]) - 2, 3):
        triList.append(tuple(tris[0][i : i + 3] + tris[1][i : i + 3] + tris[2][i : i + 3]))
    triList.sort(key=lambda t: -sum(t[6:9]))
    print 'sorted lis'
    for x1, x2, x3, y1, y2, y3, z1, z2, z3 in triList:
        nx1, ny1, nz1 = normalize(x1 - cx, y1 - cy, z1 - cz)
        nx2, ny2, nz2 = normalize(x2 - cx, y2 - cy, z2 - cz)
        nx3, ny3, nz3 = normalize(x3 - cx, y3 - cy, z3 - cz)
        shadePix = drawShadedTri(x1,y1,z1,x2,y2,z2,x3,y3,z3,nx1,ny1,nz1,nx2,ny2,nz2,nx3,ny3,nz3,lx,ly,lz,vx,vy,vz,Ia,Id,Is,Ka,Kd,Ks,a,zbuf)
        img.setPixels(shadePix)
    img.display()
    img.saveAs('sph.png')
Beispiel #2
0
def sphereshade():
    lx, ly, lz = 700,100,0
    vx, vy = 250, 250
    Ia = (100, 100, 100)
    Id = (255, 0, 0)
    Is = (255, 150, 150)
    Ks = (128, 128, 128)

    zbuf = [[None for _ in xrange(500)] for _ in ge(500)]
    tris = transform.T(250, 250, 0) * edgeMtx.sphere(200, .02)
    sts = edgeMtx.edgemtx()
    edgeMtx.addCircle(sts,0,0,0,500,.05)
    sts = transform.T(250,250,0)*transform.R('y', -45)*transform.R('x', 90)*sts
    sts = zip(*sts)[::2]
    ke=0
    for lx,ly,lz,_ in sts:
        img = Image(500,500)
        triList = []
        for i in range(0, len(tris[0]) - 2, 3):
            triList.append(tuple(tris[0][i : i + 3] + tris[1][i : i + 3] + tris[2][i : i + 3]))
        triList.sort(key=lambda t: sum(t[6:9]))
        print 'sorted lis'
        for x1, x2, x3, y1, y2, y3, z1, z2, z3 in triList:
            nx1, ny1, nz1 = normalize(x1 - 250, y1 - 250, z1)
            nx2, ny2, nz2 = normalize(x2 - 250, y2 - 250, z2)
            nx3, ny3, nz3 = normalize(x3 - 250, y3 - 250, z3)
            shadePix = drawShadedTri(x1,y1,z1,x2,y2,z2,x3,y3,z3,nx1,ny1,nz1,nx2,ny2,nz2,nx3,ny3,nz3,lx,ly,lz,vx,vy,vz,Ia,Id,Is,Ka,Kd,Ks,a,zbuf)
            img.setPixels(shadePix)
        img.savePpm('shade/%d.ppm'%(ke))
        if ke == 0: img.display()
        ke+=1
        print ke
Beispiel #3
0
def marioTest():
    from time import time
    tc = {}
    chdir('mario')
    triset = obj.parse('mario.obj','mario.mtl')
    mat = transform.T(250, 400, 0) * transform.R('z', 180) * transform.S(1.5,1.5,1.5)
    for i in range(len(triset)):
        triset[i][0] = mat * triset[i][0]
    img = Image(500,500)
    mat = transform.T(250,400,0)*transform.R('y',5)*transform.T(-250,-400,0)
    textureTriMtxs(triset,img,tc)
    print len(tc)
    img.display()
    for i in range(72):
        print 'making image...',
        a = time()
        img = Image(500,500)
        print (time() - a) * 1000, 'ms'
        print 'transforming...',
        a = time()
        for j in range(len(triset)):
            triset[j][0] = mat * triset[j][0]
        print (time() - a) * 1000, 'ms'
        print 'texturing...',
        a = time()
        textureTriMtxs(triset, img,tc)
        print (time() - a) * 1000, 'ms'
        print 'saving...',
        a = time()
        img.savePpm('../animar/%d.ppm'%(i))
        print (time() - a) * 1000, 'ms'
        print i, 'drawn'
Beispiel #4
0
def triangleTest1():
    tris = edgemtx()
    img = Image(500, 500)
    for x in range(0, 500, 50):
        for y in range(0, 500, 50):
            addTriangle(tris, x, y, 0, x + 25, y, 0, 250, 250, 250)
    drawTriangles(tris, img)
    img.display()
Beispiel #5
0
def circleTest1():
    import math
    m = edgemtx()
    addEdgesFromParam(m, lambda t: 250 + 100 * math.cos(t * 2 * math.pi),
                      lambda t: 250 + 100 * math.sin(t * 2 * math.pi),
                      lambda t: 0, 0.01)
    img = Image(500, 500)
    drawEdges(m, img)
    img.display()
Beispiel #6
0
def mtxTest1():
    m1 = [[2, 2, 3], [3, 2, 2]]
    m2 = [[1, 5], [6.5, 4], [1, -0.7]]
    m3 = [[1, 2, 3, 1], [5, 2, -1, 3], [-1, -5, 3, 6], [2, 4, -7, 2]]
    k1 = 2.5
    k2 = 3.5
    id3 = matrix.id(3)
    id2 = matrix.id(2)
    print 'identity 3x3'
    print matrix.toStr(id3)
    print 'identity 2x2'
    print matrix.toStr(id2)
    print 'm1 2x3'
    print matrix.toStr(m1)
    print 'sanity checks: m1 * id3 = m1, id2 * m1 = m1'
    m1again = matrix.multiply(m1, id3)
    m1evenmore = matrix.multiply(id2, m1)
    print matrix.toStr(m1again)
    print matrix.toStr(m1evenmore)
    print 'testing size mismatch id3 * m1:'
    try:
        matrix.multiply(id3, m1)
    except ArithmeticError:
        print 'it errored, that\'s good'
    print 'm2 3x2'
    print matrix.toStr(m2)
    m12 = matrix.multiply(m1, m2)
    print 'm1 * m2, should be a 2x2'
    print matrix.toStr(m12)
    m21 = matrix.multiply(m2, m1)
    print 'm2 * m1, should be a 3x3'
    print matrix.toStr(m21)
    print '10 * (m2 * m1)'
    print matrix.toStr(matrix.multiply(10, m21))
    print '(m2 * m1) * 10'
    print matrix.toStr(matrix.multiply(m21, 10))
    print '10 * 10'
    print matrix.multiply(10, 10)
    print 'Adding edge (1, 1, 1), (2, 3, 2.5)'
    m = edgemtx()
    addEdge(m, 1, 1, 1, 2, 3, 2.5)
    print matrix.toStr(m)
    print 'm3'
    print matrix.toStr(m3)
    print 'Transforming edge matrix'
    print matrix.toStr(matrix.multiply(m3, m))

    img = Image(500, 500)
    for loc in range(0, 500, 4):
        edges = edgemtx()
        addEdge(edges, 125, loc, 100, loc + 1, 375, 100)
        addEdge(edges, loc + 1, 375, 100, 375, 500 - loc - 2, 100)
        addEdge(edges, 375, 500 - loc - 2, 100, 500 - loc - 3, 125, 100)
        addEdge(edges, 500 - loc - 3, 125, 100, 125, loc + 4, 100)
        drawEdges(edges, img, (255 - loc / 2, loc / 2, 127))  # crossfade r + g
    img.display()
Beispiel #7
0
def shadetest():
    x1, y1, z1 = 100, 100, 200
    x2, y2, z2 = 300, 150, 0
    x3, y3, z3 = 150, 300, 0
    nx1, ny1, nz1 = normalize(x1, y1, z1)
    nx2, ny2, nz2 = normalize(x2, y2, z2)
    nx3, ny3, nz3 = normalize(x3, x3, z3)
    lx, ly, lz = 300, 300, 300
    col = (255, 150, 30)
    Ia = (255,200,150)
    Id = (255,200, 150)
    Is = (255,200,150)
    Ka = (0,200,100)
    Kd = (0,200,100)
    Ks = (255,255,255)
    a = 0.5
    img = Image(500, 500)
    shadePix = drawShadedTri(x1,y1,z1,x2,y2,z2,x3,y3,z3,nx1,ny1,nz1,nx2,ny2,nz2,nx3,ny3,nz3,lx,ly,lz,Ia,Id,Is,Ka,Kd,Ks,a)
    print shadePix
    img.setPixels(shadePix)
    img.display()
Beispiel #8
0
    print 'm1 * m2, should be a 2x2'
    print matrix.toStr(m12)
    m21 = matrix.multiply(m2, m1)
    print 'm2 * m1, should be a 3x3'
    print matrix.toStr(m21)
    print '10 * (m2 * m1)'
    print matrix.toStr(matrix.multiply(10, m21))
    print '(m2 * m1) * 10'
    print matrix.toStr(matrix.multiply(m21, 10))
    print '10 * 10'
    print matrix.multiply(10, 10)
    print 'Adding edge (1, 1, 1), (2, 3, 2.5)'
    m = edgemtx()
    addEdge(m, 1, 1, 1, 2, 3, 2.5)
    print matrix.toStr(m)
    print 'm3'
    print matrix.toStr(m3)
    print 'Transforming edge matrix'
    print matrix.toStr(matrix.multiply(m3, m))

    img = Image(500, 500)
    for loc in range(0, 500, 4):
        edges = edgemtx()
        addEdge(edges, 125, loc, 100, loc + 1, 375, 100)
        addEdge(edges, loc + 1, 375, 100, 375, 500 - loc - 2, 100)
        addEdge(edges, 375, 500 - loc - 2, 100, 500 - loc - 3, 125, 100)
        addEdge(edges, 500 - loc - 3, 125, 100, 125, loc + 4, 100)
        drawEdges(edges, img, (255 - loc / 2, loc / 2, 127))  # crossfade r + g

    img.display()
Beispiel #9
0
def marioshadetest():
    img = Image(500, 500)
    # TODO implement lights, texcache, zbuf
    lights = [Light(409.1, 409.1, 0, (30, 10, 10), (200, 50, 50), (255, 150, 150)), 
        Light(25, 250, 50, (5, 30, 10), (50, 200, 50), (150, 255, 150)),
        Light(250, 25, 100, (10, 20, 30), (50, 50, 200), (150, 150, 255))]
    fov = 90
    cam = Camera(250, 250, 200, 0, 0, 0, -250,-250, 1 / math.tan(fov / 2.))
    camT = transform.T(cam.x,cam.y,cam.z)*transform.C2(cam, 500, -500)
    print matrix.toStr(camT)
    lballs = []
    sphere = edgeMtx.sphere(20, .1)
    for l in lights:
        lightball = transform.T(l.x, l.y, l.z) * sphere
        lballs.append([lightball, l.Id])
    texcache = {}
    chdir('mario')
    tris = obj.parse('mario.obj','mario.mtl')
    mrot = transform.R('z', 180)*transform.R('y', 180)
    m = transform.T(250,380,0)*transform.S(1.2, 1.2, 1.2)*mrot
    apply(m, tris)
    applyNorms(mrot, tris)
    # ROTATE MARIO
    # mrot = transform.R('y', 5)
    # m = transform.T(250, 380, 0) * mrot * transform.T(-250, -380, 0)
    # ROTATE LIGHTS
    m = transform.T(250, 250, 0) * transform.R('z', 5) * transform.T(-250, -250, 0)
    for i in range(72):
        a = time()
        zbuf = [[None]*500 for j in xrange(500)]
        img = Image(500, 500)
        for ball, col in lballs:
            edgeMtx.drawTriangles(ball, img, col, col, False)
        tricam = applied(camT, tris)
        tricam.sort(key=lambda tri: -tri[0].z - tri[1].z - tri[2].z)
        for tri in tricam:
            #for j in xrange(3):
            #    pt = tri[j]
            #    pt.x += cam.x
            #    pt.y += cam.y
            #    pt.z += cam.z
            img.setPixels(renderTriangle(*tri + [cam.vx, cam.vy, cam.vz, lights, texcache, zbuf]))
        if i == 0:
            img.display()
            img.saveAs('proj.png')
        img.savePpm('../marshade/%d.ppm' % (i))
        # ROTATE MARIO
        # apply(m, tris)
        # applyNorms(mrot, tris)
        # ROTATE LIGHTS
        for ball in lballs:
            ball[0] = m * ball[0]
        for l in lights:
            x = dot4xyz(m[0], l.x, l.y, l.z)
            y = dot4xyz(m[1], l.x, l.y, l.z)
            z = dot4xyz(m[2], l.x, l.y, l.z)
            l.x = x
            l.y = y
            l.z = z
        print i, 'in', (time() - a) * 1000, 'ms'
    chdir('..')
    img.display()
    img.saveAs('marshade.png')