Search code examples
opengl-esglslshaderopengl-es-2.0

How to use UBO in OpenGL ES2.0 shaders


1.I need to move the shader in opengl to Opengles 2.0.so I have a problem, I don't know how to transfer this structure called UBO. 2.What should I assign to the program if the transfer is successful?

1.1 To transfer to opengles2.0 code:

layout(binding = 0) uniform UniformBufferObject 
{
    mat4 model;
    mat4 normal;
    mat4 view;
    mat4 proj;
    vec3 eyepos;
    material_struct material;
    light_struct lights[8];    
} ubo;

2.1 I want to transform the vertex data. How should I assign this UBO to the program?

//vertex 
int mPositionHandle = GLES20.GetAttribLocation(_program, "vPosition");
            GLES20.EnableVertexAttribArray(mPositionHandle);
            GLES20.VertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, _buffer);
//color
int mColorHandle = GLES20.GetAttribLocation(_program, "aColor");
            GLES20.EnableVertexAttribArray(mColorHandle);
            GLES20.VertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false, 0, _color);
//UBO???

At present, the vertex data, index, and color are all there, but the vertex data is too large. I hope to change between (-1~1).


Solution

  • Uniform blocks are not provided in OpenGL ES 2.0. See GLSL ES 1.0 specification.

    Uniform blocks are supported by OpenGL ES 3.0 respectively GLSL ES 3.00.
    See GLSL ES 3.00 specification - 4.3.7 Interface Blocks; page 43.
    But the binding layout qualifier is provided since OpenGL ES 3.1 and GLSL ES 3.10.
    See GLSL ES 3.10 specification - 4.4 Layout Qualifiers; page 51.

    In OpenGL ES 3.0 the binding of a uniform block can be set by glUniformBlockBinding and the uniform block index of the program can be get by glGetUniformBlockIndex. The program has to be successfully linked before, in both cases. Note that the uniform block index is not to be confused with the uniform location, this are different things.

    In OpenGL ES 2.0 the only possibility is to use conventional Uniform variables.