def __init__(self): self.objects = [] self.emitters = [] self.physics = Physics() self.wind_angle = 0.0 self.create_scene() self.camera = Camera(pos=Point3(10, 5, 10), look_at=Point3(0, 1, 0), width=WIDTH, height=HEIGHT) self.t = 0.0 self.clock = pyglet.clock.Clock(time_function=self.time)
class World(EventDispatcher): def __init__(self): self.objects = [] self.emitters = [] self.physics = Physics() self.wind_angle = 0.0 self.create_scene() self.camera = Camera(pos=Point3(10, 5, 10), look_at=Point3(0, 1, 0), width=WIDTH, height=HEIGHT) self.t = 0.0 self.clock = pyglet.clock.Clock(time_function=self.time) def time(self): return self.t def spawn(self, obj): self.objects.append(obj) try: model = obj.model except AttributeError: pass else: self.scene.add(model) if hasattr(obj, 'emitters'): for e in obj.emitters: self.emitters.append(e) e.start() if hasattr(obj, 'body'): self.physics.add(obj.body) obj.world = self def destroy(self, obj): self.objects.remove(obj) try: model = obj.model except AttributeError: pass else: self.scene.remove(model) if hasattr(obj, 'emitters'): for e in obj.emitters: e.stop() self.emitters = [o for o in self.emitters if o not in obj.emitters] if hasattr(obj, 'body'): self.physics.remove(obj.body) obj.world = None def update(self, dt): """Update the world through the given time step (in seconds).""" self.t += dt self.clock.tick() pyglet.media.listener.position = self.camera.pos pyglet.media.listener.forward_orientation = self.camera.eye_vector() for e in self.emitters: e.update() particles.update(dt) for o in self.objects: o.update(dt) self.physics.do_collisions() def create_scene(self): """Initialise the scene with static objects.""" self.scene = Scene(ambient=(0.2, 0.2, 0.2, 1.0), ) for m in Ship.MODELS: self.scene.prepare_model(m) # Add the particle system self.scene.add(particles) # Sun self.scene.add( Sunlight( direction=Vector3(0.82, 0.31, 0.48), colour=(1.0, 0.85, 0.6, 1.0), intensity=1, )) # Sky dome self.skydome = ModelNode(skydome, rotation=(59, 0, 1, 0)) self.scene.add(self.skydome) # Sea self.sea = SeaNode(sea_model) self.sea.shader = sea_shader self.scene.add(self.sea) def spawn_ships(self): for i in range(5): self.spawn_one_ship() def spawn_one_ship(self): bearing = random.uniform(0, tau) rng = random.uniform(50, 100) x = rng * math.sin(bearing) z = rng * math.cos(bearing) angle = random.uniform(0, tau) s = Ship(pos=Point3(x, 0, z), angle=angle) self.spawn(s) ShipAI(s).start() def draw(self): x, _, z = self.camera.pos self.skydome.pos = Point3(x, 0, z) self.sea.pos = Point3(x, 0, z) self.scene.render(self.camera)
zpos = 10 camera_rot = 0 tau = 2 * math.pi def update(dt): global camera_rot camera_rot += 0.1 * dt c.pos = v3((math.cos(camera_rot) * 15, 5, math.sin(camera_rot) * 15)) c = Camera(pos=v3((0, 0, -20)), width=WIDTH, height=HEIGHT) def on_draw(): scene.render(c) if __name__ == '__main__': from optparse import OptionParser parser = OptionParser() parser.add_option('-s', '--screenshot', metavar='FILE', help='Write screenshot to FILE') options, _ = parser.parse_args()
''' from wasabisg.loaders.objloader import ObjFileLoader from wasabisg.scenegraph import ModelNode loader = ObjFileLoader() tree_model = loader.load_obj('tree.obj') tree = ModelNode(tree_model, pos=(10, 0, 10)) scene.add(tree) ''' from wasabisg.lighting import Light # Distant light to approximate the sun sunlight = Light(pos=(100, 100, 100), colour=(1.0, 1.0, 1.0, 1.0), intensity=10, falloff=0) scene.add(sunlight) from euclid import Point3 from wasabisg.scenegraph import Camera c = Camera( pos=Point3(0, 1, -20), look_at=(0, 1, 0), width=800, # or whatever size your viewport is height=600) scene.render(c)
def test_airborne(): """looking down on the point, -z, -y direction""" root2 = math.sqrt(2) camera = Camera(pos=v3(2, 10 * root2, 10 * root2), look_at=v3(2, 0, 0)) vec_eq(project(point, camera), v3(0, 0.5 * root2, 0.5 * root2 - 20))
def test_minusxaligned(): """looking down the x axis in the +x direction""" camera = Camera(pos=v3(-20, 0, 0)) vec_eq(project(point, camera), v3(0, 1, -22))
def test_xaligned(): """looking down the x axis in the -x direction""" camera = Camera(pos=v3(20, 0, 0)) vec_eq(project(point, camera), v3(0, 1, -18))
def test_zaligned(): """looking down the z axis in the -z direction""" camera = Camera(pos=v3(0, 0, 20)) vec_eq(project(point, camera), v3(2, 1, -20))
angle = 180 tau = 2 * math.pi def update(dt): global angle angle += 90.0 * dt for r in robots: r.rotation = (angle, 0, 1, 0) c = Camera(pos=v3((0, 5, -20)), look_at=Point3(0, 5, 0), width=WIDTH, height=HEIGHT) def on_draw(): window.clear() scene.render(c) if __name__ == '__main__': from optparse import OptionParser parser = OptionParser() parser.add_option('-s', '--screenshot', metavar='FILE', help='Write screenshot to FILE')