Graphics Reference
In-Depth Information
coordinates. Alternatively, because the vertex shader is specified by the
application, vertex shaders can be used to perform custom math that
enables new transforms, lighting, or vertex-based effects not allowed in
more traditional fixed-function pipelines.
Example 1-1 shows a vertex shader written using the OpenGL ES shading
language. We explain vertex shaders in signiicant detail later in the topic.
We present this shader here just to give you an idea of what a vertex
shader looks like. The vertex shader in Example 1-1 takes a position and
its associated color data as input attributes, transforms the position using
a 4 × 4 matrix, and outputs the transformed position and color.
Example 1-1
A Vertex Shader Example
1. #version 300 es
2. uniform mat4 u_mvpMatrix; // matrix to convert a_position
3. // from model space to normalized
4. // device space
5.
6. // attributes input to the vertex shader
7. in vec4 a_position; // position value
8. in vec4 a_color; // input vertex color
9.
10. // output of the vertex shader - input to fragment
11. // shader
12. out vec4 v_color; // output vertex color
13. void main()
14. {
15. v_color = a_color;
16. gl_Position = u_mvpMatrix * a_position;
17. }
Line 1 provides the version of the Shading Language—information
that must appear on the first line of the shader ( #version 300 es
indicates the OpenGL ES Shading Language v3.00). Line 2 describes a
uniform variable u_mvpMatrix that stores the combined model view and
projection matrix. Lines 7 and 8 describe the inputs to the vertex shader
and are referred to as vertex attributes. a_position is the input vertex
position attribute and a_color is the input vertex color attribute. On
line 12, we declare the output v_color to store the output of the vertex
shader that describes the per-vertex color. The built-in variable called
gl_Position is declared automatically, and the shader must write the
transformed position to this variable. A vertex or fragment shader has
a single entry point called the main function. Lines 13-17 describe the
 
 
Search WWH ::




Custom Search