Get view and projection matrices OF vs openGL

Hi everyone,
I’m looking at ofxVolumetrics and I can’t figure out how to use the camera projection matrix.
In Tim’s example he uses

 glGetFloatv( GL_MODELVIEW_MATRIX, modl);
 glGetFloatv(GL_PROJECTION_MATRIX, proj);

instead of using the camera like so:

modl = cam.getModelViewMatrix();
proj = cam.getModelViewProjectionMatrix();

I can’t understand what is the difference from one to another. But they are clearly giving different results.
I wrote and issue here if anybody wants to see my problem in detail.

Any help will be greatly appreciated!
cheers!

Oops… I had to use getProjectionMatrix instead of cam.getModelViewProjectionMatrix.
But still, I’m not quite sure if both operations are identical or not.

Will glGetFloatv() get the current state of the camera or a default OpenGL state?

just replied to the issue on github.

thanks!

Just found the new ofGetCurrentMatrix function in 0.8.1, I’m trying to do the something similar to what is done in ofxVolumetrics::drawVolume.

I want to render to an ofFbo using the current matrix. If I understand correctly you could get the same image on screen and in an FBO using the following code:

void ofApp::draw(){
	ofMatrix4x4 modelview;
	ofMatrix4x4 projection;

	easyCam.begin();
		ofClear(ofColor::black);
		modelview = ofGetCurrentMatrix(OF_MATRIX_MODELVIEW);
		projection = ofGetCurrentMatrix(OF_MATRIX_PROJECTION);
		ofDrawAxis(10);
	easyCam.end();

    // above render would be equivalent to the following:
	fbo.begin();
		ofClear(ofColor::black);
		ofSetMatrixMode(ofMatrixMode::OF_MATRIX_PROJECTION);
		ofLoadMatrix(projection);
		ofSetMatrixMode(ofMatrixMode::OF_MATRIX_MODELVIEW);
		ofLoadMatrix(modelview);
		ofDrawAxis(10);
	fbo.end();
}

Though, I am getting the render flipped in the Y axis. Does anyone know why?

Best!

I’ve been wondering same thing before… but I just gave up and draw it upside down… fbo.draw(0,ofGetHeight(),ofGetWidth(),-ofGetHeight());

It would be nice to know why fbos are flipped…

I’ve just pushed a simple example with one possible solution to your question.
Drawing the fbo inside the cam.begin/end (which probably you don’t want)
https://github.com/wasawi/my_examples/tree/master/08_fbo_coordinates

But I found another weird thing… my fbo looks pixelated… i guess that might be because of the retina display… not sure though…

Apparently they get flipped because OpenGL has the origin in the bottom left corner, nothing else. Still it’s odd that I’ve used FBOs before without finding this issue… I guess it’s because they were rendered as you said using the current camera directly, instead of simulating it.

I haven’t tried your app but I’ve been rendering fbo’s just fine without that pixelated result.

Best!

good to know! and nice link too!

apart from the projection matrix and the model view OF internally uses an orientation matrix to do the vertical flip since by default we have 0,0 at top left. if you get the matrices from camera and then load it using ofLoadMatrix() it’ll apply the vflip but usually ofCamera is not vflipped, you can vflip the camera using camera.setVflip(true) or disable the vflip in OF when using the matrices by calling ofSetOrientation(OF_ORIENTATION_DEFAULT,false)

1 Like