for i in range(0, len(m[0]) - 1, 2): lin = line(m[0][i], m[1][i], m[0][i + 1], m[1][i + 1]) coloredlin = [xy + (color, ) for xy in lin] image.setPixels(coloredlin) if __name__ == '__main__': 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'
def __str__(self): return matrix.toStr(self.lst)
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()
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')