Пример #1
0
def box(x, y, z, w, h, d):
    p = (x, y, z)
    dim = (w, h, d)
    m = edgemtx()
    n = edgemtx()
    for i in xrange(3):
        j = (i + 1) % 3
        ai = [0, 0, 0]
        aj = [0, 0, 0]
        ak = [0, 0, 0]
        ai[i] = dim[i]
        aj[j] = dim[j]
        for k in xrange(2):
            addTriangle(m, x + ak[0], y + ak[1], z + ak[2], x + ai[0] + ak[0],
                        y + ai[1] + ak[1], z + ai[2] + ak[2],
                        x + aj[0] + ak[0], y + aj[1] + ak[1],
                        z + aj[2] + ak[2])
            addTriangle(m, x + ai[0] + ak[0], y + ai[1] + ak[1],
                        z + ai[2] + ak[2], x + aj[0] + ak[0],
                        y + aj[1] + ak[1], z + aj[2] + ak[2],
                        x + ai[0] + aj[0] + ak[0], y + ai[1] + aj[1] + ak[1],
                        z + ai[2] + aj[2] + ak[2])
            norm = [0, 0, 0]
            dir = 1 - k * 2
            norm[3 - i - j] = dir
            addEdge(n, *norm * 2)
            ak[3 - i - j] += dim[3 - i - j]
    return m, n
Пример #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
Пример #3
0
def addTorusPoints(m, x, y, z, r, R, mainStep=0.02, ringStep=0.05):
    steps = int(math.ceil(1 / mainStep))
    mCircle = edgemtx()
    addPoint(mCircle, r, 0, 0)
    addCircle(mCircle, 0, 0, 0, r, ringStep)
    addPoint(mCircle, mCircle[0][-1], mCircle[1][-1], 0)
    for theta in range(steps):
        xOuter = x + R * math.cos(theta * 2 * math.pi / steps)
        zOuter = z + R * math.sin(theta * 2 * math.pi / steps)
        movedCircle = transform.T(xOuter, y, zOuter) * transform.R(
            'y', -theta * 360. / steps) * mCircle
        addToEdgeMtx(m, movedCircle)
    return m
Пример #4
0
def runFrame(frame, commands):
    step = 0.02
    cstack = [TransMatrix()]
    img = Image(500, 500)
    objects = []
    for command in commands:
        inp = command[0]
        args = command[1:]
        if inp == 'line':
            edges = edgemtx()
            addEdge(edges, *command[1:7])
            edges = cstack[-1] * edges
            objects.append((EDGE, edges))
            #drawEdges(cstack[-1] * edges, img)
        elif inp == 'ident':
            cstack[-1] = TransMatrix()
        elif inp == 'scale':
            kval = frame[args[3]]
            cstack[-1] *= transform.S(*args[:3]) * transform.S(
                kval, kval, kval)  # scaling a scale with scale
        elif inp == 'move':
            kval = frame[args[3]]
            cstack[-1] *= transform.T(*[i * kval for i in args[:3]])
        elif inp == 'rotate':
            kval = frame[args[2]]
            cstack[-1] *= transform.R(args[0], args[1] * kval)
        elif inp == 'circle':
            edges = edgemtx()
            addCircle(*(edges, ) + command[1:5] + (.01, ))
            edges = cstack[-1] * edges
            objects.append((EDGE, edges))
            #drawEdges(cstack[-1] * edges, img)
        elif inp == 'bezier':
            edges = edgemtx()
            addBezier(*(edges, ) + command[1:9] + (.01, ))
            edges = cstack[-1] * edges
            objects.append((EDGE, edges))
            #drawEdges(cstack[-1] * edges, img)
        elif inp == 'hermite':
            edges = edgemtx()
            addHermite(*(edges, ) + command[1:9] + (.01, ))
            edges = cstack[-1] * edges
            objects.append((EDGE, edges))
            #drawEdges(cstack[-1] * edges, img)
        elif inp == 'clearstack':
            cstack = [TransMatrix()]
        elif inp == 'box':
            vxs = cstack[-1] * shape.genBoxPoints(*command[1:7])
            tris = shape.genBoxTris()
            shape.fixOverlaps(vxs, tris)
            objects.append((POLY, flatTrisFromVT(vxs, tris)))
            #polys = edgemtx()
            #shape.addBox(*(polys,) + command[1:7])
            #polys = cstack[-1] * polys
            #objects.append((POLY, polys))
            #drawTriangles(cstack[-1] * polys, img, wireframe=True)
        elif inp == 'sphere':
            vxs = cstack[-1] * shape.genSpherePoints(*command[1:5] + (step, ))
            tris = shape.genSphereTris(step)
            shape.fixOverlaps(vxs, tris)
            objects.append((POLY, autoTrianglesFromVT(vxs, tris)))
            #polys = edgemtx()
            #shape.addSphere(*(polys,) + command[1:5] + (.05,))
            #polys = cstack[-1] * polys
            #objects.append((POLY, polys))
            #drawTriangles(cstack[-1] * polys, img, wireframe=True)
        elif inp == 'torus':
            vxs = cstack[-1] * shape.genTorusPoints(*command[1:6] +
                                                    (step, step))
            tris = shape.genTorusTris(step, step)
            shape.fixOverlaps(vxs, tris)
            objects.append((POLY, autoTrianglesFromVT(vxs, tris)))
            #polys = edgemtx()
            #shape.addTorus(*(polys,) + command[1:6] + (.05, .05))
            #polys = cstack[-1] * polys
            #objects.append((POLY, polys))
            #drawTriangles(cstack[-1] * polys, img, wireframe=True)
        elif inp == 'push':
            cstack.append(cstack[-1].clone())
        elif inp == 'pop':
            cstack.pop()
    return objects
