teaching machines

Raspberry Pi + OpenGL ES

September 18, 2012 by . Filed under buster, public.

OpenGL ES works on the Raspberry Pi!

My first triangle running under OpenGL ES 2 on a Raspberry Pi

How did I do it? Well, I…

  1. Grabbed vim with sudo apt-get install vim.
  2. Grabbed my ~/.vimrc.
  3. Calmed down.
  4. Looked in /opt/vc/src and built and ran the examples there.
  5. Braced myself to plow through the EGL junk.
  6. Found pyopengles and downloaded it on my Raspberry Pi. Thanks, Mr. Divaz.
  7. Found http://benosteen.wordpress.com/2012/05/13/glsl-sandbox-on-the-raspberry-pi/. I based my first triangle off of one of the examples I found there. It renders a half-screen triangle, shaded according to its relative position on the screen. The code is:
    from pyopengles import *
    
    context = EGL()
    
    vertexShader = """
      attribute vec4 position;
      varying vec2 fposition;
    
      void main() {
        gl_Position = position;
        fposition = position.xy * 0.5 + 0.5;
      }
    """
    
    fragmentShader = """
      precision mediump float;
    
      varying vec2 fposition;
    
      void main() {
        gl_FragColor = vec4(vec3(fposition, 0.0), 1.0);
      }
    """
    
    binding = ((0, 'position'),)
    program = context.get_program(vertexShader, fragmentShader, binding)
    
    opengles.glClearColor(eglfloat(0.45), eglfloat(0.35), eglfloat(0.15), eglfloat(1.0))
    vertices = eglfloats((-1.0, -1.0, 0.0, 1.0,
                           1.0,  1.0, 0.0, 1.0,
                          -1.0,  1.0, 0.0, 1.0))
    
    opengles.glViewport(0, 0, context.width, context.height);
    opengles.glClear(GL_COLOR_BUFFER_BIT)
    opengles.glUseProgram(program)
    
    opengles.glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, vertices)
    opengles.glEnableVertexAttribArray(0)
    
    opengles.glDrawArrays(GL_TRIANGLES, 0, 3)
    openegl.eglSwapBuffers(context.display, context.surface)
    
    time.sleep(5)