0

I initialized the content of my FloatBuffer with an array of Float once. And then modify the Floats that I have put in, I would expect that it modifies also the values in the FloatBuffer but I obtain a weird result.

Am I doing wrong ? Is it possible to do that ?

EDIT: My mistake, I understood that Float was immutable so this is a normal behaviour. So my question would be, is there a way to fill FloatBuffer with mutable float wrappers so that I can easily modify my FloatBuffer content by reference ? Or is there a nicer alternative to FloatBuffer for OpenGL data transfer?

user2591935
  • 299
  • 1
  • 14

1 Answers1

0

The FloatBuffer has methods that do what you want if I understand you correctly.

With the FloatBuffer you can set its position by calling the position method (don't forget to return it to 0 when done, opengl es needs it at 0 if I remember correctly). Then you can read or write to that position with get and put methods.

So if you need to update the content of native memory you can do so from the FloatBuffer that allocated it like so:

    floatBuffer.position(position);
    floatBuffer.put(data, startIndex, count);
    floatBuffer.position(0);
satm12
  • 60
  • 1
  • 6
  • This is not what I want to do. I want to put a value once, and change the value that I've put from outside and have the value also changed in the FloatBuffer. But I think this is not possible with FloatBuffers since they only keep 'float' values and you can't do this with primitive types, so I can't change their value by reference – user2591935 Dec 28 '16 at 19:56
  • FloatBuffers allocate to native memory, where opengl es needs its data stored. The allocated memory is saved as an array therefore it is a reference. If you want a java object to access directly native memory I don't think it is possible. – satm12 Dec 28 '16 at 20:05