2011-04-05 102 views
2

我想使用繪製列表(它在OpenGL 3.0+中不推薦使用)來使用頂點數組或VBO來轉換程序,但是我沒有找到任何示例做轉換。將OpenGL繪製列表轉換爲頂點數組或VBOs

什麼是在程序現在是這樣的(恰好是Python,但真的是我很感興趣的是適當的OpenGL調用---它也可以同樣是C++爲例):

dl = glGenLists(1) 
glNewList(dl, GL_COMPILE) 
glBindTexture(GL_TEXTURE_2D, texture) 
glBegin(GL_QUADS) 
glTexCoord2f(0, 0) 
glVertex2f(0, 0) 
glTexCoord2f(0, 1) 
glVertex2f(0, height) 
glTexCoord2f(1, 1) 
glVertex2f(width, height) 
glTexCoord2f(1, 0) 
glVertex2f(width, 0) 
glEnd() 
glEndList() 

我們將紋理映射到矩形上。然後,我們將其繪製在某處:

glCallList(dl) 

如何將此轉換爲使用頂點數組?維也納組織?

回答

3

下面是一些Java代碼,在C中FloatBuffers將是數組。

public VBO createVBO(List<Vec2> vertexList, List<Vec2> texelList){ 
    FloatBuffer vertexBuffer = FloatBuffer.allocate(vertexList.size()*3); 
    FloatBuffer texelBuffer = FloatBuffer.allocate(texelList.size()*2); 
    for(Vec2 point : vertexList){ 
     vertexBuffer.put(point.x); 
     vertexBuffer.put(point.y); 
     vertexBuffer.put(0); 
    } 
    vertexBuffer.flip(); 
    for(Vec2 point : texelList){ 
     texelBuffer.put(point.x); 
     texelBuffer.put(point.y); 
    } 
    texelBuffer.flip(); 

    int[] vbo = new int[2]; 
    gl.glGenBuffers(1, vbo, 0); 
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo[0]); 
    gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexBuffer.capacity()*4, vertexBuffer, GL2.GL_STATIC_DRAW); 
    gl.glGenBuffers(1, vbo, 1); 
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo[1]); 
    gl.glBufferData(GL.GL_ARRAY_BUFFER, texelBuffer.capacity()*4, texelBuffer, GL2.GL_STATIC_DRAW); 

    return new VBO(vbo[0], vbo[1], vertexList.size()); 
} 

public void renderVBO(VBO vbo){ 
    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY); 

    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo.vertices); 
    gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);  
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo.texels); 
    gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0); 

    gl.glDrawArrays(GL.GL_TRIANGLE_FAN, 0, vbo.length); 

    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY); 
    gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY); 
}