Skip to content

Commit 5cbe0b1

Browse files
committed
FrameBuffers, PostProcessing, ClickGui animations
1 parent fb2a621 commit 5cbe0b1

File tree

12 files changed

+175
-108
lines changed

12 files changed

+175
-108
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.lambda.graphics.buffer
2+
import com.lambda.Lambda.mc
3+
import com.lambda.graphics.RenderMain
4+
import com.lambda.graphics.buffer.vao.VAO
5+
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
6+
import com.lambda.graphics.buffer.vao.vertex.VertexMode
7+
import com.lambda.graphics.gl.GlStateUtils.withBlendFunc
8+
import com.lambda.graphics.shader.Shader
9+
import com.lambda.graphics.texture.TextureUtils.bindTexture
10+
import com.lambda.graphics.texture.TextureUtils.setupTexture
11+
import com.lambda.util.math.Vec2d
12+
import org.lwjgl.opengl.GL30.*
13+
import java.nio.IntBuffer
14+
15+
class FrameBuffer {
16+
private val fbo = glGenFramebuffers()
17+
18+
private val colorAttachment = glGenTextures()
19+
private val depthAttachment = glGenTextures()
20+
21+
private var width = 0
22+
private var height = 0
23+
24+
fun write(block: () -> Unit): FrameBuffer {
25+
val prev = lastFrameBuffer ?: mc.framebuffer.fbo
26+
27+
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
28+
lastFrameBuffer = fbo
29+
30+
update()
31+
withBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, block)
32+
33+
lastFrameBuffer = prev
34+
glBindFramebuffer(GL_FRAMEBUFFER, prev)
35+
return this
36+
}
37+
38+
fun read(
39+
shader: Shader,
40+
pos1: Vec2d = Vec2d.ZERO,
41+
pos2: Vec2d = RenderMain.screenSize,
42+
shaderBlock: (Shader) -> Unit = {}
43+
): FrameBuffer {
44+
bindColorTexture()
45+
46+
shader.use()
47+
shaderBlock(shader)
48+
49+
vao.use {
50+
val uv1 = pos1 / RenderMain.screenSize
51+
val uv2 = pos2 / RenderMain.screenSize
52+
53+
putQuad(
54+
vec2(pos1.x, pos1.y).vec2(uv1.x, 1.0 - uv1.y).end(),
55+
vec2(pos1.x, pos2.y).vec2(uv1.x, 1.0 - uv2.y).end(),
56+
vec2(pos2.x, pos2.y).vec2(uv2.x, 1.0 - uv2.y).end(),
57+
vec2(pos2.x, pos1.y).vec2(uv2.x, 1.0 - uv1.y).end()
58+
)
59+
}
60+
61+
withBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) {
62+
vao.upload()
63+
vao.render()
64+
vao.clear()
65+
}
66+
67+
return this
68+
}
69+
70+
fun bindColorTexture(slot: Int = 0): FrameBuffer {
71+
bindTexture(colorAttachment, slot)
72+
return this
73+
}
74+
75+
fun bindDepthTexture(slot: Int = 0): FrameBuffer {
76+
bindTexture(depthAttachment, slot)
77+
return this
78+
}
79+
80+
private fun update() {
81+
val widthIn = mc.window.framebufferWidth
82+
val heightIn = mc.window.framebufferHeight
83+
84+
if (width != widthIn || height != heightIn) {
85+
width = widthIn
86+
height = heightIn
87+
88+
setupBufferTexture(colorAttachment)
89+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null as IntBuffer?)
90+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorAttachment, 0)
91+
92+
setupBufferTexture(depthAttachment)
93+
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null as IntBuffer?)
94+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthAttachment, 0)
95+
}
96+
97+
clear()
98+
}
99+
100+
private fun setupBufferTexture(id: Int) {
101+
bindTexture(id)
102+
setupTexture(GL_NEAREST, GL_NEAREST)
103+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
104+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
105+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
106+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
107+
}
108+
109+
private fun clear() {
110+
glClearColor(0f, 0f, 0f, 0f)
111+
glClearDepth(1.0)
112+
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
113+
}
114+
115+
companion object {
116+
private val vao = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.POS_UV)
117+
private var lastFrameBuffer: Int? = null
118+
}
119+
}

