PDA

View Full Version : handling dynamic texture objects



Kumar K
06-07-2004, 02:42 AM
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_DE CAL);

// 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

jwatte
06-07-2004, 08:08 AM
This is entirely an application data management question, which probably doesn't belong on OpenGL.org at all. At least, it doesn't belong in the advanced forum.

The easist possible way is to create a Texture object instance per texture, where your Texture class has a member named "id". When you load a new texture, you call glGenTextures() to generate a new id for that Texture. When you delete the Texture, it calls glDeleteTextures() on that id. Make sure the correct context is current when creating and deleting these objects.

Kumar K
06-07-2004, 08:11 PM
Thank u for responding to the mail.
I will make sure , not to enter these type of queries in Advanced Forum.I guess ur absolutely right in ur reply regarding creation of texture instance. Let me try that and get back to u sooner.

Thanks once again
Kumar.k