openframeworks / openframeworks/openFrameworks

ofGetCurrentMatrix(OF_MATRIX_PROJECTION) returns wrong matrix when camera is enclosed in FBO

Open
#4,349 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Without fbo:

#include "ofMain.h"

class ofApp : public ofBaseApp
{
  public:
    void setup();
    void draw();

    ofCamera cam;
};

int main()
{
  ofGLFWWindowSettings settings;

  auto window = ofCreateWindow(settings);
  auto app = make_shared<ofApp>();
  ofRunApp(window, app);

  return ofRunMainLoop();
}

void ofApp::setup()
{
}

void ofApp::draw()
{
  cam.begin();
    cout << ofGetCurrentMatrix(OF_MATRIX_PROJECTION) << endl << endl;
  cam.end();
}

With fbo:

#include "ofMain.h"

class ofApp : public ofBaseApp
{
  public:
    void setup();
    void draw();

    ofFbo fbo;
    ofCamera cam;
};

int main()
{
  ofGLFWWindowSettings settings;

  auto window = ofCreateWindow(settings);
  auto app = make_shared<ofApp>();
  ofRunApp(window, app);

  return ofRunMainLoop();
}

void ofApp::setup()
{
  fbo.allocate(ofGetWidth(), ofGetHeight());
}

void ofApp::draw()
{
  fbo.begin();
    cam.begin();
      cout << ofGetCurrentMatrix(OF_MATRIX_PROJECTION) << endl << endl;
    cam.end();
  fbo.end();
  fbo.draw(0,0);
}

Projection matrix without fbo (correct):

 1.29904,        0,        0,        0
       0,  1.73205,        0,        0
       0,        0,   -1.002,       -1
       0,        0, -13.3155,        0

Projection matrix with fbo (wrong):

 1.29904,       -0,        0,        0
       0, -1.73205,        0,        0
       0,        0,   -1.002,       -1
       0,        0, -13.3155,        0

Projection matrix with fbo taken both from cam and ofGetCurrentMatrix(OF_MATRIX_PROJECTION), side by side:

void ofApp::draw()
{
  fbo.begin();
    cam.begin();

      cout << "Matrix from cam: " << endl
           << cam.getProjectionMatrix() << endl;

      cout << "Matrix from ofGetCurrentMatrix: " << endl
           << ofGetCurrentMatrix(OF_MATRIX_PROJECTION) << endl << endl;

    cam.end();
  fbo.end();
  fbo.draw(0,0);
}

Should be the same, but they're not:

Matrix from cam: 
 1.29904,        0,        0,        0
       0,  1.73205,        0,        0
       0,        0,   -1.002,       -1
       0,        0, -13.3155,        0
Matrix from ofGetCurrentMatrix: 
 1.29904,       -0,        0,        0
       0, -1.73205,        0,        0
       0,        0,   -1.002,       -1
       0,        0, -13.3155,        0

Currently addons which rely on ofGetCurrentMatrix(OF_MATRIX_PROJECTION) command are all broken because it returns incorrect matrix if one wants record an FBO. For workaround you can pass camera directly as a parameter to custom draw() function.

Compiled against latest commit.

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 by reproducing the provided comparison using ofFbo, ofCamera, ofGetCurrentMatrix(OF_MATRIX_PROJECTION), and cam.getProjectionMatrix(). Trace how the current projection matrix is handled while an FBO and camera are active. Done means both APIs return the same projection matrix in the FBO case without changing the no-FBO result.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.