这是一些 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);
}