示例#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')
示例#2
0
文件: cool.py 项目: marksg07/gfx-hw2
from line import line
from base import Image

img = Image(500, 500)
y = lambda x: 500 * (x / 250. - 1)**2
dy = lambda x: 4 * (x / 250. - 1)
xs = range(0, 501, 4)
ys = [y(x) for x in xs]
xys = zip(xs, ys)
points = []
for x, y in xys:
    slope = dy(x)
    y0 = int(-slope * x + y)
    y500 = int(slope * (500 - x) + y)
    pts = line(0, y0, 500, y500)
    pts = filter(lambda tup: 500 > tup[0] >= 0 and 500 > tup[1] >= 0, pts)
    coloredpoints = [(px, py, (x / 2, y / 2, (x + y) / 4)) for px, py in pts]
    points.extend(coloredpoints)
img.setPixels(points)
img.saveAs('cool.png')
img.display()
示例#3
0
文件: shape.py 项目: marksg07/shapes
            norm[3 - i - j] = dir
            addEdge(n, *norm * 2)
            ak[3 - i - j] += dim[3 - i - j]
    return m, n


if __name__ == '__main__':
    from base import Image
    import transform
    cube = edgemtx()
    addBoxPoints(cube, 125, 125, -125, 250, 250, 250)
    m = edgemtx()
    xTrans = transform.T(250, 250, 0) * transform.R('x', 18) * transform.T(
        -250, -250, 0)
    yTrans = transform.T(250, 250, 0) * transform.R('y', 18) * transform.T(
        -250, -250, 0)
    for d in 'xyz':
        trans = transform.T(250, 250, 0) * transform.R(d, 15) * transform.T(
            -250, -250, 0)
        for i in range(24):
            addToEdgeMtx(m, cube)
            cube = trans * cube
        cube = m
        m = edgemtx()
    cube = transform.T(250, 250, 0) * transform.R('y', 49) * transform.R(
        'x', 67) * transform.R('z', 23) * transform.T(-250, -250, 0) * cube
    img = Image(500, 500)
    drawEdges(cube, img)
    img.display()
    img.saveAs('badsphere.png')
示例#4
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')