Search code examples
openglpython-3.xpygamevbopyopengl

Retained Mode to draw 2D texture image


I'm trying to use the "retained mode" to draw my 2D images (loaded as texztures inside pygame with an opengl context), and the way i found to do so is using VBO, but i can't find a opengl tutorial that uses VBO to draw anything but primitives. Is there an away to do it?


Solution

  • Yes, you can (or even should?) use VBOs to draw 2D graphics. What I mostly use in my 2D rendering is following:

    I have shader that takes in vertex coordinates (VBO), UV coordinates (VBO), texture map and ModelView matrix. I have centered "unit box", simple 1x1 (from (-0.5, -0.5) to (0.5, 0.5)) rectangle made from 2 triangles, with UV coordinates (0, 0) - (1, 1). I use these same two VBOs to draw all 2D bitmaps: I send position & scaling in ModelView matrix and change different bitmap to be drawn.

    For GUI purposes, it may be easier to have reference point at top left corner, that is, a rectangle from (0, 0) to (1, 1) - you can use UV coordinate buffer for this, if your shader accepts vec2 coordinates. Also, it may help if you set perspective so that you can use window coordinates.

    Following tutorials are written for C++, but OpenGL calls are the same in Python, too:

    Creating VAO & VBOs: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/

    Texturing: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/

    Text (drawing 2D bitmap): http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-11-2d-text/

    Hope this helps a bit, sorry that I can't provide any source code, as my OpenGL projects are written in D... Try also google "pygame pyopengl 2d" for finding some source code doing the trick.