Uniform block objects - any examples?

I have a question for you. Does anyone have good examples with using UBO buffers? I checked LearnOpenGL.com but it does not have some examples with UBOs. I need them for multi-light sources setup because each uniform location are too complicated for many uniform variabes like multi-ligtht sources. I am using core profile v4.5. For example:

     vec4  ambient;
     int  nLights;
     struct {
         vec4 direction;
         vec4 diffuse;
         vec4 specular;
     } lights[8];


start easier, for example with 1 color (vec4) put into a uniform block

it gets a bit complicated when want to put structs into uniform blocks, see “std140” layout
then, you either need to “pad out” your cpp struct or you have to query memory alignments

you can bind a certain buffer to an “uniform block index” with:

there is an example at learnopengl.com (a simple one)
LearnOpenGL - Advanced GLSL

For GLSL under OpenGL, your best bet is to use std140 layout. Read an overview about it and other UBO/SSBO layouts here:

For more detail and examples, see an OpenGL tutorial describing buffer layouts, the OpenGL Spec, or the GLSL Spec. For instance, here’s a page link in the LearnOpenGL tutorial that describes it with examples (search down to std140):

std430 is more similar to what you’d get by default with C++ structs. But under OpenGL that’s not supported for UBOs, only SSBOs.