Skip to content

About OpenGL and GLSL

Peter Nagy edited this page Apr 15, 2020 · 10 revisions

BZZZBZ uses OpenGL ES 2.0 to render 2D vector graphics.

Documentation is provided by Khronos Group at the appropriate reference pages. OpenGL ES is used to create a windowing context and perform related tasks while the graphics are created solely by using fragment shaders written in GLSL, a C-style shading language developed for OpenGL. Introductory resources for OpenGL (ES) 2.0 and GLSL can be found here.

Custom shaders ("visuals") can be used for which The Book of Shaders provides an extensive (though incomplete) introduction. A useful tool to sketch shaders is GLSL Sandbox. To implement this in BZZZBZ the shader source code must be saved as *.glsl using the following template:

/*this is automatically added to the shader src in shader_utils.cpp but must be changed to reflect the OpenGL version used */
#version 100 // for OpenGL ES 2.0
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif  


/*uniforms should be listed here. All uniforms must be 'active' and directly influence the output gl_FragColor() 
and must be bound in the OpenGL code */
uniform float H; //Screen height
uniform float W; //Screen width
 
void main( void ) {
    //normalise pixel positions to screen resolution so values are in the range [0.0 1.0]
    float X = gl_FragCoord.x/W;
    float Y = gl_FragCoord.y/H;
    float POS = vec2(X,Y);
    //initialise output variable
    float color = 0.0;
    
    /*write operations here*/

    gl_FragColor = vec4( color.r, color.g, color.b, 1.0 );
}

To utilise GPU acceleration on the Raspberry Pi and avoid extremely poor performance, Fake (or Full) KMS must be turned on using sudo raspi-config. Required third-party libraries can be installed using:

sudo apt-get install raspberrypi-kernel-headers freeglut3 freeglut3-dev libgl1-mesa-dev libglew-dev 

on Debian based distributions.

Clone this wiki locally