added method to get Projection matrix given near and far.

This commit is contained in:
Federico Ponchio 2012-08-29 13:21:22 +00:00
parent f16b821dc0
commit 0ac7034397
1 changed files with 257 additions and 219 deletions

View File

@ -206,6 +206,9 @@ public:
inline void SetFrustum(S dx, S sx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
//------------------
/// returns the projection matrix
vcg::Matrix44<S> GetMatrix(S nearVal, S farVal);
/// returns the frustum
inline void GetFrustum(S & sx, S & dx, S & bt, S & tp, S & nr);
@ -270,6 +273,41 @@ vcg::Point2<S> Camera<S>::Project(const vcg::Point3<S> & p) const
return q;
}
template <class S> vcg::Matrix44<S> Camera<S>::GetMatrix(S nearVal, S farVal) {
S left, right, bottom, top, nr;
GetFrustum(left, right, bottom,top, nr);
if(cameraType == PERSPECTIVE) {
S ratio = nearVal/nr;
left *= ratio;
right *= ratio;
bottom *= ratio;
top *= ratio;
}
vcg::Matrix44<S> m;
m[0][0] = 2.0*nearVal/(right - left);
m[0][1] = 0;
m[0][2] = (right + left)/(right - left);
m[0][3] = 0;
m[1][0] = 0;
m[1][1] = 2*nearVal/(top - bottom);
m[1][2] = (top + bottom)/(top - bottom);
m[1][3] = 0;
m[2][0] = 0;
m[2][1] = 0;
m[2][2] = -(farVal + nearVal)/(farVal - nearVal);
m[2][3] = - 2*farVal*nearVal/(farVal - nearVal);
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = -1;
m[3][3] = 0;
return m;
}
/// unproject a point from the camera 2d plane [-1,-1]x[1,1] (plus depth) to 3d CAMERA space
template<class S>
vcg::Point3<S> Camera<S>::UnProject(const vcg::Point2<S> & p, const S & d) const