KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/client/gl/VertexArrayObject.kt

58 lines
1.3 KiB
Kotlin

package ru.dbotthepony.kstarbound.client.gl
import org.lwjgl.opengl.GL46.*
import java.io.Closeable
class VertexArrayObject(val state: GLStateTracker) : Closeable {
val pointer = glGenVertexArrays()
init {
checkForGLError()
}
private val cleanable = state.registerCleanable(this, ::glDeleteVertexArrays, pointer)
fun bind(): VertexArrayObject {
check(isValid) { "Tried to use NULL GLVertexArrayObject" }
return state.bind(this)
}
fun unbind(): VertexArrayObject {
check(isValid) { "Tried to use NULL GLVertexArrayObject" }
return state.unbind(this)
}
fun attribute(position: Int, size: Int, type: Int, normalize: Boolean, stride: Int, offset: Long = 0L): VertexArrayObject {
check(isValid) { "Tried to use NULL GLVertexArrayObject" }
state.ensureSameThread()
glVertexAttribPointer(position, size, type, normalize, stride, offset)
checkForGLError()
return this
}
fun enableAttribute(position: Int): VertexArrayObject {
check(isValid) { "Tried to use NULL GLVertexArrayObject" }
state.ensureSameThread()
glEnableVertexArrayAttrib(pointer, position)
//glEnableVertexAttribArray(position)
checkForGLError()
return this
}
var isValid = true
private set
override fun close() {
state.ensureSameThread()
if (!isValid) return
if (state.VAO == this) {
state.VAO = null
}
cleanable.clean()
isValid = false
}
}