textgrid.glsl (1059B)
1 #version 140 2 #extension GL_ARB_explicit_attrib_location : enable 3 4 -- vertex 5 6 layout (location = 0) in vec2 aPosition; 7 layout (location = 1) in vec2 aTexCoord; 8 layout (location = 2) in uvec4 aCharacter; 9 10 out vec2 vTexCoord; 11 out vec3 vColor; 12 13 void main() { 14 ivec2 cell = uTextGrid.xy; 15 ivec2 dims = uTextGrid.zw; 16 vec4 pos = vec4(aPosition,0,1); 17 int id = gl_InstanceID; 18 19 // translate cell to destination 20 pos.xy += vec2(id % dims.x, id / dims.x) * cell; 21 22 // adjust unit texture coord to font cell rectangle 23 vec2 tadj = vec2(aCharacter.x % uint(16), aCharacter.x / uint(16)) / 16.0; 24 25 // translate texture coordinates to character position 26 vTexCoord = aTexCoord + tadj + vec2(1.0/256.0,1.0/256.0); 27 vColor = vec3(aCharacter.yzw) / 255.0; 28 pos = uOrtho * pos; 29 30 // discard via clipping 31 if (aCharacter.x == uint(0)) pos.z = -1.1; 32 33 gl_Position = pos; 34 } 35 36 -- fragment 37 38 uniform sampler2D sampler0; 39 40 in vec2 vTexCoord; 41 in vec3 vColor; 42 43 void main() { 44 float alpha = texture2D(sampler0, vTexCoord).r; 45 gl_FragColor = vec4(vColor.xyz * step(1.0,alpha), alpha); 46 }