What is ofCamera's setupPerspective method doing?

I was wondering what ofCamera’s setupPerspective is doing. It seems necessary for what I want but maybe I’m just not yet understanding ofCamera that well, since I don’t find it back in any of the ofCamera examples.

Here follow two examples. The first is doing what I expected, the second gives (what I understand to be) weird results:

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	ofBackground(255);  
	ofSetFrameRate(24);  
	ofSetVerticalSync(true);  
	  
	cam.setNearClip(0.1);  
	cam.setFarClip(100);  
	cam.setPosition(ofVec3f(ofGetWindowWidth()/2, ofGetWindowHeight()/2, 0));  
	cam.setupPerspective();  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
	cam.setPosition(  
					ofVec3f(  
							ofGetWindowWidth()/2,  
							ofGetWindowHeight()/2,  
							ofGetFrameNum()*10%500)  
					);  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	cam.begin();  
	  
	ofSetColor(0);  
	ofSetRectMode(OF_RECTMODE_CENTER);  
	ofRect( ofGetWindowWidth()/2, ofGetWindowHeight()/2, 100, 100);  
	  
	cam.end();  
}  
  

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	ofBackground(255);  
	ofSetFrameRate(24);  
	ofSetVerticalSync(true);  
	  
	cam.setNearClip(0.1);  
	cam.setFarClip(100);  
	cam.setPosition(ofVec3f(ofGetWindowWidth()/2, ofGetWindowHeight()/2, 0));  
//	cam.setupPerspective();  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
	cam.setPosition(  
					ofVec3f(  
							ofGetWindowWidth()/2,  
							ofGetWindowHeight()/2,  
							ofGetFrameNum()*10%500)  
					);  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	cam.begin();  
	  
	ofSetColor(0);  
	ofSetRectMode(OF_RECTMODE_CENTER);  
	ofRect( ofGetWindowWidth()/2, ofGetWindowHeight()/2, 100, 100);  
	  
	cam.end();  
}  
  

Thanks!

1 Like

I understand now setting nearclip and farclip both to 0 gives me the same results I get when using setupPerspective.

I’m a complete newbie but common sense seems to tell me it shouldn’t see anything when nearclip and farclip are at the exact same positions?

What are these parameters normally used for?

ofCamera’s setupPerspective is used to set the parameters that you would normally do in a projection matrix to setup the view of your scene.
(In OpenGL, Projection Matrix is used to set the clipping volume and to set the way your 3D scene is shown on the screen as 2D)

If you notice the ofCamera’s prototype inside ofCamera.h, it is defined as:

  
void setupPerspective(bool vFlip=0, float fov=60, float nearDist=0, float farDist=0);  

, which says that by default _near _and far distance default parameters of zero if you do not passing anything.

In the implementation of this function ofCamera, you would notice:

  
void setupPerspective(bool vFlip, float fov, float nearDist, float farDist){  
...  
if(nearDist == 0) nearDist = dist / 10.0f;  
if(farFist == 0) farDist = dist * 10.0f  
...  
}  

Did you notice what’s happening? Although this code above is not complete but I wanted to just point that nearClip and _farClip _are the same as you were thinking.

However, you were correct in pointing out that if both _nearClip _and _farClip _are the same, nothing should be drawn on the screen. If you set a custom projection matrix and set the _nearClip _and _farClip _as the name, you will notice that nothing gets drawn on the screen. Here’s the example code where near and far are set as the same.

  
glMatrixMode(GL_PROJECTION);  
glLoadIdentity();  
glFrustum(-1.33, 1.33, -1.0,1.0, 1.0,1.0); //glFrustum(left, right, bottom, top, near, far); If you change the last parameter to 1.1, you would notice something does get drawn on the screen  
glMatrixMode(GL_MODELVIEW);  
glLoadIdentity();  
glTranslate(0,0,-1);//Translating it z=-1 which will be the same as the near clip and far clip  
glRectf(0.0,0.0,0.5,0.5); //by default drawn at z=0;  

I was too lazy to draw a diagram to explain the perspective but should you need it, let me know in the reply here.

Okay I think I get it. Thanks for your reply!