2011-08-27 116 views
5

我写iphone至极一个简单的应用程序显示一个旋转的立方体。 我正在使用glDrawElements(openGl es)绘制立方体的三角形并旋转它。我注意到,当我增加立方体100 * 100 * 100大小的体素的显示性能得到坏 (澄清:我不画整个多维数据集,我只画出它的轮廓(目)我得到的所有三角形通过在立方体上应用行进立方体算法......最终我得到类似于120k三角形的东西来绘制哪些由40k顶点表示)...使用顶点缓冲对象(VBO)在OpenGL ES(iPhone),以提高性能

要绘制立方体我拥有一个顶点数组,颜色和数组,如果索引指向顶点。 indices数组定义了要绘制的顶点的三角形。它作为参数传递给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]; 
} 
+2

首先,你说在100^3体素的性能变得不好。这根本不算意外,因为那将是100万个体素。我假设每个体素是一个立方体,由12个三角形组成。每个渲染调用将会有1200万个三角形。这是*很多*。还要注意,当顶点和颜色数据交错时,VBOs *可能会更有效率。然而,至于iPhone(PowerVR),我不知道是否是这种情况。 – Arne

+0

是的,我没有解释我的自我好,我已经编辑了相应的问题。立方体是100 * 100 * 100,但我不画整个立方体,我只绘制它的轮廓(网格)。通过在立方体上应用行军立方体算法,我得到了网格的所有三角形...最终,我得到了一个像120k三角形的图形,绘制了由40k顶点表示的图形... – alexpov

回答

5

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

苹果的GL优化的建议是here。摘要版本是,您应该使用最小的可接受类型来使用对齐的交错数据的VBO。因此,隐含地,读取数据是一个瓶颈。使用两个单独的列表可能会使您的几何输入速率减半,并且使用浮点数可能会进一步放慢速度。

+0

你好,我已经将你建议的文档它拥有所有的答案,...谢谢:) – alexpov

相关问题