qt_ogre_render_window.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "qt_ogre_render_window.h"
30 #include "orthographic.h"
31 #include "render_system.h"
32 
33 #include <OgreRoot.h>
34 #include <OgreViewport.h>
35 #include <OgreCamera.h>
36 #include <OgreRenderWindow.h>
37 #include <OgreStringConverter.h>
38 #include <OgreGpuProgramManager.h>
39 #include <OgreRenderTargetListener.h>
40 
41 #include <ros/console.h>
42 #include <ros/assert.h>
43 
44 #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
45 #include <stdlib.h>
46 #endif
47 
48 namespace rviz
49 {
50 
52  : RenderWidget( RenderSystem::get(), parent )
53  , viewport_( 0 )
54  , ogre_root_( RenderSystem::get()->root() )
55  , ortho_scale_( 1.0f )
56  , auto_render_( true )
57  , camera_( 0 )
58  , overlays_enabled_( true ) // matches the default of Ogre::Viewport.
59  , background_color_( Ogre::ColourValue::Black ) // matches the default of Ogre::Viewport.
60  , stereo_enabled_( false )
61  , rendering_stereo_( false )
62  , left_camera_( 0 )
63  , right_camera_( 0 )
64  , right_viewport_( 0 )
65 {
66  render_window_->setVisible(true);
67  render_window_->setAutoUpdated(true);
68 
69  viewport_ = render_window_->addViewport( camera_ );
70  viewport_->setOverlaysEnabled( overlays_enabled_ );
71  viewport_->setBackgroundColour( background_color_ );
72 
73 #if OGRE_STEREO_ENABLE
74  viewport_->setDrawBuffer(Ogre::CBT_BACK);
75 #endif
76  enableStereo(true);
77 
79 }
80 
82 {
83  enableStereo(false); // free stereo resources
84 }
85 
86 //------------------------------------------------------------------------------
88 {
89  bool was_enabled = stereo_enabled_;
90  stereo_enabled_ = enable;
91  setupStereo();
92  return was_enabled;
93 }
94 
96 {
97  bool render_stereo = stereo_enabled_ && RenderSystem::get()->isStereoSupported();
98 
99  if (render_stereo == rendering_stereo_)
100  return;
101 
102  rendering_stereo_ = render_stereo;
103 
104  if (rendering_stereo_)
105  {
106  right_viewport_ = render_window_->addViewport( NULL, 1 );
107 #if OGRE_STEREO_ENABLE
108  right_viewport_->setDrawBuffer(Ogre::CBT_BACK_RIGHT);
109  viewport_->setDrawBuffer(Ogre::CBT_BACK_LEFT);
110 #endif
111 
114  if (camera_)
116 
117  // addListener causes preViewportUpdate() to be called when rendering.
118  render_window_->addListener(this);
119  }
120  else
121  {
122  render_window_->removeListener(this);
123  render_window_->removeViewport(1);
125 
126 #if OGRE_STEREO_ENABLE
127  viewport_->setDrawBuffer(Ogre::CBT_BACK);
128 #endif
129 
130  if (left_camera_)
131  left_camera_->getSceneManager()->destroyCamera( left_camera_ );
132  left_camera_ = NULL;
133  if (right_camera_)
134  right_camera_->getSceneManager()->destroyCamera( right_camera_ );
136  }
137 
138 }
139 
140 // this is called just before rendering either viewport when stereo is enabled.
142  const Ogre::RenderTargetViewportEvent& evt)
143 {
144  Ogre::Viewport* viewport = evt.source;
145 
146  const Ogre::Vector2& offset = camera_->getFrustumOffset();
147  const Ogre::Vector3 pos = camera_->getPosition();
148  const Ogre::Vector3 right = camera_->getRight();
149  const Ogre::Vector3 up = camera_->getUp();
150 
151  if (viewport == right_viewport_)
152  {
153  if (camera_->getProjectionType() != Ogre::PT_PERSPECTIVE || !right_camera_)
154  {
155  viewport->setCamera( camera_ );
156  return;
157  }
158 
159  Ogre::Vector3 newpos = pos
160  + right * offset.x
161  + up * offset.y;
162 
163  right_camera_->synchroniseBaseSettingsWith(camera_);
164  right_camera_->setFrustumOffset(-offset);
165  right_camera_->setPosition(newpos);
166  viewport->setCamera(right_camera_);
167  }
168  else if (viewport == viewport_)
169  {
170  if (camera_->getProjectionType() != Ogre::PT_PERSPECTIVE || !left_camera_)
171  {
172  viewport->setCamera( camera_ );
173  return;
174  }
175 
176  Ogre::Vector3 newpos = pos
177  - right * offset.x
178  - up * offset.y;
179 
180  left_camera_->synchroniseBaseSettingsWith(camera_);
181  left_camera_->setFrustumOffset(offset);
182  left_camera_->setPosition(newpos);
183  viewport->setCamera(left_camera_);
184  }
185  else
186  {
187  ROS_WARN("Begin rendering to unknown viewport.");
188  }
189 }
190 
192  const Ogre::RenderTargetViewportEvent& evt)
193 {
194  Ogre::Viewport* viewport = evt.source;
195 
196  if (viewport == right_viewport_)
197  {
198  // nothing to do here
199  }
200  else if (viewport == viewport_)
201  {
202  viewport->setCamera(camera_);
203  }
204  else
205  {
206  ROS_WARN("End rendering to unknown viewport.");
207  }
208 
209  if(!right_camera_->isCustomProjectionMatrixEnabled()) {
210  right_camera_->synchroniseBaseSettingsWith(camera_);
211  right_camera_->setFrustumOffset(-camera_->getFrustumOffset());
212  }
213  right_viewport_->setCamera(right_camera_);
214 }
215 
216 Ogre::Viewport* QtOgreRenderWindow::getViewport () const
217 {
218  return viewport_;
219 }
220 
221 void QtOgreRenderWindow::setCamera( Ogre::Camera* camera )
222 {
223  if (camera)
224  {
225  camera_ = camera;
226  viewport_->setCamera( camera );
227 
229 
231  {
232  left_camera_ = camera_->getSceneManager()->createCamera(camera_->getName() + "-left");
233  }
235  {
236  right_camera_ = camera_->getSceneManager()->createCamera(camera_->getName() + "-right");
237  }
238 
239  update();
240  }
241 }
242 
243 void QtOgreRenderWindow::setOverlaysEnabled( bool overlays_enabled )
244 {
245  overlays_enabled_ = overlays_enabled;
246  viewport_->setOverlaysEnabled( overlays_enabled );
247  if (right_viewport_)
248  {
249  right_viewport_->setOverlaysEnabled( overlays_enabled );
250  }
251 }
252 
253 void QtOgreRenderWindow::setBackgroundColor( Ogre::ColourValue background_color )
254 {
255  background_color_ = background_color;
256  viewport_->setBackgroundColour( background_color );
257  if (right_viewport_)
258  {
259  right_viewport_->setBackgroundColour( background_color );
260  }
261 }
262 
264 {
265  if ( camera_ )
266  {
267  camera_->setAspectRatio( Ogre::Real( width() ) / Ogre::Real( height() ) );
268  if (right_camera_ ) {
269  right_camera_->setAspectRatio( Ogre::Real( width() ) / Ogre::Real( height() ) );
270  }
271 
272  if ( camera_->getProjectionType() == Ogre::PT_ORTHOGRAPHIC )
273  {
274  Ogre::Matrix4 proj;
276  -width() / ortho_scale_ / 2, width() / ortho_scale_ / 2,
277  -height() / ortho_scale_ / 2, height() / ortho_scale_ / 2,
278  camera_->getNearClipDistance(), camera_->getFarClipDistance() );
279  camera_->setCustomProjectionMatrix(true, proj);
280  }
281  }
282 }
283 
285 {
286  ortho_scale_ = scale;
287 
289 }
290 
291 void QtOgreRenderWindow::setPreRenderCallback( boost::function<void ()> func )
292 {
293  pre_render_callback_ = func;
294 }
295 
296 void QtOgreRenderWindow::setPostRenderCallback( boost::function<void ()> func )
297 {
298  post_render_callback_ = func;
299 }
300 
301 //------------------------------------------------------------------------------
302 void QtOgreRenderWindow::paintEvent( QPaintEvent* e )
303 {
305  {
307  {
309  }
310 
311  if( ogre_root_->_fireFrameStarted() )
312  {
313 #if (OGRE_VERSION_MAJOR >= 1 && OGRE_VERSION_MINOR >= 6)
314  ogre_root_->_fireFrameRenderingQueued();
315 #endif
316 
317  render_window_->update();
318 
319  ogre_root_->_fireFrameEnded();
320  }
321 
322  if ( post_render_callback_ )
323  {
325  }
326  }
327 }
328 
329 //------------------------------------------------------------------------------
330 void QtOgreRenderWindow::resizeEvent( QResizeEvent* event )
331 {
332  RenderWidget::resizeEvent( event );
333 
334  if( render_window_ )
335  {
337 
338  if( auto_render_ )
339  {
340  update();
341  }
342  }
343 }
344 
345 } // namespace rviz
#define NULL
Definition: global.h:37
boost::function< void()> pre_render_callback_
Functor which is called before each render.
root
virtual void resizeEvent(QResizeEvent *e)
virtual void resizeEvent(QResizeEvent *event)
f
bool enableStereo(bool enable)
Enable or disable stereo rendering If stereo is not supported this is ignored.
static RenderSystem * get()
QtOgreRenderWindow(QWidget *parent=0)
boost::function< void()> post_render_callback_
Functor which is called after each render.
#define ROS_WARN(...)
Ogre::RenderWindow * render_window_
Definition: render_widget.h:63
def get(url)
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
virtual void setPreRenderCallback(boost::function< void()> func)
void setBackgroundColor(Ogre::ColourValue color)
Ogre::ColourValue background_color_
virtual void postViewportUpdate(const Ogre::RenderTargetViewportEvent &evt)
Ogre::Viewport * getViewport() const
void buildScaledOrthoMatrix(Ogre::Matrix4 &proj, float left, float right, float bottom, float top, float near, float far)
void setupStereo()
Prepare to render in stereo if enabled and supported.
virtual void preViewportUpdate(const Ogre::RenderTargetViewportEvent &evt)
virtual void paintEvent(QPaintEvent *e)
void setCamera(Ogre::Camera *camera)
void setOverlaysEnabled(bool overlays_enabled)
virtual void setPostRenderCallback(boost::function< void()> func)
void setOrthoScale(float scale)
Set the scale of the orthographic window. Only valid for an orthographic camera.


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51