I'm removing some native C code from an Android app which used a memcpy to copy an array of floats into a FloatBuffer (because an old device I had was very slow at this).
My current code looks like this:
mfVertexBuffer.position(0);
mfColourBuffer.position(0);
mfSizeBuffer.position(0);
mfVertexBuffer.put(fvertices, 0, nCount*2);
mfColourBuffer.put(fcolours, 0, nCount*4);
mfSizeBuffer.put(fsizes, 0, nCount);
I've looked at the state of mfVertexBuffer and it looks like it's behaving; .position gets incremented sensibly, and the contents of the underlying buffer seem sensible. However, nothing is being rendered onscreen (by a later process).
I rewrote the above - changing nothing else in my app whatsoever - to the following:
int vs=0;
int vd=0;
int cs=0;
int cd=0;
int ss=0;
int sd=0;
for (int i = 0; i < nCount; i++) {
mfVertexBuffer.put(vd++, fvertices[vs++]);
mfVertexBuffer.put(vd++, fvertices[vs++]);
mfColourBuffer.put(cd++, fcolours[cs++]);
mfColourBuffer.put(cd++, fcolours[cs++]);
mfColourBuffer.put(cd++, fcolours[cs++]);
mfColourBuffer.put(cd++, fcolours[cs++]);
mfSizeBuffer.put(sd++, fsizes[ss++]);
}
And it works perfectly. I don't understand this at all. How do the two pieces of code differ?
The FloatBuffers are set up like this:
mfVertexBuffer = miscHelpers.getFloatBuffer(4 * NUMBER_OF_PARTICLES * 2);
mfColourBuffer = miscHelpers.getFloatBuffer(4 * NUMBER_OF_PARTICLES * 4);
mfSizeBuffer = miscHelpers.getFloatBuffer(4 * NUMBER_OF_PARTICLES);
public static FloatBuffer getFloatBuffer(int size) {
FloatBuffer fbRet;
ByteBuffer myBB = ByteBuffer.allocateDirect(size);
myBB.order(ByteOrder.nativeOrder());
fbRet = myBB.asFloatBuffer();
return fbRet;
}
fvertices etc are simple arrays of floats, the values of which do not change.
count is the number of particles to plot this round.
NUMBER_OF_PARTICLES is a compile time limit on the maximum number of particles which can be created, and is always more than or equal to count, so the first (non-working) code is doing more work, but behaves the same if I set NUMBER_OF_PARTICLES to the same value as count.
I'm using OpenGL 1.1, Android API 17 on a Galaxy S3 running 4.2.2 (Cyanogenmod 10.1.3).