Graphics Reference
In-Depth Information
As we will see in a moment, the seting of the texture image and the
sampling parameters still need to happen when using shaders. However, the
seting of the texture environment is replaced by your fragment shader.
GLSL Texture Mapping
With GLSL, your application still needs to set up the texture array texImage
and the associations from Figure 9.1, but you must create your own associa-
tion of the texture with the texture unit. You must set up the uniform vari-
able texLoc , give it the name uImageUnit that you will see throughout this
chapter's examples, and set its value to to something (0, in this example). This
associates the name uImageUnit with the texture GL_TEXTURE0 , and you can
use any of the fragment shaders in this chapter with your application.
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D,texA );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,TEX_WIDTH,TEX_HEIGHT,
0,GL_RGB,GL_UNSIGNED_BYTE,texImage);
GLuint texLoc = glGetUniformLocation(program, “uImageUnit”);
glUniform1i( texLoc, 0 );
The GLSL built-in texture lookup functions give you access to textures
through samplers , set up through the OpenGL API. A texture sampler is a GLSL
uniform variable that has been previously associated with a particular texture
unit (e.g., Texture0 in Figure 9.1). The texture unit acts as a “docking port” for
the texture object itself. The texture object contains sampling information, such
as size, pixel format, number of dimensions, filtering methods, and number of
mip-map levels. These texture properties are taken into account as the texture
is accessed. Regardless of all of these setings, though, texture sampler func-
tions in GLSL always return a vec4 (RGBA) value.
The actual look of the texture mapping result is familiar, but seeing some
straightforward texture mapping code and the resulting image is instructive.
Search WWH ::




Custom Search