mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-18 11:54:59 -04:00
improve shader mesh struct
This commit is contained in:
parent
f5e09b27bb
commit
831b77dc90
@ -23,7 +23,7 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
|||||||
import glm_.vec2.Vec2
|
import glm_.vec2.Vec2
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
|
|
||||||
class ChunkSectionArrayMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct::class, initialCacheSize = 100000) {
|
class ChunkSectionArrayMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, initialCacheSize = 100000) {
|
||||||
|
|
||||||
fun addVertex(position: Vec3, textureCoordinates: Vec2, texture: Texture, tintColor: RGBColor?, light: Int) {
|
fun addVertex(position: Vec3, textureCoordinates: Vec2, texture: Texture, tintColor: RGBColor?, light: Int) {
|
||||||
val color = tintColor ?: ChatColors.WHITE
|
val color = tintColor ?: ChatColors.WHITE
|
||||||
|
@ -24,7 +24,7 @@ import glm_.vec2.Vec2
|
|||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
import glm_.vec3.Vec3d
|
import glm_.vec3.Vec3d
|
||||||
|
|
||||||
class ParticleMesh(renderWindow: RenderWindow) : Mesh(renderWindow, ParticleMeshStruct::class, PrimitiveTypes.POINT) {
|
class ParticleMesh(renderWindow: RenderWindow) : Mesh(renderWindow, ParticleMeshStruct, PrimitiveTypes.POINT) {
|
||||||
|
|
||||||
fun addVertex(position: Vec3d, scale: Float, texture: Texture, tintColor: RGBColor, uvMin: Vec2 = Vec2(0, 0), uvMax: Vec2 = Vec2(1, 1)) {
|
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) {
|
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
|
||||||
|
@ -17,7 +17,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
|
|||||||
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
|
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
|
||||||
import de.bixilon.minosoft.gui.rendering.util.mesh.PositionOnlyMeshStruct
|
import de.bixilon.minosoft.gui.rendering.util.mesh.PositionOnlyMeshStruct
|
||||||
|
|
||||||
class SkyboxMesh(renderWindow: RenderWindow) : Mesh(renderWindow, PositionOnlyMeshStruct::class, initialCacheSize = 6 * 2 * 3 * PositionOnlyMeshStruct.FLOATS_PER_VERTEX) {
|
class SkyboxMesh(renderWindow: RenderWindow) : Mesh(renderWindow, PositionOnlyMeshStruct, initialCacheSize = 6 * 2 * 3 * PositionOnlyMeshStruct.FLOATS_PER_VERTEX) {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
data.addAll(
|
data.addAll(
|
||||||
|
@ -19,9 +19,9 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform.IntUniformBu
|
|||||||
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.FloatVertexBuffer
|
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.FloatVertexBuffer
|
||||||
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
|
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
|
||||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||||
|
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
||||||
import glm_.vec2.Vec2i
|
import glm_.vec2.Vec2i
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
interface RenderSystem {
|
interface RenderSystem {
|
||||||
val shaders: MutableSet<Shader>
|
val shaders: MutableSet<Shader>
|
||||||
@ -71,7 +71,7 @@ interface RenderSystem {
|
|||||||
|
|
||||||
fun createShader(resourceLocation: ResourceLocation): Shader
|
fun createShader(resourceLocation: ResourceLocation): Shader
|
||||||
|
|
||||||
fun createVertexBuffer(structure: KClass<*>, data: FloatArray, primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE): FloatVertexBuffer
|
fun createVertexBuffer(structure: MeshStruct, data: FloatArray, primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE): FloatVertexBuffer
|
||||||
fun createIntUniformBuffer(bindingIndex: Int = 0, data: IntArray = IntArray(0)): IntUniformBuffer
|
fun createIntUniformBuffer(bindingIndex: Int = 0, data: IntArray = IntArray(0)): IntUniformBuffer
|
||||||
fun createFloatUniformBuffer(bindingIndex: Int = 0, data: FloatArray = FloatArray(0)): FloatUniformBuffer
|
fun createFloatUniformBuffer(bindingIndex: Int = 0, data: FloatArray = FloatArray(0)): FloatUniformBuffer
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex
|
package de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex
|
||||||
|
|
||||||
import kotlin.reflect.KClass
|
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
||||||
|
|
||||||
interface VertexBuffer {
|
interface VertexBuffer {
|
||||||
val vertices: Int
|
val vertices: Int
|
||||||
val primitiveType: PrimitiveTypes
|
val primitiveType: PrimitiveTypes
|
||||||
val structure: KClass<*>
|
val structure: MeshStruct
|
||||||
|
|
||||||
fun draw()
|
fun draw()
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.FloatOpenG
|
|||||||
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.IntOpenGLUniformBuffer
|
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.IntOpenGLUniformBuffer
|
||||||
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.vertex.FloatOpenGLVertexBuffer
|
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.vertex.FloatOpenGLVertexBuffer
|
||||||
import de.bixilon.minosoft.gui.rendering.system.opengl.vendor.*
|
import de.bixilon.minosoft.gui.rendering.system.opengl.vendor.*
|
||||||
|
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
||||||
import de.bixilon.minosoft.modding.event.CallbackEventInvoker
|
import de.bixilon.minosoft.modding.event.CallbackEventInvoker
|
||||||
import de.bixilon.minosoft.util.KUtil.synchronizedSetOf
|
import de.bixilon.minosoft.util.KUtil.synchronizedSetOf
|
||||||
import glm_.vec2.Vec2i
|
import glm_.vec2.Vec2i
|
||||||
@ -33,7 +34,6 @@ import org.lwjgl.BufferUtils
|
|||||||
import org.lwjgl.opengl.GL
|
import org.lwjgl.opengl.GL
|
||||||
import org.lwjgl.opengl.GL20.*
|
import org.lwjgl.opengl.GL20.*
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
class OpenGLRenderSystem(
|
class OpenGLRenderSystem(
|
||||||
private val renderWindow: RenderWindow,
|
private val renderWindow: RenderWindow,
|
||||||
@ -173,7 +173,7 @@ class OpenGLRenderSystem(
|
|||||||
return OpenGLShader(renderWindow, resourceLocation)
|
return OpenGLShader(renderWindow, resourceLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createVertexBuffer(structure: KClass<*>, data: FloatArray, primitiveType: PrimitiveTypes): FloatVertexBuffer {
|
override fun createVertexBuffer(structure: MeshStruct, data: FloatArray, primitiveType: PrimitiveTypes): FloatVertexBuffer {
|
||||||
return FloatOpenGLVertexBuffer(structure, data, primitiveType)
|
return FloatOpenGLVertexBuffer(structure, data, primitiveType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,30 +5,20 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.FloatVertexBu
|
|||||||
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
|
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
|
||||||
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.FloatOpenGLBuffer
|
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
|
||||||
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.glBindVertexArray
|
||||||
import org.lwjgl.opengl.ARBVertexArrayObject.glGenVertexArrays
|
import org.lwjgl.opengl.ARBVertexArrayObject.glGenVertexArrays
|
||||||
import org.lwjgl.opengl.GL11.*
|
import org.lwjgl.opengl.GL11.*
|
||||||
import org.lwjgl.opengl.GL15.glBufferData
|
import org.lwjgl.opengl.GL15.glBufferData
|
||||||
import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
|
import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
|
||||||
import org.lwjgl.opengl.GL20.glVertexAttribPointer
|
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), FloatVertexBuffer {
|
class FloatOpenGLVertexBuffer(override val structure: MeshStruct, data: FloatArray, override val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE) : FloatOpenGLBuffer(data), FloatVertexBuffer {
|
||||||
override var vertices = -1
|
override var vertices = -1
|
||||||
private set
|
private set
|
||||||
private var vao = -1
|
private var vao = -1
|
||||||
|
|
||||||
override fun init() {
|
override fun init() {
|
||||||
Util.forceClassInit(structure.java)
|
val floatsPerVertex = structure.BYTES_PER_VERTEX / Float.SIZE_BYTES
|
||||||
|
|
||||||
val bytesPerVertex = structure.companionObjectInstance!!.unsafeCast<MeshStruct>().BYTES_PER_VERTEX
|
|
||||||
|
|
||||||
val floatsPerVertex = bytesPerVertex / Float.SIZE_BYTES
|
|
||||||
|
|
||||||
vertices = data.size / floatsPerVertex
|
vertices = data.size / floatsPerVertex
|
||||||
vao = glGenVertexArrays()
|
vao = glGenVertexArrays()
|
||||||
@ -42,14 +32,11 @@ class FloatOpenGLVertexBuffer(override val structure: KClass<*>, data: FloatArra
|
|||||||
_data = null
|
_data = null
|
||||||
|
|
||||||
|
|
||||||
var stride = 0L
|
for (attribute in structure.attributes) {
|
||||||
|
glVertexAttribPointer(attribute.index, attribute.size, GL_FLOAT, false, structure.BYTES_PER_VERTEX, attribute.stride)
|
||||||
for ((index, parameter) in structure.primaryConstructor!!.parameters.withIndex()) {
|
glEnableVertexAttribArray(attribute.index)
|
||||||
val bytes = parameter.BYTES
|
|
||||||
glVertexAttribPointer(index, bytes / Float.SIZE_BYTES, GL_FLOAT, false, bytesPerVertex, stride)
|
|
||||||
glEnableVertexAttribArray(index)
|
|
||||||
stride += bytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unbind()
|
unbind()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ import de.bixilon.minosoft.data.text.RGBColor
|
|||||||
import de.bixilon.minosoft.gui.rendering.RenderWindow
|
import de.bixilon.minosoft.gui.rendering.RenderWindow
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
|
|
||||||
open class GenericColorMesh(renderWindow: RenderWindow) : Mesh(renderWindow, GenericColorMeshStruct::class) {
|
open class GenericColorMesh(renderWindow: RenderWindow) : Mesh(renderWindow, GenericColorMeshStruct) {
|
||||||
|
|
||||||
fun addVertex(position: Vec3, color: RGBColor?) {
|
fun addVertex(position: Vec3, color: RGBColor?) {
|
||||||
data.addAll(
|
data.addAll(
|
||||||
|
@ -19,11 +19,10 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveType
|
|||||||
import de.bixilon.minosoft.util.collections.ArrayFloatList
|
import de.bixilon.minosoft.util.collections.ArrayFloatList
|
||||||
import glm_.vec2.Vec2
|
import glm_.vec2.Vec2
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
abstract class Mesh(
|
abstract class Mesh(
|
||||||
val renderWindow: RenderWindow,
|
val renderWindow: RenderWindow,
|
||||||
private val struct: KClass<*>,
|
private val struct: MeshStruct,
|
||||||
private val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE,
|
private val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE,
|
||||||
initialCacheSize: Int = 10000,
|
initialCacheSize: Int = 10000,
|
||||||
) {
|
) {
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Minosoft
|
||||||
|
* Copyright (C) 2021 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.util.mesh
|
||||||
|
|
||||||
|
data class MeshAttribute(
|
||||||
|
val index: Int,
|
||||||
|
val size: Int,
|
||||||
|
val stride: Long,
|
||||||
|
)
|
@ -28,6 +28,20 @@ import kotlin.reflect.full.primaryConstructor
|
|||||||
abstract class MeshStruct(val struct: KClass<*>) {
|
abstract class MeshStruct(val struct: KClass<*>) {
|
||||||
val BYTES_PER_VERTEX: Int = calculateBytesPerVertex(struct)
|
val BYTES_PER_VERTEX: Int = calculateBytesPerVertex(struct)
|
||||||
val FLOATS_PER_VERTEX: Int = BYTES_PER_VERTEX / Float.SIZE_BYTES
|
val FLOATS_PER_VERTEX: Int = BYTES_PER_VERTEX / Float.SIZE_BYTES
|
||||||
|
val attributes: List<MeshAttribute>
|
||||||
|
|
||||||
|
init {
|
||||||
|
val attributes: MutableList<MeshAttribute> = mutableListOf()
|
||||||
|
var stride = 0L
|
||||||
|
|
||||||
|
for ((index, parameter) in struct.primaryConstructor!!.parameters.withIndex()) {
|
||||||
|
val bytes = parameter.BYTES
|
||||||
|
attributes += MeshAttribute(index, bytes / Float.SIZE_BYTES, stride)
|
||||||
|
stride += bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
this.attributes = attributes.toList()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ import de.bixilon.minosoft.gui.rendering.textures.Texture
|
|||||||
import glm_.vec2.Vec2
|
import glm_.vec2.Vec2
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
|
|
||||||
open class SimpleTextureMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SimpleTextureMeshStruct::class, initialCacheSize = 2 * 3 * SimpleTextureMeshStruct.FLOATS_PER_VERTEX) {
|
open class SimpleTextureMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SimpleTextureMeshStruct, initialCacheSize = 2 * 3 * SimpleTextureMeshStruct.FLOATS_PER_VERTEX) {
|
||||||
|
|
||||||
fun addVertex(position: Vec3, texture: Texture, textureCoordinates: Vec2, tintColor: RGBColor) {
|
fun addVertex(position: Vec3, texture: Texture, textureCoordinates: Vec2, tintColor: RGBColor) {
|
||||||
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
|
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user