【问题标题】:How to use UBO in OpenGL ES2.0 shaders如何在 OpenGL ES2.0 着色器中使用 UBO
【发布时间】:2019-09-19 19:53:35
【问题描述】:

1.我需要将opengl中的shader移动到Opengles 2.0.所以我有一个问题,我不知道如何转移这个叫做UBO的结构。 2.如果转移成功,我应该给程序分配什么?

1.1 转入opengles2.0代码:

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 我想转换顶点数据。我应该如何将此 UBO 分配给程序?

//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???

目前顶点数据、索引、颜色都在,但是顶点数据太大了。我希望在(-1~1)之间切换。

【问题讨论】:

  • OpenGL ES 2.0 中不提供统一块。你必须使用单独的制服。
  • @Rabbid76 你能把这个评论转换成答案吗,所以这个问题显示为已回答。
  • @solidpixel 我把评论变成了答案。但我不确定它是否让我满意。

标签: opengl-es glsl shader opengl-es-2.0


【解决方案1】:

OpenGL ES 2.0 中不提供统一块。见GLSL ES 1.0 specification

OpenGL ES 3.0 和 GLSL ES 3.00 分别支持统一块。
GLSL ES 3.00 specification - 4.3.7 Interface Blocks; page 43
但自 OpenGL ES 3.1 和 GLSL ES 3.10 起提供了 binding 布局限定符。
GLSL ES 3.10 specification - 4.4 Layout Qualifiers; page 51

在OpenGL ES 3.0中,统一块的绑定可以通过glUniformBlockBinding设置,程序的统一块索引可以通过glGetUniformBlockIndex获取。在这两种情况下,程序都必须先成功链接。请注意,统一块索引不要与统一位置混淆,这是不同的东西。

在 OpenGL ES 2.0 中,唯一的可能性是使用传统的 Uniform 变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多