Skip to content

Commit 6319fab

Browse files
committed
Misc render stuff
1 parent 6045b7e commit 6319fab

File tree

7 files changed

+67
-84
lines changed

7 files changed

+67
-84
lines changed

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

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,76 +21,82 @@ import org.lwjgl.opengl.GL11C.GL_FLOAT
2121
import org.lwjgl.opengl.GL11C.GL_UNSIGNED_BYTE
2222
import org.lwjgl.opengl.GL20C.glEnableVertexAttribArray
2323
import org.lwjgl.opengl.GL20C.glVertexAttribPointer
24+
import org.lwjgl.opengl.GL33.glVertexAttribDivisor
2425

25-
enum class VertexAttrib(
26+
sealed class VertexAttrib(
2627
private val componentCount: Int,
2728
componentSize: Int,
2829
private val normalized: Boolean,
30+
private val single: Boolean,
2931
private val type: Int
3032
) {
31-
Float(1, 4, false, GL_FLOAT),
32-
Vec2(2, 4, false, GL_FLOAT),
33-
Vec3(3, 4, false, GL_FLOAT),
34-
Color(4, 1, true, GL_UNSIGNED_BYTE);
33+
open class Float(
34+
normalized: Boolean = false, single: Boolean = false
35+
) : VertexAttrib(1, 4, normalized, single, GL_FLOAT) {
36+
companion object : Float()
37+
}
38+
39+
open class Vec2(
40+
normalized: Boolean = false, single: Boolean = false
41+
) : VertexAttrib(2, 4, normalized, single, GL_FLOAT) {
42+
companion object : Vec2()
43+
}
44+
45+
open class Vec3(
46+
normalized: Boolean = false, single: Boolean = false
47+
) : VertexAttrib(3, 4, normalized, single, GL_FLOAT) {
48+
companion object : Vec3()
49+
}
50+
51+
open class Color(
52+
normalized: Boolean = true, single: Boolean = false
53+
) : VertexAttrib(4, 1, normalized, single, GL_UNSIGNED_BYTE) {
54+
companion object : Color()
55+
}
3556

3657
val size = componentCount * componentSize
3758

38-
fun pointer(index: Int, pointer: Long, stride: Int) {
59+
fun link(index: Int, pointer: Long, stride: Int) {
3960
glEnableVertexAttribArray(index)
4061
glVertexAttribPointer(index, componentCount, type, normalized, stride, pointer)
62+
if (single) glVertexAttribDivisor(index, 1)
4163
}
4264

43-
enum class Group(vararg val attributes: VertexAttrib) {
44-
POS_UV(Vec2, Vec2),
65+
@Suppress("ClassName")
66+
open class Group(vararg val attributes: VertexAttrib) {
67+
object POS_UV : Group(Vec2, Vec2)
4568

4669
// GUI
47-
FONT(
48-
Vec3, // pos
49-
Vec2, // uv
50-
Color
51-
),
52-
53-
RECT_FILLED(
54-
Vec3, // pos
55-
Vec2, // uv
56-
Color
57-
),
58-
59-
RECT_OUTLINE(
60-
Vec3, // pos
61-
Vec2, // uv
62-
Float, // alpha
63-
Color
64-
),
70+
object FONT : Group(Vec3, Vec2, Color)
71+
72+
object RECT_FILLED : Group(
73+
Vec3, Vec2, Color
74+
)
75+
76+
object RECT_OUTLINE : Group(
77+
Vec3, Vec2, Float, Color
78+
)
6579

6680
// WORLD
67-
DYNAMIC_RENDERER(
68-
Vec3, // prev pos
69-
Vec3, // pos
70-
Color
71-
),
72-
73-
STATIC_RENDERER(
74-
Vec3, // pos
75-
Color
76-
),
77-
78-
PARTICLE(
79-
Vec3,
80-
Vec2, // pos
81-
Color
82-
);
81+
object DYNAMIC_RENDERER : Group(
82+
Vec3, Vec3, Color
83+
)
84+
85+
object STATIC_RENDERER : Group(
86+
Vec3, Color
87+
)
88+
89+
object PARTICLE : Group(Vec3, Vec2, Color)
8390

8491
val stride = attributes.sumOf { attribute ->
8592
attribute.size
8693
}
8794

8895
fun link() {
8996
attributes.foldIndexed(0L) { index, pointer, attrib ->
90-
attrib.pointer(index, pointer, stride)
97+
attrib.link(index, pointer, stride)
9198
pointer + attrib.size
9299
}
93100
}
94101
}
95102
}
96-

common/src/main/kotlin/com/lambda/graphics/pipeline/PersistentBuffer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class PersistentBuffer(
5151
fun upload() {
5252
val dataStart = byteBuffer.pointer + uploadOffset
5353
val dataCount = byteBuffer.bytesPut - uploadOffset
54+
if (dataCount <= 0) return
5455

5556
if (glSize != byteBuffer.capacity) {
5657
glSize = byteBuffer.capacity
@@ -73,7 +74,6 @@ class PersistentBuffer(
7374
}
7475

7576
glBuffer.update(uploadOffset.toLong(), dataCount, dataStart)
76-
println(dataCount)
7777
}
7878

7979
fun end() {

common/src/main/kotlin/com/lambda/graphics/pipeline/VertexPipeline.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class VertexPipeline(
7373
*/
7474
fun immediate(block: VertexBuilder.() -> Unit) {
7575
VertexBuilder(this).apply(block)
76-
upload(); render(); clear()
76+
uploadInternal(); render(); clear()
7777
}
7878

7979
/**
@@ -88,7 +88,7 @@ class VertexPipeline(
8888
*/
8989
fun upload(block: VertexBuilder.() -> Unit) {
9090
VertexBuilder(this).apply(block)
91-
upload()
91+
uploadInternal()
9292
}
9393

9494
/**
@@ -100,7 +100,7 @@ class VertexPipeline(
100100
*/
101101
fun upload(builder: VertexBuilder) {
102102
builder.uploadTo(this)
103-
upload()
103+
uploadInternal()
104104
}
105105

106106
/**
@@ -114,7 +114,7 @@ class VertexPipeline(
114114
/**
115115
* Uploads buffered data to GPU memory
116116
*/
117-
fun upload() {
117+
private fun uploadInternal() {
118118
vbo.upload()
119119
ibo.upload()
120120
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import kotlin.math.min
2727
object FilledRectRenderer : AbstractGUIRenderer(
2828
VertexAttrib.Group.RECT_FILLED, shader("renderer/rect_filled")
2929
) {
30-
private const val EXPAND = 0.35
30+
private const val EXPAND = 0.4
3131

3232
fun filledRect(
3333
rect: Rect,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ object ClickGui : Module(
7575
val animationCurve by setting("List Animation Curve", AnimationCurve.Normal)
7676
val smoothness by setting("Smoothness", 0.4, 0.3..0.7, 0.01) { animationCurve != AnimationCurve.Static }
7777

78-
val hudPadding by setting("Hud Padding", 3.0, 0.0..10.0, 0.1)
78+
val hudPadding by setting("Hud Padding", 2.0, 0.0..10.0, 0.1)
7979

8080
val SCREEN: LambdaScreen by lazy {
8181
gui("Click Gui") {

common/src/main/kotlin/com/lambda/module/modules/render/Particles.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ object Particles : Module(
101101
shader.use()
102102
shader["u_CameraPosition"] = mc.gameRenderer.camera.pos
103103

104-
pipeline.upload()
104+
pipeline.upload(builder)
105105
withDepth(false, pipeline::render)
106106
pipeline.clear()
107107
}

common/src/main/resources/assets/lambda/shaders/renderer/rect_filled.glsl

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,18 @@ uniforms {
1313
};
1414

1515
#include "rect"
16-
#define SMOOTHING 0.2
17-
#define round getRoundAlpha()
16+
#define SMOOTHING 0.5
1817

19-
float getRoundRadius() {
20-
bool xcmp = v_TexCoord.x > 0.5;
21-
bool ycmp = v_TexCoord.y > 0.5;
18+
float roundedRectSDF() {
19+
vec4 r = vec4(u_RoundRightBottom, u_RoundRightTop, u_RoundLeftBottom, u_RoundLeftTop);
20+
r.xy = (v_TexCoord.x > 0.5) ? r.xy : r.zw;
21+
r.x = (v_TexCoord.y > 0.5) ? r.x : r.y;
2222

23-
float r = 0.0;
24-
25-
if (xcmp) {
26-
if (ycmp) { r = u_RoundRightBottom; }
27-
else { r = u_RoundRightTop; }
28-
} else {
29-
if (ycmp) { r = u_RoundLeftBottom; }
30-
else { r = u_RoundLeftTop; }
31-
}
32-
33-
return r;
34-
}#
35-
36-
vec4 getRoundAlpha() {
37-
vec2 halfSize = u_Size * 0.5;
38-
39-
float radius = max(getRoundRadius(), SMOOTHING);
40-
41-
vec2 smoothVec = vec2(SMOOTHING);
42-
vec2 coord = mix(-smoothVec, u_Size + smoothVec, v_TexCoord);
43-
44-
vec2 center = halfSize - coord;
45-
float distance = length(max(abs(center) - halfSize + radius, 0.0)) - radius;
46-
47-
float alpha = 1.0 - smoothstep(-SMOOTHING, SMOOTHING, distance);
48-
return vec4(1.0, 1.0, 1.0, clamp(alpha, 0.0, 1.0));
23+
vec2 q = u_Size * (abs(v_TexCoord - 0.5) - 0.5) + r.x;
24+
return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r.x;
4925
}#
5026

5127
void fragment() {
52-
color = v_Color * shade * round + noise;
28+
float a = 1.0 - smoothstep(-SMOOTHING, 0.0, roundedRectSDF());
29+
color = v_Color * vec4(shade.rgb, shade.a * a) + noise;
5330
}#

0 commit comments

Comments
 (0)