OpenGL Matrix Transform, how to get back to normal drawing?

Hi All,

Below is the draw function from my big screen AR application that I am currently working on. It has again exposed my lack of OpenGL knowledge.

  
void testApp::draw(){  
	ofSetColor(0xffffff);  
	grabber.draw(0, 0, ofGetWidth(), ofGetHeight());  
	  
    //this is where we use the calculated matrix from ARToolkitPlus to put  
	//in our graphics at the location and orientation of the marker.  
	//- the matrix has the 0,0 point as the center of the marker.   
	  
	int numDetected = tracker->getNumDetectedMarkers();  
	//cout<<"numberofmarkers:"<<numDetected<<endl;  
	  
	//glViewport(0, 0, 640, 480 );  
	glViewport(0,0,ofGetWidth(), ofGetHeight());  
	glMatrixMode( GL_PROJECTION );  
	glLoadMatrixf(tracker->getProjectionMatrix());  
	  
	for(int i=0; i<numDetected; i++)  
	{  
		ARToolKitPlus::ARMarkerInfo marker = tracker->getDetectedMarker(i);  
		  
		float m34[ 3 ][ 4 ];  
		float c[ 2 ] = { 0.0f, 0.0f };  
		float w = 40.0f;  
		  
		tracker->rppGetTransMat( &marker, c, w, m34 );  
		  
		float m[ 16 ]; //this is some crazy matrix transformative stuff. I think initially it came out of one of the arToolkit functions.... but i got it from here: [http://chihara.naist.jp/people/STAFF/imura/computer/OpenGL/artp/disp-content](http://chihara.naist.jp/people/STAFF/imura/computer/OpenGL/artp/disp-content)  
		for ( int i = 0; i < 3; i++ ) {  
			for ( int j = 0; j < 4; j++ ) {  
				m[ j * 4 + i ] = m34[ i ][ j ];  
			}  
		}  
		for ( int j = 0; j < 3; j++ ) {  
			m[ j * 4 + 3 ] = 0.0f;  
		}  
		m[ 3 * 4 + 3 ] = 1.0f;  
		  
		glMatrixMode( GL_MODELVIEW );  
		glLoadMatrixf( m );  
		  
//		glPushMatrix();  
//		glRotatef(-90, 0, 0, 1);  
//		ofSetColor(255 , 255, 255 );  
//		grabber.draw(-30,-20,60,40);  
//		glPopMatrix();  
		  
		glPushMatrix();  
		//glRotatef(-90, 1, 0, 0); //just taking out rotations to be consitent  
		glRotatef(-180, 1, 0, 0);  
		//glRotatef(-180, 0, 1, 0);		  
		glRotatef(-180, 0, 0, 1);  
		ofSetColor(255 , 255, 255 );  
		//idNumb.drawString(ofToString(marker.id), -idNumb.stringWidth(ofToString(marker.id))/2,0);  
		glTranslatef(0,75,0);  
		  
		glEnable (GL_DEPTH_TEST);  
		heads[currentHead]->draw();  
		glDisable(GL_DEPTH_TEST);  
		glPopMatrix();  
	}  
	  
	screen.grabScreen(0,0, ofGetWidth(), ofGetHeight());  
	screenImageToFlip.setFromPixels(screen.getPixels(), screen.getWidth(), screen.getHeight());  
	screenImageToFlip.mirror(true, false);  
	  
	screenImageToFlip.draw(0,0);  
}  

This produces the following result:
[attachment=0:mqyt7ekw]wierd.jpg[/attachment:mqyt7ekw]

I want to redraw the flipped/mirrored image so that when displayed on a big screen, everything feels a bit more natural for the viewer.

How to I get rid of the tranformation and get back to 0,0 being the top left of the screen proper?

Cheers,

JGL

Saftest way? use glPushMatrix() before doing any of your own transformations, then use glPopMatrix() after you’re done.

Easiest way? call glLoadIdentity() after you’re finished with your custom transformations, this though is slightly risky because you might be building on other transformations that openframeworks has setup for normal drawing.

You are pushing and popping right in your ModelView Matrix, but there’s some funky stuff happening in your projection matrix that you need to get rid off.

Add a glPushMatrix right after

glMatrixMode( GL_PROJECTION );

then, to get back your original projection matrix, just switch back and pop the matrix:

glMatrixMode( GL_PROJECTION );
glPopMatrix();

oh, and you might want to change back to the Modelviewmatrix afterwards:

glMatrixMode(GL_MODELVIEW);

that should put you back to the state you were in before.
Hope this helps,

f

A quick hack is just to use ofSetupScreen() :slight_smile: That will do all the code so that its just like when you start a program (almost)