Skip to content

Commit

Permalink
Added getPerspective() method to Matrix* and SceneView
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed Sep 29, 2003
1 parent 65b9a16 commit f7eb5b7
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 53 deletions.
60 changes: 37 additions & 23 deletions examples/osgunittests/osgunittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ void testFrustum(double left,double right,double bottom,double top,double zNear,
osg::Matrix f;
f.makeFrustum(left,right,bottom,top,zNear,zFar);

double c_left=0;
double c_right=0;
double c_top=0;
double c_bottom=0;
double c_zNear=0;
double c_zFar=0;

double c_zNear = f(3,2) / (f(2,2)-1.0f);
double c_zFar = f(3,2) / (1.0f+f(2,2));

double c_left = c_zNear * (f(2,0)-1.0f) / f(0,0);
double c_right = c_zNear * (1.0f+f(2,0)) / f(0,0);

double c_top = c_zNear * (1+f(2,1)) / f(1,1);
double c_bottom = c_zNear * (f(2,1)-1.0f) / f(1,1);

f.getFrustum(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar);

std::cout << "testFrustum"<<std::endl;
std::cout << "testFrustum"<<f.getFrustum(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar)<<std::endl;
std::cout << " left = "<<left<<" compute "<<c_left<<std::endl;
std::cout << " right = "<<right<<" compute "<<c_right<<std::endl;

Expand All @@ -40,19 +36,14 @@ void testOrtho(double left,double right,double bottom,double top,double zNear,do
osg::Matrix f;
f.makeOrtho(left,right,bottom,top,zNear,zFar);

double c_zNear = (f(3,2)+1.0f) / f(2,2);
double c_zFar = (f(3,2)-1.0f) / f(2,2);

double c_left = -(1.0f+f(3,0)) / f(0,0);
double c_right = (1.0f-f(3,0)) / f(0,0);

double c_bottom = -(1.0f+f(3,1)) / f(1,1);
double c_top = (1.0f-f(3,1)) / f(1,1);

f.getOrtho(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar);

double c_left=0;
double c_right=0;
double c_top=0;
double c_bottom=0;
double c_zNear=0;
double c_zFar=0;

std::cout << "testOrtho"<<std::endl;
std::cout << "testOrtho "<< f.getOrtho(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar) << std::endl;
std::cout << " left = "<<left<<" compute "<<c_left<<std::endl;
std::cout << " right = "<<right<<" compute "<<c_right<<std::endl;

Expand All @@ -65,6 +56,26 @@ void testOrtho(double left,double right,double bottom,double top,double zNear,do
std::cout << std::endl;
}

void testPerspective(double fovy,double aspect,double zNear,double zFar)
{
osg::Matrix f;
f.makePerspective(fovy,aspect,zNear,zFar);

double c_fovy=0;
double c_aspect=0;
double c_zNear=0;
double c_zFar=0;

std::cout << "testPerspective "<< f.getPerspective(c_fovy,c_aspect,c_zNear,c_zFar) << std::endl;
std::cout << " fovy = "<<fovy<<" compute "<<c_fovy<<std::endl;
std::cout << " aspect = "<<aspect<<" compute "<<c_aspect<<std::endl;

std::cout << " zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
std::cout << " zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;

std::cout << std::endl;
}

void testLookAt(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)
{
osg::Matrix mv;
Expand Down Expand Up @@ -158,6 +169,9 @@ int main( int argc, char** argv )
testOrtho(0,1,1,2,2.1,1000);
testOrtho(-1,10,1,20,2.5,100000);

testPerspective(20,1,1,1000);
testPerspective(90,2,1,1000);

testLookAt(osg::Vec3(10.0,4.0,2.0),osg::Vec3(10.0,4.0,2.0)+osg::Vec3(0.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0));
testLookAt(osg::Vec3(10.0,4.0,2.0),osg::Vec3(10.0,4.0,2.0)+osg::Vec3(1.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0));

Expand Down
16 changes: 12 additions & 4 deletions include/osg/Matrixd
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class SG_EXPORT Matrixd

/** Get the othorgraphic settings of the orthographic projection matrix.
* Note, if matrix is not an orthographic matrix then invalid values will be returned.*/
void getOrtho(double& left, double& right,
bool getOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);

Expand All @@ -145,15 +145,23 @@ class SG_EXPORT Matrixd

/** Get the frustum setting of a perspective projection matrix.
* Note, if matrix is not an perspective matrix then invalid values will be returned.*/
void getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
bool getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);

/** Set to a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
void makePerspective(double fovy,double aspectRatio,
double zNear, double zFar);

/** Get the frustum setting of a symetric perspective projection matrix.
* Returns false if matrix is not a perspective matrix, where parameter values are undefined.
* Note, if matrix is not a symetric perspective matrix then the shear will be lost.
* Asymetric metrices occur when stereo, power walls, caves and reality center display are used.
* In these configuration one should use the AsFrustum method instead.*/
bool getPerspective(double& fovy,double& aspectRatio,
double& zNear, double& zFar);

/** Set to the position and orientation modelview matrix, using the same convention as gluLookAt. */
void makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up);

Expand Down
19 changes: 14 additions & 5 deletions include/osg/Matrixf
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class SG_EXPORT Matrixf

/** Get the othorgraphic settings of the orthographic projection matrix.
* Note, if matrix is not an orthographic matrix then invalid values will be returned.*/
void getOrtho(double& left, double& right,
bool getOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);

Expand All @@ -142,16 +142,25 @@ class SG_EXPORT Matrixf
double zNear, double zFar);

/** Get the frustum setting of a perspective projection matrix.
* Note, if matrix is not an perspective matrix then invalid values will be returned.*/
void getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
* Returns false if matrix is not an orthographic matrix, where parameter values are undefined.*/
bool getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);

/** Set to a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
void makePerspective(double fovy,double aspectRatio,
double zNear, double zFar);

/** Get the frustum setting of a symetric perspective projection matrix.
* Returns false if matrix is not a perspective matrix, where parameter values are undefined.
* Note, if matrix is not a symetric perspective matrix then the shear will be lost.
* Asymetric metrices occur when stereo, power walls, caves and reality center display are used.
* In these configuration one should use the AsFrustum method instead.*/
bool getPerspective(double& fovy,double& aspectRatio,
double& zNear, double& zFar);


/** Set to the position and orientation modelview matrix, using the same convention as gluLookAt. */
void makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up);

Expand Down
17 changes: 13 additions & 4 deletions include/osgUtil/SceneView
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,26 @@ class OSGUTIL_EXPORT SceneView : public osg::Referenced
const osg::Matrix& getProjectionMatrix() const { return *_projectionMatrix; }

/** Get the othorgraphic settings of the orthographic projection matrix.
* Note, if matrix is not an orthographic matrix then invalid values will be returned.*/
void getProjectionMatrixAsOrtho(double& left, double& right,
* Returns false if matrix is not an orthographic matrix, where parameter values are undefined.*/
bool getProjectionMatrixAsOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);

/** Get the frustum setting of a perspective projection matrix.
* Note, if matrix is not an perspective matrix then invalid values will be returned.*/
void getProjectionMatrixAsFrustum(double& left, double& right,
* Returns false if matrix is not a perspective matrix, where parameter values are undefined.*/
bool getProjectionMatrixAsFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);

/** Get the frustum setting of a symetric perspective projection matrix.
* Returns false if matrix is not a perspective matrix, where parameter values are undefined.
* Note, if matrix is not a symetric perspective matrix then the shear will be lost.
* Asymetric metrices occur when stereo, power walls, caves and reality center display are used.
* In these configuration one should use the AsFrustum method instead.*/
bool getProjectionMatrixAsPerspective(double& fovy,double& aspectRatio,
double& zNear, double& zFar);


/** Set the view matrix. Can be thought of as setting the position of the world relative to the camera in camera coordinates. */
void setViewMatrix(const osg::Matrix& matrix);

Expand Down
42 changes: 33 additions & 9 deletions src/osg/Matrix_implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ void Matrix_implementation::set( value_type a00, value_type a01, value_type a02,
SET_ROW(3, a30, a31, a32, a33 )
}

#define QX q._fv[0]
#define QY q._fv[1]
#define QZ q._fv[2]
#define QW q._fv[3]
#define QX q._v[0]
#define QY q._v[1]
#define QZ q._v[2]
#define QW q._v[3]

