Game Development Reference
In-Depth Information
The first six lines in the vertex shader declare the variables of different data types
using the so-called storage qualifiers in terms of GLSL. The data typesvec2 and
vec4 stand for the float vectors with two and four components, respectively, while
mat4 stands for a square matrix of order four of floats. The attribute qualifier is
only available in the vertex shader and denotes the inputs that are passed from the
vertex arrays sent by the application. As mentioned before, these inputs are the
position, color, and texture coordinates of one vertex at a time. The varying qualifier
denotes the variables that are readable and writeable in the context of the vertex
shader but read only to the fragment shader. Thus, the variables using this qualifier
allow the transfer of additional information from the vertex shader to the fragment
shader. Last but not least, the uniform qualifier denotes the variables that are known
to be constant during multiple executions of the shader in the same draw call. The
variable u_projTrans is automatically set to the combined projection-model view
matrix by the LibGDX's Spritebatch class.
The next step is the declaration of the main() function. It is the entry point of the
shader where the execution begins. In this vertex shader, we pass the input values
of the color and texture coordinates via varying variables to the fragment shader for
later use. The variable gl_Position is a predefined GLSL output variable (refer to
GLES2 Reference Card) that holds the projected position vector and is computed by
the multiplication of the combined projection-model view matrix ( u_projTrans ) and
the position vector ( a_position ) of the current vertex.
Next, create another new file monochrome.fs in the shaders directory and add the
following code:
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform float u_amount;
void main() {
vec4 color = v_color * texture2D(u_texture, v_texCoords);
float grayscale = dot(color.rgb, vec3(0.222, 0.707, 0.071));
color.rgb = mix(color.rgb, vec3(grayscale), u_amount);
gl_FragColor = color;
}
 
Search WWH ::




Custom Search