From a62211d3a14a5f763b19a6a6bfc2031a33e04d5e Mon Sep 17 00:00:00 2001 From: Sebastian Snoer <23051740+Seba244c@users.noreply.github.com> Date: Sat, 24 Feb 2024 20:12:49 +0100 Subject: [PATCH] fix: update UV coordinates in GLSL shader to correctly sample texture (#44) --- .../opengl/components/SpriteRenderer.java | 6 ++++- .../opengl/renderer/GL2DRenderer.java | 4 +-- .../opengl/renderer/GLSpriteRenderer.java | 25 +++++++++++++++++++ .../spellbook/shaders/SpellbookSprite.glsl | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/components/SpriteRenderer.java b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/components/SpriteRenderer.java index da44beb..a55f2c1 100644 --- a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/components/SpriteRenderer.java +++ b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/components/SpriteRenderer.java @@ -75,7 +75,9 @@ public SpriteRenderer(Identifier spritesheet, String sprite) { * @param renderer Batchrenderer to render with */ public void draw(GLSpriteRenderer renderer) { + if(sprite==null) onEnable(); Rect uvRect = sprite.getUV(); + Vector3f pos = entity.transform.getGlobalPosition(); Vector2f size = new Vector2f(sprite.getOffset().width, sprite.getOffset().height); @@ -86,6 +88,7 @@ public void draw(GLSpriteRenderer renderer) { } public void onEnable() { + if(sprite != null) return; if(spriteSheetSprite != null) { // If this sprite is set using a spritesheet this.spriteSheet = (SpriteSheet) AssetManager.getAssetS(identifier); this.sprite = spriteSheet.spr(spriteSheetSprite); @@ -100,7 +103,8 @@ public void onEnable() { @Override public void render() { - Spellbook.FRAME_DATA.addRenderSprite(this); + if(sprite != null) + Spellbook.FRAME_DATA.addRenderSprite(this); } @Override diff --git a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GL2DRenderer.java b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GL2DRenderer.java index 119a025..698650b 100644 --- a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GL2DRenderer.java +++ b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GL2DRenderer.java @@ -25,10 +25,10 @@ public class GL2DRenderer { @Getter protected final GLSLShaderProgram shader; + protected final FloatBuffer vertices; private final VBO vbo; - private final FloatBuffer vertices; private final VAO vao; - private int numVertices = 0; + protected int numVertices = 0; /** * A GLBatchRenderer using the provided shader diff --git a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteRenderer.java b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteRenderer.java index 36176c5..9dac295 100644 --- a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteRenderer.java +++ b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteRenderer.java @@ -2,6 +2,7 @@ import dk.sebsa.spellbook.asset.Identifier; import dk.sebsa.spellbook.ecs.Camera; +import dk.sebsa.spellbook.math.Rect; import org.joml.Matrix4f; /** @@ -21,4 +22,28 @@ public void begin(Matrix4f mProj) { super.begin(mProj); shader.setUniform("mView", Camera.activeCamera.getViewMatrix()); } + + @Override + public void drawQuad(Rect rect, Rect uv) { + if (vertices.remaining() < Float.BYTES * 4) flush(); + float x1 = rect.x; + float x2 = rect.x + rect.width; + float y1 = rect.y; + float y2 = rect.y + rect.height; + + float s1 = uv.x; + float s2 = uv.x + uv.width; + float t2 = uv.y; + float t1 = uv.y + uv.height; + + vertices.put(x1).put(y2).put(s1).put(t2); + vertices.put(x2).put(y2).put(s2).put(t2); + vertices.put(x2).put(y1).put(s2).put(t1); + + vertices.put(x2).put(y1).put(s2).put(t1); + vertices.put(x1).put(y1).put(s1).put(t1); + vertices.put(x1).put(y2).put(s1).put(t2); + + numVertices += 6; + } } diff --git a/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl b/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl index a06272d..e434e97 100644 --- a/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl +++ b/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl @@ -27,5 +27,5 @@ uniform sampler2D texSampler; uniform vec4 matColor; void main() { - fragColor = matColor * texture(texSampler, vec2(uvCoords.x, 1.0-uvCoords.y)); + fragColor = matColor * texture(texSampler, vec2(uvCoords.x, uvCoords.y)); } \ No newline at end of file