void Matrix_implementation::set(const Quat& q)
{
Expand Down Expand Up @@ -420,10 +420,12 @@ void Matrix_implementation::makeOrtho(double left, double right,
SET_ROW(3, tx, ty, tz, 1.0f )
}

void Matrix_implementation::getOrtho(double& left, double& right,
bool Matrix_implementation::getOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar)
{
if (_mat[0][3]!=0.0f || _mat[1][3]==!0.0f || _mat[2][3]!=0.0f || _mat[3][3]!=1.0f) return false;

zNear = (_mat[3][2]+1.0f) / _mat[2][2];
zFar = (_mat[3][2]-1.0f) / _mat[2][2];

Expand All @@ -432,6 +434,8 @@ void Matrix_implementation::getOrtho(double& left, double& right,

bottom = -(1.0f+_mat[3][1]) / _mat[1][1];
top = (1.0f-_mat[3][1]) / _mat[1][1];

return true;
}


Expand All @@ -450,10 +454,13 @@ void Matrix_implementation::makeFrustum(double left, double right,
SET_ROW(3, 0.0f, 0.0f, D, 0.0f )
}

void Matrix_implementation::getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar)
bool Matrix_implementation::getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar)
{
if (_mat[0][3]!=0.0f || _mat[1][3]==!0.0f || _mat[2][3]!=-1.0f || _mat[3][3]!=0.0f) return false;


zNear = _mat[3][2] / (_mat[2][2]-1.0f);
zFar = _mat[3][2] / (1.0f+_mat[2][2]);

Expand All @@ -462,11 +469,13 @@ void Matrix_implementation::getFrustum(double& left, double& right,

top = zNear * (1.0f+_mat[2][1]) / _mat[1][1];
bottom = zNear * (_mat[2][1]-1.0f) / _mat[1][1];

return true;
}


void Matrix_implementation::makePerspective(double fovy,double aspectRatio,
double zNear, double zFar)
double zNear, double zFar)
{
// calculate the appropriate left, right etc.
double tan_fovy = tan(DegreesToRadians(fovy*0.5));
Expand All @@ -477,6 +486,21 @@ void Matrix_implementation::makePerspective(double fovy,double aspectRatio,
makeFrustum(left,right,bottom,top,zNear,zFar);
}

bool Matrix_implementation::getPerspective(double& fovy,double& aspectRatio,
double& zNear, double& zFar)
{
double right = 0.0;
double left = 0.0;
double top = 0.0;
double bottom = 0.0;
if (getFrustum(left,right,bottom,top,zNear,zFar))
{
fovy = RadiansToDegrees(atan(top/zNear)-atan(bottom/zNear));
aspectRatio = (right-left)/(top-bottom);
return true;
}
return false;
}

void Matrix_implementation::makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up)
{
Expand Down
27 changes: 19 additions & 8 deletions src/osgUtil/SceneView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,30 +932,41 @@ void SceneView::setProjectionMatrixAsPerspective(double fovy,double aspectRatio,
zNear, zFar));
}

void SceneView::getProjectionMatrixAsOrtho(double& left, double& right,
bool SceneView::getProjectionMatrixAsOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar)
{
if (_projectionMatrix.valid())
{
_projectionMatrix->getOrtho(left, right,
bottom, top,
zNear, zFar);
return _projectionMatrix->getOrtho(left, right,
bottom, top,
zNear, zFar);
}
return false;
}

void SceneView::getProjectionMatrixAsFrustum(double& left, double& right,
bool SceneView::getProjectionMatrixAsFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar)
{
if (_projectionMatrix.valid())
{
_projectionMatrix->getFrustum(left, right,
bottom, top,
zNear, zFar);
return _projectionMatrix->getFrustum(left, right,
bottom, top,
zNear, zFar);
}
return false;
}

bool SceneView::getProjectionMatrixAsPerspective(double& fovy,double& aspectRatio,
double& zNear, double& zFar)
{
if (_projectionMatrix.valid())
{
return _projectionMatrix->getPerspective(fovy, aspectRatio, zNear, zFar);
}
return false;
}

void SceneView::setViewMatrix(const osg::Matrix& matrix)
{
Expand Down

0 comments on commit f7eb5b7

Please sign in to comment.