Graphics Programs Reference
In-Depth Information
+ "#endif \n"
+ "varying vec2 vCoord; \n"
+ "uniform sampler2D uSampler; \n"
+ "void main() { \n"
+ " gl_FragColor = texture2D(uSampler,vCoord); \n"
+ "} \n";
The attribute variable aCoord (of type vec2 ) in the vertex shader receives the tex-
ture coordinate ( Listing 5-4 ) and passes it to the fragment shader as a varying vari-
able vCoord (of type vec2 ). The fragment shader uses this varying variable to get
the interpolated texture coordinates for fetching the texture units ( texels ) from the
loaded texture.
The uniform variable uSampler in the fragment shader ( Listing 5-6 ) is of type
sampler2D . Variables of type sampler* ( sampler2D and samplerCube )
are special types of a uniform variable, used to fetch from a texture map. The
sampler* uniform has to be loaded with a value specifying the number of current
texture (in a zero-based manner).
Note sampler2D uniform is used with 2D textures, and
samplerCube uniform is used with cubemap textures.
This value is loaded using the glActiveTexture function. If a single texture is
used, this function takes constant GL_TEXTURE0 as argument, as shown in Listing
5-7 . For every successive texture used, the next higher constant is used.
Listing
5-7. GL TEXTURE/src/com/apress/android/gltexture/GLES20Render-
er.java
GLES20.glUseProgram(_planeProgram);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, _textureId);
GLES20.glUniform1i(_planeUSamplerLocation, 0);
GLES20.glUniformMatrix4fv(_planeUMVPLocation, 1,
false, _MVPMatrix, 0);
 
 
Search WWH ::




Custom Search