From 7ea0c288a83794e05c5e36c4232d5f6136ffacf5 Mon Sep 17 00:00:00 2001 From: Sebastian August Snoer Date: Sat, 24 Feb 2024 21:13:50 +0100 Subject: [PATCH] feat: Support for entity rotations through model matricies --- .../spellbook/graphics/opengl/Sprite2D.java | 6 ++-- .../opengl/components/SpriteRenderer.java | 35 ++++++++----------- .../renderer/GLSpriteEntityRenderer.java | 31 ++++++++++++++++ .../opengl/renderer/GLSpriteRenderer.java | 5 +-- .../spellbook/shaders/SpellbookSprite.glsl | 7 ++-- 5 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteEntityRenderer.java diff --git a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/Sprite2D.java b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/Sprite2D.java index 039d7a6..9197881 100644 --- a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/Sprite2D.java +++ b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/Sprite2D.java @@ -4,7 +4,7 @@ import dk.sebsa.spellbook.asset.Identifier; import dk.sebsa.spellbook.core.events.EngineLoadEvent; import dk.sebsa.spellbook.graphics.opengl.components.SpriteRenderer; -import dk.sebsa.spellbook.graphics.opengl.renderer.GLSpriteRenderer; +import dk.sebsa.spellbook.graphics.opengl.renderer.GLSpriteEntityRenderer; import dk.sebsa.spellbook.io.GLFWWindow; import dk.sebsa.spellbook.math.Rect; import lombok.CustomLog; @@ -21,7 +21,7 @@ @CustomLog public class Sprite2D { private static Mesh2D mainMesh; - private static GLSpriteRenderer renderer; + private static GLSpriteEntityRenderer renderer; /** * Inits the renderer @@ -32,7 +32,7 @@ public static void init(EngineLoadEvent e) { if (mainMesh != null) return; mainMesh = Mesh2D.getRenderMesh(); - renderer = new GLSpriteRenderer(new Identifier("spellbook", "shaders/SpellbookSprite.glsl")); + renderer = new GLSpriteEntityRenderer(new Identifier("spellbook", "shaders/SpellbookSprite.glsl")); } /** 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 a55f2c1..29647e2 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 @@ -7,10 +7,8 @@ import dk.sebsa.spellbook.graphics.opengl.GL2D; import dk.sebsa.spellbook.graphics.opengl.Sprite; import dk.sebsa.spellbook.graphics.opengl.SpriteSheet; -import dk.sebsa.spellbook.graphics.opengl.renderer.GLSpriteRenderer; -import dk.sebsa.spellbook.math.Rect; +import dk.sebsa.spellbook.graphics.opengl.renderer.GLSpriteEntityRenderer; import dk.sebsa.spellbook.math.Vector2f; -import dk.sebsa.spellbook.math.Vector3f; import lombok.CustomLog; import lombok.Getter; @@ -51,7 +49,8 @@ public class SpriteRenderer extends Component { /** * SpriteRenderer with the default sprite */ - public SpriteRenderer() {} + public SpriteRenderer() { + } /** * @param identifier Identifier of a sprite @@ -74,22 +73,15 @@ 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); + public void draw(GLSpriteEntityRenderer renderer) { + if (sprite == null) onEnable(); - renderer.drawQuad( - new Rect(new Vector2f(pos.x,pos.y).sub(size.mul(anchor).mul(scale)), - size.mul(scale)), - uvRect); + renderer.drawSprite(this); } public void onEnable() { - if(sprite != null) return; - if(spriteSheetSprite != null) { // If this sprite is set using a spritesheet + 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); } else // If it is just a sprite @@ -103,16 +95,19 @@ public void onEnable() { @Override public void render() { - if(sprite != null) + if (sprite != null) Spellbook.FRAME_DATA.addRenderSprite(this); } @Override public void onDisable() { - if(spriteSheetSprite != null) { - spriteSheet.unreference(); sprite = null; spriteSheet = null; + if (spriteSheetSprite != null) { + spriteSheet.unreference(); + sprite = null; + spriteSheet = null; } else if (sprite != null && sprite != GL2D.missingSprite) { - sprite.unreference(); sprite = null; + sprite.unreference(); + sprite = null; } } } diff --git a/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteEntityRenderer.java b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteEntityRenderer.java new file mode 100644 index 0000000..e40a4cc --- /dev/null +++ b/spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/renderer/GLSpriteEntityRenderer.java @@ -0,0 +1,31 @@ +package dk.sebsa.spellbook.graphics.opengl.renderer; + +import dk.sebsa.spellbook.asset.Identifier; +import dk.sebsa.spellbook.graphics.opengl.components.SpriteRenderer; +import dk.sebsa.spellbook.math.Rect; +import dk.sebsa.spellbook.math.Vector2f; + +/** + * Renders 2D objects in batches + * (Same as GL2DRender just with model matricies) + * + * @author sebs + * @since 1.0.0 + */ +public class GLSpriteEntityRenderer extends GLSpriteRenderer { + public GLSpriteEntityRenderer(Identifier shaderI) { + super(shaderI); + shader.createUniform("mModel"); + } + + public void drawSprite(SpriteRenderer sr) { + Rect uvRect = sr.getSprite().getUV(); + + Vector2f size = new Vector2f(sr.getSprite().getOffset().width, sr.getSprite().getOffset().height); + + drawQuad(new Rect(size.mul(sr.anchor).mul(-sr.scale), size.mul(sr.scale)), + uvRect); + shader.setUniform("mModel", sr.getEntity().transform.getMatrix2D()); + flush(); + } +} 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 9dac295..1096f97 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 @@ -7,15 +7,16 @@ /** * Renders 2D objects in batches - * (Same as GL2DRender just with model matricies) + * (Same as GL2DRender just with view matricies) * - * @since 1.0.0 * @author sebs + * @since 1.0.0 */ public class GLSpriteRenderer extends GL2DRenderer { public GLSpriteRenderer(Identifier shaderI) { super(shaderI); shader.createUniform("mView"); + shader.createUniform("mModel"); } public void begin(Matrix4f mProj) { diff --git a/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl b/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl index e434e97..cac92ed 100644 --- a/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl +++ b/spellbook/src/main/resources/spellbook/shaders/SpellbookSprite.glsl @@ -1,18 +1,19 @@ #version 330 core -layout (location=0) in vec2 position; -layout (location=1) in vec2 texcoord; +layout (location = 0) in vec2 position; +layout (location = 1) in vec2 texcoord; out vec2 uvCoords; uniform mat4 mProj; uniform mat4 mView; +uniform mat4 mModel; void main() { uvCoords = texcoord; - vec4 pos = vec4(position, 0.0, 1.0); + vec4 pos = vec4(position, 0.0, 1.0) * mModel; gl_Position = mProj * mView * pos; }