Hello Guest

Buffer Usage in LWJGL

  • 2 Replies
Buffer Usage in LWJGL
« on: October 02, 2012, 22:01:18 »
LWJGL uses Java.nio Buffers as parameters for various methods, in Java buffers are native memory wrapped in an object with internal fields like limit, capacity, etc.
Which of these buffer fields/values does LWJGL adhere to when a method like glBufferData() is called?

If I pass a buffer of allocated size 1MB to a LWJGL method that requires a buffer, but if I only fill the first 2800 bytes and set the limit to 2800, what does LWJGL read? All 1MB or just 2800 bytes?

In short, does LWJGL obey limit() or capacity() when uploading buffered data?

Thanks for your time  :)
« Last Edit: October 02, 2012, 22:03:02 by TeamworkGuy2 »

Re: Buffer Usage in LWJGL
« Reply #1 on: October 03, 2012, 12:34:07 »
Here is the code for glBufferData with a FloatBuffer. Notice it uses the remaining() method to determine how much data it has and uses position() to determine where to start reading data:
Code: [Select]
public static void glBufferData(int target, FloatBuffer data, int usage) {
ContextCapabilities caps = GLContext.getCapabilities();
long function_pointer = caps.glBufferData;
nglBufferData(target, (data.remaining() << 2), data, data.position() << 2, usage, function_pointer);
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: Buffer Usage in LWJGL
« Reply #2 on: October 03, 2012, 12:48:37 »
Ok, cool, I was afraid I was going to have to use slice() before uploading a buffer.
I guess I will just need to remember to set position() and limit() before send a buffer to LWJGL.
Thanks so much for your time and advice :)