openframeworks / openframeworks/openFrameworks

gl_FragCoord is upside down inside FBO

Open
#3,353 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug core section-2D
Dominant language
C++
Stars
10.4k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
9

Description

FBOs are rendered upside down compared to not using an FBO, which means anything based gl_FragCoord (which is not affected by any model or view matrix transformations) gets rendered upside down.

so if you do this:

#version 120
uniform float height;
void main() {
    float y = gl_FragCoord.y / height;
    gl_FragColor = vec4(vec3(y), 1.);
}
#include "ofMain.h"
class ofApp : public ofBaseApp {
public:
    ofShader shader;
    void setup() {
        shader.setup("", "shader.fs");
    }
    void draw() {
        shader.begin();
        shader.setUniform1f("height", ofGetHeight());
        ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight());
        shader.end();
    }
};
int main() {
    ofSetupOpenGL(1280, 720, OF_WINDOW);
    ofRunApp(new ofApp());
}

you get black at top and white at bottom.

and then you stick everything inside an FBO:

...
    void draw() {
        ofFbo fbo;
        fbo.allocate(ofGetWidth(), ofGetHeight());
        fbo.begin();
        shader.begin();
        shader.setUniform1f("height", ofGetHeight());
        ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight());
        shader.end();
        fbo.end();
        fbo.draw(0, 0);
    }
};
...

you get white at top and black at bottom.

not sure if there's anything we can do about this, but i wanted to at least have an issue for it in case someone else runs into it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shader.fs example and the ofApp::draw path, focusing on the ofShader and ofFbo calls shown in the report. Reproduce the comparison between direct drawing and FBO drawing, then trace the relevant FBO and coordinate-handling entry points; done means gl_FragCoord-based output has consistent orientation in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.