Loading raw VBO data via MappedByteBuffer into OpenGL (not working)
I've attempted to load raw, uncompressed VBO data via the method presented in a talk Google did at GDC 2011. This method uses a MappedByteBuffer to quickly load the data in a subsequent call to glBufferData. Unfortunately, for me, it's just not working. I was able to find a hacky work around for it (its commented out in my code below), but I would like to get this working without that hack. Here is a sample of my code:
FileInputStream fis = new FileInputStream(new File(location));
FileChannel fc = fis.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_ONLY, 0, fc.size());
// Hackery because passing mbb to glBufferData isn't working.
//FloatBuffer fb = mbb.asFloatBuffer();
//float triangles[] = new float[fb.capacity()];
//for(int i = 0; i < triangles.length; i++) {
// triangles[i] = fb.get(i);
//}
//fb = FloatBuffer.wrap(triangles);
bu开发者_StackOverflowfferInfo = new int[3];
bufferInfo[0] = newBufferID();
int size = (int)fc.size();
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferInfo[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, size, mbb, GLES20.GL_STATIC_DRAW);
Two years passed since your post, but this is the only entry on Stack Overflow regarding MappedByteBuffer and loading data to VBO and I think I found the solution.
In my opinion problem is not with this code, but with the data you're trying to load. In my case it helped when I've changed endianness of stored vertex data to little endian.
This is just like Google does it in provided example: point are generated using sphere.c, so if I run their program I get values in little endian and that's why their demo works. When I tried to store values from Java using DataOutputStream I got big endian order.
精彩评论