View Full Version : Updating a texture?

03-05-2011, 03:02 PM
So... I'm working with SDL/OpenGL, and I'm trying to create a 2d application. I have most of the display code up, but I'm running into a grey area. I have an image on the screen (a textured quad), and what I want to do is keep drawing lines and dots to it over time.

The image data is stored in an SDL_Surface object - it contains some information about the format of the image, and then the pixel data. It starts out as a black image, so I pass this pixel data and the correct flags to the glTexImage2D function (after calling glGenTextures and enabling 2d textures and what not).

After I change the pixel data in the SDL_Surface object... how do I get that information back into a texture? I haven't seen a function to delete a texture, and it seems like I'd be at risk for a memory leak if I just called glTexImage2D again (the image changes size as the screen resizes, so there's different amounts of pixel data as well as different pixel data).

So... is it OK to call glTexImage2D to overwrite a texture?

[also, first post. Hi! I'm neurofuzzy.]

Alfonse Reinheart
03-05-2011, 03:23 PM
So... is it OK to call glTexImage2D to overwrite a texture?

Yes. However, if the image hasn't changed size, then it's a waste of time. You should check to see if the image changed size yourself. If it did, use glTexImage2d; if it didn't, use glTexSubImage2d to just update the pixel data.

In general, it is impossible to leak memory with OpenGL (assuming the driver is doing everything correctly of course) unless you actually lose track of an object (like your texture object) or fail to delete it. Think of subsequent glTexImage2d calls like calls to "realloc".