Skip to content

Instantly share code, notes, and snippets.

@thetooth
Last active August 29, 2015 13:57
Show Gist options
  • Save thetooth/9406187 to your computer and use it in GitHub Desktop.
Save thetooth/9406187 to your computer and use it in GitHub Desktop.
// Vertex
#version 150 core
uniform mat4 trans;
in vec2 pos;
in float id;
out mat4 projection;
out float gId;
void main() {
gId = id;
projection = gl_ModelViewProjectionMatrix*trans;
gl_Position = projection*vec4(pos, 0.0, 1.0);
}
// Geometry
#version 150 core
uniform vec2 tileSize = vec2(16, 16);
uniform vec2 sheetSize = vec2(128, 128);
layout(points) in;
layout(triangle_strip, max_vertices = 6) out;
in mat4 projection []; // Current world projection matrix
in float gId []; // Tile sprite ID used to calculate texture coords
out vec2 fCoord; // Output to fragment shader
void main() {
float leng = sheetSize.x / tileSize.x;
float col = mod(gId[0], leng)*tileSize.x/sheetSize.x;
float row = floor(gId[0] / leng)*tileSize.y/sheetSize.y;
vec2 ts = tileSize/sheetSize;
// 0,0
fCoord = vec2(col, row);
gl_Position = gl_in[0].gl_Position + (projection[0]*vec4(0.0, 0.0, 0.0, 0.0));
EmitVertex();
// 1,0
fCoord = vec2(col+ts.x, row);
gl_Position = gl_in[0].gl_Position + (projection[0]*vec4(tileSize.x, 0.0, 0.0, 0.0));
EmitVertex();
// 1,1
fCoord = vec2(col+ts.x, row+ts.y);
gl_Position = gl_in[0].gl_Position + (projection[0]*vec4(tileSize.x, tileSize.y, 0.0, 0.0));
EmitVertex();
// 1,1
fCoord = vec2(col+ts.x, row+ts.y);
gl_Position = gl_in[0].gl_Position + (projection[0]*vec4(tileSize.x, tileSize.y, 0.0, 0.0));
EmitVertex();
// 0,1
fCoord = vec2(col, row+ts.y);
gl_Position = gl_in[0].gl_Position + (projection[0]*vec4(0.0, tileSize.y, 0.0, 0.0));
EmitVertex();
// 0,0
fCoord = vec2(col, row);
gl_Position = gl_in[0].gl_Position + (projection[0]*vec4(0.0, 0.0, 0.0, 0.0));
EmitVertex();
EndPrimitive();
}
// Fragment
#version 150 core
uniform sampler2D image;
in vec2 fCoord;
out vec4 outColor;
void main() {
outColor = texture(image, fCoord);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment