How to set up multiple textures


I am able to set up textures, but sometimes I do have problems with it. I want to load a few textures into the memory. I am doing this using:

GLuint texture;
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

So texture should be a uniwue identifier for each texture. But if I am trying to load different (and also textures which are already in the memory) textures. The texture id keeps the same. So that from time to time a wrong texture is mapped on a surface.

I think it could be, because OpenGL recognizes, that the texture is already loaded.

Someone has an idea?


Each time you call glGenTextures you should get a unique texture ID and you need to keep that ID and re-bind it each time you want to use it.

Without looking at some code we can’t possibly do anything other than guess what is going wrong for you. But from what you have posted I am wondering if you are overwriting the GLuint texture variable you have each time you ask for a new texture ID and consequently losing the IDs of previous textures.

Your theory about GL recognizing identical texture data and ignoring them is wrong though, and from your description I really suspect something is going on elsewhere in your code.

Replace glGenTextures(1 , &texture) by glGenTextures(N, texture)

where texture is GLuint* type and N > 1.

This way you will have different tex ids for each texture[i] , i belonging to set [1,N]

Then for each id do this

glBindTexture( GL_TEXTURE_2D, texture[i] );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB,