wip: abstract render buffers

This commit is contained in:
Bixilon 2021-07-08 11:58:17 +02:00
parent b3c0dfe168
commit 875d75c2bc
23 changed files with 281 additions and 125 deletions

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.gui.rendering.block
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.data.registries.effects.DefaultStatusEffects
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.opengl.FloatUniformBuffer
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.FloatOpenGLUniformBuffer
import de.bixilon.minosoft.gui.rendering.util.VecUtil.ONE
import de.bixilon.minosoft.gui.rendering.util.VecUtil.clamp
import de.bixilon.minosoft.gui.rendering.util.VecUtil.lerp
@ -33,7 +33,7 @@ import kotlin.math.sin
class LightMap(private val connection: PlayConnection) {
private val nightVisionStatusEffect = connection.registries.statusEffectRegistry[DefaultStatusEffects.NIGHT_VISION]
private val conduitPowerStatusEffect = connection.registries.statusEffectRegistry[DefaultStatusEffects.CONDUIT_POWER]
private val uniformBuffer = FloatUniformBuffer(1, FloatArray(16 * 16 * 4) { 1.0f })
private val uniformBuffer = FloatOpenGLUniformBuffer(1, FloatArray(16 * 16 * 4) { 1.0f })
fun init() {

View File

@ -119,7 +119,7 @@ class WorldRenderer(
}
}
if (meshCollection.transparentSectionArrayMesh!!.primitiveCount == 0) {
if (meshCollection.transparentSectionArrayMesh!!.data.isEmpty) {
meshCollection.transparentSectionArrayMesh = null
}
return meshCollection
@ -332,24 +332,24 @@ class WorldRenderer(
sectionMap[index]?.let {
it.opaqueSectionArrayMesh.unload()
meshes--
triangles -= it.opaqueSectionArrayMesh.primitiveCount
triangles -= it.opaqueSectionArrayMesh.vertices
it.transparentSectionArrayMesh?.let {
it.unload()
meshes--
triangles -= it.primitiveCount
triangles -= it.vertices
}
}
meshCollection.opaqueSectionArrayMesh.let {
it.load()
meshes++
triangles += it.primitiveCount
triangles += it.vertices
}
meshCollection.transparentSectionArrayMesh?.let {
it.load()
meshes++
triangles += it.primitiveCount
triangles += it.vertices
}
@ -414,12 +414,12 @@ class WorldRenderer(
meshCollection.opaqueSectionArrayMesh.let {
it.unload()
this.meshes--
triangles -= it.primitiveCount
triangles -= it.vertices
}
meshCollection.transparentSectionArrayMesh?.let {
it.unload()
this.meshes--
triangles -= it.primitiveCount
triangles -= it.vertices
}
}
}

View File

@ -41,7 +41,7 @@ class ChunkSectionArrayMesh : Mesh(SectionArrayMeshStruct::class, initialCacheSi
Float.fromBits(textureLayer),
Float.fromBits(texture.properties.animation?.animationId ?: -1),
Float.fromBits(color.rgb),
Float.fromBits(light)
Float.fromBits(light),
))
}

View File

@ -15,17 +15,15 @@ package de.bixilon.minosoft.gui.rendering.particle
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.textures.Texture
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import glm_.vec2.Vec2
import glm_.vec3.Vec3
import glm_.vec3.Vec3d
import org.lwjgl.opengl.ARBVertexArrayObject.glBindVertexArray
import org.lwjgl.opengl.GL15.GL_POINTS
import org.lwjgl.opengl.GL15.glDrawArrays
class ParticleMesh : Mesh(ParticleMeshStruct::class) {
class ParticleMesh : Mesh(ParticleMeshStruct::class, PrimitiveTypes.POINT) {
fun addVertex(position: Vec3d, scale: Float, texture: Texture, tintColor: RGBColor, uvMin: Vec2 = Vec2(0, 0), uvMax: Vec2 = Vec2(1, 1)) {
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
@ -34,8 +32,9 @@ class ParticleMesh : Mesh(ParticleMeshStruct::class) {
(texture.arrayId shl 24) or texture.arrayLayer
}
data.addAll(floatArrayOf(
position.x.toFloat(), // ToDo: Use doubles
data.addAll(
floatArrayOf(
position.x.toFloat(), // ToDo: Use doubles
position.y.toFloat(),
position.z.toFloat(),
uvMin.x,
@ -50,12 +49,6 @@ class ParticleMesh : Mesh(ParticleMeshStruct::class) {
}
override fun draw() {
glBindVertexArray(vao)
glDrawArrays(GL_POINTS, 0, primitiveCount)
}
data class ParticleMeshStruct(
val position: Vec3,
val minUVCoordinates: Vec2,

View File

@ -0,0 +1,5 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer
interface FloatBuffer {
val data: FloatArray
}

View File

@ -0,0 +1,5 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer
interface IntBuffer {
val data: IntArray
}

View File

@ -0,0 +1,13 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer
interface RenderBuffer {
val type: RenderBufferTypes
fun init()
fun initialUpload()
fun upload()
fun bind()
fun unbind()
fun unload()
}

View File

@ -0,0 +1,7 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer
enum class RenderBufferDrawTypes {
DYNAMIC,
STATIC,
;
}

View File

@ -0,0 +1,15 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer
import de.bixilon.minosoft.util.KUtil
import de.bixilon.minosoft.util.enum.ValuesEnum
enum class RenderBufferTypes {
UNIFORM_BUFFER,
ARRAY_BUFFER,
;
companion object : ValuesEnum<RenderBufferTypes> {
override val VALUES: Array<RenderBufferTypes> = values()
override val NAME_MAP: Map<String, RenderBufferTypes> = KUtil.getEnumValues(VALUES)
}
}

View File

@ -0,0 +1,7 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBuffer
interface UniformBuffer : RenderBuffer {
val bindingIndex: Int
}

View File

@ -0,0 +1,9 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex
enum class PrimitiveTypes {
POINT,
LINE,
TRIANGLE,
QUADS,
;
}

View File

@ -0,0 +1,10 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex
import kotlin.reflect.KClass
interface VertexBuffer {
val primitiveType: PrimitiveTypes
val structure: KClass<*>
fun draw()
}

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.gui.rendering.system.base.shader
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.opengl.UniformBuffer
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.OpenGLUniformBuffer
import de.bixilon.minosoft.gui.rendering.textures.TextureArray
import de.bixilon.minosoft.util.MMath
import de.bixilon.minosoft.util.Previous
@ -50,7 +50,7 @@ interface Shader {
fun setArray(uniformName: String, array: Array<*>)
fun setRGBColor(uniformName: String, color: RGBColor)
fun setTexture(uniformName: String, textureId: Int)
fun setUniformBuffer(uniformName: String, uniformBuffer: UniformBuffer)
fun setUniformBuffer(uniformName: String, uniformBuffer: OpenGLUniformBuffer)
fun setVec3(uniformName: String, vec3: Vec3d) {
setVec3(uniformName, Vec3(vec3))
@ -68,7 +68,7 @@ interface Shader {
is Vec3 -> setVec3(uniformName, data)
is Vec2 -> setVec2(uniformName, data)
is RGBColor -> setRGBColor(uniformName, data)
is UniformBuffer -> setUniformBuffer(uniformName, data)
is OpenGLUniformBuffer -> setUniformBuffer(uniformName, data)
// ToDo: Texture
else -> error("Don't know what todo with uniform type ${data::class.simpleName}!")
}

View File

@ -247,7 +247,7 @@ class OpenGLRenderSystem(
FaceTypes.LEFT -> GL_LEFT
FaceTypes.RIGHT -> GL_RIGHT
FaceTypes.FRONT_AND_BACK -> GL_FRONT_AND_BACK
else -> throw IllegalArgumentException("OpenGL does not face type: $this")
else -> throw IllegalArgumentException("OpenGL does not support face type: $this")
}
}
}

View File

@ -19,6 +19,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.base.shader.code.glsl.GLSLShaderCode
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.OpenGLUniformBuffer
import glm_.mat4x4.Mat4
import glm_.vec2.Vec2
import glm_.vec3.Vec3
@ -156,7 +157,7 @@ class OpenGLShader(
glUniform1i(getUniformLocation(uniformName), textureId)
}
override fun setUniformBuffer(uniformName: String, uniformBuffer: UniformBuffer) {
override fun setUniformBuffer(uniformName: String, uniformBuffer: OpenGLUniformBuffer) {
glUniformBlockBinding(shader, glGetUniformBlockIndex(shader, uniformName), uniformBuffer.bindingIndex)
}

View File

@ -0,0 +1,25 @@
package de.bixilon.minosoft.gui.rendering.system.opengl.buffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.FloatBuffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBufferDrawTypes
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBufferTypes
import org.lwjgl.opengl.GL15.glBufferData
import org.lwjgl.opengl.GL15.glBufferSubData
open class FloatOpenGLBuffer(protected var _data: FloatArray?) : OpenGLRenderBuffer(RenderBufferTypes.ARRAY_BUFFER), FloatBuffer {
override val data: FloatArray
get() = _data!!
override val drawTypes: RenderBufferDrawTypes = RenderBufferDrawTypes.STATIC
override fun initialUpload() {
bind()
glBufferData(type.gl, data, drawTypes.gl)
unbind()
}
override fun upload() {
bind()
glBufferSubData(type.gl, 0, data)
unbind()
}
}

View File

@ -0,0 +1,51 @@
package de.bixilon.minosoft.gui.rendering.system.opengl.buffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBuffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBufferDrawTypes
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBufferTypes
import org.lwjgl.opengl.GL15.*
import org.lwjgl.opengl.GL31.GL_UNIFORM_BUFFER
abstract class OpenGLRenderBuffer(override val type: RenderBufferTypes) : RenderBuffer {
abstract val drawTypes: RenderBufferDrawTypes
protected var id: Int = -1
private set
override fun init() {
id = glGenBuffers()
}
override fun bind() {
glBindBuffer(type.gl, id)
}
override fun unbind() {
glBindBuffer(type.gl, 0)
}
override fun unload() {
glDeleteBuffers(id)
}
protected companion object {
val RenderBufferTypes.gl: Int
get() {
return when (this) {
RenderBufferTypes.UNIFORM_BUFFER -> GL_UNIFORM_BUFFER
RenderBufferTypes.ARRAY_BUFFER -> GL_ARRAY_BUFFER
else -> throw IllegalArgumentException("OpenGL does not support buffer type: $this")
}
}
val RenderBufferDrawTypes.gl: Int
get() {
return when (this) {
RenderBufferDrawTypes.DYNAMIC -> GL_DYNAMIC_DRAW
RenderBufferDrawTypes.STATIC -> GL_STATIC_DRAW
else -> throw IllegalArgumentException("OpenGL does not support buffer draw type: $this")
}
}
}
}

View File

@ -11,25 +11,26 @@
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.system.opengl
package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform
import org.lwjgl.opengl.ARBUniformBufferObject.GL_UNIFORM_BUFFER
import org.lwjgl.opengl.GL15.*
import de.bixilon.minosoft.gui.rendering.system.base.buffer.FloatBuffer
import org.lwjgl.opengl.GL15.glBufferData
import org.lwjgl.opengl.GL15.glBufferSubData
class IntUniformBuffer(bindingIndex: Int = 0, var data: IntArray = IntArray(0)) : UniformBuffer(bindingIndex) {
class FloatOpenGLUniformBuffer(bindingIndex: Int = 0, override var data: FloatArray = FloatArray(0)) : OpenGLUniformBuffer(bindingIndex), FloatBuffer {
override val size: Int
get() = data.size
override fun initialUpload() {
bind()
glBufferData(GL_UNIFORM_BUFFER, data, GL_DYNAMIC_DRAW)
glBufferData(type.gl, data, drawTypes.gl)
unbind()
}
override fun upload() {
check(initialSize == size) { "Can not change buffer size!" }
bind()
glBufferSubData(GL_UNIFORM_BUFFER, 0, data)
glBufferSubData(type.gl, 0, data)
unbind()
}

View File

@ -11,25 +11,26 @@
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.system.opengl
package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform
import org.lwjgl.opengl.ARBUniformBufferObject.GL_UNIFORM_BUFFER
import org.lwjgl.opengl.GL15.*
import de.bixilon.minosoft.gui.rendering.system.base.buffer.IntBuffer
import org.lwjgl.opengl.GL15.glBufferData
import org.lwjgl.opengl.GL15.glBufferSubData
class FloatUniformBuffer(bindingIndex: Int = 0, var data: FloatArray = FloatArray(0)) : UniformBuffer(bindingIndex) {
class IntOpenGLUniformBuffer(bindingIndex: Int = 0, override var data: IntArray = IntArray(0)) : OpenGLUniformBuffer(bindingIndex), IntBuffer {
override val size: Int
get() = data.size
override fun initialUpload() {
bind()
glBufferData(GL_UNIFORM_BUFFER, data, GL_DYNAMIC_DRAW)
glBufferData(type.gl, data, drawTypes.gl)
unbind()
}
override fun upload() {
check(initialSize == size) { "Can not change buffer size!" }
bind()
glBufferSubData(GL_UNIFORM_BUFFER, 0, data)
glBufferSubData(type.gl, 0, data)
unbind()
}

View File

@ -11,34 +11,28 @@
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.system.opengl
package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBufferDrawTypes
import de.bixilon.minosoft.gui.rendering.system.base.buffer.RenderBufferTypes
import de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform.UniformBuffer
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.OpenGLRenderBuffer
import org.lwjgl.opengl.ARBUniformBufferObject.*
import org.lwjgl.opengl.GL15.glBindBuffer
import org.lwjgl.opengl.GL15.glGenBuffers
abstract class UniformBuffer(val bindingIndex: Int) {
private var id = -1
abstract class OpenGLUniformBuffer(override val bindingIndex: Int) : OpenGLRenderBuffer(RenderBufferTypes.UNIFORM_BUFFER), UniformBuffer {
override val drawTypes: RenderBufferDrawTypes = RenderBufferDrawTypes.DYNAMIC
protected abstract val size: Int
protected var initialSize = -1
fun init() {
id = glGenBuffers()
override fun init() {
super.init()
initialUpload()
glBindBufferRange(GL_UNIFORM_BUFFER, bindingIndex, id, 0, size.toLong())
initialSize = size
}
protected fun bind() {
glBindBuffer(GL_UNIFORM_BUFFER, id)
}
protected fun unbind() {
glBindBuffer(GL_UNIFORM_BUFFER, 0)
}
fun use(shader: Shader, bufferName: String) {
shader.use()
@ -46,8 +40,4 @@ abstract class UniformBuffer(val bindingIndex: Int) {
shader[bufferName] = this
glBindBufferBase(GL_UNIFORM_BUFFER, bindingIndex, id)
}
protected abstract fun initialUpload()
abstract fun upload()
}

View File

@ -0,0 +1,71 @@
package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.vertex
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.VertexBuffer
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.FloatOpenGLBuffer
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct.Companion.BYTES
import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.Util
import org.lwjgl.opengl.ARBVertexArrayObject.glBindVertexArray
import org.lwjgl.opengl.ARBVertexArrayObject.glGenVertexArrays
import org.lwjgl.opengl.GL11.*
import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
import org.lwjgl.opengl.GL20.glVertexAttribPointer
import kotlin.reflect.KClass
import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.full.primaryConstructor
class FloatOpenGLVertexBuffer(override val structure: KClass<*>, data: FloatArray, override val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE) : FloatOpenGLBuffer(data), VertexBuffer {
var vertices = -1
private set
private var vao = -1
override fun init() {
super.init()
Util.forceClassInit(structure.java)
val bytesPerVertex = structure.companionObjectInstance!!.unsafeCast<MeshStruct>().BYTES_PER_VERTEX
val floatsPerVertex = bytesPerVertex / Float.SIZE_BYTES
vertices = data.size / floatsPerVertex
vao = glGenVertexArrays()
glBindVertexArray(vao)
super.init()
super.initialUpload()
_data = null
var stride = 0L
for ((index, parameter) in structure.primaryConstructor!!.parameters.withIndex()) {
val bytes = parameter.BYTES
glVertexAttribPointer(index, bytes / Float.SIZE_BYTES, GL_FLOAT, false, bytesPerVertex, stride)
glEnableVertexAttribArray(index)
stride += bytes
}
unbind()
}
override fun draw() {
glBindVertexArray(vao)
glDrawArrays(primitiveType.gl, 0, vertices)
}
private companion object {
val PrimitiveTypes.gl: Int
get() {
return when (this) {
PrimitiveTypes.POINT -> GL_POINTS
PrimitiveTypes.LINE -> GL_LINE
PrimitiveTypes.TRIANGLE -> GL_TRIANGLES
PrimitiveTypes.QUADS -> GL_QUADS
}
}
}
}

View File

@ -18,7 +18,7 @@ import de.bixilon.minosoft.data.assets.AssetsManager
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.opengl.IntUniformBuffer
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.IntOpenGLUniformBuffer
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
@ -256,7 +256,7 @@ class TextureArray(val allTextures: MutableMap<ResourceLocation, Texture>) {
inner class Animator {
val animatedTextures: MutableList<TextureAnimation> = mutableListOf()
private val uniformBuffer = IntUniformBuffer()
private val uniformBuffer = IntOpenGLUniformBuffer()
var lastRun = 0L
var initialized = false

View File

@ -13,95 +13,46 @@
package de.bixilon.minosoft.gui.rendering.util.mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct.Companion.BYTES
import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.Util
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.vertex.FloatOpenGLVertexBuffer
import de.bixilon.minosoft.util.collections.ArrayFloatList
import glm_.vec2.Vec2
import glm_.vec3.Vec3
import org.lwjgl.opengl.GL11.GL_TRIANGLES
import org.lwjgl.opengl.GL11.glDrawArrays
import org.lwjgl.opengl.GL30.*
import kotlin.reflect.KClass
import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.full.primaryConstructor
abstract class Mesh(
protected val struct: KClass<*>,
private val struct: KClass<*>,
private val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE,
initialCacheSize: Int = 10000,
) {
protected var _data: ArrayFloatList? = ArrayFloatList(initialCacheSize)
private var _data: ArrayFloatList? = ArrayFloatList(initialCacheSize)
var data: ArrayFloatList
get() = _data!!
set(value) {
_data = value
}
protected var vao: Int = -1
protected var vbo: Int = -1
var primitiveCount: Int = -1
protected lateinit var buffer: FloatOpenGLVertexBuffer
var vertices: Int = -1
protected set
var state = MeshStates.PREPARING
protected set
open fun load() {
Util.forceClassInit(struct.java)
val bytesPerVertex = struct.companionObjectInstance!!.unsafeCast<MeshStruct>().BYTES_PER_VERTEX
initializeBuffers(bytesPerVertex / Float.SIZE_BYTES)
var stride = 0L
for ((index, parameter) in struct.primaryConstructor!!.parameters.withIndex()) {
val bytes = parameter.BYTES
glVertexAttribPointer(index, bytes / Float.SIZE_BYTES, GL_FLOAT, false, bytesPerVertex, stride)
glEnableVertexAttribArray(index)
stride += bytes
}
unbind()
fun load() {
buffer = FloatOpenGLVertexBuffer(struct, data.toArray(), primitiveType)
buffer.init()
vertices = buffer.vertices
}
protected fun initializeBuffers(floatsPerVertex: Int) {
check(state == MeshStates.PREPARING) { "Mesh already loaded: $state" }
primitiveCount = data.size / floatsPerVertex
vao = glGenVertexArrays()
vbo = glGenBuffers()
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, data.toArray(), GL_STATIC_DRAW)
state = MeshStates.LOADED
_data = null
fun draw() {
buffer.draw()
}
protected fun unbind() {
glBindBuffer(GL_ARRAY_BUFFER, 0)
}
open fun draw() {
// check(state == MeshStates.LOADED) { "Mesh not loaded: $state" }
glBindVertexArray(vao)
glDrawArrays(GL_TRIANGLES, 0, primitiveCount)
}
fun unload(checkLoaded: Boolean = true) {
if (checkLoaded) {
check(state == MeshStates.LOADED) { "Mesh not loaded: $state" }
} else {
if (state != MeshStates.LOADED) {
return
}
}
glDeleteVertexArrays(vao)
glDeleteBuffers(vbo)
state = MeshStates.UNLOADED
fun unload(todo: Boolean = false) {
buffer.unload()
}
fun addQuad(start: Vec3, end: Vec3, textureStart: Vec2 = Vec2(0.0f, 0.0f), textureEnd: Vec2 = Vec2(1.0f, 1.0f), vertexConsumer: (position: Vec3, textureCoordinate: Vec2) -> Unit) {
@ -117,6 +68,7 @@ abstract class Mesh(
Vec2(textureStart.x, textureEnd.y),
textureEnd,
)
for ((vertexIndex, textureIndex) in QUAD_DRAW_ODER) {
vertexConsumer.invoke(positions[vertexIndex], texturePositions[textureIndex])
}