Search code examples
javaopengllwjgl

Why use Buffers in OpenGL (LWJGL) if simple Arrays work?


So I am currently learning OpenGL and the tutorial I am following says:

The first thing that we must do is to store our array of floats into a FloatBuffer. This is mainly due to the fact that we must interface with the OpenGL library, which is C-based, so we must transform our array of floats into something that can be managed by the library.

And I totally get the point, but what was strange that every OpenGL method provided by LWJGL works with Buffers, but also with simple Arrays.

Why is this the case and why should I use buffers then?

Some examples:

This works with indices being an IntBuffer but also being a int[].

glBufferData(GL_ARRAY_BUFFER, indices, GL_STATIC_DRAW);

This works with value being a FloatBuffer but also being a float[].

glUniformMatrix4fv(
     uniforms.get(uniformName),
     false,
     value
);

Solution

  • Based on the quote, I'm assuming the tutorial wasn't written for LWJGL 3.

    LWJGL 3.0.0 was the first version to add support to "Java array parameters" flavored methods that previously only used/supported NIO buffers.

    Added support for Java array parameters and HotSpot Critical Natives.

    Based on a discussion that can be found in issue 175