glGenTexture in a Display List

Hello to all,
i am usign display list in order to draw my scene. Some textures are attached to some object and so i generate and bind the texture inside the Dispaly List as following:

if ( IsTextured() )
		glEnable( GL_TEXTURE_2D );
		osg::ImagePtr img = m_textures->getImage();
		GLuint textureID = 0;
        glGenTextures(1, &textureID);							// Create The Texture
        glBindTexture(GL_TEXTURE_2D, textureID); 
        glTexImage2D(GL_TEXTURE_2D, 0, img->getPixelFormat() , img->getWidth(), img->getHeight(), 0, img->getPixelFormat(), img->getDataType(), img->getData());
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);                
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);                    
		SetTextureObject(textureID); //store the current textureId for a future use

After drawing correctly the scene i need to do some shading operation using glsl and the loaded texture. So i need to pass to the shaders the correct textureID but when i try to read that value:

unsigned int textureID = vrobject->GetTextureID();

the returned value is always equal to the last texture id generated.
Any idea about?
thank you very much.

ps. i think i could try using texture object insted, generate them outiside display list generation and call them inside but i would like to understand why it is not working the proposed solution.

Basically a Display List is the actual settings on the GPU defined. Not a list of instructions. I am sure there is a better way to explain that, but basically it doesn’t do any of that setup stuff any more it simply has the settings for the texture and so returns the same value AFAIK. This is why dlists are fast. They don’t think. They just shuffle numbers in effect.

Also you can’t use that command in a display list AFAIK. But in any case IMHO you shouldn’t! The acceleration you get from setup stuff in dlists is negligible. What they are really intended for is drawing lots of geometry, or repeatedly using the same geometry and so on… The idea being you alter things like texture settings and matrices outside the dlist, and then call it again to get a different colour tree in a new location… and so on…

Do bear in mind that Display Lists are also deprecated in GL3.0 so you should really move away from them and focus on buffer objects, which is effectively what display list really are if used properly.

Do also bear in mind that creating a texture is an expensive operation so much better to make them in batches at the beginning of a program, or reuse them at run time.

OpenGL spec 2.1, section 5.4 Display Lists, page 244:

"Certain commands, when called while compiling a display list, are not compiled into the display list but are executed immediately. These commands fall in several categories including

Pixels and textures: PixelStore, ReadPixels, GenTextures, DeleteTextures, and AreTexturesResident.
Other queries: All query commands whose names begin with Get and Is (see chapter 6).