Flipping fbo (or its texture) upside down

Hi, did some searches in the old post but didn’t found a working solution…

I have a fbo, binded to a distorded mesh. So far so good…

fbo.getTextureReference().bind();
primitive.draw();
fbo.unbind();

I would like to flip the content of the fbo (not the mesh itself) during draw.

I tried to find a post about texture rotation, and found some from the forum which I adapted this way

    fbo.getTextureReference().bind();
	ofSetMatrixMode(OF_MATRIX_TEXTURE);
	ofPushMatrix();
	ofMatrix4x4 m;
	m.rotate(0.2, 0, 0, 1);
	ofMultMatrix(m);
	primitive.draw();
	ofPopMatrix();
	ofSetMatrixMode(OF_MATRIX_MODELVIEW);
	fbo.unbind();

But this does not work. All I get is a kinda zoom effect, no matter the values of m.rotate()

Any thoughts ? Thanks !!

You might try using shaders for that.

gl_FragColor = texture2D(sampler, vec2(texcoord.x, height - texcoord.y));

Height = 1 if you’re not using ARB textures. Take a look at shader examples that come with OF

You also may be able to do

fbo.getTextureReference().getTextureData().bFlipTexture = true;
2 Likes

I tried both solutions but didn’t had any success.
For the bFlipTexture I did

fbo.getTextureReference().bind();
fbo.getTextureReference().getTextureData().bFlipTexture = true;
primitive.draw();
fbo.unbind();

This had no effect

Then I tried the more more complex shader way

rotate.vert

#version 150

in vec4 position;
in vec4 color;
in vec2 texcoord;

out vec2 texcoord2;
    
void main(){
	texcoord2 = vec2(texcoord.x, 1.0 - texcoord.y);
}

rotate.frag

#version 150

uniform sampler2D texture;
in vec2 texcoord2;
out vec4 FragColor;

void main(){
	FragColor = texture(texture, texcoord2);
}

I didn’t understand (even reading http://www.openframeworks.cc/tutorials/graphics/shaders.html) where to put the shader.begin and end so I tried

    shader.begin();
	shader.setUniformTexture("texture", fbo.getTextureReference(), 0);
	fbo.getTextureReference().bind();
	primitive.draw();
	shader.end();
	fbo.unbind();

I also tried to put shader.begin after the bind() call.
In any cases, all I got was a black screen…

I also tried to put shader.begin & shader.end inside the fbo.begin and fbo.end : black screen

Is use ofDisableArbTex(); in app.setup() and until now my textures coordinates seems to be ok (expressed by values between 0.0 and 1.0)

It may not work for your case but I would try the bFlipTexture right after you allocate the ofFbo

1 Like

Just tried, it does nothing…

I really wonder what’s wrong with the two simple shaders, it’s the first time I use shaders and it’s been difficult to find simple tutorials provided that many of them use deprecated functions (glsl 2 ?)

It’s an old thread, but I was asking myself the same question. I need to draw an fbo on the left side of the screen, and then draw it, reflected horizontally, on the right part of the screen. I did it like this and it worked:

    // in the setup method
    fbo.allocate(ofGetWidth()/2., ofGetHeight(), GL_RGBA);
    fbo.begin();
    ofClear(255, 255, 255, 0);
    fbo.end();

    //in the draw method
    // draw on the left side of the screen
    fbo.draw(0,0);
    // draw the same texture flipped horizontally, in the right
    // side of the screen
    ofTexture flipped = fbo.getTexture();
    float pos = ofGetWidth()/2.;
    flipped.draw(ofGetWidth(), 0, -pos, ofGetHeight());

As you see, the draw method accept 4 parameters, x, y width and height. But a negative width, that has a starting point the whole width, flips the image. So I assume that in your case:

ofTexture flippedVert = fbo.getTexture();
float pos = ofGetWidth()/2.;
flippedVert.draw(0, ofGetHeight(), ofGetWidth(), -ofGetHeight());

Would have worked.

2 Likes