gamnit :: Simple2dProgram :: defaultinit
# Graphic program to display simple models with a texture, translation, rotation and scale
private class Simple2dProgram
super GamnitProgramFromSource
redef var vertex_shader_source = """
// Vertex coordinates
attribute vec4 coord;
// Vertex color tint
attribute vec4 color;
// Vertex translation
attribute vec4 translation;
// Vertex scaling
attribute float scale;
// Vertex coordinates on textures
attribute vec2 tex_coord;
// Model view projection matrix
uniform mat4 mvp;
// Current world time, in seconds
uniform float time;
// Rotation matrix
attribute vec4 rotation_row0;
attribute vec4 rotation_row1;
attribute vec4 rotation_row2;
attribute vec4 rotation_row3;
// Animation speed, frames per seconds
attribute float a_fps;
// Number of frames in the animation
attribute float a_n_frames;
// World coordinate of the animation (for aspect ratio)
attribute vec2 a_coord;
// Animation texture coordinates of the first frame
attribute vec2 a_tex_coord;
// Animation texture coordinates difference between frames
attribute vec2 a_tex_diff;
// Animation start time, in reference to `time`
attribute float a_start;
// Number of loops to play of the animation
attribute float a_loops;
mat4 rotation()
{
return mat4(rotation_row0, rotation_row1, rotation_row2, rotation_row3);
}
// Output to the fragment shader
varying vec4 v_color;
varying vec2 v_coord;
// Is there an active animation?
varying float v_animated;
void main()
{
vec3 c; // coords
float end = a_start + a_loops/a_fps*a_n_frames;
if (a_fps != 0.0 && (a_loops == -1.0 || time < end)) {
// in animation
float frame = mod(floor((time-a_start)*a_fps), a_n_frames);
v_coord = a_tex_coord + a_tex_diff*frame;
c = vec3(a_coord, coord.z);
v_animated = 1.0;
} else {
// static
v_coord = tex_coord;
c = coord.xyz;
v_animated = 0.0;
}
gl_Position = (vec4(c * scale, 1.0) * rotation() + translation)* mvp;
v_color = vec4(color.rgb*color.a, color.a);
}
""" @ glsl_vertex_shader
redef var fragment_shader_source = """
precision mediump float;
// Does this object use a texture?
uniform bool use_texture;
// Texture to apply on this object
uniform sampler2D texture0;
// Texture to apply on this object
uniform sampler2D animation;
// Input from the vertex shader
varying vec4 v_color;
varying vec2 v_coord;
varying float v_animated;
void main()
{
if (v_animated > 0.5) {
gl_FragColor = v_color * texture2D(animation, v_coord);
if (gl_FragColor.a <= 0.01) discard;
} else if (use_texture) {
gl_FragColor = v_color * texture2D(texture0, v_coord);
if (gl_FragColor.a <= 0.01) discard;
} else {
gl_FragColor = v_color;
}
}
""" @ glsl_fragment_shader
# Vertices coordinates
var coord = attributes["coord"].as(AttributeVec4) is lazy
# Should this program use the texture `texture`?
var use_texture = uniforms["use_texture"].as(UniformBool) is lazy
# Visible texture unit
var texture = uniforms["texture0"].as(UniformSampler2D) is lazy
# Coordinates on the textures, per vertex
var tex_coord = attributes["tex_coord"].as(AttributeVec2) is lazy
# Color tint per vertex
var color = attributes["color"].as(AttributeVec4) is lazy
# Translation applied to each vertex
var translation = attributes["translation"].as(AttributeVec4) is lazy
# Rotation matrix, row 0
var rotation_row0 = attributes["rotation_row0"].as(AttributeVec4) is lazy
# Rotation matrix, row 1
var rotation_row1 = attributes["rotation_row1"].as(AttributeVec4) is lazy
# Rotation matrix, row 2
var rotation_row2 = attributes["rotation_row2"].as(AttributeVec4) is lazy
# Rotation matrix, row 3
var rotation_row3 = attributes["rotation_row3"].as(AttributeVec4) is lazy
# Scaling per vertex
var scale = attributes["scale"].as(AttributeFloat) is lazy
# Model view projection matrix
var mvp = uniforms["mvp"].as(UniformMat4) is lazy
# World time, in seconds
var time = uniforms["time"].as(UniformFloat) is lazy
# ---
# Animations
# Texture of all the frames of the animation
var animation_texture = uniforms["animation"].as(UniformSampler2D) is lazy
# Frame per second of the animation
var animation_fps = attributes["a_fps"].as(AttributeFloat) is lazy
# Number of frames in the animation
var animation_n_frames = attributes["a_n_frames"].as(AttributeFloat) is lazy
# Coordinates of each frame (mush be shared by all frames)
var animation_coord = attributes["a_coord"].as(AttributeVec2) is lazy
# Texture coordinates of the first frame
var animation_tex_coord = attributes["a_tex_coord"].as(AttributeVec2) is lazy
# Coordinate difference between each frame
var animation_tex_diff = attributes["a_tex_diff"].as(AttributeVec2) is lazy
# Animation start time, in seconds and in reference to `dt`
var animation_start = attributes["a_start"].as(AttributeFloat) is lazy
# Number of loops of the animation, -1 for infinite
var animation_loops = attributes["a_loops"].as(AttributeFloat) is lazy
end
lib/gamnit/flat/flat_core.nit:634,1--818,3