opengl: abstract vao

Thought I am smarter than opengl, but it totally wasn't good. Keeping the abstraction though
This commit is contained in:
Moritz Zwerger 2023-12-14 23:32:49 +01:00
parent e7daeed5ae
commit 14ee590e98
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 79 additions and 34 deletions

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -21,7 +21,7 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import java.nio.FloatBuffer
class DummyVertexBuffer(
override val structure: MeshStruct,
override val struct: MeshStruct,
override var buffer: FloatBuffer,
override val type: RenderableBufferTypes = RenderableBufferTypes.ARRAY_BUFFER,
) : FloatVertexBuffer {

View File

@ -26,7 +26,6 @@ object RegistriesLoader {
registries.setDefaultParents(version)
return registries
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -18,7 +18,7 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
interface VertexBuffer {
val vertices: Int
val primitiveType: PrimitiveTypes
val structure: MeshStruct
val struct: MeshStruct
fun draw()
}

View File

@ -21,39 +21,29 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveType
import de.bixilon.minosoft.gui.rendering.system.opengl.OpenGLRenderSystem
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.FloatOpenGLBuffer
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
import org.lwjgl.opengl.GL20.glVertexAttribPointer
import org.lwjgl.opengl.GL30.*
import java.nio.FloatBuffer
class FloatOpenGLVertexBuffer(
renderSystem: OpenGLRenderSystem,
override val structure: MeshStruct,
override val struct: MeshStruct,
data: FloatBuffer,
override val primitiveType: PrimitiveTypes,
) : FloatOpenGLBuffer(renderSystem, data), FloatVertexBuffer {
private val vao = OpenGLVAO(renderSystem, struct)
override var vertices = -1
private set
private var vao = -1
override fun init() {
val floatsPerVertex = structure.BYTES_PER_VERTEX / Float.SIZE_BYTES
val floatsPerVertex = struct.BYTES_PER_VERTEX / Float.SIZE_BYTES
vertices = buffer.position() / floatsPerVertex
vao = glGenVertexArrays()
super.init()
glBindVertexArray(vao)
super.initialUpload()
bind()
super.initialUpload()
_data = null
for (attribute in structure.attributes) {
glVertexAttribPointer(attribute.index, attribute.size, GL_FLOAT, false, structure.BYTES_PER_VERTEX, attribute.stride)
glEnableVertexAttribArray(attribute.index)
}
vao.init()
unbind()
}
@ -63,16 +53,12 @@ class FloatOpenGLVertexBuffer(
return
}
super.unbind()
glBindVertexArray(0)
vao.unbind()
}
fun bindVao() {
super.bind()
if (renderSystem.boundVao == vao) {
return
}
glBindVertexArray(vao)
renderSystem.boundVao = vao
vao.bind()
}
override fun draw() {
@ -82,13 +68,7 @@ class FloatOpenGLVertexBuffer(
}
override fun unload() {
if (state == RenderableBufferStates.UPLOADED) {
glDeleteVertexArrays(vao)
if (renderSystem.boundVao == vao) {
renderSystem.boundVao = -1
}
vao = -1
}
vao.unload()
super.unload()
}
@ -103,5 +83,4 @@ class FloatOpenGLVertexBuffer(
}
}
}
}

View File

@ -0,0 +1,67 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.vertex
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.system.opengl.OpenGLRenderSystem
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
import org.lwjgl.opengl.GL20.glVertexAttribPointer
import org.lwjgl.opengl.GL30.*
class OpenGLVAO(
val system: OpenGLRenderSystem,
val struct: MeshStruct
) {
private var handle = -1
fun init() {
if (handle >= 0) throw IllegalArgumentException("VAO already loaded!")
handle = glGenVertexArrays()
bind()
for (attribute in struct.attributes) {
glVertexAttribPointer(attribute.index, attribute.size, GL_FLOAT, false, struct.BYTES_PER_VERTEX, attribute.stride)
glEnableVertexAttribArray(attribute.index)
}
unbind()
}
fun bind() {
if (handle < 0) throw IllegalArgumentException("VAO not initialized!")
if (system.boundVao == handle) {
return
}
glBindVertexArray(handle)
system.boundVao = handle
}
fun unbind() {
if (RenderConstants.DIRTY_BUFFER_UNBIND) {
return
}
if (handle < 0) throw IllegalArgumentException("VAO not initialized!")
glBindVertexArray(-1)
system.boundVao = -1
}
fun unload() {
if (handle < 0) throw IllegalArgumentException("VAO not initialized!")
glDeleteVertexArrays(handle)
if (system.boundVao == handle) {
system.boundVao = -1
}
handle = -1
}
}