Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CameraViewFrustum.cpp
1 #include "CameraViewFrustum.hh"
2 #include <Base/Common/W32Compat.hh>
3 #include <Base/Debug/Error.hh>
4 
5 
6 #include <iostream>
7 #include <math.h>
8 
9 #ifdef BIAS_HAVE_OPENGL
10 # include <Gui/biasgl.h>
11 #endif
12 
13 
14 using namespace std;
15 using namespace BIAS;
16 
17 
19 {
20  InitMembers();
21 }
22 
23 
25  // unsymmetric frustum calculated separately
26  double fovY = fabs(atan2(top, zNear)); // y,x ; y/x
27  fovY += fabs(atan2(bottom, zNear));
28  // convert from rad to degree
29  fovY *= 180. / M_PI;
30  return fovY;
31 }
32 
34  double fovX = fabs(atan2(left,zNear)); // y,x
35  fovX += fabs(atan2(right,zNear));
36  fovX *= 180. / M_PI;
37  return fovX;
38 }
39 
41  const double w = right - left;
42  const double h = top - bottom;
43  BIASASSERT(h!=0);
44  return w/h;
45 }
46 
47 void BIAS::CameraViewFrustum::SetPerspective(const double & fovYdeg,
48  const double & fovAspect,
49  const double & zNearX,
50  const double & zFarX)
51 {
52  // set symmetric frustum like glulookat does
53  this->zNear= zNearX;
54  this->zFar = zFarX;
55  top = this->zNear * tan(fovYdeg/2. *M_PI/180.);
56  bottom = -top;
57  right = fovAspect * top;
58  left = - right;
59 }
60 
61 std::ostream & BIAS::CameraViewFrustum::Print(std::ostream & os) const
62 {
63  os<<"CameraViewFrustum: "<<endl
64  <<"left "<<left<<endl
65  <<"right "<<right<<endl
66  <<"bottom "<<bottom<<endl
67  <<"top "<<top<<endl
68  <<"near "<<zNear<<endl
69  <<"far "<<zFar<<endl
70  ;
71  return os;
72 }
73 
74 void BIAS::CameraViewFrustum::AdaptWidth(const double & vpAspectRatioDesired )
75 {
76  BIASASSERT(vpAspectRatioDesired >0);
77  BIASASSERT(this->GetAspect()>0);
78 
79  const double scale = vpAspectRatioDesired / this->GetAspect();
80  // increase left and right arounf center
81  // to keep center in the middle
82  const double center = left +0.5*(right-left);
83 
84  // shift to center
85  this->left -= center;
86  this->right -= center;
87 
88  // scale
89  this->left *= scale;
90  this->right *= scale;
91 
92  // shift back
93  this->left += center;
94  this->right += center;
95 }
96 
97 
98 #ifdef BIAS_HAVE_OPENGL
100 {
101  BIASASSERT(left<right);
102  BIASASSERT(bottom<top);
103  BIASASSERT(zNear<zFar);
104 
105  glMatrixMode(GL_PROJECTION);
106  glLoadIdentity();
107  //gluPerspective( (GLdouble)GetFovY(), (GLdouble)GetAspect(), (GLdouble)zNear, (GLdouble)zFar);
108  glFrustum(
109  (GLdouble) left,
110  (GLdouble) right,
111  (GLdouble) bottom,
112  (GLdouble) top,
113  (GLdouble) zNear,
114  (GLdouble) zFar
115  );
116  glMatrixMode(GL_MODELVIEW);
117 }
118 #endif // BIAS_HAVE_OPENGL
119 
void SetPerspective(const double &fovYdeg, const double &fovAspect, const double &zNear=1., const double &zFar=1000.)
sets a symmetric, centered frustum like gluPerspective does fovYdeg is the y field of view in degree...
std::ostream & Print(std::ostream &os=std::cout) const
void AdaptWidth(const double &vpAspectRatioDesired)
adapt left and right to match an aspect ratio useful to adapth frustum to viewport bound to windows s...