Jump to content
  • Advertisement
Sign in to follow this  
LGAB

OpenGL Update textures and cleaning up memory

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I have a number of textures which needs to be updated with user interaction, basically a way to preview a sequence of image files as a texture in OpenGL. There's two things I need to solve, one is that the used memory keeps growing as I update the texture with new images. I'm trying to delete the memory structures as soon as they're uploaded to texture memory, but I'm doing something wrong. The other thing is that I can't seem to update the texture, the only thing that works right now is to make a new texture and call glTexImage2D(), but I want to use glTexSubImage2D() to speed up the process of switching textures. But as soon as I use 'SubImage' the texture turns white. What am I doing wrong? [looksaround] Here's the code I'm using to load images, via freeImage.
int txtRes = 256; 	// texture resolution

// Convert string to char*
char * cstr = new char[c.length()];
strcpy(cstr, c.c_str());

// Try to load image (freeImage library)
img = readImage(cstr);

// Test for success, and proceed
if (img.image != NULL) {
	img.image = FreeImage_Rescale(img.image, txtRes, txtRes, FILTER_BOX);	// resize to a square resolution

	unsigned char *data = FreeImage_GetBits(img.image);	// convert to unsigned char, maybe not necessary?

	if (!loaded) {
		glGenTextures(1, &img.texID);
		glBindTexture(GL_TEXTURE_2D, img.texID);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // alt: GL_LINEAR
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  txtRes, txtRes, 0, GL_BGRA, GL_UNSIGNED_BYTE, data); 
	} else {
		glBindTexture(GL_TEXTURE_2D, img.texID);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, txtRes, txtRes, GL_BGRA, GL_UNSIGNED_BYTE, data);
	}

	loaded = true;	// Next time, use glTexSubImage
	
	// image uploaded to texture memory, clean up memory
	delete[] data;
	FreeImage_Unload(img.image);
} else { 
	//cout << " .. load failed!!" << endl;
	fname = "";
}
// clean up tmp cstring
delete[] cstr;

[Edited by - LGAB on July 25, 2008 3:57:39 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hey again, I seem to have solved my problems! Here's what I did in case someone else needs it.

I solved the glTexSubImage2D issue by changing my freeimage wrapper routine to return by reference instead, that also meant one less duplication of the image structure.

I also removed a conversion to 32bit by checking the bitdepth and choose GL_RGB and GL_RGBA depending on the format instead of converting all of them to 32.

Then I found something similar to this in an example at the OpenGL.org forums:

FIBITMAP* temp = img.image;
img.image = FreeImage_Rescale(img.image, txtRes, txtRes, FILTER_BOX);
FreeImage_Unload(temp);

Seems the rescale routine creates a leak unless you copy the old pointer and unload the original image so you only have the rescaled image in memory.

Now I can update the texture as much as I want with new images without any memory leaking!

Share this post


Link to post
Share on other sites
You don't need four calls to glTexParameter. Just the glTexParameteri ones will suffice (i is technically more correct for GL_MAG or GL_MIN filters I suppose, though I'm not sure it actually matters).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!