LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: kululu123 on September 17, 2013, 21:34:43

Title: what is the difference between the 2 statements
Post by: kululu123 on September 17, 2013, 21:34:43
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);
Title: Re: what is the difference between the 2 statements
Post by: 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
Title: Re: what is the difference between the 2 statements
Post by: kululu123 on September 17, 2013, 22:29:37
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 ?
Title: Re: what is the difference between the 2 statements
Post by: Cornix on September 17, 2013, 22:39:49
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html
Title: Re: what is the difference between the 2 statements
Post by: kululu123 on September 18, 2013, 10:52:21
Quote from: Cornix on September 17, 2013, 22:39:49
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html
so if I use allocateDirect() lwjgl could use this buffer?