Graphics Programs Reference
In-Depth Information
private final String _pointVertexShaderCode =
"attribute vec4 aPosition;"
+ "void main() {"
+ " gl_PointSize = 15.0;"
+ " gl_Position = aPosition;"
+ "}";
To supply input data, we use attribute variables. Like gl_PointSize and
gl_Position variables, attribute variables are only available in the vertex shader.
They are used to specify the per-vertex inputs to the vertex shader (per-vertex inputs,
such as position and color).
Using the glGetAttribLocation method, we first access the attribute variable
inside the vertex shader of a program . Only then can we pass data to it. For
the Renderer class, field _pointAVertexLocation stores the location of the
attribute variable aPosition using _pointAVertexLocation =
GLES20.glGetAttribLocation(_pointProgram, "aPosition") .
Add this line after ES 2.0 function glLinkProgram shown in Listing 3-16 .
Finally, inside the onDrawFrame method, we use the FloatBuffer
_pointVFB to pass per-vertex data (in this case, the per-vertex data is the vertex
itself; that is, the coordinate representing the vertex) to the attribute variable aPos-
ition . If we only want to render a single point sprite, there is no need to go through
all these steps. We can directly write to the gl_Position variable as shown in
Listing 3-8 . To obtain output as shown in Figure 3-10 , replace the onDrawFrame
method with Listing 3-20 .
To pass per-vertex data (such as per-vertex colors, per-vertex normals, or, as in this
case, the coordinate representing this vertex) to the aPosition variable, we use the
method glVertexAttribPointer(int indx, int size, int type,
boolean normalized, int stride, Buffer ptr) . Based on the type
of per-vertex data we are dealing with, we specify the size of this data as int size .
For example, if we are passing per-vertex position data (that is, vertex coordinate),
for a single vertex (x, y, z) or a set of vertices ( pointVFA in Listing 3-17 ) ,
then size will be '3.' Similarly, if we are passing per-vertex colors (r, g, b,
a) , size will be '4.'
Listing 3-19. GLPOINTADVANCED/src/com/apress/android/glpointadvanced/
Search WWH ::

Custom Search