Search code examples
javaniolibgdx

How to convert float array to java.nio.Buffer for glVertexPointer?


I am using libgdx and have following code

    float[] x;
    ...
    Buffer vVertices=x;//what to do here?
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vVertices);

But I am not sure how I can pass array x to Buffer? Any idea?


Solution

  • 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);
    

    Both ByteBuffer and FloatBuffer inherit from Buffer.