sravan
sravan

Reputation: 138

How to pass float array to glVertexAttribPointer

i'm working on OpenGL ES 2.0 shaders on android...

i have a float array with position of vertices along with other attributes of vertices. position and other attributes may change over time.

how can i pass this modified array to glVertexAttribPointer, so that i can draw the scene with updated values

when i tried to pass it, i got

The method glVertexAttribPointer(int, int, int, boolean, int, Buffer) in the type GLES20 is not applicable for the arguments (int, int, int, boolean, int, float[])

Upvotes: 1

Views: 1688

Answers (2)

Kamen
Kamen

Reputation: 3595

Is it java.nio.Buffer? Because it seems you need

java.nio.FloatBuffer

and call floatBuffer.array()

to get the float[] array out of it.

But, obviously it is final in FloatBuffer.java:

final float[] hb;   

So it seems that you'll need to... maybe extend FloatBuffer and make the array non-final.

Upvotes: 0

Egor
Egor

Reputation: 40203

FloatBuffer yourFloatBuffer;
float[] yourFloatArray;    

FloatBuffer byteBuf = ByteBuffer.allocateDirect(yourFloatArray.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    yourFloatBuffer = byteBuf.asFloatBuffer();
    yourFloatBuffer.put(yourFloatArray);
    yourFloatBuffer.position(0);

This should work.

Upvotes: 2

Related Questions