Search code examples
openglkotlinlwjgl

GL_INVALID_OPERATION on glBufferData() in kotlin


I am currently creating a game in Kotlin with OpenGL via LWJGL (for learning Kotlin). I created a class representing a VBO, which looks like this:

package de.pascal_riesinger.Testing.gfx

import de.pascal_riesinger.Testing.Log
import de.pascal_riesinger.Testing.LogLevel
import de.pascal_riesinger.Testing.logGLError
import org.lwjgl.opengl.GL15
import java.nio.FloatBuffer

class VBO() {

    private var id: Int = 0

    init {
        Log(LogLevel.DEBUG, "VBO", "Allocating new VBO")
        id = GL15.glGenBuffers()
        Log(LogLevel.DEBUG, "VBO", "Allocated VBO with id $id")
    }

    fun bufferData(data: FloatBuffer, usage: Int) {
        bind()
        GL15.glBufferData(id, data, usage)
        unbind()
    }

    fun bind() {
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, id)
    }

    fun unbind() {
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0)
    }

}

Yes, I know that this code is not that beautiful, but I'm still very new to Kotlin's concepts. Now when I run the following snippet:

  var vbo = VBO()
  var vertexBuffer = BufferUtils.createFloatBuffer(6)
  vertexBuffer.put(vertices)
  vertexBuffer.flip()

  //vbo.bind()
  vbo.bufferData(vertexBuffer, GL15.GL_STATIC_DRAW)

I will get an OpenGL error 1282, which is GL_INVALID_OPERATION right after the call to glBufferData().

I googled for the error and per the OpenGL specification, GL_INVALID_OPERATION is thrown by glBufferData() only in the following two cases:

An INVALID_OPERATION error is generated by BufferData if zero is bound to target.

An INVALID_OPERATION error is generated if the BUFFER_IMMUTABLE_STORAGE flag of the buffer object is TRUE.

The latter one cannot be the case, as my buffer is not immutable, and I verified that the generated ID of the buffer is not zero (it seems to always be one).

Note that uncommenting the vbo.bind() line does not fix this.

Thanks for your help!


Solution

  • The first parameter of GL15.glBufferData has to be a target (in your case GL15.GL_ARRAY_BUFFER), not the handle of the buffer.