what is the difference between the 2 statements

Started by kululu123, September 17, 2013, 21:34:43

Previous topic - Next topic

kululu123

I'm new to openGL and lwjgl so I started learning abit and lets say I want to make a new buffer of type float and store allocate enough data to store my array of vertecies so 1 code is using the BufferUtils class to allocate space to the array (atleast thats what I think the method "createFloatBuffer" does because I didnt completly understand the doc [englihs isnt my main lang], and the second code using the allocate func of java's default buffer class)


FloatBuffer fBuffer = BufferUtils.createFloatBuffer(fArray.length);
FloatBuffer fBuffer = FloatBuffer.allocate(fArray.length);

Cornix

The buffers used by LWJGL need to be direct buffers. If you just use FloatBuffer.allocate(x) then this will not be a direct buffer and thus does not work with LWJGL.
In order to create a direct buffer you have to do this:
FloatBuffer buffer = ByteBuffer.allocateDirect(floatArray.length * FLOAT_SIZE_IN_BYTES).order(ByteBuffer.nativeOrder()).toFloatBuffer();

And this is exactly what the BufferUtil-Class is doing.

Here is the source code if you want to see for yourself:
http://grepcode.com/file/repo1.maven.org/maven2/org.lwjgl.lwjgl/lwjgl/2.8.0/org/lwjgl/BufferUtils.java#BufferUtils.createByteBuffer%28int%29

kululu123

Quote from: Cornix on September 17, 2013, 21:59:23
The buffers used by LWJGL need to be direct buffers. If you just use FloatBuffer.allocate(x) then this will not be a direct buffer and thus does not work with LWJGL.
In order to create a direct buffer you have to do this:
FloatBuffer buffer = ByteBuffer.allocateDirect(floatArray.length * FLOAT_SIZE_IN_BYTES).order(ByteBuffer.nativeOrder()).toFloatBuffer();

And this is exactly what the BufferUtil-Class is doing.

Here is the source code if you want to see for yourself:
http://grepcode.com/file/repo1.maven.org/maven2/org.lwjgl.lwjgl/lwjgl/2.8.0/org/lwjgl/BufferUtils.java#BufferUtils.createByteBuffer%28int%29

mind explaining me what do you mean by direct buffer ?