Skip to content

Instantly share code, notes, and snippets.

@fu5ha
Last active March 20, 2019 01:33
Show Gist options
  • Save fu5ha/12780b51ac46e91b1135144ceaf8d86b to your computer and use it in GitHub Desktop.
Save fu5ha/12780b51ac46e91b1135144ceaf8d86b to your computer and use it in GitHub Desktop.
#version 150 core
uniform sampler2D textureAtlas;
in vec2 v_TexCoord;
out vec4 Target0;
void main() {
Target0 = texture(textureAtlas, v_TexCoord);
}
#version 150 core
// Comes from "canonical quad"
in vec2 a_Pos;
in vec2 a_TexCoord;
// Comes from instance data. These are both in "tiles" units
in vec2 i_PosOffset;
in vec2 i_TexCoordOffset;
// Data gets sent to frag shader and interpolated
out vec2 v_TexCoord;
// Texture atlas size, screen pixel size, and tile size in pixels. These should probably be uniforms.
const vec2 atlasPixelSize = vec2(512.0, 512.0);
const vec2 atlasTilePixelSize = vec2(16.0, 16.0);
// Screen size and screen display tile size
const vec2 screenSize = vec2(640.0, 480.0);
const vec2 screenTileSize = vec2(32.0, 32.0);
// Size of one tile in texture coords
const vec2 tileTexSize = tilePixelSize/atlasTilePixelSize;
// Size of one tile in screen coords
const vec2 tileScreenSize = screenTileSize/screenSize * 2.0;
void main() {
v_TexCoord = (a_TexCoord + i_TexCoordOffset) * tileTexSize;
// Since OGL is +y = up, but we want top left tile = (0,0)
i_PosOffset.y *= -1.0;
a_Pos.y *= -1.0;
gl_Position = (a_Pos + i_PosOffset) * tileScreenSize - vec2(1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment