Skip to content

Commit e51f562

Browse files
committed
Watermark, HUD autodocking
1 parent eba45bf commit e51f562

File tree

12 files changed

+176
-32
lines changed

12 files changed

+176
-32
lines changed

common/src/main/kotlin/com/lambda/graphics/buffer/FrameBuffer.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class FrameBuffer {
4747
shaderBlock(shader)
4848

4949
vao.use {
50+
grow(4)
51+
5052
val uv1 = pos1 / RenderMain.screenSize
5153
val uv2 = pos2 / RenderMain.screenSize
5254

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.lambda.graphics.renderer.gui
2+
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.shader.Shader
8+
import com.lambda.graphics.texture.Texture
9+
import com.lambda.module.modules.client.GuiSettings
10+
import com.lambda.util.math.Rect
11+
import com.lambda.util.math.Vec2d
12+
import org.lwjgl.glfw.GLFW.glfwGetTime
13+
14+
object TextureRenderer {
15+
private val vao = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.POS_UV)
16+
private val shader = Shader("renderer/pos_tex")
17+
private val shaderColored = Shader("renderer/pos_tex_shady")
18+
19+
fun drawTexture(texture: Texture, rect: Rect) {
20+
texture.bind()
21+
shader.use()
22+
23+
drawInternal(rect)
24+
}
25+
26+
fun drawTextureShaded(texture: Texture, rect: Rect, shadeWidthScale: Double = 1.0) {
27+
texture.bind()
28+
shaderColored.use()
29+
30+
shaderColored["u_Time"] = glfwGetTime() * GuiSettings.colorSpeed * 5.0
31+
shaderColored["u_Color1"] = GuiSettings.shadeColor1
32+
shaderColored["u_Color2"] = GuiSettings.shadeColor2
33+
shaderColored["u_Size"] = RenderMain.screenSize / Vec2d(GuiSettings.colorWidth, GuiSettings.colorHeight) / shadeWidthScale
34+
35+
drawInternal(rect)
36+
}
37+
38+
private fun drawInternal(rect: Rect) {
39+
val pos1 = rect.leftTop
40+
val pos2 = rect.rightBottom
41+
42+
vao.use {
43+
grow(4)
44+
45+
putQuad(
46+
vec2(pos1.x, pos1.y).vec2(0.0, 0.0).end(),
47+
vec2(pos1.x, pos2.y).vec2(0.0, 1.0).end(),
48+
vec2(pos2.x, pos2.y).vec2(1.0, 1.0).end(),
49+
vec2(pos2.x, pos1.y).vec2(1.0, 0.0).end()
50+
)
51+
}
52+
53+
vao.upload()
54+
vao.render()
55+
vao.clear()
56+
}
57+
}

common/src/main/kotlin/com/lambda/graphics/texture/MipmapTexture.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package com.lambda.graphics.texture
33
import com.lambda.graphics.texture.TextureUtils.rescale
44
import com.lambda.graphics.texture.TextureUtils.setupLOD
55
import com.lambda.graphics.texture.TextureUtils.upload
6+
import com.lambda.util.LambdaResource
67
import org.lwjgl.opengl.GL14.*
78
import java.awt.image.BufferedImage
9+
import javax.imageio.ImageIO
810

911
class MipmapTexture(image: BufferedImage, levels: Int = 4) : Texture() {
1012
private var lastLod: Float? = null
@@ -32,4 +34,9 @@ class MipmapTexture(image: BufferedImage, levels: Int = 4) : Texture() {
3234

3335
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, targetLod)
3436
}
37+
38+
companion object {
39+
fun fromResource(path: String, levels: Int = 4): MipmapTexture =
40+
MipmapTexture(ImageIO.read(LambdaResource(path).stream), levels)
41+
}
3542
}

common/src/main/kotlin/com/lambda/module/HudModule.kt

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,32 @@ abstract class HudModule(
2323

2424
private var relativePosX by setting("Position X", 0.0, -10000.0..10000.0, 0.1) { false }
2525
private var relativePosY by setting("Position Y", 0.0, -10000.0..10000.0, 0.1) { false }
26-
private var relativePos get() = Vec2d(relativePosX, relativePosY); set(value) {
27-
val vec = absToRelative(relativeToAbs(value))
28-
relativePosX = vec.x; relativePosY = vec.y
29-
}
26+
27+
private var relativePos get() = Vec2d(relativePosX, relativePosY)
28+
set(value) { relativePosX = value.x; relativePosY = value.y }
3029

3130
var position get() = relativeToAbs(relativePos).let {
3231
val x = it.x.coerceIn(0.0, screenSize.x - width)
3332
val y = it.y.coerceIn(0.0, screenSize.y - height)
3433
Vec2d(x, y)
35-
}; set(value) { relativePos = absToRelative(value) }
34+
}; set(value) {
35+
relativePos = absToRelative(value)
36+
autoDocking()
37+
}
3638

3739
private fun relativeToAbs(posIn: Vec2d) = posIn + (screenSize - size) * dockingMultiplier
3840
private fun absToRelative(posIn: Vec2d) = posIn - (screenSize - size) * dockingMultiplier
3941

