Is it possible to modify by reference the content of a FloatBuffer?

324 views Asked by At

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?

1

There are 1 answers

2
satm12 On

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