Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RenderContextBase.hh
1 #ifndef __RENDER_CONTEXT_BASE_HH__
2 #define __RENDER_CONTEXT_BASE_HH__
3 
4 #include <bias_config.h>
5 
6 #ifndef BIAS_HAVE_OPENGL
7 # error You need BIAS with USE_OpenGL Please recompile BIAS.
8 #endif // BUILD_GLviewer
9 
10 #include <vector>
11 
12 #include <Base/Common/W32Compat.hh>
13 #include <Base/Image/Image.hh>
14 
15 #include <GLviewer/Scenes/SceneBase.hh>
16 #include <GLviewer/GLProjectionParametersInterface.hh>
17 #include <GLviewer/Controller/ControlInterface.hh>
18 #include <GLviewer/View.hh>
19 
20 #ifdef BIAS_HAVE_PTHREADS
21 #include <Utils/MutexSingleton.hh>
22 #endif
23 
24 namespace BIAS {
25  class BIASGLviewer_EXPORT SceneBGImage;
26 
27  /** \class RenderContextBase
28  \brief Base for all classes creating interface
29  between GL and "window manager".
30  new version without glprojection
31  \ingroup g_glviewer
32  */
33  class BIASGLviewer_EXPORT RenderContextBase {
34  public:
36  virtual ~RenderContextBase() {}
37 
38  /** \brief prints to current status of the OpenGL context*/
39  void PrintGLStatus();
40 
41  /** \brief Simple rendering routine.
42  * Assumes that all viewport settings are good and renders scene.
43  * \return 0 all good. */
44  virtual int Render();
45  /**\brief Demands a rendering from the Context; */
46  virtual void Redisplay() = 0;
47 
48  /** \brief try to make this OpenGl context current */
49  virtual void MakeGLContextCurrent() = 0;
50 
51 #ifdef BIAS_HAVE_PTHREADS
52  int LockContextSwitch();
53  int ReleaseContextSwitch();
54 #endif
55 
56  /** brief Get the currently displayed image as unsigned char color image.
57  * Set lock to false if no context lock should be used (i.e. if called from
58  within an already context locked environment*/
59  virtual int GetImage(BIAS::Image<unsigned char>& Image, bool lock = true);
60  /** brief Get the currently displayed image as unsigned char grey image
61  * Set lock to false if no context lock should be used (i.e. if called from
62  within an already context locked environment*/
63  virtual int GetGreyImage(BIAS::Image<unsigned char>& Image, bool lock = true);
64  /** brief Get the currently displayed depth image
65  * Set lock to false if no context lock should be used (i.e. if called from
66  within an already context locked environment*
67  * \param invalid[in]: depth value which is set as invalid (default = 0.0) */
68 
69  virtual int GetDepthMap(BIAS::Image<float>& Image, const float invalid = 0.0, bool lock = true);
70  /** brief Get the currently displayed zbuffer image (values 0-1) *
71  * Set lock to false if no context lock should be used (i.e. if called from
72  within an already context locked environment*/
73  virtual int GetZBuffer(BIAS::Image<float>& Image, bool lock = true);
74  /** brief Get the currently displayed zbuffer image in metric (real) values
75  * Set lock to false if no context lock should be used (i.e. if called from
76  within an already context locked environment.
77  * \param invalid[in]: z value which is set as invalid (default = 0.0)*
78  */
79  virtual int GetMetricZMap(BIAS::Image<float>& Image, const float invalid = 0.0, bool lock = true);
80 
81  /** \brief attach scenes to render here
82  * \attention Do not append scene of type SceneBGImage here, for correct
83  * background image rendering rather use SetBackgroundImageScene(). */
84  void AppendScene(SceneBase* scene, bool push_front = false);
85  /** \brief removes scene with the given pointer from the vector of scenes */
86  void RemoveScene(SceneBase* scene, bool noWarning = false);
87  /**\brief Inserts or deletes a scene at the specified position
88  * in std::vector<SceneBase*> scenes_. */
89  void InsertSceneAt(SceneBase* scene, unsigned int index);
90  /** \brief removes scene with the given index in the vector of scenes */
91  void DeleteSceneAt(unsigned int index);
92  /** \brief returns scene with the given index in the vector of scenes */
93  SceneBase* GetSceneAt(unsigned int index);
94 
95  /** \brief Deactivate background image rendering pass by passing NULL here,
96  * or deactivating scene itself.
97  * First choice will speed up rendering more (if noticeable anyway). */
98  void SetBackgroundImageScene(SceneBGImage* backgroundImageScene);
99  /** \brief Delete the background image scene.
100  * \attention The scene is deleted by call to delete, do not delete again outside.*/
101  void DeleteBackgroundImageScene();
102 
103  /** \brief Delete the attached scenes.
104  * \attention The scenes are destroyed, do not delete outside again.*/
105  void DeleteScenes();
106  /** \brief Delete the attached scenes.
107  * \attention The scenes are NOT destroyed, delete outside if intended
108  * or use DeleteScenes() instead.*/
109  void ClearScenes();
110  void DeleteControler() {delete control_; control_ = NULL;}
111  void DeleteController() {delete control_; control_ = NULL;}
112 
113  /**\brief return a vector with pointers to all attached scenes
114  * \return vector with pointers to scenes of Type SceneBase */
115  const std::vector<SceneBase*>& GetScenes() const {
116  return scenes_;
117  };
118  /**\brief Get the number of attached scenes.
119  * \return Number of attached scenes*/
120  int GetSceneCount();
121 
122  /** \brief additional control listeners can be attached, or removed here
123  * @{ */
125  {if(listener!=NULL) additionalListeners_.push_back(listener); }
126  void ClearAdditionalListeners() {additionalListeners_.clear(); }
127  void RemoveAdditionalListener(ControlInterface& listener);
128  /** @} */
129 
130  //control implementation:
131  /** .
132  * Control is implemented the following way: cout<<"F1"<<endl;
133  * All input is passed to control_ first, if this is not handling
134  * the input, the information is passed to all scenes_. One can think of
135  * extending this to different behaviour models, like
136  * not passing anything to scenes or always passing input to scenes.
137  * Distingushing between the scenes (exluding scene of information)
138  * must be handled on scene side.
139  *
140  * @{ */
141 
142  /**\brief Set the controller.
143  * \param newControl[in]: Pointer to a ControlInterface
144  * such as TrackballControl, FlyControl, etc.*/
145  void SetControl(BIAS::ControlInterface* newControl);
146  /** \brief delete the current controller
147  * \attention the controller is deleted, do not delete outside again*/
148  void DeleteControl() {delete control_; control_ = NULL;}
149 
150  bool LeftMouseDoubleClicked(int x, int y,int m);
151  bool LeftMouseSingleClicked(int x, int y);
152  bool LeftMouseDown(int x, int y);
153  bool LeftMouseUp(int x, int y);
154  bool LeftMouseMoved(int x1, int x2, int y1, int y2);
155  bool RightMouseSingleClicked(int x, int y);
156  bool RightMouseDoubleClicked(int x, int y);
157  bool RightMouseMoved(int x1, int x2, int y1, int y2);
158  bool RightMouseDown(int x, int y);
159  bool RightMouseUp(int x, int y);
160  bool MiddleMouseMoved(int x1, int x2, int y1, int y2);
161  bool MiddleMouseDown(int x, int y);
162  bool MiddleMouseUp(int x, int y);
163  bool LeftAndRightMouseMoved(int x1, int x2, int y1, int y2);
164  bool MouseLeftWindow();
165  bool SpecialKeyPressed(int key);
166  bool SpecialKeyUp(int key);
167  bool StandardKeyPressed(unsigned char ,int ,int );
168  bool StandardKeyUp(unsigned char ,int ,int );
169  /** Always informs both camera and scenes! */
170  bool TimerExpired();
171  bool WindowReshape(int width, int height);
172  bool MouseWheelUsed(double stepsize);
173  bool MouseMoved(int x, int y);
174  /** @} */
175 
176  /**\brief activate timer with intervall in ms */
177  virtual void SetTimer(unsigned int interval) =0;
178  /**\brief deactivate timer*/
179  virtual void DeactivateTimer() =0;
180 
181  /**\brief get the camera
182  * \return current camera projection parameters, can be of type
183  * GLProjection of any from GLProjectionParametersBase derived class.
184  * \deprecated Use GetGLProjectionParametersInterface() instead*/
186  BIASWARN("Deprecated Use GetGLProjectionParametersInterface() instead");
187  return camera_;}
188 
189  /**\brief Delete the camera
190  * \attention The camera pointer is deleted, do not delete outside again!
191  * \deprecated Use DeleteGLProjectionParametersInterface() instead*/
192  void DeleteCamera() {
193  BIASWARN("Deprecated Use DeleteGLProjectionParametersInterface() instead");
194  delete camera_; camera_ = NULL;}
195 
196  /**\brief get the camera parameters
197  * \return current camera projection parameters, can be of type
198  * GLProjection of any from GLProjectionParametersBase derived class.*/
200  {return camera_;}
201  /**\brief set the camera parameters
202  * \param newCamera[in]: the new camera projection parameters, can be of type
203  * GLProjection of any from GLProjectionParametersBase derived class.*/
204  void SetGLProjectionParametersInterface(GLProjectionParametersInterface* newCamera);
205 
206  /**\brief Turn AutoClipping on, now the near and far clipping plane is automatically
207  * derived and set. */
208  void SetAutoClipping(bool on) { AutoClipping_ = on;}
209 
210  /** @brief sets minimum znear for autoclipping
211  if autoclpiing is enabled the z-near and z-far plane are computed using
212  the boudingspheres of the scenes. SetAutoClipMinZNear defines the minimal
213  used znear value. */
214  void SetAutoClipMinZNear(double znear)
215  { AutoMinZNear_ = znear; };
216 
217  /**
218  * Returns the center of all bounding boxes of appended scenes
219  * and the radius which include all scenes. Return true iff
220  * at least one scene with a bounding box was found, else false
221  */
222  bool GetSceneCenter(BIAS::Vector3<double>& center,
223  BIAS::Vector3<double>& radius);
224 
225  /**\brief set the opengl clear color for color buffer
226  * \param cc[in]: clear color, clamped to range [0..1] */
227  inline void SetClearColor(const BIAS::Vector4<float>& cc)
228  {
229  ClearColor_ = cc;
230  if (camera_) camera_->SetClearColor(ClearColor_);
231 
232  };
233 
234  /**\brief En-/Disable blending */
235  bool SetBlending(bool blend=true);
236  /**\brief En-/Disable backface culling */
237  bool SetBackfaceCulling(bool cull=true);
238  /**\brief center the viewport */
239  void CenterViewport(bool center = true);
240  /**\brief Append a view to the context.
241  * \param view [in]: the view to append */
242  void AppendView(View* view);
243 
244  /**\brief switches rendering of bounding boxes for all scenes*/
245  inline void GlobalBoundingBoxSwitch(bool theswitch) {
246  for (unsigned int i=0; i<scenes_.size(); i++) {
247  scenes_[i]->SetRenderBoundingBox(theswitch);
248  }
249  };
250 
251  /**\brief Enable the useage of stencil buffer*/
252  void EnableStencilUsage(bool enable);
253 
254  /** \brief Enable or disable selection mode. In selection mode
255  * the interception point with a scene is calculated and SelectObjectAt() is
256  * called on the scene when double clicked. */
257  virtual void SetSelectionMode(bool selectionmode)
258  { bSelectionMode_ = selectionmode; };
259  /**\brief Get the selectinmode */
260  virtual bool GetSelectionMode(){return bSelectionMode_;};
261 
262  protected:
263  /** called once when rendering starts */
264  void Init_();
265 
266  protected:
267  std::vector<SceneBase*> scenes_;
272 
273  /** Listening for special keys only */
274  std::vector<ControlInterface*> additionalListeners_;
275 
276  unsigned int TimerInterval_; //0 = disabled
279  bool culling_;
285 
286 #ifdef BIAS_HAVE_PTHREADS
287  private:
288  static BIAS::MutexSingleton *contextLock_;
289 #endif //BIAS_HAVE_PTHREADS
290 
291  std::vector<View*> views_;
292  };
293 
294 }//end namespace
295 #endif
const GLProjectionParametersInterface * GetCamera()
get the camera
void GlobalBoundingBoxSwitch(bool theswitch)
switches rendering of bounding boxes for all scenes
void DeleteCamera()
Delete the camera.
Scene that renders a background image behind all other scenes.
Definition: SceneBGImage.hh:42
const GLProjectionParametersInterface * GetGLProjectionParametersInterface()
get the camera parameters
SceneBGImage * backgroundImageScene_
Wrapper for pthread mutex for use with &#39;automatic&#39; initialization.
class BIASGLviewer_EXPORT SceneBGImage
ControlInterface * control_
Abstract interface class to handle changes in rendering parameters by controllers and in rendering co...
void AppendAdditionalListener(ControlInterface *listener)
additional control listeners can be attached, or removed here
std::vector< ControlInterface * > additionalListeners_
Listening for special keys only.
A view is a rectangular part of a render context in which a set of scenes is rendered using a GLProje...
Definition: View.hh:25
void SetAutoClipMinZNear(double znear)
sets minimum znear for autoclipping if autoclpiing is enabled the z-near and z-far plane are computed...
Base class for all scenes.
Definition: SceneBase.hh:68
void DeleteControl()
delete the current controller
GLProjectionParametersInterface * camera_
virtual void SetSelectionMode(bool selectionmode)
Enable or disable selection mode.
void SetClearColor(const BIAS::Vector4< float > &cc)
set the opengl clear color for color buffer
void SetAutoClipping(bool on)
Turn AutoClipping on, now the near and far clipping plane is automatically derived and set...
std::vector< SceneBase * > scenes_
class BIASGLviewer_EXPORT RenderContextBase
Definition: SceneText.hh:27
BIAS::Vector4< float > ClearColor_
Defines the common interface used for communication between rendering components. ...
const std::vector< SceneBase * > & GetScenes() const
return a vector with pointers to all attached scenes
virtual bool GetSelectionMode()
Get the selectinmode.
Base for all classes creating interface between GL and &quot;window manager&quot;.