mirroring an ofTexture

I’m wondering if anyone knows how I can vertically flip the pixels from an ofTexture?

I know I could do it by reading the pixels, then manually looping and inverting them… but there must be a faster/better way?

I use glReadPixels() to get the raw pixel data… but I need that data flipped vertically.

Well… here is the solution I have where I read the pixels, then manually flip them.

This is a getPixels() method I wrote for the ofFBOTexture class… Seems to be working pretty nicely and doesn’t seem to slow anything down.

The part that’s relevant to this post’s topic is in the if(invertY) block. Similar technique could be used for anytime you are needing to vertically flip some raw pixel data.

  
  
unsigned char* ofFBOTexture::getPixels(unsigned char* pixels, int x, int y, int w, int h){  
	if(w <= 0 || w > this->getWidth())  
		w = this->getWidth();  
	if(h <= 0 || h > this->getHeight())  
		h = this->getHeight();  
	if(x > this->getWidth())  
		x = 0;  
	if(y > this->getHeight())  
		y = 0;  
	if((x + w) > this->getWidth())  
		x -= (x+w-this->getWidth());  
	if((y + h) > this->getHeight())  
		y -= (y+h-this->getHeight());  
	  
	bool shouldSwapOut = false;  
	if(!this->isSwappedIn()){  
		this->swapIn();  
		this->setupScreenForMe();  
		shouldSwapOut = true;  
	}  
  
	if(invertY){  
		unsigned char* tmpPix = new unsigned char[w*h*4];  
		glReadPixels(x, this->getHeight()-y-h, w, h, GL_RGBA, GL_UNSIGNED_BYTE, tmpPix);  
		for(int i=0; i < h; i++)  
			memcpy(pixels+(i*w*4), tmpPix+((h-i-1)*w*4), w*4);  
		delete tmpPix;  
	}  
	else  
		glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);  
	  
	if(shouldSwapOut){  
		this->setupScreenForThem();  
		this->swapOut();  
	}  
	  
	return pixels;  
}