#### Archived

This topic is now archived and is closed to further replies.

# Textures - how to free texture memory

## Recommended Posts

Hi all! I am not sure how I can deallocate texture memory on-the-fly while playing my little game. I have an algorithm to load (.bmp) 2D image which uses variable ( char *m_rgb; - global var) to store all image data in function LoadBMPimage(filename). I only allocate memory there but dont deallocate anything. Now in the function below loadTexture2D(..) I use that variable m_rgb again already with filled-in image data to load textures. My question is if I should include at the end of the funcion below this following line -> delete [] m_rgba; to deallocate memory? is it right?. Obviously I want to use memory as less as possible thus this question! void CBitmap::loadTexture2D(char *filename, GLboolean alpha, GLenum minfilter, GLenum magfilter, GLenum wrap) { loadImageBMP(filename); //load .bmp image .....etc /* Set texture image then load the texture*/ if (minfilter == GL_LINEAR || minfilter == GL_NEAREST) { glTexImage2D(GL_TEXTURE_2D, 0, 3, m_bmpWidth, m_bmpHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, m_rgb); } else { gluBuild2DMipmaps(GL_TEXTURE_2D, 3, m_bmpWidth, m_bmpHeight, GL_RGB, GL_UNSIGNED_BYTE, m_rgb); } HERE ==>> delete [] m_rgba; <<==HERE } I did test that after adding the extra line and the program still runs ok. I can see all my textures even though I deleted the memory! The memory storage is destroyed but the textures are still there!!? How does it work!? is it put into somewherelse in memory? because the textures are still there?! NOW! my main question is.. for example imagine I loaded 100 textures and it took about 50% of my total memory available on my system and then I destroyed the m_rgb array containing the image data for each already loaded (.bmp) image by using delete!! Does it mean I don't have to worry anymore about the memory taken previously by 100 textures. is the memory completely freed? am I doing right? If thats not all then how do I deallocate texture memory on the fly. I mean for instance while flying above terrain and I want to get rid of some textures and load others? DO I have to do anything else? BTW: is it what you guys normally do? I mean destroy the unused tex and load the new ones when dealing with many textures? or you think it is more efficient if you load all textures at once at the beginning of a program execution? Thanks! Sorry but I am just a beginner!! Edited by - sandrew on January 14, 2002 10:09:45 PM

#### Share this post

##### Share on other sites
After you call glTexImage2D (or some other function) you can delocate the memory that was holding the image data. To delete the texture created by glTexImage2D use glDeleteTexture.

[Resist Windows XP''s Invasive Production Activation Technology!]

#### Share this post

##### Share on other sites
Thanks 4 help "Null and Void"!!

What do you think is better?

1. load ALL textures (eg. 40) at the beginning of the program execution and just immediately delete all those arrays after image loading which hold the image data (*m_rgb in my case) and just use those textures till the end of the program without using glDeleteTexure. Just swapping them whenever needed without deleting them OR

2. load the only textures I currently need (eg. 10), immediately delete those ten arrays *m_rgb after image loading and then use glDeleteTexture whenever I want to get rid of any of those ten textures? and repeat the cycle again and again? I think this is how it happens in real games! doesn't it?

Edited by - sandrew on January 14, 2002 10:36:00 PM

Edited by - sandrew on January 14, 2002 10:38:29 PM

#### Share this post

##### Share on other sites
Load all the textures you''ll be needing in the short future (or now) at one time. When you''re done with them get rid of them. For example: you may want to keep a ''font texture'' in memory at all times for quick usage (so load it at the beginning and delete it at the end), but you will only need the ''brick 25'' texture during one level (so load it when the level begins, and delete it after the level is over).

[Resist Windows XP''s Invasive Production Activation Technology!]

#### Share this post

##### Share on other sites
OK!! thanks!!

but.. do u think that if dont use glDeleteTexture then the texture will hold as much memory as the array into which I loaded the image data which then I deleted?

BTW: there is almost no documentation about glDeleteTexture function. Only 9 pages found on GOOGLE! is it standard OpenGL? or some kind of an exension that I need?

I get an error :
error C2065: 'glDeleteTexture' : undeclared identifier

What is the 1st parameter of glDeleteTexture? I am not sure!

Edited by - sandrew on January 14, 2002 11:05:59 PM

#### Share this post

##### Share on other sites
Sorry, my fault, it''s glDeleteTextures (I left off the ''s''). Yes, the correctly spelled version is standard. Yes, the texture takes up approximately as much memory as your array did until you call glDeleteTextures (except that it may be video memory, system memory, AGP, whatever). You use it like this:
  GLuint SomeTexture;/* Load Texture Here...*/glDeleteTextures(1, &SomeTexture);

To delete more than one texture:
  GLuint SomeTexture[3];/* Load Three Textures Here...*/glDeleteTextures(3, SomeTexture);

[Resist Windows XP''s Invasive Production Activation Technology!]

• ### Forum Statistics

• Total Topics
628366
• Total Posts
2982274

• 10
• 9
• 13
• 24
• 11