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.

7b7aefbc459629eb4c864b825894c9c4?s=128

Michael Fogleman

October 23, 2014
Tweet

Transcript

  1. Michael Fogleman 2014-10-23 michaelfogleman.com Modern OpenGL for Python pg

  2. Introduction

  3. None
  4. None
  5. None
  6. None
  7. Links https://github.com/fogleman/pg http://pg.readthedocs.org/

  8. Installation

  9. 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
  10. Windowing

  11. 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)
  12. Keyboard and Mouse

  13. 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
  14. Shaders

  15. 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
  16. Built-in Shaders

  17. context = pg.Context(pg.DirectionalLightProgram()) context.light_direction = ... context.specular_multiplier = ...

  18. 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 '''
  19. None
  20. Vertex Buffers

  21. 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)
  22. Matrices

  23. 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
  24. Cameras

  25. 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()
  26. Textures

  27. texture = pg.Texture(0, 'path/to/image.png') context.sampler = texture

  28. Fonts

  29. font = pg.Font(0, 'Arial.ttf', 12) font.render('Hello, world!', (x, y))

  30. 3D Geometric Primitives

  31. 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()
  32. 3D Models

  33. mesh = pg.OBJ('path/to/model.obj') mesh = pg.STL('path/to/model.stl') mesh.draw(context) mesh.bounding_box() # etc.

  34. None
  35. Constructive Solid Geometry (CSG)

  36. a = pg.Solid(sphere) b = pg.Solid(cylinder) c = a -

    b mesh = c.mesh() mesh.draw(context)
  37. None
  38. Sprites

  39. 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)
  40. Scenes

  41. 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()
  42. Threading

  43. pg.async(func, *args, **kwargs) pg.call_after(func, *args, **kwargs) pg.Worker(...)

  44. Questions? Live Coding Demo?