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'
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')