def test_simple_example(self): print("\nSimple example", end="") scene = Scene("example") self.assertEqual(scene.name, "example") ruby_material = ColorMaterial(Color(255, 0, 0), Vector3d(0.1745, 0.01175, 0.01175), Vector3d(0.61424, 0.04136, 0.04136), Vector3d(0.727811, 0.626959, 0.626959), 76.8) sphere = Sphere(Vector3d(0, 0, 0), 10) object = RenderableObject(sphere, ruby_material) light = SpotLight(Vector3d(10, 10, 10)) camera = Camera(Vector3d(0, 0, -10), Vector3d(0, 0, 0), Vector3d(0, 1, 0), 60, 4 / 3, 1) scene.addObject(object) self.assertEqual(scene.objCnt, 1) scene.clearObjects() self.assertEqual(scene.objCnt, 0) scene.addLight(light) self.assertEqual(scene.lightCnt, 1) self.assertTrue(light.state) scene.setOnOffLight(0, False) self.assertFalse(light.state) scene.clearLights() self.assertEqual(scene.lightCnt, 0) scene.addCamera(camera) self.assertEqual(scene.camCnt, 1) scene.clearCameras() self.assertEqual(scene.camCnt, 0)
def test_simple_scene(self): scene = Scene("example") ruby_material = ColorMaterial(Color(255, 0, 0), Vector3d(0.1745, 0.01175, 0.01175), Vector3d(0.61424, 0.04136, 0.04136), Vector3d(0.727811, 0.626959, 0.626959), 76.8) scene.addObject( RenderableObject(Sphere(Vector3d(0, 0, 0), 3), ruby_material)) scene.addObject( RenderableObject(Sphere(Vector3d(4, 4, -4), 1), ruby_material)) scene.addLight(SpotLight(Vector3d(10, 10, -10))) scene.addLight(SpotLight(Vector3d(0, 10, -5))) scene.addCamera( Camera(Vector3d(0, 0, -15), Vector3d(0, 0, 0), Vector3d(0, 1, 0), 60, 4 / 3, 1)) image = Image(scene.frameWidth, scene.frameHeight) frames_cnt = 30 start = time.time() for i in range(frames_cnt): scene.getFrame(image) elapsed = time.time() - start print( "\nSimple case frames per second : Time = {}s, Frames = {}, FPS = {}" .format(round(elapsed, 4), frames_cnt, round(frames_cnt / elapsed, 4)), end="")
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
def create_scene_sample(camera_cnt=1, obj_cnt=1, light_cnt=1): scene = Scene("example") for i in range(camera_cnt): scene.addCamera( Camera(Vector3d(2 * i + 1, 3 * i + 1, 4 * i + 1), Vector3d(0, 0, 0), Vector3d(0, 1, 0), 60, 4 / 3, 1), True) ruby_material = ColorMaterial(Color(255, 0, 0), Vector3d(0.1745, 0.01175, 0.01175), Vector3d(0.61424, 0.04136, 0.04136), Vector3d(0.727811, 0.626959, 0.626959), 76.8) for i in range(obj_cnt): scene.addObject( RenderableObject(Sphere(Vector3d(i * 10), i * 5), ruby_material)) for i in range(light_cnt): scene.addLight(SpotLight(Vector3d(-i * 2, i * 4, i * 5))) return scene
def test(name="example"): width = 640 height = 480 scene = Scene(name, width, height) ruby_material = ColorMaterial(Color(255, 0, 0), Vector3d(0.1745, 0.01175, 0.01175), Vector3d(0.61424, 0.04136, 0.04136), Vector3d(0.727811, 0.626959, 0.626959), 76.8) scene.addObject(RenderableObject(Sphere(Vector3d(0), 2), ruby_material)) #scene.addObject(RenderableObject(Sphere(Vector3d(4,4,-4), 1), ruby_material)) #scene.addLight(SpotLight(Vector3d(10,10,10))) scene.addLight(SpotLight(Vector3d(5, 6, -6))) scene.addCamera( Camera(Vector3d(0, 7, 7), Vector3d(0, 0, 0), Vector3d(0, -1, 1).normalized(), 75, width * 1.0 / height, 2), True) scene.addCamera( Camera(Vector3d(0, 10, 0), Vector3d(0, 0, 0), Vector3d(0, 0, 1), 75, width * 1.0 / height, 2), False) scene.addCamera( Camera(Vector3d(10, 0, 0), Vector3d(0, 0, 0), Vector3d(0, -1, 0), 75, width * 1.0 / height, 2), False) scene.addCamera( Camera(Vector3d(0, 0, 10), Vector3d(0, 0, 0), Vector3d(0, -1, 0), 75, width * 1.0 / height, 2), False) with open(name + '.json', 'w') as f: json.dump(json.loads(repr(scene)), f) dump_scene_image(scene)
def test_simple_1000_spheres(self): scene = Scene("example") ruby_material = ColorMaterial(Color(255, 0, 0), Vector3d(0.1745, 0.01175, 0.01175), Vector3d(0.61424, 0.04136, 0.04136), Vector3d(0.727811, 0.626959, 0.626959), 76.8) x0 = -15 y0 = -15 for i in range(1000): x = x0 + i % 30 y = y0 + i // 30 z = 0 r = 0.5 scene.addObject( RenderableObject(Sphere(Vector3d(x, y, z), r), ruby_material)) scene.addLight(SpotLight(Vector3d(10, 10, -10))) scene.addCamera( Camera(Vector3d(0, 0, -25), Vector3d(0, 0, 0), Vector3d(0, 1, 0), 60, 4 / 3, 1)) image = Image(scene.frameWidth, scene.frameHeight) frames_cnt = 30 start = time.time() for i in range(frames_cnt): scene.getFrame(image) elapsed = time.time() - start print( "\n1000 spheres frames per second : Time = {}s, Frames = {}, FPS = {}" .format(round(elapsed, 4), frames_cnt, round(frames_cnt / elapsed, 4)), end="")
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 test_custom_constructor(self): print("\nConstructor with parameters", end="") scene = Scene("test") self.assertEqual(scene.name, "test")
def test_constructor(self): print("\nConstructor without parameters", end="") scene = Scene() self.assertEqual(scene.name, "unnamed")
# -*- coding: utf-8 -*- """запуск простейшей игры""" from engine import Scene from my_bee import MyBee # импортируем код класса пчелы scene = Scene( # создаем нужную сцену name="My little garden", flowers_count=10, resolution=(1000, 500), ) bee = MyBee() # вот оно - рождение пчелы! scene.go() # и запускаем наш виртуальный мир крутиться... # для запуска соревнования # # scene = Scene( # name="My little garden", # beehives_count=2, # flowers_count=80, # speed=40, # resolution=(1000, 500) # ) # # # from my_bee import MyBee # from my_bee import MyBee as OtherBee # # bees = [MyBee() for i in range(10)] # В классе MyBee должна быть указана команда team = 1 # bees_2 = [OtherBee() for i in range(10)] # В классе OtherBee должна быть указана команда team = 2
def __init__(self, assets=set(), scene=Scene(), camera=Camera()): self.assets = assets self.scene = scene self.camera = camera