Graphics Programs Reference
In-Depth Information
GLES20.glVertexAttribPointer(_lineAColorLocation, 4,
GLES20.GL_FLOAT, false, 0, _lineCFB);
GLES20.glEnableVertexAttribArray(_lineAColorLocation);
GLES20.glLineWidth(3);
GLES20.glDrawArrays(GLES20.GL_LINES, 0, 2);
}
Next, take a look at the vertex and fragment shaders (
Listing 3-22
). Notice how the
varying variable
vColor
is shared by each of these shaders. As previously men-
tioned, its type should match in both the shaders.
Inside the vertex shader,
vColor
receives per-vertex color data (blue and yellow
color) from the attribute variable
aColor
. When the rendering pipeline processes
the fragments using fragment shader, it interpolates this per-vertex data across the
primitive (that is, across the fragments occupied by the primitive). This is why blue
color at one endpoint of the line primitive interpolates to yellow color at the other
(
Figure 3-12
).
Listing 3-22.
GL VARYING/src/com/apress/android/glvarying/GLES20Render-
er.java
private final String _lineVertexShaderCode =
"attribute vec4 aPosition;"
+ "attribute vec4 aColor;"
+ "varying vec4 vColor;"
+ "void main() {"
+ " vColor = aColor;"
+ " gl_Position = aPosition;"
+ "}";
private final String _lineFragmentShaderCode =
"#ifdef GL_FRAGMENT_PRECISION_HIGH \n"
+ "precision highp float;"
+ "#else \n"
+ "precision mediump float;"
+ "#endif \n"
+ "varying vec4 vColor;"
+ "void main() {"
+ " gl_FragColor = vColor;"
Search WWH ::

Custom Search