Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GLProjectionParametersBase.hh
1 #ifndef __GLPROJECTIONPARAMSBASE_HH__
2 #define __GLPROJECTIONPARAMSBASE_HH__
3 
4 #include <bias_config.h>
5 
6 #if !(defined(BIAS_HAVE_OPENGL)&&defined(BIAS_HAVE_GLEW))
7 #error You need BIAS with OpenGL and Glew. Please recompile BIAS
8 #endif //!(defined(BIAS_HAVE_OPENGL)&&defined(BIAS_HAVE_GLEW))
9 
10 #include <Gui/biasgl.h>
11 #include <Geometry/ProjectionParametersBase.hh>
12 #include <Base/Image/Image.hh>
13 #include <Base/Geometry/Quaternion.hh>
14 #include <Base/Debug/Debug.hh>
15 #include <GLviewer/GLProjectionParametersInterface.hh>
16 #include <GLviewer/OffscreenRendering.hh>
17 #include <GLviewer/Scenes/SceneBase.hh>
18 #include <GLviewer/OutputAttachments/GLProjectionOutputAttachment.hh>
19 #include <deque>
20 
21 #define GetMyselfAsProjectionParameterBase GetMyselfAsProjectionParametersBase
22 
23 namespace BIAS {
24 
25  class BIASGLviewer_EXPORT SceneBGImage;
26 
27  /** @class GLProjectionParametersBase
28  @brief GLPPB encapsulates the opengl rendering interface for the
29  projectionparameter classes.
30  GLPPB replaces the glprojection. In constrast to the good old glprojection
31  every projection type (e.g. perspective or fisheye) implements its own
32  class based on the GLPPB interface. This hopefully reduces the code
33  complextity and the need for gigantic case differentiations.
34  Unlike the glprojection it does not inherit anything from the
35  related projection class. the specific implementations
36  (e.g. GLprojectionsparametersperspective ) are derived from GLPPB and
37  a projectionparameter class (e.g. projectionparametersperspective).
38  the GLPPbase class should stay generic and must not contain methods
39  or members only related to a specific projection type. (e.g. distortion)
40  \ingroup g_glviewer
41  \author MIP
42  */
43 
44  class BIASGLviewer_EXPORT GLProjectionParametersBase
46  {
47  public:
48  // standard constructor without parameter object
50  virtual ~GLProjectionParametersBase( );
51 
52  virtual void SetDebugLevel(const long int lv);
53  virtual void AddDebugLevel(const long int lv);
54 
55  /**
56  the pure virtual GetMyselfAsProjectionParameterBase() forces the child
57  classes to inherit from a projectionparameterbase class and
58  allows to access the projectionparams in cases where only a GLPPB* is
59  available. this avoids the undesired need to derive GLPPB from PPB.
60  */
62  BIASERR("Base class called, should be overloaded!");
63  return NULL;
64  };
65 
66  /* Implement functions from GLProjectionParametersInterface*/
67  virtual int SetExtrinsics(const BIAS::HomgPoint3D& C, const BIAS::RMatrix& R);
68  virtual int SetExtrinsics(const BIAS::Vector3<double>& C, const BIAS::RMatrix& R);
69  virtual int SetExtrinsics(const BIAS::HomgPoint3D& C,const BIAS::Quaternion<double>& Q);
70  virtual int SetExtrinsics(const BIAS::Vector3<double>& C,const BIAS::Quaternion<double>& Q);
71 
72 
73  /** Set extrinsic similar to GL's gluLookAt.
74  * Defines the camera coordinate base through specified scenepoint
75  * that is observed from given position (the eye point). And the up
76  * direction in the image (which is the -y axis of image cooridinates).
77  * Assuming orthonormal base, extrinsics are calculated.
78  * Orthonormality is assured.
79  * @{ */
80  virtual int SetExtrinsics(float eyex, float eyey, float eyez,
81  float scenex, float sceney, float scenez,
82  float upx, float upy, float upz); /** @} */
83  virtual int SetExtrinsics(const BIAS::Vector3<double>& C,
84  const BIAS::Vector3<double>& scenePoint,
85  const BIAS::Vector3<double>& up);
86 
87  virtual void SetIntrinsics(BIAS::ProjectionParametersBase *p);
88  virtual void Set(BIAS::ProjectionParametersBase *p);
89 
90  virtual int GetExtrinsics(BIAS::Vector3<double>& C,
91  BIAS::Vector3<double>& up) const;
92 
93  virtual int GetExtrinsics(BIAS::Vector3<double>& C,
94  BIAS::RMatrix& R) const;
95 
96  void SwitchToOffscreenRendering(bool offscreen = true);
97 
98  void AppendOutputAttachment(int type, std::vector<int> args=std::vector<int>());
99 
100  // workaround for underwater needing to change parameters from outside, fkellner
101  GLProjectionOutputAttachment *GetAttachmentByType(int type);
102 
103 
104  virtual bool PoseValid(){ return(GetMyselfAsProjectionParameterBase()->PoseValid()); }
105 
106  BIAS::HomgPoint3D UnProject(const unsigned int x, const unsigned int y);
107  BIAS::HomgPoint3D UnProject(const BIAS::HomgPoint2D& p);
108  BIAS::HomgPoint3D UnProject(BIAS::HomgPoint2D& point2D, double depth);
109  BIAS::HomgPoint2D Project(BIAS::HomgPoint3D &point);
110 
111  /**
112  Renders scenes passed as argument beginning with the smallest index.
113  <b>Depth test is enabled per default with depth func set to GL_LESS<\b>.
114  Therfore scenes rendered first will also appear in front if competing
115  with other scenes over depth-values.
116  If different behaviour is wanted change this within specific scene code!
117  The background image scene must be passed through a special parameter
118  and should not be contained in the 'scenes' vector.
119  */
120  virtual int Draw(std::vector<BIAS::SceneBase *>& scenes,
121  BIAS::SceneBGImage* backgroundImageScene = NULL,
122  BIAS::glfFramebufferObject* theTarget=NULL);
123 #ifdef BIAS_HAVE_XML2
124  virtual int XMLRead (const std::string &Filename)=0;
125  virtual int XMLWrite(const std::string &Filename) const =0;
126  virtual int Load (const std::string& filename)=0;
127  virtual int Save (const std::string& filename) const =0;
128 #endif
129  //virtual void Assign(BIAS::GLProjectionParametersBase& p);
130  virtual void Assign(BIAS::ProjectionParametersBase& p);
131 
132  /** Returns the absolute world coordinate frame position of camera */
133 
134 
135  void Rescale(float ratio);
136 
137  /** Setting the near and far z-Clipping planes.
138  * @{ */
139  void SetZClippingPlanes(float n, float f);
140  void SetNearClippingPlane(float n);
141  void SetFarClippingPlane(float f);
142  float GetNearClippingPlane() const { return (float)zNear_; }
143  float GetFarClippingPlane() const { return (float)zFar_; }
144  /** @} */
145 
146 
147  /** evaluate depth.
148  Point is given in pixel coords relative to upper left image corner
149  with positive x axis directed to the right and the positive y-axis
150  pointing down. Result is the depth on optical ray.
151  \attention Point coordinates passed to this method must lie within
152  used viewport and at the same time have the origin in the upper left
153  corner with the y-axis pointing down the viewport side.
154  \attention Result is only valid if rendering is up to date
155  \attention make sure your rendering context is current
156  */
157  virtual double GetDepth(const unsigned int x, const unsigned int y);
158  virtual double GetDepth(const BIAS::HomgPoint2D& p);
159 
160  /** Method loads the parameters into the projection and modelview Matrix.
161  * \params forceUpdate do not check if camera state changed always
162  * recalculate.
163  * \attention make sure your rendering context is current.
164  * \attention Method is not equivalent to draw yet.
165  */
166  virtual int SetGLMatrices(bool forceUpdate = true) = 0;
167 
168  virtual int UpdateGLProjectionMatrix_(unsigned int x0, unsigned int y0,
169  unsigned int width, unsigned int height)=0;
170 
171  /** Sets gl's Modelview matrix for rendering
172  * relative to camera coordinate system (ignoring camera pose).
173  * \attention changes gl state, take care of resets yourself.
174  * \attention make sure your rendering context ist current
175  */
176  virtual void SetGLCameraToLocal();
177 
178  /** Calculates depth map from current z-Buffer state. */
179  int GetDepthMap(BIAS::Image<float>& depthMap, const float invalid=0.0);
180  /** @brief helper function which converts a z map obtained with the last
181  camera into a depth map for the last camera */
182  virtual void TranslateZToDepth(BIAS::Image<float>& ztodepth,
183  const float invalid = 0.0,
184  const bool flip = true) const;
185  /** @brief helper function which converts a z map obtained with the last
186  camera into a depth map for the last camera */
187  virtual void TranslateDepthToZ(BIAS::Image<float>& depthtoz,
188  const float invalid = 0.0,
189  const bool flip = true) const;
190 virtual void TranslateZToMetricZ(BIAS::Image<float>& depthMap,
191  const float invalid = 0.0,
192  const bool flip = true) const;
193 
194  int GetZBuffer(BIAS::Image<float>& zBuffer);
195  int GetMetricZMap(BIAS::Image<float>& metricZMap,
196  const float invalid = 0.0);
197  void GetImage(BIAS::Image<unsigned char>& image);
198  void GetGreyImage(BIAS::Image<unsigned char>& image);
199 
200  /** Rendering will call UpdateCameraRelatedState() on scenes. */
201  void ForceSceneUpdate();
202 
203  /** Is true if in multipass rendering procedure is rendering
204  for second or subsequent time. In single pass rendering
205  it never should return true.
206  */
207  bool FirstRenderingPassFinished() {return FirstRenderingPassFinished_;}
208 
209  void SubtractFromBackgroundImage(bool subtract = true) {
210  subtractFromBackgroundImage_ = subtract;
211  }
212 
214  if(offscreenRenderingEnabled_) {
215  AutoViewingMatchBehaviour_ = Deny;
216  }
217  else
218  AutoViewingMatchBehaviour_ = newBehaviour;
219  }
220 
221  static int instanceCounter;
222 
223  /**
224  \brief Takes x, y as window coordinates, checks whether the coordinates
225  lie in the rendered image (in the viewrange of projection). If so
226  \return the coordinates relative to the upper left image corner of the used
227  viewport and true. else coordintes are unchanged and return value is
228  false.
229  */
230  bool GetViewportCoordinates(int& x, int& y);
231 
233  return GLProjectionMatrix_.Transpose();
234  }
236  return GLModelViewMatrix_.Transpose();
237  }
238 
239 
240  /**
241  setclearcolor is pure virtual.
242  in some cases like for example distortion rendering
243  the lower render layers (offscreen surfaces) must informed about changes of
244  the clear color thus the child classes have to deal with the clear color on
245  their own.
246  */
247  virtual void SetClearColor(const BIAS::Vector4<float>& cc) = 0;
248 
249  /** @brief set the clear depth for render targets */
250  void SetClearDepth(const float d){
251  ClearDepth_=d;
252  };
253 
255  return ClearColor_;
256  };
257 
258  virtual float GetClearDepth(){
259  return ClearDepth_;
260  };
261 
262  virtual void SetRenderTarget(BIAS::glfRenderTarget* RT);
263  virtual BIAS::glfRenderTarget* GetRenderTarget();
264 
265 
266  protected:
267 
268  int GetGLViewport_(int &lowerleftx,int &lowerlefty,
269  int &sizex,int &sizey);
270  int SetGLViewport_(int lowerleftx,int lowerlefty,
271  int sizex,int sizey);
272 
273  virtual int BeginDraw_(BIAS::glfFramebufferObject* theTarget=NULL);
274  virtual int EndDraw_(BIAS::glfFramebufferObject* theTarget=NULL);
275  /** Implement in deriving classes */
276  virtual int Draw_(std::vector<SceneBase *>& scenes,
277  BIAS::glfFramebufferObject* theTarget = NULL)=0;
278 
279  virtual void Rescale_(float scale) = 0;
280  int UpdateGLModelViewMatrix_();
281  void SetToUnknownState_();
282  void InitBeforeDraw_();
283  void InitRaw_();
284 
285  virtual double Z2Depth_(const int x,const int y, const double z) const =0;
286 
287  /** used by GetDepthMap() which writes the current zbuffer in depthMap.
288  AdjustRawZBuffer_ computes the distance from the camera to every pixel */
289  virtual void ZBufferToDepthMap_(BIAS::Image<float>& depthMap);
290 
291  /** Takes care of adapting internal parameters and gl viewport
292  by specified behaviour scheme. External parameters update
293  is not performed here. */
294  virtual int DrawWithMatchedParamsAndViewport_(int viewport[4],
295  int imageWidth, int imageHeight,
296  std::vector<SceneBase*>& scenes);
297 
298  /** Renders a rectangular part of actual image.
299  Use if not full resolution is available for example with scrollbars.
300  Does not update external parameter forces internal parameter update
301  upon next call to Draw().
302  \attention make sure your rendering context ist current
303  */
304  int DrawPartOfImage_(const unsigned int ulX,
305  const unsigned int ulY,
306  const unsigned int width,
307  const unsigned int height,
308  std::vector<SceneBase *> scenes);
309 
310 
311  void ReadDepthBufferToTexture_(int texID, int width, int height);
312 
313  void PrepareOutputAttachments_();
314 
315  protected:
316  unsigned int instanceNum_;
318 
322 
323  /**Keeping track which part of image was drawn last time Draw() was called
324  and where it was drawn.
325  Set by Draw_(); */
327 
328  /** Remembering the last full viewport prevent unecessary calculations in
329  AutoRescaleParams behaviour */
331 
332  /** When cutting the image this field is telling the projection
333  the upper left corner of the drawn image part. */
335 
336  /** Keeping track which image part was drawn last time Draw() was called.
337  The width and height of the drawn image is contained in
338  lastUsedGLViewport */
340 
342 
343  /** Changes to the parameters or GL state that lead to new calculation of
344  GL_MODELVIEW matrix. */
346 
347  /** Changes to the parameters or GL state that lead to new calculation of
348  GL_PROJECTION matrix. */
350 
351  /** Abstract projection params have changed, scenes have to be informed
352  about this. */
354  double zNear_, zFar_;
355 
356  /**
357  * \attention BIAS::Matrix4x4 stores the matrix in row major order
358  * where GL expects a matrix in column major order!
359  * Since we want to use those matrices for rendering they are stored
360  * the way they can directly be used in GL. Before operating on those
361  * matrices and other Matrix4x4 objects some transpositions might
362  * be neccessary! @{*/
365  /**}@*/
366 
368 
370  float ClearDepth_;
371 
372 #ifdef BIAS_HAVE_CLASS_OFFSCREENRENDERING
373  OffscreenRendering offscreenRendering_;
374 #endif
375 
376 
378 
380 
381  bool distort_;
382 
384 
385  std::deque<BIAS::GLProjectionOutputAttachment *> outputAttachments_;
386  std::deque<std::vector<int> > outputAttachmentOptions_;
388  };
389 }
390 #endif
391 
392 
BIAS::Vector2< unsigned int > lastDrawingUpperLeft_
Keeping track which image part was drawn last time Draw() was called.
BIAS::Vector4< int > lastFullGLViewport_
Remembering the last full viewport prevent unecessary calculations in AutoRescaleParams behaviour...
GLPPB encapsulates the opengl rendering interface for the projectionparameter classes.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
BIAS::Matrix4x4< double > GetGLProjectionMatrixTransposed()
std::deque< BIAS::GLProjectionOutputAttachment * > outputAttachments_
Scene that renders a background image behind all other scenes.
Definition: SceneBGImage.hh:42
bool ExtrinsicStateChanged_
Changes to the parameters or GL state that lead to new calculation of GL_MODELVIEW matrix...
bool IntrinsicStateChanged_
Changes to the parameters or GL state that lead to new calculation of GL_PROJECTION matrix...
BIAS::Vector4< int > lastUsedGLViewport_
Keeping track which part of image was drawn last time Draw() was called and where it was drawn...
Down-, Upsampling routines and Resize.
Definition: Rescale.hh:71
3D rotation matrix
Definition: RMatrix.hh:49
Abstract interface class to handle changes in rendering parameters by controllers and in rendering co...
BIAS::GLProjectionParametersInterface::ViewingReshapeBehaviour AutoViewingMatchBehaviour_
BIAS::Vector2< unsigned int > drawingUpperLeft_
When cutting the image this field is telling the projection the upper left corner of the drawn image ...
Matrix4x4 Transpose() const
Definition: Matrix4x4.hh:315
std::deque< std::vector< int > > outputAttachmentOptions_
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
Utility class for offscreen rendering used by GLProjection.
bool FirstRenderingPassFinished()
Is true if in multipass rendering procedure is rendering for second or subsequent time...
virtual BIAS::Vector4< float > GetClearColor()
void SetClearDepth(const float d)
set the clear depth for render targets
bool informScenesOfChange_
Abstract projection params have changed, scenes have to be informed about this.
virtual BIAS::ProjectionParametersBase * GetMyselfAsProjectionParametersBase() const
the pure virtual GetMyselfAsProjectionParameterBase() forces the child classes to inherit from a proj...
Class for extra rendering pass in GLProjection (for now only used in glutGLviewer) ...
void SubtractFromBackgroundImage(bool subtract=true)
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
Interface for render targets.
BIAS::Matrix4x4< double > GetGLModelViewMatrixTransposed()
void SetAutoReshapeBehaviour(BIAS::GLProjectionParametersInterface::ViewingReshapeBehaviour newBehaviour)