glTexImage2D and glTexSubImage2D

Started by
4 comments, last by snoutmate 14 years, 6 months ago
Ok, so this question might branch out alittle so be prepared... :) first, I'd like to know this, I've been strugling with this for a day now... When you get pixeldata and put it into the graphics card using glTexImage2D the pixel info will be stored there and will be given a tag from glGenTextures() I have no problem understanding this but heres my shady part. Lets say we called glGenTextures( 1, &texID ); glBindTexture( TexID ); and then you called glTexImage2D( ...... pixeldata ); The pixel data you had would be stored into that texture 'slot' But now lets say we advance 20 frames and we want this texture, TexID to have different pixeldata. Would you do this... glBindTexture( TexID ); glTexImage2D( ..... newpixeldata ); ? This doesn't seem right because what happens to the first round of pixeldata? MemoryLeak? Do I need to call glDeleteTexture(TexID); then redo the entire process or would I use glTexSubImage2D. This function I recently found but I'm too afraid to try it out because I don't want any memory leaks. Excpecialy with textures. So would glTexSubImage2D() REPLACE the pixeldata or not? Yay, hopefuly you got through that... Part 2 :D I'm making a demo game that will support dynamic lightmaps. Now these lightmaps will not be calculated every frame, because that would bring frame count down to < 1. I'dd calc the lightmaps in the loading screen. Basicly the lightmaps are just pixeldata... UHOH more pixel data. Ill do a simple example of what I'm going for. You have 1 quad and 1 light. The light IS NOT an opengl light, its a struct I made that holds xpos ypos zpos, red, green blue, radius. ( these lights aren't directional they go in all directions ) Now lets say the quad is 20 units away from the light and the lights radius was 50. The light intensity would be calculated, then multiplyed by the RGB values and walla, you have pixel info based on these calcs. ( Now ofcourse there will be some more calculations on my part, but this is a question, not a tutorial :) So the data we have right now is just 1 rgb value. I'd get something like 16 x 16 sample calcs so I'd end up with a 16 x 16 image. Theres your lightmap! But now Instead of multiTexturing that onto the quad I'd like to get the texture pixeldata thats on the quad. So If a texture was applyed to this quad and the entire texture was a shade of red ( 255, 0, 0 ) I'd need to get the texture pixeldata and AVG the lightmap to this data. How do you get the texture pixelData? Would you use glGetTexImage()? if so how do you specify which texture you want to be selecting since that function doesn't have an ID argument? Thanks for spending your time reading this long multipart question. Hope you've had fun :D Now try and have some more fun by answering this multipart question :D Thanks
Advertisement
glTexSubImage2D will replace the data.

I wouldn't recommend reading texture data back to the CPU, since it will be very slow. If you really want to take this path you should store a copy of the texture on the CPU, and manipulate that and then re-upload it with glTexSubImage2D.
It is usually better to do such things with blending and multi-texturing, and shaders for more advanced effects.

In the long run you want to do as little as at all possible on the CPU, and do everything on the GPU with shaders etc, but for starters you should be fine if you just upload to the GPU, and don't read back.
Would you know what happens if you don't call glDeleteTextures();
Is the Pixel Data still stored on the card which results in a memoryLeak?
Probably, until your program exits. When the program exits everything is freed.
That being said, you should always delete resources that you are done with, completely done with.
You do not need to delete and re-create a texture to change its data though, only when you don't want that texture anymore.
No, it doesn't leak. Calling glTexImage2D again removes the old pixel storage and creates a new one.
Quote:Original post by zaneski13
if so how do you specify which texture you want to be selecting since that function doesn't have an ID argument?

OpenGL is a state machine, all texture functions operate on currently bound texture (that is the texture you set with last call to glBindTexture). It's the same for VBOs, FBOs etc.

This topic is closed to new replies.

Advertisement