Help end child hunger

Simple Texture

Prev: Spot Light Per Pixel Next: Texture + Fragment

In order to perform texturing operations in GLSL we need to have access to the texture coordinates per vertex. GLSL provides some attribute variables, one for each texture unit:

	attribute vec4 gl_MultiTexCoord0;
	attribute vec4 gl_MultiTexCoord1;
	attribute vec4 gl_MultiTexCoord2;
	attribute vec4 gl_MultiTexCoord3;
	attribute vec4 gl_MultiTexCoord4;
	attribute vec4 gl_MultiTexCoord5;
	attribute vec4 gl_MultiTexCoord6;
	attribute vec4 gl_MultiTexCoord7;

GLSL also provides access to the texture matrices for each texture unit in an uniform array.

	uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords];

The vertex shader has access to the attributes defined above to get the texture coordinates specified in the OpenGL application. Then it must compute the texture coordinate for the vertex and store it in the pre defined varying variable gl_TexCoord[i], where i indicates the texture unit.

The simple following instruction sets the vertex texture coordinate for texture unit 0 just by copying the texture coordinate specified in the OpenGL application.

	gl_TexCoord[0]  = gl_MultiTexCoord0;

A simple example of a vertex shader to setup texture coordinates for a texture, using texture unit 0, could be:

void main() {

	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = ftransform();

If we wanted to use the texture matrix then we could write:

void main() {

	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
	gl_Position = ftransform();

As mentioned before gl_TexCoord is a varying variable, i.e. it will be used in the fragment shader to access the interpolated texture coordinate.

In order to access the texture values it is necessary to declare a special type of variable in the fragment shader. For a 2D texture we could write:

	uniform sampler2D tex;

Data types for 1D and 3D textures are also available, the general format is: sampleriD, where i is the dimensionality of the texture.

The user defined tex variable contains the texture unit we are going to use, in this case 0. The function that gives us a texel, a pixel in the texture image, is texture2D. This function receives a sampler2D, the texture coordinates, and it returns the texel value. The signature is as follows:

	vec4 texture2D(sampler2D, vec2);

The returned value takes into account all the texture settings as defined in the OpenGL application, for instance the filtering, mipmap, clamp, etc…

Our fragment shader can then be written as:

uniform sampler2D tex;

void main()
	vec4 color = texture2D(tex,gl_TexCoord[0].st);
	gl_FragColor = color;

Notice the usage of selector st when accessing gl_TexCoord. As mentioned in section Data Types and Variables, when accessing texture coordinates the following selectors can be used: s,t,p,q. (Note that r is not used to avoid conflicts with rgb selectors).

Texture Textured Cube

A Shader Designer project is available in here.


Prev: Spot Light Per Pixel Next: Texture + Fragment

2 Responses to “Simple Texture”

  1. Hi, i would like to leave you a minor mistake, i guess you wanted to say fragment instead of frament here: “In order to access the texture values it is necessary to declare a special type of variable in the frament “

© 2014 Suffusion theme by Sayontan Sinha