簡體   English   中英

在OpenGL ES(Iphone)中使用頂點緩沖對象(VBO)來提高性能

[英]Using Vertex Buffer Objects (VBO) in OpenGL es (Iphone) to improve performance

我正在為iphone編寫一個簡單的應用程序,以顯示旋轉的立方體。 我正在使用glDrawElements(openGl es)繪制立方體的三角形並旋轉它。 我注意到,當我將立方體的尺寸增加到100 * 100 * 100體素時,顯示性能會變差(澄清:我沒有繪制整個立方體,只繪制了輪廓(網格)。我得到了所有的三角形通過在多維數據集上應用行進多維數據集算法來划分網格...最終,我得到了類似於120k三角形的圖形,這些圖形由40k個頂點表示)...

為了繪制立方體,我持有一個頂點數組,一個顏色數組和一個頂點索引數組。 indexs數組定義要繪制的頂點的三角形。 它作為參數傳遞給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];
}

首先,要繪制100x100x100的多維數據集地圖,您不應單獨繪制每個多維數據集。 例如,如果連續有六個框,則應將它們繪制為一個長的長方體,總共十二個三角形。 絕對不需要考慮被六個側面包圍的任何多維數據集。 您應該應用類似的策略來顯着減少幾何數量。

蘋果關於GL優化的建議在這里 摘要版本是您的目標是將VBO與對齊的,交錯的數據一起使用,並使用最小的可接受類型。 因此,隱式地,讀取數據是一個瓶頸。 可以想象,使用兩個單獨的列表可以使幾何輸入速率減半,而使用浮點數則可能會進一步降低它的速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM