开发者

Converting OpenGL draw lists to vertex arrays or VBOs

I'm trying to convert a program using draw lists, which are deprecated in OpenGL 3.0+, to use either vertex arrays or VBOs, but I'm not f开发者_运维知识库inding any examples of how to do the conversion.

What's in the program now is this (happens to be Python, but really what I'm interested in is the appropriate OpenGL calls---it could just as well be C++, for example):

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()

We're mapping a texture onto a rectangle. Then later we're drawing it somewhere:

glCallList(dl)

How do I convert this to use vertex arrays? VBOs?


Here's some Java code, in C the FloatBuffers would be arrays.

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