如何将浮点数数组转换为java.nio.Buffer以供glVertexPointer使用?

4
我正在使用libgdx,并有以下代码:
    float[] x;
    ...
    Buffer vVertices=x;//what to do here?
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vVertices);

但我不确定如何将数组 x 传递给缓冲区?有什么想法吗?

2个回答

5
ByteBuffer byteBuf = ByteBuffer.allocateDirect(x.length * Float.BYTES); //4 bytes per float
byteBuf.order(ByteOrder.nativeOrder());
FloatBuffer buffer = byteBuf.asFloatBuffer();
buffer.put(x);
buffer.position(0);

ByteBuffer和FloatBuffer都继承自Buffer。


4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接