Skip to content

Commit 6170a30

Browse files
committed
Outline renderer & many misc stuff
1 parent ab4107a commit 6170a30

File tree

25 files changed

+362
-175
lines changed

25 files changed

+362
-175
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.awt.Color
55
interface IRenderContext {
66
fun vec3(x: Double, y: Double, z: Double): IRenderContext
77
fun vec2(x: Double, y: Double): IRenderContext
8+
fun float(v: Double): IRenderContext
89
fun color(color: Color): IRenderContext
910
fun end(): Int
1011

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.lambda.graphics.gl.Memory.byteBuffer
77
import com.lambda.graphics.gl.Memory.capacity
88
import com.lambda.graphics.gl.Memory.color
99
import com.lambda.graphics.gl.Memory.copy
10+
import com.lambda.graphics.gl.Memory.float
1011
import com.lambda.graphics.gl.Memory.int
1112
import com.lambda.graphics.gl.Memory.vec2
1213
import com.lambda.graphics.gl.Memory.vec3
@@ -89,6 +90,11 @@ class VAO(
8990
return this
9091
}
9192

93+
override fun float(v: Double): VAO {
94+
verticesPosition += float(verticesPosition, v)
95+
return this
96+
}
97+
9298
override fun color(color: Color): VAO {
9399
verticesPosition += color(verticesPosition, color)
94100
return this

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package com.lambda.graphics.buffer.vao.vertex
22

33
import com.lambda.graphics.gl.GLObject
44
import org.lwjgl.opengl.GL11C.*
5+
import org.lwjgl.opengl.GL20C.GL_MAX_VERTEX_ATTRIBS
56

67
enum class VertexAttrib(val componentCount: Int, componentSize: Int, val normalized: Boolean, override val gl: Int) : GLObject {
8+
Float(1, 4, false, GL_FLOAT),
79
Vec2(2, 4, false, GL_FLOAT),
810
Vec3(3, 4, false, GL_FLOAT),
911
Color(4, 1, true, GL_UNSIGNED_BYTE);
@@ -13,7 +15,8 @@ enum class VertexAttrib(val componentCount: Int, componentSize: Int, val normali
1315
enum class Group(vararg val attributes: VertexAttrib) {
1416
// GUI
1517
FONT(Vec2, Vec2, Color), // pos, uv, color
16-
RECT(Vec2, Vec2, Vec3, Color), // pos, uv, <sizeX, sizeY, roundRadius>, color
18+
RECT_FILLED(Vec2, Vec2, Vec2, Float, Float, Color), // pos, uv, size, roundRadius, shade, color
19+
RECT_OUTLINE(Vec2, Float, Float, Color), // pos, alpha, shade, color
1720
BLUR(Vec2, Vec2), // pos, uv
1821

1922
// WORLD

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.nio.Buffer
88
import java.nio.ByteBuffer
99

1010
object Memory {
11+
private val floatSize = VertexAttrib.Float.size
1112
private val vec2Size = VertexAttrib.Vec2.size
1213
private val vec3Size = VertexAttrib.Vec3.size
1314
private val colorSize = VertexAttrib.Color.size
@@ -41,8 +42,9 @@ object Memory {
4142
MemoryUtil.memPutInt(address, value)
4243
}
4344

44-
fun float(address: Long, value: Double) {
45+
fun float(address: Long, value: Double): Int {
4546
MemoryUtil.memPutFloat(address, value.toFloat())
47+
return floatSize
4648
}
4749

4850
fun address(buffer: Buffer): Long {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.lambda.graphics.renderer.gui.rect
2+
3+
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
4+
import com.lambda.graphics.renderer.IRenderEntry
5+
import com.lambda.graphics.renderer.gui.AbstractGuiRenderer
6+
import com.lambda.graphics.shader.Shader
7+
import com.lambda.module.modules.client.GuiSettings
8+
import com.lambda.util.math.Vec2d
9+
import org.lwjgl.glfw.GLFW.glfwGetTime
10+
11+
abstract class AbstractRectRenderer <T : IRenderEntry<T>> (
12+
vertexType: VertexAttrib.Group,
13+
shader: Shader
14+
) : AbstractGuiRenderer<T>(vertexType, shader) {
15+
override fun preRender() {
16+
shader["u_Time"] = glfwGetTime() * GuiSettings.colorSpeed * 5.0
17+
shader["u_Color1"] = GuiSettings.shadeColor1
18+
shader["u_Color2"] = GuiSettings.shadeColor2
19+
shader["u_Size"] = Vec2d.ONE / Vec2d(GuiSettings.colorWidth, GuiSettings.colorHeight)
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lambda.graphics.renderer.gui.rect
2+
3+
import com.lambda.graphics.renderer.IRenderEntry
4+
import com.lambda.util.math.Rect
5+
import java.awt.Color
6+
7+
interface IRectEntry <T : IRenderEntry<T>> : IRenderEntry<T> {
8+
var position: Rect
9+
var roundRadius: Double
10+
var shade: Boolean
11+
12+
fun color(leftTop: Color, rightTop: Color, rightBottom: Color, leftBottom: Color)
13+
fun color(color: Color) = color(color, color, color, color)
14+
fun colorH(left: Color, right: Color) = color(left, right, right, left)
15+
fun colorV(top: Color, bottom: Color) = color(top, top, bottom, bottom)
16+
17+
interface Filled : IRectEntry<Filled>
18+
19+
interface Outline : IRectEntry<Outline> {
20+
var outerGlow: Double
21+
var innerGlow: Double
22+
}
23+
}

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

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

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

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.lambda.graphics.renderer.gui.rect.filled
2+
3+
import com.lambda.graphics.buffer.vao.IRenderContext
4+
import com.lambda.graphics.renderer.gui.rect.IRectEntry
5+
import com.lambda.util.math.MathUtils.toInt
6+
import java.awt.Color
7+
import kotlin.math.min
8+
9+
class FilledRectEntry(
10+
override val owner: FilledRectRenderer,
11+
override val updateBlock: IRectEntry.Filled.() -> Unit
12+
) : IRectEntry.Filled {
13+
override var position by owner.positionRect()
14+
override var roundRadius by owner.field(0.0)
15+
override var shade by owner.field(false)
16+
17+
private var leftTop by owner.field(Color.WHITE)
18+
private var rightTop by owner.field(Color.WHITE)
19+
private var rightBottom by owner.field(Color.WHITE)
20+
private var leftBottom by owner.field(Color.WHITE)
21+
22+
override fun color(leftTop: Color, rightTop: Color, rightBottom: Color, leftBottom: Color) {
23+
this.leftTop = leftTop
24+
this.rightTop = rightTop
25+
this.rightBottom = rightBottom
26+
this.leftBottom = leftBottom
27+
}
28+
29+
override fun build(ctx: IRenderContext) = ctx.use {
30+
val pos1 = position.leftTop
31+
val pos2 = position.rightBottom
32+
33+
val size = pos2 - pos1
34+
if (size.x < MIN_SIZE || size.y < MIN_SIZE) return@use
35+
36+
val halfSize = size * 0.5
37+
val maxRadius = min(halfSize.x, halfSize.y)
38+
39+
val round = min(roundRadius, maxRadius)
40+
41+
val p1 = pos1 - 0.75
42+
val p2 = pos2 + 0.75
43+
val s = shade.toInt().toDouble()
44+
45+
grow(4)
46+
47+
putQuad(
48+
vec2(p1.x, p1.y).vec2(0.0, 0.0).vec2(size.x, size.y).float(round).float(s).color(leftTop).end(),
49+
vec2(p1.x, p2.y).vec2(0.0, 1.0).vec2(size.x, size.y).float(round).float(s).color(leftBottom).end(),
50+
vec2(p2.x, p2.y).vec2(1.0, 1.0).vec2(size.x, size.y).float(round).float(s).color(rightBottom).end(),
51+
vec2(p2.x, p1.y).vec2(1.0, 0.0).vec2(size.x, size.y).float(round).float(s).color(rightTop).end()
52+
)
53+
}
54+
55+
companion object {
56+
private const val MIN_SIZE = 0.5
57+
}
58+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.lambda.graphics.renderer.gui.rect.filled
2+
3+
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
4+
import com.lambda.graphics.renderer.gui.rect.AbstractRectRenderer
5+
import com.lambda.graphics.renderer.gui.rect.IRectEntry
6+
import com.lambda.graphics.shader.Shader
7+
8+
class FilledRectRenderer : AbstractRectRenderer<IRectEntry.Filled>(
9+
VertexAttrib.Group.RECT_FILLED, shader
10+
) {
11+
override fun newEntry(block: IRectEntry.Filled.() -> Unit) =
12+
FilledRectEntry(this, block)
13+
14+
companion object {
15+
private val shader = Shader("renderer/rect_filled")
16+
}
17+
}

0 commit comments

Comments
 (0)