Skip to content

Commit

Permalink
feat: Support for entity rotations through model matricies
Browse files Browse the repository at this point in the history
  • Loading branch information
Seba244c committed Feb 24, 2024
1 parent a9802be commit 7ea0c28
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,7 @@
@CustomLog
public class Sprite2D {
private static Mesh2D mainMesh;
private static GLSpriteRenderer renderer;
private static GLSpriteEntityRenderer renderer;

/**
* Inits the renderer
Expand All @@ -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"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -51,7 +49,8 @@ public class SpriteRenderer extends Component {
/**
* SpriteRenderer with the default sprite
*/
public SpriteRenderer() {}
public SpriteRenderer() {
}

/**
* @param identifier Identifier of a sprite
Expand All @@ -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
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down

0 comments on commit 7ea0c28

Please sign in to comment.