Skip to content

Commit

Permalink
fix: update UV coordinates in GLSL shader to correctly sample texture (
Browse files Browse the repository at this point in the history
  • Loading branch information
Seba244c authored Feb 24, 2024
1 parent ffe7653 commit a62211d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

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

0 comments on commit a62211d

Please sign in to comment.