40-
private val dockingH by setting("Docking H", HAlign.LEFT).apply {
42+
private val autoDocking by setting("Auto Docking", true)
43+
44+
private var dockingH by setting("Docking H", HAlign.LEFT).apply {
4145
onValueChange { from, to ->
4246
val delta = to.multiplier - from.multiplier
4347
relativePosX += delta * (size.x - screenSize.x)
4448
}
4549
}
46-
private val dockingV by setting("Docking V", VAlign.TOP).apply {
50+
51+
private var dockingV by setting("Docking V", VAlign.TOP).apply {
4752
onValueChange { from, to ->
4853
val delta = to.multiplier - from.multiplier
4954
relativePosY += delta * (size.y - screenSize.y)
@@ -72,14 +77,33 @@ abstract class HudModule(
7277
}
7378
}
7479

75-
@Suppress("UNUSED")
80+
private fun autoDocking() {
81+
if (!autoDocking) return
82+
83+
val screenCenterX = (screenSize.x * 0.3333)..(screenSize.x * 0.6666)
84+
val screenCenterY = (screenSize.y * 0.3333)..(screenSize.y * 0.6666)
85+
86+
val drawableCenter = rect.center
87+
88+
dockingH = when {
89+
drawableCenter.x < screenCenterX.start -> HAlign.LEFT
90+
drawableCenter.x > screenCenterX.endInclusive -> HAlign.RIGHT
91+
else -> HAlign.CENTER
92+
}
93+
94+
dockingV = when {
95+
drawableCenter.y < screenCenterY.start -> VAlign.TOP
96+
drawableCenter.y > screenCenterY.endInclusive -> VAlign.BOTTOM
97+
else -> VAlign.CENTER
98+
}
99+
}
100+
76101
enum class HAlign(val multiplier: Float) {
77102
LEFT(0.0f),
78103
CENTER(0.5f),
79104
RIGHT(1.0f)
80105
}
81106

82-
@Suppress("UNUSED")
83107
enum class VAlign(val multiplier: Float) {
84108
TOP(0.0f),
85109
CENTER(0.5f),

common/src/main/kotlin/com/lambda/module/hud/TestHudModule.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.lambda.module.hud
2+
3+
import com.lambda.graphics.renderer.gui.TextureRenderer.drawTexture
4+
import com.lambda.graphics.renderer.gui.TextureRenderer.drawTextureShaded
5+
import com.lambda.graphics.texture.MipmapTexture
6+
import com.lambda.module.HudModule
7+
8+
object Watermark : HudModule(
9+
"Watermark"
10+
) {
11+
private val shade by setting("Shade", true)
12+
13+
override val width = 50.0
14+
override val height = 50.0
15+
16+
private val normalTexture = MipmapTexture.fromResource("textures/lambda.png")
17+
private val monoTexture = MipmapTexture.fromResource("textures/lambda_mono.png")
18+
19+
init {
20+
onRender {
21+
if (shade) drawTextureShaded(monoTexture, rect, 0.1)
22+
else drawTexture(normalTexture, rect)
23+
}
24+
}
25+
}

common/src/main/kotlin/com/lambda/module/modules/client/GuiSettings.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ object GuiSettings : Module(
2626

2727
// Colors
2828
val primaryColor by setting("Primary Color", Color(130, 200, 255), visibility = { page == Page.Colors })
29-
val secondaryColor by setting("Secondary Color", Color(225, 130, 225), visibility = { page == Page.Colors && (shade || shadeBackground) })
29+
val secondaryColor by setting("Secondary Color", Color(225, 130, 225), visibility = { page == Page.Colors })
3030
val backgroundColor by setting("Background Color", Color(50, 50, 50, 150), visibility = { page == Page.Colors })
3131
val shade by setting("Shade", true, visibility = { page == Page.Colors })
3232
val shadeBackground by setting("Shade Background", true, visibility = { page == Page.Colors })
33-
val colorWidth by setting("Color Width", 400.0, 10.0..1000.0, 10.0, visibility = { page == Page.Colors && (shade || shadeBackground) })
34-
val colorHeight by setting("Color Height", 400.0, 10.0..1000.0, 10.0, visibility = { page == Page.Colors && (shade || shadeBackground) })
35-
val colorSpeed by setting("Color Speed", 1.0, 0.1..10.0, 0.1, visibility = { page == Page.Colors && (shade || shadeBackground) })
33+
val colorWidth by setting("Shade Width", 400.0, 10.0..1000.0, 10.0, visibility = { page == Page.Colors })
34+
val colorHeight by setting("Shade Height", 400.0, 10.0..1000.0, 10.0, visibility = { page == Page.Colors })
35+
val colorSpeed by setting("Color Speed", 1.0, 0.1..10.0, 0.1, visibility = { page == Page.Colors })
3636

3737
val mainColor: Color get() = if (shade) Color.WHITE else primaryColor
3838

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 330 core
2+
3+
uniform sampler2D u_Texture;
4+
5+
in vec2 v_TexCoord;
6+
out vec4 color;
7+
8+
void main() {
9+
color = texture(u_Texture, v_TexCoord);
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#version 330 core
2+
3+
uniform sampler2D u_Texture;
4+
uniform float u_Time;
5+
uniform vec4 u_Color1;
6+
uniform vec4 u_Color2;
7+
uniform vec2 u_Size;
8+
9+
in vec2 v_Position;
10+
in vec2 v_TexCoord;
11+
12+
out vec4 color;
13+
14+
vec4 shade() {
15+
vec2 pos = v_Position * u_Size;
16+
float p = sin(pos.x - pos.y - u_Time) * 0.5 + 0.5;
17+
18+
return mix(u_Color1, u_Color2, p);
19+
}
20+
21+
void main() {
22+
color = texture(u_Texture, v_TexCoord) * shade();
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#version 330 core
2+
3+
layout (location = 0) in vec4 pos;
4+
layout (location = 1) in vec2 uv;
5+
6+
uniform mat4 u_ProjModel;
7+
8+
out vec2 v_Position;
9+
out vec2 v_TexCoord;
10+
11+
void main() {
12+
gl_Position = u_ProjModel * pos;
13+
v_Position = gl_Position.xy * 0.5 + 0.5;
14+
v_TexCoord = uv;
15+
}

0 commit comments

Comments
 (0)