1

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[])

genpfault
  • 51,148
  • 11
  • 85
  • 139
sravan
  • 138
  • 1
  • 10
  • The error is telling you that you need to take that array of float vertexes you have and somehow convert it into a buffer object. I'm no openGL expert, so thats as far as I can answer this. – Eric May 28 '11 at 08:49
  • thanx Eric, but i cant modify the buffer... so want to use arrays intead – sravan May 28 '11 at 09:03

2 Answers2

2
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.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • this works Egor, i know that. but i want to modify the float array later during runtime... with this code i can't achieve – sravan May 28 '11 at 09:07
  • I thinks, you should perform this code everytime you change the array. – Egor May 28 '11 at 09:09
  • ya we can do that, but the problem is when i pass the buffer to shader, GPU might be using it and i dont have control over it... may be thats why i'm getting exception – sravan May 28 '11 at 09:21
0

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.

Kamen
  • 3,575
  • 23
  • 35