View Full Version : Better implementation for getting pixels from texture

02-22-2016, 11:11 AM
Hello, I am looking for better implementation for getting pixels from texture(haven't have much luck on google)

here is part of my code where memory leak is happening:

unsigned char* Texture::getPixels(){

if(isLoaded()&&_newTex){/*isLoaded check if opengl initialized texture(_textureID with glGenTextures) and _newTex check if new image for texture is loaded*/
glBindTexture(GL_TEXTURE_2D, _textureID);//bind texture

_texBuffer=new unsigned char[iWidth*iHeight*4]; //create a array pointer for pixels

glGetTexImage (GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, _texBuffer);// get pixels to the array pointer

glBindTexture(GL_TEXTURE_2D, 0);// unbind

_newTex=false; //change value to avoid alocating and use of glGetTexImage when it's not needed(texture have not changed).
return _texBuffer; //return buffer

I am using this functions to marge / combine two textures in to the one texture, but with this implementation there will be always new pointer to pixels for example in case with texture with Dynamic text on it.

Is there a better implementation ? or is there a better way to merge / combine two textures ?

02-22-2016, 01:43 PM
It might be best if you said why you wish to merge/combine two textures. There's probably a much better way of doing this on the GPU.

02-23-2016, 01:04 PM
Well, for example in Photoshop you have "layers" and when you are done, you can merge all layers in to one single image, in my case renderable texture... I thought about letting a shaders merge two textures, but then I thought:
1. If I use sampler2D, then maximum of textures that I can merge together(for example merging 5 textures/images together) will be limited by number of defined samplers(2d) in the shader.
2. I would must add merge samplers to all shaders I ever make to support this functionality.
3. If I only wanted to merge multiple textures before initialization and main loop, calculating merge on every frame on GPU could slow down performance. (so in short I just load for ex. two textures merge to third texture and then delete former textures and just use that combined/merged texture the whole runtime)

and to properly answer your question, I am making some kind of GUI/Framework and I need to update dynamic Text texture with some other texture/s as a background.

02-23-2016, 01:51 PM
You can render into a texture. If you want to merge several images you simply render them on top of each other. For more information you should read up on Frame Buffer Objects (FBO's).