在我的应用程序中(在Mali-400 GPU上运行)我正在使用OpenGL ES 2.0来绘制UI。为此我设置正交投影矩阵并使用2D矢量来描述几何(仅x,y属性)。
所以我的顶点结构看起来像这样:
struct my_vertext {
struct vec2 pos;
struct unsigned int color;
}
稍后我准备GL上下文,设置着色器:
顶点:
uniform mat4 matrix;
attribute vec2 pos;
attribute vec4 color;
varying vec4 frag_color;
void main() {
frag_color = color;
gl_Position = matrix * vec4(pos.xy, 0, 1);
};
片段:
precision mediump float;
varying vec4 frag_color;
void main() {
gl_FragColor = frag_color;
};
并将它们与属性数组绑定:
GLuint prog = glCreateProgram(); CHECK_GL;
// create shaders, load source and compile them
...
/// binding with attributes
GLuint attrib_pos, attrib_col, vertex_index = 0;
glBindAttribLocation(prog, vertex_index, "pos"); CHECK_GL;
attrib_pos = vertex_index++;
glBindAttribLocation(prog, vertex_index, "color"); CHECK_GL;
attrib_col = vertex_index++;
// link program
glLinkProgram(prog); CHECK_GL;
glUseProgram(prog); CHECK_GL;
当我渲染几何体时(为简单起见,我一个接一个地绘制2个矩形) - 只有第一次调用 glDrawElements 会在屏幕上产生图像(深灰色矩形),第二个(红色) ) - 没有。 对于渲染,我使用带有2个绑定缓冲区的顶点数组对象 - 一个用于几何(GL_ARRAY_BUFFER),第二个用于索引(GL_ELEMENT_ARRAY_BUFFER)。所有几何体都放置在此缓冲区中,稍后使用 glDrawElements 调用绘制,提供所需的偏移量。
我的绘图代码:
glBindVertexArray(vao); CHECK_GL;
...
GLuint *offset = 0;
for each UI object:
{
glDrawElements(GL_TRIANGLES, (GLsizei)ui_object->elem_count,
GL_UNSIGNED_SHORT, offset); CHECK_GL;
offset += ui_object->elem_count;
}
这让我很困惑,因为我检查了glXXX函数的每个返回码,并且所有这些都返回 GL_NO_ERROR 。另外,我在Mali Graphics Debugger中运行我的程序,后者没有发现任何问题/错误。
两次调用的几何和索引(从Mali Graphics Debugger获得):
First rectangle geometry (which is shown on screen):
0 Position=[30.5, 30.5] Color=[45, 45, 45, 255]
1 Position=[1250.5, 30.5] Color=[45, 45, 45, 255]
2 Position=[1250.5, 690.5] Color=[45, 45, 45, 255]
3 Position=[30.5, 690.5] Color=[45, 45, 45, 255]
Indices: [0, 1, 2, 0, 2, 3]
Second rectangle geometry (which isnt` shown on screen):
4 Position=[130.5, 130.5] Color=[255, 0, 0, 255]
5 Position=[230.5, 130.5] Color=[255, 0, 0, 255]
6 Position=[230.5, 230.5] Color=[255, 0, 0, 255]
7 Position=[130.5, 230.5] Color=[255, 0, 0, 255]
Indices: [4, 5, 6, 4, 6, 7]
P.S。:在我的桌面上,一切都很完美。我想它与嵌入式Open GL的限制/特性有关。
On my desktop:
$ inxi -F
$ ....
$ GLX Renderer: Mesa DRI Intel Ivybridge Desktop GLX Version: 3.0 Mesa 10.1.3
答案 0 :(得分:0)
我对OpenGL ES知之甚少,但这部分看起来不对:
glBindVertexArray(vao); CHECK_GL;
...
GLuint *offset = 0;
for each UI object:
{
glDrawElements(GL_TRIANGLES, (GLsizei)ui_object->elem_count,
GL_UNSIGNED_SHORT, offset); CHECK_GL;
offset += ui_object->elem_count;
}
与
相比glBindVertexArray(vao); CHECK_GL;
...
GLuint offset = 0;
for each UI object:
{
glDrawElements(GL_TRIANGLES, (GLsizei)ui_object->elem_count,
GL_UNSIGNED_SHORT, &offset); CHECK_GL;
offset += ui_object->elem_count;
}