优化 OpenGL ES 操作?
我使用以下代码在屏幕上绘制字符(每个 UTF8 字符都是一个纹理):
int row = 0;
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glVertexPointer(2, GL_FLOAT, 0, vertices);
for (StdStr* line in _lines) {
const char* str = [line cString];
for (int i = 0; i < strlen(str); i++) {
((XX::OGL::GLESContext*)context)->viewport(C_WIDTH*i,
C_HEIGHT*row,
C_WIDTH,
C_HEIGHT);
glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, _textures[0] + *(str + i));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
row++;
}
当字符很多时,代码运行时间会更长。在本例中,几乎 99% 的时间都花费在 glDrawArrays
例程中。是否可以最大程度地减少对 glDrawArrays
的调用量? OpenGL ES 版本为 1.1。
I'm using the following code to draw characters on screen (each UTF8 character is a texture):
int row = 0;
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glVertexPointer(2, GL_FLOAT, 0, vertices);
for (StdStr* line in _lines) {
const char* str = [line cString];
for (int i = 0; i < strlen(str); i++) {
((XX::OGL::GLESContext*)context)->viewport(C_WIDTH*i,
C_HEIGHT*row,
C_WIDTH,
C_HEIGHT);
glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, _textures[0] + *(str + i));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
row++;
}
When there are a lot of characters, the code takes longer to run. In this case, almost 99% of the time is spent in the glDrawArrays
routine. Is it possible to minimise the amount of calls to glDrawArrays
? The OpenGL ES version is 1.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上我认为你应该尝试限制对
viewport
、glBindTexture
和glDrawArrays
的调用量。从技术上讲,您应该将所有角色打包在一个纹理中,以便您可以将其绑定一次。
然后,您可以像实际一样在循环中计算顶点和纹理坐标,但自己进行视口数学计算,并将结果累积到 CPU 数组中。一旦你的数组构成,你应该提交一次绘制调用,提供这个数组。
您可能可以在这里找到灵感:
http://www.angelcode.com/products/bmfont/
http://sourceforge.net/projects/oglbmfont/
Actually I think that you should try to limit the amount of calls to
viewport
,glBindTexture
andglDrawArrays
.Technically, you should pack all your characters in a single texture, so that you can bind it once.
Then, you could compute the vertices and texcoords in a loop like you do actually, but doing the viewport maths yourself, and accumulating results in a CPU array. Once your array constituted, you should submit a draw call once, providing this array.
You can probably find inpiration here:
http://www.angelcode.com/products/bmfont/
http://sourceforge.net/projects/oglbmfont/