Upgrade to Pro — share decks privately, control downloads, hide ads and more …

pg: Modern OpenGL for Python

pg: Modern OpenGL for Python

pg is a high-level, light-weight framework for creating OpenGL applications in Python.

Michael Fogleman

October 23, 2014
Tweet

More Decks by Michael Fogleman

Other Decks in Technology

Transcript

  1. git clone https://github.com/fogleman/pg.git cd pg python setup.py develop PyPI GitHub

    pip install pg GLFW brew tap homebrew/versions brew install glfw3
  2. import pg ! class Window(pg.Window): def setup(self): pass def teardown(self):

    pass def update(self, t, dt): pass def draw(self): pass ! if __name__ == '__main__': pg.run(Window)
  3. class Window(pg.Window): def on_size(self, width, height): pass def on_cursor_pos(self, x,

    y): pass def on_mouse_button(self, button, action, mods): pass def on_key(self, key, scancode, action, mods): pass def on_char(self, codepoint): pass
  4. program = pg.Program(vs_src, fs_src) context = pg.Context(program) context.matrix = matrix

    # uniform context.position = vb # attribute context.draw() # GL_TRIANGLES by default
  5. class DirectionalLightProgram(BaseProgram): '''Renders the scene with a single, directional light

    source. Optionally, primitives can be textured or independently colored. ! :param matrix: the model-view-projection matrix, required :param model_matrix: the model matrix, required :param position: vertex buffer containing vertex positions, required :param normal: vertex buffer containing vertex normals, required :param uv: vertex buffer containing vertex texture coordinates, required if use_texture == True :param color: vertex buffer containing vertex colors, required if use_color == True :param sampler: texture to use, required if use_texture == True :param camera_position: the camera position in model space, required :param normal_matrix: the normal matrix, transposed inverse of model matrix, required :param light_direction: vector specifying light direction, default: (1, 1, 1) normalized :param object_color: color for all vertices if textures and color attributes are disabled, default: (0.4, 0.6, 0.8) :param ambient_color: ambient light color, default: (0.3, 0.3, 0.3) :param light_color: directional light color, default: (0.7, 0.7, 0.7) :param specular_power: controls exponent used in specular lighting, default: 32.0 :param specular_multiplier: controls intensity of specular lighting, default: 1.0 :param use_texture: controls whether a texture is to be used, default: False :param use_color: controls whether per-vertex colors are provided, default: False '''
  6. vb = pg.VertexBuffer([(0, 0), (1, 0), (1, 1)]) context.position =

    vb ! ! sphere = pg.Sphere(...) vb = pg.VertexBuffer(sphere.positions) ! ! vb = pg.VertexBuffer(pg.interleave( sphere.positions, sphere.normals)) context.position, context.normal = vb.slices(3, 3)
  7. matrix = pg.Matrix() # identity matrix = matrix.rotate((0, 1, 0),

    pi) # y-axis, 180-degrees matrix = matrix.translate((1, 2, 3)) matrix = matrix.perspective(65, self.aspect, 1, 100) ! matrix * (1, 1, 1) matrix * mesh # transform all vertices ! context.matrix = matrix # glUniformMatrix4fv
  8. wasd = pg.WASD(self) # window_or_scene context.camera_position = wasd.position context.matrix =

    wasd.get_matrix() ! camera = pg.Camera() camera.look_at((1, 1, 1), (0, 0, 0)) context.camera_position = camera.position context.matrix = camera.get_matrix()
  9. sphere = pg.Sphere(3, 0.5, (0, 0, 0)) cube = pg.Cuboid(-1,

    1, -1, 1, -1, 1) cylinder = pg.Cylinder((0, -1, 0), (0, 1, 0), 0.5, 18) cone = pg.Cylinder((0, -1, 0), (0, 1, 0), 0.5, 18) plane = pg.Plane((0, 0, 0), (0, 1, 0), 10) axes = pg.Axes(100) crosshairs = pg.Crosshairs()
  10. a = pg.Solid(sphere) b = pg.Solid(cylinder) c = a -

    b mesh = c.mesh() mesh.draw(context)
  11. sheet = pg.SpriteSheet(0, 'path/to/folder') batch = pg.SpriteBatch(sheet) sprite = sheet.star(self.batch)

    sprite.position = (x, y) sprite.rotation = pi / 2 sprite.scale = 0.5 matrix = pg.Matrix().orthographic(0, w, 0, h, -1, 1) batch.draw(matrix)
  12. class Scene(pg.Scene): def setup(self): pass def teardown(self): pass def update(self,

    t, dt): pass def draw(self): pass ! window.set_scene(scene) window.push_scene(scene) window.pop_scene()