Exemple #1
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
Exemple #2
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'
Exemple #3
0
def textureTest():
    help(Reader)
    r = Reader(file=open('tesx.png'))
    rgb = list(r.asRGBA()[2])
    print len(rgb),len(rgb[0])
    img = Image(500,500)
    drawTexturedTri(150,150,300,100,100,300,1,0,0,1,1,1,rgb,(255,255,0),img)
    img.savePpm('t.ppm')
Exemple #4
0
    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()


def circleTest2():
    m = edgemtx()
    for theta in range(36):
        costheta = math.cos(theta)

        def fx(t):
            return 250 + 100 * math.cos(t * 2 * math.pi) * costheta


if __name__ == '__main__':
    import transform

    m = transform.T(250, 250, 0) * transform.R('x', 30) * transform.R(
        'y', 30) * sphere(200, .05)
    mat = transform.T(250, 250, 0) * transform.R('y', 5) * transform.T(
        -250, -250, 0)
    for i in range(72):
        img = Image(500, 500)
        drawTriangles(m, img)
        m = mat * m
        print i, 'iter'
        img.savePpm('sphere/%d.ppm' % (i))
Exemple #5
0
def camtest():
    import shape
    fov = 100
    cam = Camera(0.5,0.5,0.8,0,0,0,0,0,1 / math.tan(fov / 2.))
    camargs = [100,100,-50,-300]
    camT = transform.C3(*camargs)*transform.T(-250, -250,-175)
    print camT
    ncamT = transform.C3invT(*camargs)
    print ncamT
    v = [250,250,1000]
    lights = [Light(500,0,500,(20,20,20),(200,200,200),(255,255,255)),
              Light(500,500,200,(20,20,20),(200,200,200),(255,255,255)),
              Light(0,250,500,(20,20,20),(200,200,200),(255,255,255))
    ]
    camlights = []
    for l in lights:
        x = dot4xyz(camT[0], l.x, l.y, l.z)
        y = dot4xyz(camT[1], l.x, l.y, l.z)
        z = dot4xyz(camT[2], l.x, l.y, l.z)
        w = dot4xyz(camT[3], l.x, l.y, l.z)*1.
        print x/w*250,y/w*250,z/w*250
        camlights.append(Light(x/w*250, y/w*250, z/w*250,l.Ia,l.Id,l.Is))
    tris, norms = shape.box(200,200,-100,100,100,200)
    print norms
    print ncamT * norms
    print list(triIter(tris))
    trot = transform.R('y',5)
    nrot = transform.TransMatrix()
    nrot.lst = matrix.transpose(transform.R('y',-5))
    tmat = transform.T(250,250,0)*trot*transform.T(-250,-250,0)
    tris = tmat*tmat*tmat*tris
    norms= trot*trot*trot*norms
    print norms
    print ncamT*norms
    amb = Texture(False, [255,0,0])
    diff = Texture(False, [255,0,0])
    spec = Texture(False, [255,150,150])
    mat = Material(amb, diff, spec, 10)
    for i in range(72):
        #print tris
        tricam = camT * tris
        #print tricam
        #tricam[3] = [1.] * len(tricam[3])
        #tricam = transform.T(*v) * tricam
        print 'trans done'
        a = time()
        zbuf = [[None]*500 for _ in range(500)]
        img = Image(500,500)
        trit = list(triIter(tricam))
        #print trit,tricam
        trit.sort(key=lambda tri: -tri[0][2] - tri[1][2] - tri[2][2])
        normcam = ncamT*norms
        normt = []
        for j in range(len(normcam[0])):
            sgn = (normcam[3][j] > 0) * 2 - 1
            normt.append(normalize(normcam[0][j]*sgn, normcam[1][j]*sgn, normcam[2][j]*sgn))
        print normt
        #print len(trit), len(normt)
        for j in range(len(trit)):
            # (p1, p2, p3, mat, vx, vy, vz, lights, texcache, zbuf):
            t = trit[j]
            ps = []
            for pt in t:
                pt[0]+=250
                pt[1]+=250
                pt[2]+=0
                print pt
                ps.append(Point(*pt + normt[j] + [0,0]))
            img.setPixels(renderTriangle(*ps + [mat] + v + [camlights, {}, zbuf]))
        for t in trit:
            l = line(*t[0][:2]+t[1][:2])
            l += line(*t[0][:2]+t[2][:2])
            l += line(*t[2][:2]+t[1][:2])
            img.setPixels([p + ((0,0,0),) for p in l])

        for j in range(len(trit)):
            t = trit[j]
            ps = []
            for pt in t:
                nls = line(pt[0] - 4, pt[1], pt[0] + 4, pt[1])
                nls += line(pt[0], pt[1] - 4, pt[0], pt[1] + 4)
                nls += line(pt[0] - 4, pt[1] - 4, pt[0] + 4, pt[1] + 4)
                nls += line(pt[0] - 4, pt[1] + 4, pt[0] + 4, pt[1] - 4)
                nls += line(pt[0], pt[1], pt[0] + normt[j][0]*20, pt[1] + normt[j][1]*20)
                print normt[j][0], normt[j][1]
                img.setPixels([p + ((0,255,0),) for p in nls])
        
        img.savePpm('cube/%d.ppm'%(i))
        tris = tmat * tris
        norms = nrot * norms
        print norms
        print i, (time() - a) * 1000, 'ms'
Exemple #6
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')