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"
33 
34 #include <OgreRoot.h>
35 #include <OgreViewport.h>
36 #include <OgreCamera.h>
37 #include <OgreRenderWindow.h>
38 #include <OgreStringConverter.h>
39 #include <OgreGpuProgramManager.h>
40 #include <OgreRenderTargetListener.h>
41 
42 #include <ros/console.h>
43 #include <ros/assert.h>
44 
45 #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
46 #include <stdlib.h>
47 
48 
49 #include <utility>
50 
51 #endif
52 
53 namespace rviz
54 {
56  : RenderWidget(RenderSystem::get(), parent)
57  , viewport_(nullptr)
58  , ogre_root_(RenderSystem::get()->root())
59  , ortho_scale_(1.0f)
60  , auto_render_(true)
61  , camera_(nullptr)
62  , overlays_enabled_(true) // matches the default of Ogre::Viewport.
63  , background_color_(Ogre::ColourValue::Black) // matches the default of Ogre::Viewport.
64  , stereo_enabled_(false)
65  , rendering_stereo_(false)
66  , left_camera_(nullptr)
67  , right_camera_(nullptr)
68  , right_viewport_(nullptr)
69 {
70  render_window_->setVisible(true);
71  render_window_->setAutoUpdated(true);
72 
73  viewport_ = render_window_->addViewport(camera_);
74  viewport_->setOverlaysEnabled(overlays_enabled_);
75  viewport_->setBackgroundColour(background_color_);
76 
77 #if OGRE_STEREO_ENABLE
78  viewport_->setDrawBuffer(Ogre::CBT_BACK);
79 #endif
80  enableStereo(true);
81 
83 }
84 
86 {
87  enableStereo(false); // free stereo resources
88 }
89 
90 //------------------------------------------------------------------------------
92 {
93  bool was_enabled = stereo_enabled_;
94  stereo_enabled_ = enable;
95  setupStereo();
96  return was_enabled;
97 }
98 
100 {
101  bool render_stereo = stereo_enabled_ && RenderSystem::get()->isStereoSupported();
102 
103  if (render_stereo == rendering_stereo_)
104  return;
105 
106  rendering_stereo_ = render_stereo;
107 
108  if (rendering_stereo_)
109  {
110  right_viewport_ = render_window_->addViewport(nullptr, 1);
111 #if OGRE_STEREO_ENABLE
112  right_viewport_->setDrawBuffer(Ogre::CBT_BACK_RIGHT);
113  viewport_->setDrawBuffer(Ogre::CBT_BACK_LEFT);
114 #endif
115 
118  if (camera_)
120 
121  // addListener causes preViewportUpdate() to be called when rendering.
122  render_window_->addListener(this);
123  }
124  else
125  {
126  render_window_->removeListener(this);
127  render_window_->removeViewport(1);
128  right_viewport_ = nullptr;
129 
130 #if OGRE_STEREO_ENABLE
131  viewport_->setDrawBuffer(Ogre::CBT_BACK);
132 #endif
133 
134  if (left_camera_)
135  left_camera_->getSceneManager()->destroyCamera(left_camera_);
136  left_camera_ = nullptr;
137  if (right_camera_)
138  right_camera_->getSceneManager()->destroyCamera(right_camera_);
139  right_camera_ = nullptr;
140  }
141 }
142 
143 // this is called just before rendering either viewport when stereo is enabled.
144 void QtOgreRenderWindow::preViewportUpdate(const Ogre::RenderTargetViewportEvent& evt)
145 {
146  Ogre::Viewport* viewport = evt.source;
147 
148  const Ogre::Vector2& offset = camera_->getFrustumOffset();
149  const Ogre::Vector3 pos = camera_->getPosition();
150  const Ogre::Vector3 right = camera_->getRight();
151  const Ogre::Vector3 up = camera_->getUp();
152 
153  if (viewport == right_viewport_)
154  {
155  if (camera_->getProjectionType() != Ogre::PT_PERSPECTIVE || !right_camera_)
156  {
157  viewport->setCamera(camera_);
158  return;
159  }
160 
161  Ogre::Vector3 newpos = pos + right * offset.x + 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 - right * offset.x - up * offset.y;
177 
178  left_camera_->synchroniseBaseSettingsWith(camera_);
179  left_camera_->setFrustumOffset(offset);
180  left_camera_->setPosition(newpos);
181  viewport->setCamera(left_camera_);
182  }
183  else
184  {
185  ROS_WARN("Begin rendering to unknown viewport.");
186  }
187 }
188 
189 void QtOgreRenderWindow::postViewportUpdate(const Ogre::RenderTargetViewportEvent& evt)
190 {
191  Ogre::Viewport* viewport = evt.source;
192 
193  if (viewport == right_viewport_)
194  {
195  // nothing to do here
196  }
197  else if (viewport == viewport_)
198  {
199  viewport->setCamera(camera_);
200  }
201  else
202  {
203  ROS_WARN("End rendering to unknown viewport.");
204  }
205 
206  if (!right_camera_->isCustomProjectionMatrixEnabled())
207  {
208  right_camera_->synchroniseBaseSettingsWith(camera_);
209  right_camera_->setFrustumOffset(-camera_->getFrustumOffset());
210  }
211  right_viewport_->setCamera(right_camera_);
212 }
213 
214 Ogre::Viewport* QtOgreRenderWindow::getViewport() const
215 {
216  return viewport_;
217 }
218 
219 void QtOgreRenderWindow::setCamera(Ogre::Camera* camera)
220 {
221  if (camera)
222  {
223  camera_ = camera;
224  viewport_->setCamera(camera);
225 
227 
229  {
230  left_camera_ = camera_->getSceneManager()->createCamera(camera_->getName() + "-left");
231  }
233  {
234  right_camera_ = camera_->getSceneManager()->createCamera(camera_->getName() + "-right");
235  }
236 
237  update();
238  }
239 }
240 
241 void QtOgreRenderWindow::setOverlaysEnabled(bool overlays_enabled)
242 {
243  overlays_enabled_ = overlays_enabled;
244  viewport_->setOverlaysEnabled(overlays_enabled);
245  if (right_viewport_)
246  {
247  right_viewport_->setOverlaysEnabled(overlays_enabled);
248  }
249 }
250 
251 void QtOgreRenderWindow::setBackgroundColor(Ogre::ColourValue background_color)
252 {
253  background_color_ = background_color;
254  viewport_->setBackgroundColour(background_color);
255  if (right_viewport_)
256  {
257  right_viewport_->setBackgroundColour(background_color);
258  }
259 }
260 
262 {
263  if (camera_)
264  {
265  camera_->setAspectRatio(Ogre::Real(width()) / Ogre::Real(height()));
266  if (right_camera_)
267  {
268  right_camera_->setAspectRatio(Ogre::Real(width()) / Ogre::Real(height()));
269  }
270 
271  if (camera_->getProjectionType() == Ogre::PT_ORTHOGRAPHIC)
272  {
273  Ogre::Matrix4 proj;
274  buildScaledOrthoMatrix(proj, -width() / ortho_scale_ / 2, width() / ortho_scale_ / 2,
275  -height() / ortho_scale_ / 2, height() / ortho_scale_ / 2,
276  camera_->getNearClipDistance(), camera_->getFarClipDistance());
277  camera_->setCustomProjectionMatrix(true, proj);
278  }
279  }
280 }
281 
283 {
284  ortho_scale_ = scale;
285 
287 }
288 
289 void QtOgreRenderWindow::setPreRenderCallback(boost::function<void()> func)
290 {
291  pre_render_callback_ = std::move(func);
292 }
293 
294 void QtOgreRenderWindow::setPostRenderCallback(boost::function<void()> func)
295 {
296  post_render_callback_ = std::move(func);
297 }
298 
299 //------------------------------------------------------------------------------
300 void QtOgreRenderWindow::paintEvent(QPaintEvent* /*e*/)
301 {
303  {
305  {
307  }
308 
309  if (ogre_root_->_fireFrameStarted())
310  {
311  ogre_root_->_fireFrameRenderingQueued();
312 
313  render_window_->update();
314 
315  ogre_root_->_fireFrameEnded();
316  }
317 
319  {
321  }
322  }
323 }
324 
325 //------------------------------------------------------------------------------
326 void QtOgreRenderWindow::resizeEvent(QResizeEvent* event)
327 {
329 
330  if (render_window_)
331  {
333 
334  if (auto_render_)
335  {
336  update();
337  }
338  }
339 }
340 
341 } // namespace rviz
render_system.h
rviz::QtOgreRenderWindow::QtOgreRenderWindow
QtOgreRenderWindow(QWidget *parent=nullptr)
Definition: qt_ogre_render_window.cpp:55
rviz::QtOgreRenderWindow::left_camera_
Ogre::Camera * left_camera_
Definition: qt_ogre_render_window.h:165
Ogre
Definition: axes_display.h:35
rviz::QtOgreRenderWindow::ortho_scale_
float ortho_scale_
Definition: qt_ogre_render_window.h:155
rviz::RenderSystem::get
static RenderSystem * get()
Definition: render_system.cpp:76
version_check.h
rviz::RenderSystem::isStereoSupported
bool isStereoSupported()
Definition: render_system.h:92
rviz::RenderWidget
Definition: render_widget.h:44
rviz::QtOgreRenderWindow::viewport_
Ogre::Viewport * viewport_
Definition: qt_ogre_render_window.h:148
rviz::QtOgreRenderWindow::post_render_callback_
boost::function< void()> post_render_callback_
Functor which is called after each render.
Definition: qt_ogre_render_window.h:153
get
def get(url)
f
f
rviz::QtOgreRenderWindow::setPreRenderCallback
virtual void setPreRenderCallback(boost::function< void()> func)
Definition: qt_ogre_render_window.cpp:289
rviz::QtOgreRenderWindow::overlays_enabled_
bool overlays_enabled_
Definition: qt_ogre_render_window.h:159
rviz::QtOgreRenderWindow::setupStereo
void setupStereo()
Prepare to render in stereo if enabled and supported.
Definition: qt_ogre_render_window.cpp:99
rviz::QtOgreRenderWindow::enableStereo
bool enableStereo(bool enable)
Enable or disable stereo rendering If stereo is not supported this is ignored.
Definition: qt_ogre_render_window.cpp:91
orthographic.h
console.h
rviz::buildScaledOrthoMatrix
void buildScaledOrthoMatrix(Ogre::Matrix4 &proj, float left, float right, float bottom, float top, float near, float far)
Definition: orthographic.cpp:36
rviz::QtOgreRenderWindow::camera_
Ogre::Camera * camera_
Definition: qt_ogre_render_window.h:158
rviz::QtOgreRenderWindow::rendering_stereo_
bool rendering_stereo_
Definition: qt_ogre_render_window.h:164
update
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
rviz
Definition: add_display_dialog.cpp:54
rviz::QtOgreRenderWindow::setCameraAspectRatio
void setCameraAspectRatio()
Definition: qt_ogre_render_window.cpp:261
rviz::RenderSystem
Definition: render_system.h:45
ROS_WARN
#define ROS_WARN(...)
rviz::QtOgreRenderWindow::stereo_enabled_
bool stereo_enabled_
Definition: qt_ogre_render_window.h:163
rviz::QtOgreRenderWindow::setBackgroundColor
void setBackgroundColor(Ogre::ColourValue color)
Definition: qt_ogre_render_window.cpp:251
rviz::QtOgreRenderWindow::postViewportUpdate
void postViewportUpdate(const Ogre::RenderTargetViewportEvent &evt) override
Definition: qt_ogre_render_window.cpp:189
rviz::QtOgreRenderWindow::setOrthoScale
void setOrthoScale(float scale)
Set the scale of the orthographic window. Only valid for an orthographic camera.
Definition: qt_ogre_render_window.cpp:282
rviz::QtOgreRenderWindow::pre_render_callback_
boost::function< void()> pre_render_callback_
Functor which is called before each render.
Definition: qt_ogre_render_window.h:152
rviz::QtOgreRenderWindow::preViewportUpdate
void preViewportUpdate(const Ogre::RenderTargetViewportEvent &evt) override
Definition: qt_ogre_render_window.cpp:144
rviz::QtOgreRenderWindow::paintEvent
void paintEvent(QPaintEvent *e) override
Definition: qt_ogre_render_window.cpp:300
rviz::QtOgreRenderWindow::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: qt_ogre_render_window.cpp:326
rviz::QtOgreRenderWindow::setCamera
void setCamera(Ogre::Camera *camera)
Definition: qt_ogre_render_window.cpp:219
rviz::QtOgreRenderWindow::auto_render_
bool auto_render_
Definition: qt_ogre_render_window.h:156
rviz::QtOgreRenderWindow::~QtOgreRenderWindow
~QtOgreRenderWindow() override
Definition: qt_ogre_render_window.cpp:85
rviz::RenderWidget::resizeEvent
void resizeEvent(QResizeEvent *e) override
Definition: render_widget.cpp:89
rviz::QtOgreRenderWindow::setPostRenderCallback
virtual void setPostRenderCallback(boost::function< void()> func)
Definition: qt_ogre_render_window.cpp:294
rviz::QtOgreRenderWindow::ogre_root_
Ogre::Root * ogre_root_
Definition: qt_ogre_render_window.h:150
rviz::QtOgreRenderWindow::setOverlaysEnabled
void setOverlaysEnabled(bool overlays_enabled)
Definition: qt_ogre_render_window.cpp:241
rviz::QtOgreRenderWindow::right_viewport_
Ogre::Viewport * right_viewport_
Definition: qt_ogre_render_window.h:167
root
root
assert.h
rviz::QtOgreRenderWindow::getViewport
Ogre::Viewport * getViewport() const
Definition: qt_ogre_render_window.cpp:214
rviz::QtOgreRenderWindow::right_camera_
Ogre::Camera * right_camera_
Definition: qt_ogre_render_window.h:166
rviz::QtOgreRenderWindow::background_color_
Ogre::ColourValue background_color_
Definition: qt_ogre_render_window.h:160
qt_ogre_render_window.h
rviz::RenderWidget::render_window_
Ogre::RenderWindow * render_window_
Definition: render_widget.h:65


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Dec 13 2024 03:31:02