common/src/main/kotlin/com/lambda/graphics/buffer/vao/vertex/VertexAttrib.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ enum class VertexAttrib(val componentCount: Int, componentSize: Int, val normali
1313
val size = componentCount * componentSize
1414

1515
enum class Group(vararg val attributes: VertexAttrib) {
16+
POS_UV(Vec2, Vec2),
17+
1618
// GUI
1719
FONT(Vec2, Vec2, Color), // pos, uv, color
1820
RECT_FILLED(Vec2, Vec2, Vec2, Float, Float, Color), // pos, uv, size, roundRadius, shade, color
1921
RECT_OUTLINE(Vec2, Float, Float, Color), // pos, alpha, shade, color
20-
BLUR(Vec2, Vec2), // pos, uv
2122

2223
// WORLD
2324
DYNAMIC_RENDERER(Vec3, Vec3, Color), // prev pos, pos, color

common/src/main/kotlin/com/lambda/graphics/gl/GlStateUtils.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ object GlStateUtils {
4747
glLineWidth(1f)
4848
}
4949

50+
fun withBlendFunc(
51+
sfactorRGB: Int, dfactorRGB: Int,
52+
sfactorAlpha: Int = sfactorRGB, dfactorAlpha: Int = dfactorRGB,
53+
block: () -> Unit
54+
) {
55+
glBlendFuncSeparate(sfactorRGB, dfactorRGB, sfactorAlpha, dfactorAlpha)
56+
block()
57+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
58+
}
59+
5060
@JvmStatic
5161
fun capSet(id: Int, flag: Boolean) {
5262
val field = when (id) {

common/src/main/kotlin/com/lambda/graphics/renderer/gui/BlurPostProcessor.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/graphics/renderer/gui/rect/AbstractRectRenderer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ abstract class AbstractRectRenderer(
1515
) {
1616
protected val vao = VAO(VertexMode.TRIANGLES, attribGroup)
1717

18-
open fun render() {
18+
fun render() {
1919
shader.use()
2020
shader["u_Time"] = glfwGetTime() * GuiSettings.colorSpeed * 5.0
2121
shader["u_Color1"] = GuiSettings.shadeColor1
2222
shader["u_Color2"] = GuiSettings.shadeColor2
2323

24-
shader["u_Size"] = RenderMain.screenSize /Vec2d(GuiSettings.colorWidth, GuiSettings.colorHeight)
24+
shader["u_Size"] = RenderMain.screenSize / Vec2d(GuiSettings.colorWidth, GuiSettings.colorHeight)
2525

2626
vao.upload()
2727
vao.render()

common/src/main/kotlin/com/lambda/graphics/renderer/gui/rect/FilledRectRenderer.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ class FilledRectRenderer : AbstractRectRenderer(
5757
)
5858
}
5959

60-
override fun render() {
61-
shader.use()
62-
super.render()
63-
}
64-
6560
companion object {
6661
private const val MIN_SIZE = 0.5
6762
private const val MIN_ALPHA = 3

common/src/main/kotlin/com/lambda/graphics/renderer/gui/rect/OutlineRectRenderer.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ class OutlineRectRenderer : AbstractRectRenderer(
9393
if (drawOuter) drawStripWith(genVertices(outerGlow, true))
9494
}
9595

96-
override fun render() {
97-
shader.use()
98-
super.render()
99-
}
100-
10196
companion object {
10297
private val shader = Shader("renderer/rect_outline")
10398
}

common/src/main/kotlin/com/lambda/gui/api/component/WindowComponent.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class WindowComponent<T : ChildComponent>(
3535
private var dragOffset: Vec2d? = null
3636
private val padding get() = ClickGui.windowPadding
3737

38-
final override val rect get() = Rect.basedOn(position, width, renderHeight + titleBarHeight)
38+
final override val rect get() = Rect.basedOn(position, width, renderHeightAnimation + titleBarHeight)
3939
private val contentRect get() = rect.shrink(padding).moveFirst(Vec2d(0.0, titleBarHeight - padding))
4040

4141
private val titleBar get() = Rect.basedOn(rect.leftTop, rect.size.x, titleBarHeight)
@@ -51,7 +51,6 @@ abstract class WindowComponent<T : ChildComponent>(
5151

5252
private val actualHeight get() = height + padding * 2 * isOpen.toInt()
5353
private var renderHeightAnimation by animation.exp({ 0.0 }, ::actualHeight, 0.6, ::isOpen)
54-
private val renderHeight get() = lerp(0.0, renderHeightAnimation, childShowAnimation)
5554

5655
open val contentComponents = ChildLayer.Drawable<T, WindowComponent<T>>(gui, this, contentRenderer, ::contentRect)
5756

@@ -61,6 +60,7 @@ abstract class WindowComponent<T : ChildComponent>(
6160
when (e) {
6261
is GuiEvent.Show -> {
6362
dragOffset = null
63+
renderHeightAnimation = if (isOpen) actualHeight else 0.0
6464
}
6565

6666
is GuiEvent.Render -> {
@@ -119,7 +119,7 @@ abstract class WindowComponent<T : ChildComponent>(
119119
Mouse.Button.Right -> {
120120
// Don't let user spam
121121
val targetHeight = if (isOpen) actualHeight else 0.0
122-
if (abs(targetHeight - renderHeight) > 1) return
122+
if (abs(targetHeight - renderHeightAnimation) > 1) return
123123

124124
isOpen = !isOpen
125125

common/src/main/kotlin/com/lambda/gui/impl/AbstractClickGui.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.lambda.gui.impl
22

33
import com.lambda.Lambda.mc
44
import com.lambda.graphics.animation.Animation.Companion.exp
5+
import com.lambda.graphics.buffer.FrameBuffer
6+
import com.lambda.graphics.shader.Shader
57
import com.lambda.gui.AbstractGuiConfigurable
68
import com.lambda.gui.api.GuiEvent
79
import com.lambda.gui.api.LambdaGui
@@ -29,6 +31,9 @@ abstract class AbstractClickGui(name: String, owner: Module? = null) : LambdaGui
2931
child == hoveredWindow && !closing
3032
}
3133

34+
private val frameBuffer = FrameBuffer()
35+
private val shader = Shader("post/cgui_animation", "renderer/pos_tex")
36+
3237
abstract val moduleFilter: (Module) -> Boolean
3338
abstract val configurable: AbstractGuiConfigurable
3439

@@ -41,6 +46,16 @@ abstract class AbstractClickGui(name: String, owner: Module? = null) : LambdaGui
4146
while (actionPool.isNotEmpty()) actionPool.removeLast().invoke()
4247

4348
when (e) {
49+
is GuiEvent.Render -> {
50+
frameBuffer.write {
51+
windows.onEvent(e)
52+
}.read(shader) {
53+
it["u_Progress"] = childShowAnimation
54+
}
55+
56+
return
57+
}
58+
4459
is GuiEvent.Show -> {
4560
hoveredWindow = null
4661
closing = false

common/src/main/resources/assets/lambda/shaders/fragment/post/blur.frag

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)