I have a drawing inside fbo
, and trying to decrease fbo
size by cropping to the visible parts using bounding box. Here is a visual representation what I'm trying to achieve:
bounding box example
I found solution: pass fbo
data to pixels
and then detect first colored pixels on the left, right, top, bottom part of image.
In the following part of code I'm trying to detect the bottom intersection. But I've got a weird problem with pixels.
int w = 4;
int h = 4;
fbo.allocate(w, h, GL_RGBA);
fbo.begin();
ofClear(0, 0);
fbo.end();
fbo.begin();
ofSetColor(255, 0, 0);
ofDrawRectangle(0, 0, 4, 2);
fbo.end();
pixels.allocate(w, h, GL_RGBA);
fbo.readToPixels(pixels);
for(auto line = pixels.getLines().end(); line != pixels.getLines().begin(); --line){
for(auto pixel: line.getPixels()){
cout << "line: " << line.getLineNum() << " color: " << pixel.getColor() << endl;
}
}
output:
line: 4 color: 24, 215, 83, 118
line: 4 color: 255, 127, 0, 0
line: 4 color: 173, 7, 0, 0
line: 4 color: 1, 0, 0, 0
line: 3 color: 0, 0, 0, 0
line: 3 color: 0, 0, 0, 0
line: 3 color: 0, 0, 0, 0
line: 3 color: 0, 0, 0, 0
line: 2 color: 0, 0, 0, 0
line: 2 color: 0, 0, 0, 0
line: 2 color: 0, 0, 0, 0
line: 2 color: 0, 0, 0, 0
line: 1 color: 255, 0, 0, 255
line: 1 color: 255, 0, 0, 255
line: 1 color: 255, 0, 0, 255
line: 1 color: 255, 0, 0, 255
line:1
looks fine but what is wrong with line:4
, what is the random colors doing there? After rebuilding app they may have gone, but with the random chance. Maybe there is another way to crop fbo
by visible parts of image?
This solution doesn't work for me.
Your loop is the problem. Line 4 points into a not initialized memory area while line 0 is missing. Your rectangle is drawn in line 0 and line 1.
From the iterator::end() documentation:
Return iterator to end Returns an iterator referring to the past-the-end element in the vector container.
The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced.
A question with answers which will help you: Iterating C++ vector from the end to the begin