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:

#version 100 // for OpenGL ES 2.0
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif  

/*above is automatically added to the shader src in shader_utils.cpp */

/*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 );
}

If the code is implemented on versions of Raspberry Pi 4 and above or on a desktop, other versions

Clone this wiki locally