Skip to content

Commit d36a4d1

Browse files
authored
Merge pull request #123 from gasti-jm/cambios_gasty
Cambios gasty
2 parents 18e9b0e + b5dad20 commit d36a4d1

3 files changed

Lines changed: 35 additions & 22 deletions

File tree

resources/shaders/default.frag

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ void main() {
1212

1313
vec4 finalColor = tex * vColor;
1414

15-
if (finalColor.a <= 0.01)
16-
discard;
15+
if (tex.a < 0.5) discard;
1716

1817
FragColor = finalColor;
1918
}

src/main/java/org/aoclient/engine/renderer/BatchRenderer.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class BatchRenderer {
3131
private Texture currentTexture = null;
3232
private boolean drawing = false;
33-
private boolean currentBlend = false;
33+
private boolean currentBlend;
3434

3535
private static final int MAX_QUADS = 10000;
3636
private static final int VERTICES_PER_QUAD = 6;
@@ -89,8 +89,8 @@ public void begin() {
8989
buffer.clear();
9090
vertexCount = 0;
9191
currentTexture = null;
92-
currentBlend = false;
9392
drawing = true;
93+
currentBlend = false;
9494
}
9595

9696
// =========================
@@ -114,23 +114,27 @@ public void submitQuad(
114114
return;
115115
}
116116

117-
if (currentBlend != blend) {
117+
118+
if (currentTexture != texture || currentBlend != blend) {
118119
flush();
120+
119121
currentBlend = blend;
120122

121123
if (blend) {
122124
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
123125
} else {
124126
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125127
}
126-
}
127128

128-
if (currentTexture != texture) {
129-
flush();
130129
currentTexture = texture;
131130
currentTexture.bind();
132131
}
133132

133+
if (vertexCount + 6 >= MAX_QUADS * VERTICES_PER_QUAD) {
134+
flush();
135+
}
136+
137+
134138
push(x, y, u0, v1, r, g, b, a);
135139
push(x + w, y, u1, v1, r, g, b, a);
136140
push(x + w, y + h, u1, v0, r, g, b, a);
@@ -144,7 +148,15 @@ private void flush() {
144148
if (vertexCount == 0) return;
145149

146150
buffer.flip();
151+
147152
glBindBuffer(GL_ARRAY_BUFFER, vbo);
153+
154+
glBufferData(
155+
GL_ARRAY_BUFFER,
156+
buffer.capacity() * Float.BYTES,
157+
GL_DYNAMIC_DRAW
158+
);
159+
148160
glBufferSubData(GL_ARRAY_BUFFER, 0, buffer);
149161

150162
glBindVertexArray(vao);
@@ -176,7 +188,6 @@ public void end() {
176188
flush();
177189
drawing = false;
178190
currentTexture = null;
179-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
180191
}
181192

182193
public void render() {

src/main/java/org/aoclient/engine/renderer/Renderer.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ public class Renderer {
1515
private final Shader shader;
1616
private final Matrix4f projection;
1717

18+
private final FloatBuffer projBuf = MemoryUtil.memAllocFloat(16);
19+
private final FloatBuffer viewBuf = MemoryUtil.memAllocFloat(16);
1820
private final BatchRenderer batch = new BatchRenderer();
1921

22+
2023
public Renderer() {
2124
camera = new Camera2D(0, 0);
2225

@@ -45,6 +48,7 @@ public void draw(
4548
float v1 = srcY / tex.getTex_height();
4649
float v0 = (srcY + srcH) / tex.getTex_height();
4750

51+
4852
batch.submitQuad(
4953
tex,
5054
x, y,
@@ -54,8 +58,8 @@ public void draw(
5458
color.getRed(),
5559
color.getGreen(),
5660
color.getBlue(),
57-
alpha
58-
);
61+
alpha);
62+
5963
}
6064

6165
public void render(Scene scene) {
@@ -64,23 +68,22 @@ public void render(Scene scene) {
6468
int texLoc = glGetUniformLocation(shader.getId(), "uTexture");
6569
glUniform1i(texLoc, 0);
6670

71+
setMatrices();
72+
73+
batch.begin();
74+
scene.render();
75+
batch.end();
76+
77+
shader.unbind();
78+
}
79+
80+
private void setMatrices() {
6781
int projLoc = glGetUniformLocation(shader.getId(), "uProjection");
68-
FloatBuffer projBuf = MemoryUtil.memAllocFloat(16);
6982
projection.get(projBuf);
7083
glUniformMatrix4fv(projLoc, false, projBuf);
7184

7285
int viewLoc = glGetUniformLocation(shader.getId(), "uView");
73-
FloatBuffer viewBuf = MemoryUtil.memAllocFloat(16);
7486
camera.getViewMatrix().get(viewBuf);
7587
glUniformMatrix4fv(viewLoc, false, viewBuf);
76-
77-
batch.begin();
78-
scene.render();
79-
batch.end();
80-
81-
MemoryUtil.memFree(projBuf);
82-
MemoryUtil.memFree(viewBuf);
83-
84-
shader.unbind();
8588
}
8689
}

0 commit comments

Comments
 (0)