Pixels from FBO giving me random colors

Hi I am trying to get pixel data out of an FBO, but for some reason getting just a string of seemingly random colors.
Basically what I’m trying to do is read the pixels from a pointer to an FBO, then crop those pixels down to a single line of pixels. But, when I output or draw those pixels to the screen, I get what are seemingly random colors that are not updating from the FBO. Any idea what’s the dilly here? Thanks!

Essentially here is my code:

void  updateFbo(ofFbo * fbo){

    ofPixels imageToCrop;
    imageToCrop.allocate(800, 600, OF_IMAGE_COLOR);
    
    fbo->readToPixels(imageToCrop);
    
    imageToCrop.cropTo(topStripPixels, _x,_y,_w,1);
    imageToCrop.cropTo(bottomStripPixels, _x,_y+_h-1,_w,1);
}

So I’ve been staring at this code for the past 8 hours and still can’t make sense of what is going on… I’m drawing a video to an FBO, then I draw the FBO to the screen, no problem. I readToPixels to an ofImage object, then draw the image to the screen I get random colors.

But, if I draw the same ofImage to another FBO, and draw that FBO to the screen, no problem!

Here’s my code, a bit simplified:

update(){
    myFbo.readToPixels(topStripImage.getPixelsRef());
    topStripImage.update();
    
    topStripImage.crop(_x, _y, _w, 1);
    
    topStripFBO.begin();
    topStripImage.draw(0, 0);
        topStripFBO.end();
    }
    
draw(){
   myFbo.draw(0,0);
   topStripImage.draw(10,380);
   topStripFBO.draw(10,385); 
   }

Here’s a link to an image:
https://www.evernote.com/shard/s63/sh/49c8510a-5186-448c-8e10-befe8bb1855b/4b56f6bbe68b8ff2e071612a6145cbd6

Come across this issue before, are you allocating the pixel object. The random colours are when the pixel object does not match the dimensions of the fbo object.

Try this out

// In the .h file
ofPixels pixels;
ofFbo fbo;
ofImage image;

//--------------------------------------------------------------
void ofApp::setup()
{
    fbo.allocate(320, 240);
    pixels.allocate(320,240,GL_RGB);
    fbo.begin();
    ofClear(0);
    fbo.end();
}
//--------------------------------------------------------------
void ofApp::update()
{
    fbo.begin();
    ofSetColor(0);
    ofRect(0,0,320,240);
    ofSetColor(255);
    ofCircle(mouseX,mouseY,40);
    fbo.end();
    
    fbo.readToPixels(pixels);
    image.setFromPixels(pixels);
}
//--------------------------------------------------------------
void ofApp::draw()
{
    ofBackground(0);
    ofSetColor(255);
    fbo.draw(0,0);
    ofDrawBitmapStringHighlight("FBO", 10,10);
    image.draw(320, 0, 320,240);
    ofDrawBitmapStringHighlight("Pixels", 330,10);
    ofImage img;
    img = image;
    img.crop(0, 0, 160, 120);
    img.draw(0, 240, 320,240);
    ofDrawBitmapStringHighlight("Cropped Image", 10,250);
    
    ofPushStyle();
    ofNoFill();
    ofSetColor(255);
    ofRect(0, 0, 320, 240);
    ofRect(320, 0, 320,240);
    ofRect(0, 240, 320,240);
    ofPopStyle();
}

Oh hang about are you trying to get pixel data from the image to pass to LEDs?

Yes! I am. How did you know?
I’m using the Lumigeek LED driver, and sending data to the LEDs using OSC.

I was allocating the pixel object with the final dimensions after cropping. I’ll try allocating it to the full size of the FBO.

By the way here is a link to the code on Github: https://github.com/veev/jrh/tree/master/JRHMemorialWall

Thanks!

Sok.

I wrote an addon that allows you to communicate with the FadeCandy NeoPixel Driver.
Essentially same process, it grabs visual data from what ever is beneath the virtual LED objects.
Code here if it helps -> https://github.com/DHaylock/ofxOPC

Thanks for the help, problem solved!

Turns out it was a problem with my object references. I was making a copy of my objects and updated the copy, as opposed to making a pointer and updating the object directly.

I didn’t catch the problem earlier because the FBO was working fine… not sure exactly why but I assume the data on the GPU was still being referenced/linked properly, but the data on the CPU was not.

You have to call update on the image.

from a touch screeeeee. #autocorrect