Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

nio

libgdx

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?

like image 932
Tae-Sung Shin Avatar asked Jan 18 '23 22:01

Tae-Sung Shin


1 Answers

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.

like image 166
Robert Rouhani Avatar answered Feb 23 '23 16:02

Robert Rouhani