Пример #5
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = TransMatrix()

    p = mdl.parseFile(filename)
    #print p
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    frames = None
    basename = 'anim'
    varying = False
    # pass 1
    for command in commands:
        cmd = command[0]
        args = command[1:]
        if cmd == 'frames':
            frames = args[0]
        elif cmd == 'basename':
            basename = args[0]
        elif cmd == 'vary':
            varying = True

    if varying:
        if frames is None:
            err('Frames not set.')
        if basename == 'anim':
            warn('Basename not set, using default of anim.')
        # pass 2
        upd = lambda d, n: 0 if d.update(n) else d
        frameList = [
            upd({k: v[1]
                 for k, v in symbols.iteritems() if v[0] == 'knob'}, {None: 1})
            for _ in range(frames)
        ]
        for command in commands:
            cmd = command[0]
            args = command[1:]
            if cmd == 'set':
                for frame in frameList:
                    frame[args[0]] = args[1]
            elif cmd == 'setall':
                for frame in frameList:
                    for key in frame.keys():
                        frame[key] = args[0]
            elif cmd == 'vary':
                val = args[3]
                inc = (1. * args[4] - args[3]) / (args[2] - args[1])
                for frid in range(args[1], args[2] + 1):
                    frameList[frid][args[0]] = val
                    val += inc
        imgs = []
        # pass for each frame
        print 'Pass 2 complete, beginning image rendering...'
        a = time.time()
        for frame in frameList:
            objects = runFrame(frame, commands)
            img = Image(500, 500)
            draw(objects, img)
            imgs.append(img)
        print 'Images rendered in %f ms' % (int(
            (time.time() - a) * 1000000) / 1000.)
        print 'Saving images...'
        a = time.time()
        for i in range(len(imgs)):
            imgs[i].savePpm('anim/%s%03d.ppm' % (basename, i))
        print 'Images saved in %f ms' % (int(
            (time.time() - a) * 1000000) / 1000.)
        print 'Creating animation... (converting to gif)'
        a = time.time()
        makeAnimation(basename, 'ppm')
        print 'Animation created in %f ms' % (int(
            (time.time() - a) * 1000000) / 1000.)
        # clearAnim()

    else:
        cstack = [TransMatrix()]
        frc = 0
        img = Image(500, 500)
        objects = []
        for command in commands:
            inp = command[0]
            if inp == 'line':
                edges = edgemtx()
                addEdge(edges, *command[1:7])
                edges = cstack[-1] * edges
                objects.append((EDGE, edges))
                #drawEdges(cstack[-1] * edges, img)
            elif inp == 'ident':
                cstack[-1] = TransMatrix()
            elif inp == 'scale':
                cstack[-1] *= transform.S(*command[1:4])
            elif inp == 'move':
                cstack[-1] *= transform.T(*command[1:4])
            elif inp == 'rotate':
                cstack[-1] *= transform.R(*command[1:3])
            elif inp == 'display':
                drawObjects(objects, img)
                img.flipUD().display()
            elif inp == 'save':
                drawObjects(objects, img)
                if inp[-4:] == '.ppm':
                    img.flipUD().savePpm(command[1])
                else:
                    img.flipUD().saveAs(command[1])
            elif inp == 'saveframe':
                drawObjects(objects, img)
                img.flipUD().savePpm('%s%d.ppm' % (command[1], frc))
                frc += 1
            elif inp == 'circle':
                edges = edgemtx()
                addCircle(*(edges, ) + command[1:5] + (.01, ))
                edges = cstack[-1] * edges
                objects.append((EDGE, edges))
                #drawEdges(cstack[-1] * edges, img)
            elif inp == 'bezier':
                edges = edgemtx()
                addBezier(*(edges, ) + command[1:9] + (.01, ))
                edges = cstack[-1] * edges
                objects.append((EDGE, edges))
                #drawEdges(cstack[-1] * edges, img)
            elif inp == 'hermite':
                edges = edgemtx()
                addHermite(*(edges, ) + command[1:9] + (.01, ))
                edges = cstack[-1] * edges
                objects.append((EDGE, edges))
                #drawEdges(cstack[-1] * edges, img)
            elif inp == 'clear':
                img = Image(500, 500)
            elif inp == 'clearstack':
                cstack = [TransMatrix()]
            elif inp == 'box':
                polys = edgemtx()
                shape.addBox(*(polys, ) + command[1:7])
                polys = cstack[-1] * polys
                objects.append((POLY, polys))
                #drawTriangles(cstack[-1] * polys, img, wireframe=True)
            elif inp == 'sphere':
                polys = edgemtx()
                shape.addSphere(*(polys, ) + command[1:5] + (.05, ))
                polys = cstack[-1] * polys
                objects.append((POLY, polys))
                #drawTriangles(cstack[-1] * polys, img, wireframe=True)
            elif inp == 'torus':
                polys = edgemtx()
                shape.addTorus(*(polys, ) + command[1:6] + (.05, .05))
                polys = cstack[-1] * polys
                objects.append((POLY, polys))
                #drawTriangles(cstack[-1] * polys, img, wireframe=True)
            elif inp == 'push':
                cstack.append(cstack[-1].clone())
            elif inp == 'pop':
                cstack.pop()
