2011-08-27 41 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

相關問題