将OpenGL绘图列表转换为顶点数组或VBO

时间:2011-04-05 09:58:30

标签: opengl vbo vertex-array draw-list

我正在尝试使用在OpenGL 3.0+中弃用的绘图列表来转换程序,以使用顶点数组或VBO,但我找不到任何有关如何进行转换的示例。

现在程序中有什么(碰巧是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)

如何将其转换为使用顶点数组?维也纳各组织?

1 个答案:

答案 0 :(得分: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);
}