Creating a Texture

Initialises OpenGLES texture object

The steps below demonstrate how to create a texture object in OpenGL ES, set its parameters and fill it with texture data.

What are Textures?:

When OpenGL ES draws a polygon to the screen, it must be given colour values for each pixel the polygon covers. The most popular method to do this is called texturing, where the colour data used to colour the polygon is called a texture. A texture is essentially an image, which is sampled by each pixel a polygon covers.

First generate a new texture handle so the application can refer to it.
 	glGenTextures(1, &_texture);
 

This function binds this texture to the target GL_TEXTURE_2D so texture can be modified. Binding a texture means any OpenGL ES calls which refer to the target, GL_TEXTURE_2D, will affect this texture.

 	glBindTexture(GL_TEXTURE_2D, _texture);
 

This function generates the texture pattern on-the-fly into a block of CPU-side memory.

 	generateTextureData();
 

These functions set the texture wrap parameters. In this case, the texture will need to repeat if texture co-ordinates go above 1 or below 0.

 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 

These functions set the minification and magnification filter parameters. In this case, basic filtering without mipmaps is required, so GL_LINEAR is used.

 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 

The texture can now be loaded with data.

When calling this function, the properties of the texture image also have to be set. These include:

     1) Level of detail - mipmapping is not used here so this is set to 0 for the base image level.

     2) Internal format and the format - these specify the number of colour components and how pixel data is stored. In this case these are set to RGBA.

     3) Image dimensions.

     4) Border - this is always set to 0.

     5) Type - this is the data type of the pixel data. Here it is set to GL_UNSIGNED_BYTE.

Finally, the _image variable refers to where the actual data is stored in memory.

 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, _image);
 

Check for any GL Errors after an GL call.

 	if (!testGLError(_surfaceData, "glTexImage2D"))
 	{
 		return false;
 	}
 	return true;