LWJGL Forum

Programming => OpenGL => Topic started by: Corendos on March 30, 2018, 20:55:53

Title: glNamedBufferStorage not working as expected
Post by: Corendos on March 30, 2018, 20:55:53
Hi,

I'm currently playing a bit with Lwjgl while I'm learning Kotlin.

I'm experiencing some issues with the glNamedBufferStorage function. Whenever I try to fill the buffer using a FloatBuffer, nothing appears on the screen.

The problem is that it's only due to the fact I'm using a FloatBuffer, because when I use a FloatArray, it works as intended. Does anyone have an idea about what I'm missing ?

Here is the code where the problem appears, with the two version :

        val a = FloatArray(data.size * componentCount)
        val buffer: FloatBuffer = FloatBuffer.allocate(data.size * componentCount)

        data.forEach {
            buffer.put(it.x)
            buffer.put(it.y)
            buffer.put(it.z)
        }
        buffer.flip()

        data.forEachIndexed() {
            index, data ->
                a[componentCount * index] = data.x
                a[componentCount * index + 1] = data.y
                a[componentCount * index + 2] = data.z
        }

        GL45.glNamedBufferStorage(id, buffer, flags) // This does not work
        GL45.glNamedBufferStorage(id, a, flags) // This does work


If you need anything else, I'll give it.
Thanks
Title: Re: glNamedBufferStorage not working as expected
Post by: KaiHH on March 30, 2018, 21:01:50
You must only use direct NIO buffers, not ones backed by arrays. LWJGL provides a very convenient BufferUtils class to allocate such buffers.

If you want to delve deeper into the topic of memory management in LWJGL3, read this nice blog entry: https://blog.lwjgl.org/memory-management-in-lwjgl-3/
Title: Re: glNamedBufferStorage not working as expected
Post by: Corendos on March 30, 2018, 21:22:41
Thanks

I tried with direct NIO buffers but the result is the same, nothing appears on the screen.
I checked that the buffer was direct, so the problem might elsewhere.
I tried using FloatBuffer allocated using BufferUtils.createFloatBuffer, do I need to do something else for it to work ?

Here is the code

val buffer = BufferUtils.createFloatBuffer(data.size * componentCount)
        println(buffer.isDirect)

        data.forEach {
            buffer.put(it.x)
            buffer.put(it.y)
            buffer.put(it.z)
        }
        buffer.flip()

        data.forEachIndexed() {
            index, data ->
                a[componentCount * index] = data.x
                a[componentCount * index + 1] = data.y
                a[componentCount * index + 2] = data.z
        }

        GL45.glNamedBufferStorage(id, buffer, flags)


EDIT: Everything is working, I did a beginner mistake. Thank you for your help :)