Graphics Reference
In-Depth Information
The vertex shader takes in a two-component texture coordinate as a vertex
input and passes it as an output to the fragment shader. The fragment
shader consumes that texture coordinate and uses it for the texture fetch.
The fragment shader declares a uniform variable of type
sampler2D
called
s_texture
. A sampler is a special type of uniform variable that is used to
fetch from a texture map. The sampler uniform will be loaded with a value
specifying the texture unit to which the texture is bound; for example,
specifying that a sampler with a value of 0 says to fetch from unit
GL_TEXTURE0
, specifying a value of 1 says to fetch from
GL_TEXTURE1
, and
so on. Textures are bound to texture units in the OpenGL ES 3.0 API by
using the
glActiveTexture
function.
void
glActiveTexture
(GLenum
texture
)
the texture unit to make active:
GL_TEXTURE0
,
GL_TEXTURE1
,
… ,
GL_TEXTURE31
texture
The function
glActiveTexture
sets the current texture unit so
that subsequent calls to
glBindTexture
will bind the texture to the
currently active unit. The number of texture units available to the
fragment shader on an implementation of OpenGL ES can be queried
for by using
glGetintegerv
with the parameter
GL_MAX_TEXTURE_
IMAGE_UNITS
. The number of texture units available to the vertex
shader can be queried for by using
glGetIntegerv
with the parameter
GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS
.
The following example code from the
Simple_Texture2D
example shows
how the sampler and texture are bound to the texture unit.
// Get the sampler locations
userData->samplerLoc = glGetUniformLocation(
userData->programObject,
“s_texture”);
// ...
// Bind the texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, userData->textureId);
// Set the sampler texture unit to 0
glUniformli(userData->samplerLoc, 0);
At this point, we have the texture loaded, the texture bound to texture
unit 0, and the sampler set to use texture unit 0. Going back to the
fragment shader in the
Simple_Texture2D
example, we see that the