Posts Tagged ‘OpenGL textures parameters’

OpenGL glTexParameters

Wednesday, August 22nd, 2012

Silly me, I thought that whenever you bind a texture for rendering, you need to set the texture parameters at that time. Through a bit of trial and error, and a lot of reading, unless there is a need to change the texture parameters during runtime, you can set them once and forget about them at initial texture loading to the GPU. As long as this texture is valid (not been deleted by glDeleteTextures(…)), then these parameters will remain valid and associated with this texture. Of course, loading of textures should ideally be done once, at the start of the application or game’s level.

// allocate a new texture, bind it and set the initial parameters
GLuint name = 0;
glGenTextures(1,&name);
glBindTexture(GL_TEXTURE_2D,name);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
// set other texture parameters
...
// load our texture data
glTexImage2D(GL_TEXTURE_2D,...);

 

Now, when we want to render our quads (double triangle strips), we just bind our texture (assuming that textureID is value obtained from the code snippet above and saved someplace logical).

glBindTexture(GL_TEXTURE_2D,textureID);

And submit our quad information

GLfloat verticies[8] = {...}; // 4 x,y pairs
GLfloat texCoords[8] = {...}; // 4 s,t pairs
GLubyte colors[32] = {...}; // 4 r,g,b,a quartets
glVertexPointer(2,GL_FLOAT,0,verticies);
glTexCoordPointer(2,GL_FLOAT,0,texCoords);
glColorPointer(4,GL_UBYTE,0,colors);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);

The less OpenGL calls you need to perform, the better your game’s performance will be. Hopefully this bit of information will help.