pompy wtryskowe|cheap huarache shoes| bombas inyeccion|cheap jordans|cheap air max| cheap sneaker|wholesale jordans|cheap china jordans|cheap wholesale jordans
Menu
OpenGL ES Tutorials

OpenGL ES

Texture Functions



Tutorials > OpenGL ES > Texture Functions

View Full Source

Introduction

Texture Functions In the previous tutorial, we dealt with how to load a bitmap and display it as a texture on an object. You were briefly introduced to the glTexParameterf function.

This tutorial will discuss this function further. Various aspects will be discussed.

Texture Filters

Texture filters allow textures to be displayed with differing qualities.

Repeating and Clamping

Textures can be repeated on an object or clamped(stop texturing after displaying texture once).

Mipmaps

Mipmaps are extra textures generated of the same image. If you have a 64x64 image, extra images will be generated (32x32, 16x16, ..., 1x1). The correct texture will then be shown depending on how far away the object is. Obviously objects far away will use a smaller texture. This can save precious processor time.

This tutorial is built upon the previous tutorial.

Contents of main.cpp :


We are going to be creating 4 different textures in this tutorial. We also want to keep track of what texture we are using by specifying a filter variable.

GLuint texture[4];

short filter = 0;

Our texture coordinates remain the same except for the sides. Remember we said that the value can be between 0 and 1. We can use a value above 1, but this will either cause the texture to repeat or to stop when hitting 1. This will be explained further below.

GLfloat texCoords[] = {
	// FRONT
	 0.0f, 0.0f,
	 1.0f, 0.0f,
	 0.0f, 1.0f,
	 1.0f, 1.0f,
	// BACK
	 1.0f, 0.0f,
	 1.0f, 1.0f,
	 0.0f, 0.0f,
	 0.0f, 1.0f,
	// LEFT
	 2.0f, 0.0f,
	 2.0f, 2.0f,
	 0.0f, 0.0f,
	 0.0f, 2.0f,
	// RIGHT
	 2.0f, 0.0f,
	 2.0f, 2.0f,
	 0.0f, 0.0f,
	 0.0f, 2.0f,
	// TOP
	 0.0f, 0.0f,
	 1.0f, 0.0f,
	 0.0f, 1.0f,
	 1.0f, 1.0f,
	// BOTTOM
	 1.0f, 0.0f,
	 1.0f, 1.0f,
	 0.0f, 0.0f,
	 0.0f, 1.0f
};

Our loadTextures function loads the zeus.bmp file as before.

bool loadTextures()
{
	BITMAPINFOHEADER info;
	unsigned char *bitmap = NULL;
	
	bitmap = loadBMP("zeus.bmp", &info);

	if (!bitmap)
		return false;

We need to generate 4 texture names.

	glGenTextures(4, texture);

We select our first texture and set the properties of the texture as before. The difference here is that we pass GL_NEAREST onto our filter properties instead of GL_LINEAR. This is a faster filter but does not look as good.

	// Texture 1
	glBindTexture(GL_TEXTURE_2D, texture[0]);

	glTexParameterf(GL_TEXTURE_2D, 
		GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D,
		GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, info.biWidth,
		info.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
		bitmap);

Our second texture uses GL_LINEAR to create a better looking texture. This is what was used in the previous tutorial.

	// Texture 2
	glBindTexture(GL_TEXTURE_2D, texture[1]);

	glTexParameterf(GL_TEXTURE_2D, 
		GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,
		GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, info.biWidth,
		info.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
		bitmap);

The third texture is the same as the second texture except that two new properties are being set. These are the GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T properties. This specifies how textures should be wrapped in the horizontal and vertical directions respectively.

You can pass either GL_REPEAT or GL_CLAMP_TO_EDGE for these properties. The default value is GL_REPEAT. GL_REPEAT repeats the texture if you specify a texture coordinate above 1. GL_CLAMP_TO_EDGE will clamp the value to prevent repeating of the texture.

	// Texture 3
	glBindTexture(GL_TEXTURE_2D, texture[2]);

	glTexParameterf(GL_TEXTURE_2D, 
		GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,
		GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,
		GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D,
		GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, info.biWidth,
		info.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
		bitmap);

The fourth texture uses the technique of mipmaps. Mipmaps require more memory but they may cause your program to run a lot faster. To automatically generate mipmaps, you can set the GL_GENERATE_MIPMAP property to GL_TRUE by calling the glParameterf function. Your GL_TEXTURE_MAG_FILTER will remain the same but your GL_TEXTURE_MIN_FILTER property must change.

This filter must be set to GL_X_MIPMAP_Y where X and Y can be either LINEAR or NEAREST. This is used to specify the quality of the textures being displayed. Obviously NEAREST does not look as good as LINEAR.

	// Texture 4
	glBindTexture(GL_TEXTURE_2D, texture[3]);

	glTexParameterf(GL_TEXTURE_2D,
		GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, 
		GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,
		GL_GENERATE_MIPMAP, GL_TRUE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, info.biWidth,
		info.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
		bitmap);

	delete[] bitmap;

	glBindTexture(GL_TEXTURE_2D, texture[filter]);

	return true;
}

Our menu function changes slightly to make way for a third menu option (Change Filter). This alternates between all textures that were created earlier.

	case 3 :
		++filter %= 4;

		glBindTexture(GL_TEXTURE_2D, texture[filter]);

		break;

You should now be able to use different filters and texturing techniques to create different quality textures, speed up your programs and repeat and clamp textures.

Please let me know of any comments you may have : Contact Me

GLUT|ES Source Files : Embedded Visual C++ 4.0
UG Source Files : Embedded Visual C++ 4.0

Last Updated : 20 November 2005


< Tutorial 17 - Texture Mapping Tutorial 19 - Blending >

Back to Top


All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer

Links