class Viewport(Entity): def __init__(self, *args): """Create a viewport entity that displays contents of a camera inside the world. Arguments are either (Camera, int, int) for a new camera or (Camera) for an existing one """ Entity.__init__(self) if len(args) == 1: self.camera = args[0] elif len(args) == 3: scene = args[0] width = args[1] height = args[2] self.camera = Camera(scene, width, height) else: raise TypeError, "Viewport's constructor takes either 4 or 2 arguments, %i given" % len(args)+1 width = property(lambda self: self.camera.width) height = property(lambda self: self.camera.height) def _render(self, camera, offset): #TODO: Recursion if camera == self.camera: rect = ( self.x - offset[0], self.y - offset[1], self.width, self.height ) #camera.surface.fill((0,0,0), rect) camera.surface.blit(self.camera.surface, offset) return self.camera.render() camera.surface.blit(self.camera.surface, offset)
def __init__(self, *args): """Create a viewport entity that displays contents of a camera inside the world. Arguments are either (Camera, int, int) for a new camera or (Camera) for an existing one """ Entity.__init__(self) if len(args) == 1: self.camera = args[0] elif len(args) == 3: scene = args[0] width = args[1] height = args[2] self.camera = Camera(scene, width, height) else: raise TypeError, "Viewport's constructor takes either 4 or 2 arguments, %i given" % len(args)+1
def clearUserEnv(self): '''Clears user environment to a clean slate''' UE = self.userenv for asset in list(UE.assets): self.delAsset(asset) UE.focus = None UE.camera = Camera() UE.scene = Scene() UE.assets.clear() try: shutil.rmtree(datapath("save/assets/meshes")) shutil.rmtree(datapath("save/assets/textures")) os.mkdir(datapath("save/assets/meshes")) os.mkdir(datapath("save/assets/textures")) except: pass
from engine import (scale_space, rotate_space, draw_line, draw_polygon, draw_sympolygon, draw_circle) # Screen dimensions. screenWidth = 1000 screenHeight = 1000 desiredDepth = 1000 # Place our light sources. light0 = Lighting((1000, 0, 0), (255, 0, 0), 1000) light1 = Lighting((0, 1000, 0), (0, 255, 0), 1000) light2 = Lighting((0, 0, 1000), (0, 0, 255), 1000) lights = [light0, light1, light2] # Define the camera. camera = Camera( ((-1, screenWidth + 1), (-1, screenHeight + 1), (-1, desiredDepth + 1)), lights) # Build renderer. renderer = Renderer(camera) def draw_starfield(renderer): """ Generate 250 random stars. """ for point in range(250): renderer.camera.objectsInWorld.append( Star((randrange( 0, renderer.screenWidth), randrange(0, renderer.screenHeight), randrange(0, renderer.desiredDepth)), renderer)) renderer.camera.drawScene()
def main(): c = Context("pygame", (800, 400), "Renderer") init_engine() clock = Clock() obj0 = Obj(r"./assets/objects/tetrahedron.obj") obj1 = Obj(r"./assets/objects/halfcube.obj") obj2 = Obj(r"./assets/objects/octahedron.obj") obj3 = Obj(r"./assets/objects/dodecahedron.obj") obj4 = Obj(r"./assets/objects/teapot.obj") obj5 = Obj(r"./assets/objects/pointer.obj") tex0 = Tex(r"./assets/textures/_default.png") tex1 = Tex(r"./assets/textures/metal.jpg") tex2 = Tex(r"./assets/textures/abstract.jpg") tex3 = Tex(r"./assets/textures/white.png") model0 = Model(obj0, tex0, pos=Point(0.0, 0.0, 0.0), scale=0.8) model1 = Model(obj1, tex2, pos=Point(-1.5, 0.0, 0.0), scale=0.8) model2 = Model(obj2, tex0, pos=Point(0.0, 0.0, 0.0), scale=0.8) model3 = Model(obj3, tex0, pos=Point(0.0, 0.0, 0.0), scale=0.8) model4 = Model(obj4, tex3, pos=Point(10, 10, 10), scale=0.8) model5 = Model(obj5, tex1, pos=Point(0.0, 0.0, 0.0), scale=2.0) camera = Camera(pos=Point(0.0, 0.0, 3.0), rot=Rot(0.0, 0.0, 0.0), fovy=90) scene = Scene({model1, model4, model5}) camera_rotdeltas = { pygame.K_LEFT: Rot(0.0, -0.04, 0.0), pygame.K_RIGHT: Rot(0.0, 0.04, 0.0), pygame.K_DOWN: Rot(-0.04, 0.0, 0.0), pygame.K_UP: Rot(0.04, 0.0, 0.0), pygame.K_COMMA: Rot(0.0, 0.0, -0.04), pygame.K_PERIOD: Rot(0.0, 0.0, 0.04) } camera_posdeltas = { pygame.K_a: Point(-0.1, 0.0, 0.0), pygame.K_d: Point(0.1, 0.0, 0.0), pygame.K_s: Point(0.0, 0.0, 0.1), pygame.K_w: Point(0.0, 0.0, -0.1), pygame.K_f: Point(0.0, -0.1, 0.0), pygame.K_r: Point(0.0, 0.1, 0.0) } while True: clock.tick(60) c.idle() model0.rot += Rot(0.01, 0.02, 0.03) model1.rot += Rot(0.02, 0.03, 0.01) model2.rot += Rot(0.03, 0.01, 0.02) model3.rot += Rot(0.03, 0.02, 0.01) ## model4.rot += Rot(0.01, 0.03, 0.02) model5.rot += Rot(0.02, 0.01, 0.03) # rotate camera from keyboard inputs pygame.event.pump() pressed_keys = pygame.key.get_pressed() for k in camera_rotdeltas: if pressed_keys[k]: camera.rot += camera_rotdeltas[k] rx, ry, rz = camera.rot defacto_rot = Rot(rx, -ry, -rz) for k in camera_posdeltas: if pressed_keys[k]: camera.pos += defacto_rot.get_transmat() * camera_posdeltas[k] X, Y = c.getres() glViewport(0, 0, X, Y) scene.render(camera, aspect=X / Y, mode="full") c.dispbuffer()
def __init__(self, assets=set(), scene=Scene(), camera=Camera()): self.assets = assets self.scene = scene self.camera = camera