summaryrefslogtreecommitdiff
path: root/tiled.glsl
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-04-23 03:38:03 +0100
committerdavidovski <david@davidovski.xyz>2023-04-23 03:38:03 +0100
commit50a8424269b868bc89be72fe19756bab8f6d12e9 (patch)
treea2ace5a6e1d2f29b100c9a2e2dae737fea05c920 /tiled.glsl
parentbb3034a4cb0f793e31608ebee4eff2ef8c1f8379 (diff)
Render map based on passed tilemap
Diffstat (limited to 'tiled.glsl')
-rw-r--r--tiled.glsl36
1 files changed, 24 insertions, 12 deletions
diff --git a/tiled.glsl b/tiled.glsl
index 2a0e5d9..9e59b65 100644
--- a/tiled.glsl
+++ b/tiled.glsl
@@ -12,10 +12,12 @@ uniform vec2 resolution;
uniform vec2 offset;
uniform float zoom;
uniform ivec2 atlasSize;
-uniform int tilemap[4][4];
+uniform int tilemap[4][8];
out vec4 finalColor;
+const vec4 none = vec4(0.0f, 0.0f, 0.0f, 0.0f);
+
ivec2 calcTileOffset(int tileIndex) {
tileIndex -= 1;
int x = tileIndex % atlasSize.x;
@@ -23,35 +25,45 @@ ivec2 calcTileOffset(int tileIndex) {
return ivec2(x, y);
}
-vec4 tile(vec2 coords, int tileIndex) {
- ivec2 texSize = textureSize(texture1, 1);
-
- // for now just render the first tile
-
-
- vec2 tileSize = texSize / atlasSize;
+vec4 tile(vec2 coords, int tileIndex, vec2 tileSize) {
if (tileIndex == 0
|| coords.x > tileSize.x
|| coords.x < 0
|| coords.y > tileSize.y
|| coords.y < 0) {
- return vec4(0.0f, 0.0f, 0.0f, 0.0f);
+ return none;
} else {
ivec2 tileOffset = calcTileOffset(tileIndex);
vec2 texCoords = coords + (tileOffset*tileSize);
- texCoords /= texSize;
+ texCoords /= tileSize;
+ texCoords /= atlasSize;
return texture(texture1, texCoords);
}
}
void main() {
- vec2 uv = fragTexCoord;
+ ivec2 texSize = textureSize(texture1, 1);
+ vec2 tileSize = texSize / atlasSize;
+
+ vec2 uv = fragTexCoord * tileSize;
uv.x *= resolution.x / resolution.y;
uv *= zoom;
uv -= offset;
- finalColor = tile(uv, tilemap[2][2]);
+ // get position in tiled world wow
+ ivec2 tilemapPos = ivec2(floor(uv.x / tileSize.x), floor(uv.y / tileSize.y));
+
+ if (uv.x < 0
+ || uv.y < 0
+ || uv.x > 8*tileSize.x
+ || uv.y > 8*tileSize.y) {
+
+ finalColor = none;
+ } else {
+ vec2 position = mod(uv,tileSize);
+ finalColor = tile(position, tilemap[tilemapPos.y][tilemapPos.x], tileSize);
+ }
}