ofxFBOTexture > ofImage > Save working weird

Hey everyone

So I’m drawing things in a ofxFBOTexture, saving the pixels to an ofImage and then saving it to disc… but I’m having some weird issues.

Some times the ofImage get the picels correctly, but sometimes the pixels offset, so the image distorts. It’s very weird, because I’m using exactly the same width and height for the texture and for the image, and they are both color.

This is my code:

  
  
ofxFBOTexture tex;  
		tex.allocate(colors.size(), 300, GL_RGB);  
		  
		tex.begin();  
		  
		for (int i = 0; i < colors.size(); i++)   
		{  
			ofSetColor(colors[i], colors[i], colors[i]);  
			ofRect(i, 0, 1, tex.getHeight());  
		}  
		  
		tex.end();  
		  
		ofImage screenGrab;  
		screenGrab.setFromPixels((unsigned char *) tex.getPixels(), colors.size(), 300, OF_IMAGE_COLOR);  
		screenGrab.saveImage("images/" + fileNames[curFile] + ".png");  
		images.push_back(screenGrab);  
  

this is a correct picture:

http://www.versionize.com/photos/right.png

This is a wrong picture:

http://www.versionize.com/photos/wrong.png

It’s the setFromPixels call that goes wrong, because the texture draws perfectly.

Any ideas what I’m doing wrong? The displacement of the pixels seem to be 100 or 200 pixels when it goes wrong.

UPDATE:

Something is definitely wrong with the getPixels method of the FBO texture. Doing pure OpenGL calls fixed the issue:

  
  
unsigned char * pixels = new unsigned char[w*h*3];  
		  
		tex.begin();  
		glPixelStorei(GL_PACK_ALIGNMENT, 1);  
		glReadPixels(0, 0, tex.getWidth(), tex.getHeight(), GL_RGB, GL_UNSIGNED_BYTE, pixels);  
		screenGrab.setFromPixels(pixels, tex.getWidth(), tex.getHeight(), OF_IMAGE_COLOR);  
		screenGrab.saveImage("images/" + fileNames[curFile] + ".png");  
		tex.end();