def setup(self): self.wasd = pg.WASD(self, speed=8) self.wasd.look_at((-10, 0, 0), (0, 0, 0)) # cuboids self.context = pg.Context(pg.DirectionalLightProgram()) self.context.use_color = True self.context.ambient_color = (0.5, 0.5, 0.5) self.context.light_color = (0.5, 0.5, 0.5) self.context.light_direction = pg.normalize((-1, 1, 1)) data = [] n = 16 for x in range(256): z = random.randint(-n, n) y = random.randint(-n, n) cuboid = pg.Cuboid(x, x + 1, y - 0.5, y + 0.5, z - 0.5, z + 0.5) color = pg.hex_color(random.randint(0, 0xffffff)) colors = [color] * len(cuboid.positions) data.extend(pg.interleave( cuboid.positions, cuboid.normals, colors)) self.context.position, self.context.normal, self.context.color = ( pg.VertexBuffer(data).slices(3, 3, 3)) # bullets self.bullet = pg.Context(pg.DirectionalLightProgram()) self.bullet.ambient_color = (0.5, 0.5, 0.5) self.bullet.light_color = (0.5, 0.5, 0.5) sphere = pg.Sphere(3, 0.05, (0, 0, 0)) self.bullet.position = pg.VertexBuffer(sphere.positions) self.bullet.normal = pg.VertexBuffer(sphere.normals) self.bullets = [] # crosshairs self.crosshairs = pg.Context(pg.SolidColorProgram()) self.crosshairs.position = pg.VertexBuffer(pg.Crosshairs().positions)
def setup(self): self.wasd = pg.WASD(self, speed=30) self.wasd.look_at((-20, 20, -8), (0, 0, 0)) self.context = pg.Context(pg.DirectionalLightProgram()) self.context.use_color = True self.context.specular_power = 8.0 self.context.specular_multiplier = 0.3 normals = defaultdict(list) position = [] normal = [] color = [] size = 50 # generate height map height = {} colors = {} for x in xrange(-size, size + 1): for z in xrange(-size, size + 1): height[(x, z)] = noise(x, z) colors[(x, z)] = generate_color(x, z) # generate triangles and track normals for all vertices for x in xrange(-size, size): for z in xrange(-size, size): t1 = [x + 0, z + 0, x + 1, z + 0, x + 0, z + 1] t2 = [x + 0, z + 1, x + 1, z + 0, x + 1, z + 1] for t in [t1, t2]: x1, z1, x2, z2, x3, z3 = t p1 = (x1, height[(x1, z1)], z1) p2 = (x2, height[(x2, z2)], z2) p3 = (x3, height[(x3, z3)], z3) c1 = colors[(x1, z1)] c2 = colors[(x2, z2)] c3 = colors[(x3, z3)] position.extend([p3, p2, p1]) color.extend([c3, c2, c1]) n = pg.normalize(pg.cross(pg.sub(p3, p1), pg.sub(p2, p1))) normals[(x1, z1)].append(n) normals[(x2, z2)].append(n) normals[(x3, z3)].append(n) # compute average normal for all vertices for key, value in normals.items(): normals[key] = pg.normalize(reduce(pg.add, value)) for x, y, z in position: normal.append(normals[(x, z)]) # generate vertex buffer vb = pg.VertexBuffer(pg.interleave(position, normal, color)) self.context.position, self.context.normal, self.context.color = ( vb.slices(3, 3, 3))
def set_matrix(self, x, y, z, a): matrix = pg.Matrix().rotate((0, 1, 0), a).translate((x, y, z)) inverse = pg.Matrix().rotate((0, 1, 0), -a) self.context.light_direction = inverse * pg.normalize((1, 1, 1)) self.context.camera_position = matrix.inverse() * self.wasd.position matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.1, 1000) self.context.matrix = matrix
def rotate_satellite(self, position): dx, dy, dz = pg.normalize(position) rx = atan2(dz, dx) + pi / 2 ry = asin(dy) - pi / 2 matrix = pg.Matrix() matrix = matrix.rotate((0, 1, 0), rx) matrix = matrix.rotate((cos(rx), 0, sin(rx)), -ry) return matrix
def rotate_moon(self, position): # TODO: account for libration dx, dy, dz = pg.normalize(position) rx = atan2(dz, dx) + pi / 2 ry = asin(dy) matrix = pg.Matrix() matrix = matrix.rotate((0, 1, 0), rx) matrix = matrix.rotate((cos(rx), 0, sin(rx)), -ry) return matrix
def get_sun(self): lat, lng = self.get_lat_lng() observer = ephem.Observer() observer.lat = radians(lat) observer.lon = radians(lng) sun = ephem.Sun(observer) elevation = degrees(sun.alt) azimuth = degrees(sun.az) return pg.normalize(to_xyz(lat, lng, elevation, azimuth))
def set_defaults(self, context): context.model_matrix = pg.Matrix() context.normal_matrix = pg.Matrix().inverse().transpose() context.specular_power = 32.0 context.specular_multiplier = 0.2 context.ambient_color = (0.4, 0.4, 0.4) context.light_color = (0.8, 0.8, 0.8) context.fog_color = (0.74, 0.70, 0.64) context.fog_distance = 10000 context.light_direction = pg.normalize((1, 0.5, 1))
def update(self, t, dt): matrix = pg.Matrix().rotate((0, 1, 0), t / 4) self.context.light_direction = matrix.inverse() * pg.normalize((1, 1, 1)) self.context.camera_position = matrix.inverse() * self.camera.position matrix = self.camera.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.01, 100) self.context.matrix = matrix if (t / 4) % 2.0 < 1.0: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) else: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
def setup(self): self.font = pg.Font(self, 1, '/Library/Fonts/Arial.ttf', 24) self.wasd = pg.WASD(self) self.wasd.look_at(pg.normalize((1, 0, 1)), (0, 0, 0)) self.context = pg.Context(pg.DirectionalLightProgram()) self.context.sampler = pg.Texture(0, 'examples/earth.png') self.context.use_texture = True sphere = pg.Sphere(4, 0.5, (0, 0, 0)) self.context.position = pg.VertexBuffer(sphere.positions) self.context.normal = pg.VertexBuffer(sphere.normals) self.context.uv = pg.VertexBuffer(sphere.uvs)
def update(self, t, dt): self.wasd.y = 1.5 self.clear() for man in self.men: man.update(t, dt) a = man.a + pi / 2 matrix = pg.Matrix().rotate((0, 1, 0), a).translate((-man.x, 0, -man.z)) inverse = pg.Matrix().rotate((0, 1, 0), -a) self.context.light_direction = inverse * pg.normalize((1, 1, 1)) self.context.camera_position = matrix.inverse() * self.wasd.position matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.1, 1000) self.context.matrix = matrix self.mesh.draw(self.context)
def setup(self): self.camera = pg.Camera() position = pg.mul(pg.normalize((-1, 0.5, 1)), 3) self.camera.look_at(position, (0, 0, 0)) self.context = pg.Context(pg.DirectionalLightProgram()) shapes = [ pg.Sphere(4, 0.5, (0, 0, 0)), pg.Cone((0, 0.5, 0), (0, -0.5, 0), 0.5, 36), pg.Cuboid(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5), pg.Cylinder((0, -0.5, 0), (0, 0.5, 0), 0.5, 36), ] offsets = [(-1, 0), (0, 1), (1, 0), (0, -1)] self.shapes = [] for (dx, dz), shape in zip(offsets, shapes): m = 1.5 matrix = pg.Matrix().translate((dx * m, 0, dz * m)) shape = matrix * shape self.shapes.append(shape)