Dynamic Creation of Texture Objects

Hi Folks,

   We have been developing an app which needs bitmap textures to be loaded dynamically into memory and released from memory if they r not in use. I know how to do for a fixed textures count means " n textures". I want the process to be dynamic,ie; select image and load that texture, how to handle this.

    How to handle the glGenTextures and glBindTextures?

This is my code for 3 textures.

/////// texture object array
GLuint textures[100];

////// on create func
{
InitGL();
glGenTextures(3,textures);
SetupDefaultRC();
}

//////// loading bitmaps
void SetupDefaultRC()
{
BYTE *pBytes;
int nWidth, nHeight;
char strBuffer1[50];
char strBuffer2[10];

glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);

// Load the texture objects
pBytes = gltReadBMPBits("Default/default_grid_tex.bmp", &nWidth, &nHeight);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,nWidth, nHeight, 0,
	GL_BGR_EXT, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);

pBytes = gltReadBMPBits("Default/default_inner_wall_tex.bmp", &nWidth, &nHeight);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,nWidth, nHeight, 0,
	GL_BGR_EXT, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);

pBytes = gltReadBMPBits("Default/default_outer_wall_tex.bmp", &nWidth, &nHeight);
glBindTexture(GL_TEXTURE_2D, textures[2]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,nWidth, nHeight, 0,
	GL_BGR_EXT, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);

}

Thanks
kumar.k

what’s the problem?

when another image is read in, store the RGBA data in a pixel buffer, generate a texture ID,
bind it and call glTexture2D.

when an image is no longer needed, delete the texture from texture memory.

Hi Thanks for reponding.

can u explain it bit clearly with dynamic statergy. Give me some code lines for glGenTextures and glBindTextures and glTexture2D for the new texture Object Instance.

Thanks
kumar.k

glGebTextures provides you with a new texture object ID that you can use to upload texture data. Threre’s nothing stopping you from using it dynamically.

unsigned texture;
glGenTextures(1, &texture);

//Bind texture and upload data

What part of this do you not understand? How to allocate data structures dynamically has nothing to do with OpenGL.