Пример #6
0
r/n 0   ?
0 t/n   ?
0   0   ?   
0   0   ?   ?
'''


def iparse(inp):
    return [float(i.strip()) for i in inp.split(' ')]


if __name__ == '__main__':  # parser
    from edgeMtx import edgemtx, addEdge, drawEdges, addBezier, addHermite, addCircle
    from base import Image
    import shape
    edges = edgemtx()
    trans = TransMatrix()
    frc = 0
    cam = Camera(250, 250, 700, 90, 0, 0, -250, -250, 250)
    while (True):
        try:
            inp = raw_input('')
        except EOFError:  # script file
            break
        if inp == 'line':
            inp = raw_input('')
            addEdge(edges, *iparse(inp))
        elif inp == 'ident':
            trans = TransMatrix()
        elif inp == 'scale':
            inp = raw_input('')
Пример #7
0
                        z + ai[2] + ak[2], x + aj[0] + ak[0],
                        y + aj[1] + ak[1], z + aj[2] + ak[2],
                        x + ai[0] + aj[0] + ak[0], y + ai[1] + aj[1] + ak[1],
                        z + ai[2] + aj[2] + ak[2])
            norm = [0, 0, 0]
            dir = 1 - k * 2
            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(
Пример #8
0
 from render import drawObjectsNicely
 import render
 import shape
 cstack = [TransMatrix()]
 frc = 0
 cam = Camera(250, 250, 700, 90, 0, 0, -250, -250, 250)
 img = Image(500, 500)
 objects = []
 while (True):
     try:
         inp = raw_input('').strip()
     except EOFError:  # script file
         break
     if inp == 'line':
         inp = raw_input('')
         edges = edgemtx()
         addEdge(edges, *iparse(inp))
         edges = cstack[-1] * edges
         objects.append((EDGE, edges))
         #drawEdges(cstack[-1] * edges, img)
     elif inp == 'ident':
         cstack[-1] = TransMatrix()
     elif inp == 'scale':
         inp = raw_input('')
         cstack[-1] *= S(*iparse(inp))
     elif inp == 'move':
         inp = raw_input('')
         cstack[-1] *= T(*iparse(inp))
     elif inp == 'rotate':
         inp = raw_input('')
         axis, t = (i.strip() for i in inp.split(' '))