ofCamera into a shader?

How do I match my ofCamera in openframeworks with my perspective in a shader? I’d like to control my shader perspective with easyCam or with ofCamera. I can’t find anything on this, or at least in lamens. Thanks for the help.

Okay, this is what I got so far. I’m just trying to control my worldMatrix and my perspectiveMatrix directly with ofEasyCam.

In setup.

  
  
        // these are the camera settings I want to start with.  
	easyCam.setupPerspective(false, 60, 0.1,17000);  
	easyCam.setGlobalPosition(ofVec3f(0,0,500));  
	easyCam.setTarget(ofVec3f(0,0,2500));  
	//easyCam.lookAt(ofVec3f(0,0,2500));  
	easyCam.setOrientation(ofVec3f(0,1,0));  
  

in Update.

  
  
        easyCam.begin();  
	easyCam.end();  
  
	ofVec3f eye = easyCam.getGlobalPosition();  
	ofVec3f center = ofVec3f(easyCam.getTarget().getPosition());  
        ofVec3f up = easyCam.getOrientationEuler();  
  
	worldMatrix.makeLookAtViewMatrix(eye, center, up);  
	perspectiveMatrix.set(easyCam.getProjectionMatrix()) ;  
  
        glUseProgram(program);  
	glUniformMatrix4fv(uWorldMatrix, 1, 0,   worldMatrix.getPtr());  
        glUniformMatrix4fv(uPerspectiveMatrix, 1, 0,   perspectiveMatrix.getPtr());  
  

It seems to be quirky. EasyCam is controlling it but it doesn’t seem accurate. Any thoughts?

Also, I’ve been printing out my results using

  
  
	cout <<"globalPos " << easyCam.getGlobalPosition() << endl;  
	cout <<"Lookat " << ofVec3f(easyCam.getTarget().getPosition()) << endl;  
	cout <<"orientation " << easyCam.getOrientationEuler() << endl;  
  

at first I’m getting perfect results.
globalPos 0, 0, 500
Lookat 0, 0, 2500
orientation 0, 1, 0

but as soon as I do

  
  
	easyCam.begin();  
	easyCam.end();  
  

my globalPos gets messed up.
globalPos 15.477, 0, 3386.67 <--------
Lookat 0, 0, 2500
orientation 0, 1, 0

I don’t understand what would change after I do easyCam.begin() if I’m not clicking the screen.

Instead of using

  
easyCam.setupPerspective(false, 60, 0.1,17000);  

I tried adjusting each parameter separately just in case it was something with setupPerspective.

  
  
	easyCam.setFov(60);  
	easyCam.setNearClip(0.1);  
	easyCam.setFarClip(17000);  
  

No change. It’s obviously something with my easyCam global position, but what changes after easyCam.begin() and why is my control of easyCam with my mouse messed up? Anybody have the same issue or a solution?

Figured it out with some help from Kris Temmerman. Thanks. Openframeworks very conveniently has access to camera matrixes through getGlobalTransformMatrix and uPerspectiveMatrix which is all a glsl shader needs. Using a pointer to a ofEasyCam myCam from my testApp, this is my code:

glUniformMatrix4fv(uWorldMatrix, 1, 0, ofMatrix4x4::getInverseOf(myCam->getGlobalTransformMatrix()).getPtr());
glUniformMatrix4fv(uPerspectiveMatrix, 1, 0, myCam->getProjectionMatrix().getPtr());

and in the vertex shader simply put:

uniform mat4 worldMatrix;
uniform mat4 perspMatrix;

1 Like