OpenGl es 2.0 Geometry实例化

时间:2011-04-25 04:48:26

标签: opengl-es opengl-es-2.0

我有170个要绘制的对象,每个都是从312个顶点构建的。 我有一个对象,我用不同的martixes绘制了170次,我已经发现我不需要调用一些函数,如果我逐个绘制它们,所以我只在开始时调用它们,这给了我大约5fps,我我正在使用非索引三角形和drawArrays。

if(!started)
{
    glUseProgram( __programObject );
    glEnableVertexAttribArray(attPosition);
    glVertexAttribPointer(attPosition, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), vVertices);//3*sizeof(float)
    glEnableVertexAttribArray(attNormals);
    glVertexAttribPointer(attNormals, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), vNormals);//3*sizeof(float)
}

有没有办法让它在es 2.0下更快?我在sgx 540上得到大约23fps, 将顶点细节降低到每个对象36不会增加帧速率,矩阵计算中有大约10fps(缩放,乘法,平移,转换,反转)但它们是在cpu上制作的,我不认为将它们移动到着色器是个好主意。我知道每次传球都会消耗大部分时间。我知道有一种方法来实例化对象并传递制服并在一次调用中绘制它但我找不到任何描述它的教程,你知道我在哪里可以找到它吗?

2 个答案:

答案 0 :(得分:3)

尝试在一个数组中包含法线和顶点,如下所示:

sqTex.getVertexBuffer().position(sqTex.VERT_OFFSET);
GLES20.glVertexAttribPointer(
                                GLES20.glGetAttribLocation(programTextured, "aPosition"), 3,
                                GLES20.GL_FLOAT, false, 5 * 4, sqTex.getVertexBuffer());
GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(programTextured, "aPosition"));

sqTex.getVertexBuffer().position(sqTex.TEXT_OFFSET);
GLES20.glVertexAttribPointer(
                                GLES20.glGetAttribLocation(programTextured, "aTextureCoord"), 2,
                                GLES20.GL_FLOAT, false, 5 * 4, sqTex.getVertexBuffer());
GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(programTextured, "aTextureCoord"));

在这个例子中,我有一个顶点和tex坐标数组

来自OpenGL ES 2.0编程指南:

 How to store different attributes of a vertex
We described the two most common ways of storing vertex attributes—
array of structures and structure of arrays. The question to ask is which allocation
method would be the most efficient for OpenGL ES 2.0 hardware
implementations. The answer is array of structures. The reason is that the
attribute data for each vertex can be read in sequential fashion and so will
most likely result in an efficient memory access pattern. A disadvantage of
using array of structures is when an application wants to modify specific
attributes. If a subset of vertex attribute data needs to be modified (e.g., texture
coordinates), this will result in strided updates to the vertex buffer.
When vertex buffer is supplied as a buffer object, the entire vertex attribute
buffer will need to be reloaded. One can avoid this inefficiency by storing
vertex attributes that are dynamic in nature in a separate buffer.

那本书也有以这种方式使用的例子

答案 1 :(得分:1)

您可以尝试通过属性而不是制服传递每个实例的数据。它有时会帮助桌面OpenGL。

因此,顶点着色器将如下所示:

attribute vec3 position;
attribute mat4 proj_view_matrix;
void main()
{
    gl_Position = proj_view_matrix * position;
}

渲染代码如下所示:

glUseProgram( programObject );
glEnableVertexAttribArray(attPosition);
glVertexAttribPointer(attPosition, ..., pPositions);
glDisableVertexAttribArray(attProjViewMatrix+0);
glDisableVertexAttribArray(attProjViewMatrix+1);
glDisableVertexAttribArray(attProjViewMatrix+2);
glDisableVertexAttribArray(attProjViewMatrix+3);

foreach( instance )
{
    glVertexAttrib4fv(attProjViewMatrix+0, instance.proj_view_matrix.column0);
    glVertexAttrib4fv(attProjViewMatrix+1, instance.proj_view_matrix.column1);
    glVertexAttrib4fv(attProjViewMatrix+2, instance.proj_view_matrix.column2);
    glVertexAttrib4fv(attProjViewMatrix+3, instance.proj_view_matrix.column3);
    glDrawArrays( ... );
}