def main(): name = get_argument_example() or get_menu_example() if name is None: return module = import_module('examples.%s' % name) if hasattr(module, 'main'): module.main() else: names = ['Window', 'Scene'] for name in names: if hasattr(module, name): pg.run(getattr(module, name)) return
self.a += random.randint(-1, 1) * pi / 8 dx = cos(self.a) dz = sin(self.a) self.x += dx * dt self.z += dz * dt class Window(pg.Window): def setup(self): self.wasd = pg.WASD(self, speed=10) self.wasd.look_at((0, 8, 30), (0, 0, 0)) self.context = pg.Context(pg.DirectionalLightProgram()) self.mesh = pg.OBJ('examples/lego.obj').centered().smoothed() self.men = [LegoMan() for _ in xrange(50)] 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) if __name__ == "__main__": pg.run(Window)
import pg class Window(pg.Window): def setup(self): self.wasd = pg.WASD(self, speed=10) self.wasd.look_at((0, 3, 12), (0, 0, 7)) self.context = pg.Context(pg.DirectionalLightProgram()) self.points = pg.poisson_disc(-10, -10, 10, 10, 1.5, 32) self.mats = [pg.Matrix().translate((x, 0, z)) for x, z in self.points] self.sphere = pg.Sphere(4, 0.7) def draw(self): self.clear() self.context.camera_position = self.wasd.position matrix = self.wasd.get_matrix() matrix = matrix.perspective(65, self.aspect, 0.01, 100) for (x, z), mat in zip(self.points, self.mats): self.context.model_matrix = mat self.context.matrix = matrix * mat self.sphere.draw(self.context) if __name__ == "__main__": pg.run(Window)
varying vec2 frag_uv; void main() { gl_Position = matrix * position; frag_uv = uv; } ''' FS = ''' #version 120 uniform sampler2D sampler; uniform float opacity; varying vec2 frag_uv; void main() { vec4 color = texture2D(sampler, frag_uv); color.a = min(color.a, opacity); if (color.a == 0) { discard; } gl_FragColor = color; } ''' def set_defaults(self, context): context.opacity = 1.0 if __name__ == "__main__": pg.run(Window, full_screen=True)