Why my shader is flipping FBO?

Hi,
I’m applying a glow shader to an FBO, but the result is a flipped content. The same happens with a simple shader as you can see in my example.

main.cpp

int main( ){
    ofGLFWWindowSettings settings;
    settings.glVersionMajor = 3;
    settings.glVersionMinor = 3;
    settings.width = 1920;
    settings.height = 1080;
    settings.windowMode = OF_WINDOW;
    ofCreateWindow(settings);
    ofRunApp(new ofApp());
}

ofApp.cpp

void ofApp::setup(){
    projection.set(ofGetCurrentMatrix(ofMatrixMode::OF_MATRIX_PROJECTION));
    modelview.set(ofGetCurrentMatrix(ofMatrixMode::OF_MATRIX_MODELVIEW));
    
    ofFbo::Settings settings;
    settings.width = ofGetWidth();
    settings.height = ofGetHeight();
    settings.internalformat = GL_RGB;
    sceneFBO.allocate(settings);
    
    shader.load("shaders/shader.vert", "shaders/shader.frag");
}

void ofApp::draw(){
    ofBackground(0);
    
// draw ellipse inside FBO
    sceneFBO.begin();
    ofClear(0);    
    ofSetColor(255, 0, 0);
    ofDrawEllipse(0, 0, 200, 200);
    sceneFBO.end();

// draw fbo with shader
    shader.begin();
    shader.setUniformMatrix4f("projection", projection);
    shader.setUniformMatrix4f("modelview", modelview);
    ofSetColor(255);
    sceneFBO.draw(0, 0); 
    shader.end();
}

shader.vert

#version 330

uniform mat4 projection;
uniform mat4 modelview;

in vec4 position;
in vec2 texcoord;

out vec2 vertTexCoord;

void main()
{
    gl_Position = projection * modelview * position;   
    vertTexCoord = texcoord;
}

shader.frag

#version 330

uniform sampler2DRect tex0;

in vec2 vertTexCoord;
out vec4 outputColor;

void main()
{
    vec4 result = texture(tex0, vertTexCoord);
    outputColor = result;
}

If I don’t use the shader the FBO is printed correctly.
There’s something wrong? I miss something?

Thanks in advance
paolofuse

texture coordinates have their y coordinate grow from top down but opengl coordinates by default go the other way so when setting a shader like this and use it inside an fbo the texture coordinates will be flipped.

OF actually solves this in most cases, try not passing the matrices manually and instead just declaring them as:

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;

those uniforms are automatically filled in by OF internally and flipped in a way that usually just works no matter if there’s an fbo bound or not.

in some edge cases though, you’ll need to flip the texture coordinates yourself if an fbo is bound.

Changing my vert shader in this way it works:

shader.vert

#version 330

uniform mat4 modelViewProjectionMatrix;

in vec4 position;
in vec2 texcoord;

out vec2 vertTexCoord;

void main()
{
    gl_Position = modelViewProjectionMatrix * position;
    vertTexCoord = texcoord;
}

Thank you.
paolofuse