|
| 1 | +package com.github.turboscript.graphics.ogl |
| 2 | + |
| 3 | +import com.mojang.blaze3d.platform.GlStateManager |
| 4 | +import org.lwjgl.opengl.GL46.* |
| 5 | +import kotlin.properties.ReadWriteProperty |
| 6 | +import kotlin.reflect.KProperty |
| 7 | + |
| 8 | +object OpenGlState { |
| 9 | + |
| 10 | + private val blend0 = GlState(false) { |
| 11 | + if (it) { |
| 12 | + GlStateManager._enableBlend() |
| 13 | + GlStateManager._blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
| 14 | + } else { |
| 15 | + GlStateManager._disableBlend() |
| 16 | + } |
| 17 | + } |
| 18 | + var blend by blend0 |
| 19 | + |
| 20 | + private val depth0 = GlState(false) { |
| 21 | + if (it) GlStateManager._enableDepthTest() |
| 22 | + else GlStateManager._disableDepthTest() |
| 23 | + } |
| 24 | + var depth by depth0 |
| 25 | + |
| 26 | + private val cull0 = GlState(false) { |
| 27 | + if (it) GlStateManager._enableCull() |
| 28 | + else GlStateManager._disableCull() |
| 29 | + } |
| 30 | + var cull by cull0 |
| 31 | + |
| 32 | + private val lineSmooth0 = GlState(false) { |
| 33 | + if (it) glEnable(GL_LINE_SMOOTH) |
| 34 | + else glDisable(GL_LINE_SMOOTH) |
| 35 | + } |
| 36 | + var lineSmooth by lineSmooth0 |
| 37 | + |
| 38 | + private val scissor0 = GlState(false) { |
| 39 | + if (it) GlStateManager._enableScissorTest() |
| 40 | + else GlStateManager._disableScissorTest() |
| 41 | + } |
| 42 | + var scissor by scissor0 |
| 43 | + |
| 44 | + fun bindTexture(unit: Int, texture: Int) { |
| 45 | + GlStateManager._activeTexture(GL_TEXTURE0 + unit) |
| 46 | + GlStateManager._bindTexture(texture) |
| 47 | + } |
| 48 | + |
| 49 | + private val vertexArray0 = GlState(0) { glBindVertexArray(it) } |
| 50 | + var vertexArray by vertexArray0 |
| 51 | + |
| 52 | + private val program0 = GlState(0) { glUseProgram(it) } |
| 53 | + var program by program0 |
| 54 | + |
| 55 | + fun reset() { |
| 56 | + blend0.forceSetValue(false) |
| 57 | + depth0.forceSetValue(false) |
| 58 | + cull0.forceSetValue(false) |
| 59 | + lineSmooth0.forceSetValue(false) |
| 60 | + vertexArray0.forceSetValue(0) |
| 61 | + program0.forceSetValue(0) |
| 62 | + scissor0.forceSetValue(false) |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +class GlState<T>(valueIn: T, private val action: (T) -> Unit) : ReadWriteProperty<Any?, T> { |
| 68 | + |
| 69 | + private var value = valueIn |
| 70 | + |
| 71 | + override operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value |
| 72 | + |
| 73 | + override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { |
| 74 | + if (this.value != value) { |
| 75 | + this.value = value |
| 76 | + action.invoke(value) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fun forceSetValue(value: T) { |
| 81 | + this.value = value |
| 82 | + action.invoke(value) |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments