【问题标题】:Using Vertex Buffer Objects (VBO) in OpenGL es (Iphone) to improve performance在 OpenGL es (Iphone) 中使用顶点缓冲对象 (VBO) 来提高性能
【发布时间】:2011-11-05 03:08:04
【问题描述】:

我正在为 iphone 编写一个显示旋转立方体的简单应用程序。 我正在使用 glDrawElements (openGl es) 来绘制立方体的三角形并旋转它。我注意到当我将立方体的大小增加到 100*100*100 体素时,显示性能会变差 (澄清:我不画整个立方体,我只画它的轮廓(网格)。我通过在立方体上应用行进立方体算法来获得网格的所有三角形......最终我得到了类似 120k 三角形的东西,它们是由 40k 个顶点表示)...

为了绘制立方体,我持有一个顶点数组、颜色数组和一个顶点索引数组。索引数组定义要绘制的顶点的三角形。它作为参数传递给 glDrawElements。

最近我对使用顶点缓冲区对象 (VBO) 绘制立方体的不同技术感到不满。我已经实现了它,但性能比以前的技术更差

这是我的代码,也许我犯了一个愚蠢的错误,任何改进建议都会受到好评:)

顺便说一句,我参考了以下文章:

http://playcontrol.net/ewing/jibberjabber/opengl_vertex_buffer_object.html http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html

//all the 7 variables down are initialized by other function at the beginning 
GLushort* meshIndices;    //array of indices (ushort)
MeshVertex* meshVertices; //array of vertices (floats)
Color3D* meshColors;      //array of colors  (floats)

int numberOfTriangles; //number of Triangle to draw the cube
int numberOfVertices;  //number of all Vertices to draw the cube
int numberOfIndices;   //number of all Indices to draw the cube, each 3 indices define 3 vertices which define 1 triangle
int numberOfColors;    //number of colors used to draw the cube. each color is of tip Color3D

//in this function i initializing the VBOs 
- (void) setupMeshVBOs { 

    glGenBuffers(1, &triangleVBO);
    glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);

    const GLsizeiptr vertex_size = numberOfVertices * sizeof(MeshVertex);
    const GLsizeiptr color_size = numberOfColors * sizeof(Color3D);

    glBufferData(GL_ARRAY_BUFFER, vertex_size + color_size, 0, GL_STATIC_DRAW);

    GLvoid* vbo_buffer = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES); 
    memcpy(vbo_buffer, meshVertices, vertex_size);

    GLbyte* temp = (GLbyte*)vbo_buffer;
    temp += vertex_size;
    memcpy((GLvoid*)temp, meshColors, color_size);

    glUnmapBufferOES(GL_ARRAY_BUFFER); 

    glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)((char*)NULL));

    glColorPointer(4, GL_FLOAT, 0, (GLvoid*)((char*)NULL+vertex_size));

    glGenBuffers(1, &triangleIBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfIndices * sizeof(GLushort), meshIndices, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

//this function is the one which draws the VBOs
- (void)drawView:(GLView*)view;
{

    static GLfloat rot = 0.0;


    glLoadIdentity();
    glTranslatef(-1.0f,-2.0f,-20.0f);
    glRotatef(rot,1.0f,1.0f,1.0f);
    glClearColor(0.7, 0.7, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIBO);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);

        glDrawElements(GL_TRIANGLE_STRIP, numberOfIndices, GL_UNSIGNED_SHORT, (GLvoid*)((char*)NULL));

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);

    static NSTimeInterval lastDrawTime;
    if (lastDrawTime)
    {
        NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
        rot+=50 * timeSinceLastDraw;                
    }
    lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
}

【问题讨论】:

  • 首先,您说在 100^3 体素处性能变差。这一点也不奇怪,因为那将是 100 万个体素。我假设每个体素是一个立方体,由 12 个三角形组成。这将是每个渲染调用 1200 万个三角形。那是很多。另请注意,当顶点和颜色数据交错时,VBO可能更有效。不过,至于iPhone(PowerVR)我不知道是不是这样。
  • 是的,我没有很好地解释我的自我,我已经相应地编辑了问题。立方体是 100*100*100 但我不画整个立方体,我只画它的轮廓(网格)。我通过在立方体上应用行进立方体算法来获得网格的所有三角形......最终我得到了类似于 120k 三角形的东西,它们由 40k 个顶点表示......

标签: objective-c performance ios4 opengl-es vbo


【解决方案1】:

首先,要绘制 100x100x100 的立方体地图,您不应单独绘制每个立方体。如果有,比如说,连续六个盒子,那么你应该把它们画成一个长方体,总共十二个三角形。任何被六面包围的立方体绝对不需要考虑。您应该应用类似的策略来显着减少几何数。

Apple 对 GL 优化的建议是here。总结版本是,您应该致力于使用具有对齐、交错数据的 VBO,使用最小的可接受类型。因此,隐含地,读取数据是一个瓶颈。可以想象,使用两个单独的列表可能会使几何输入速率减半,而使用浮点数可能会进一步减慢速度。

【讨论】:

  • 你好,我已经把你建议的文档弄红了,它包含了所有的答案,...谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多