Go to the documentation of this file.00001 #include "qt_ogre_render_window.h"
00002 #include "orthographic.h"
00003 #include "render_system.h"
00004
00005 #include <OGRE/OgreRoot.h>
00006 #include <OGRE/OgreViewport.h>
00007 #include <OGRE/OgreCamera.h>
00008 #include <OGRE/OgreRenderWindow.h>
00009 #include <OGRE/OgreStringConverter.h>
00010 #include <OGRE/OgreGpuProgramManager.h>
00011
00012 #include <ros/console.h>
00013 #include <ros/assert.h>
00014
00015 #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
00016 #include <stdlib.h>
00017 #endif
00018
00019 namespace rviz
00020 {
00021
00022 QtOgreRenderWindow::QtOgreRenderWindow( QWidget* parent )
00023 : RenderWidget( RenderSystem::get(), parent )
00024 , viewport_( 0 )
00025 , ogre_root_( RenderSystem::get()->root() )
00026 , ortho_scale_( 1.0f )
00027 , auto_render_( true )
00028 , camera_( 0 )
00029 , overlays_enabled_( true )
00030 , background_color_( Ogre::ColourValue::Black )
00031 {
00032 render_window_->setVisible(true);
00033 render_window_->setAutoUpdated(true);
00034
00035 viewport_ = render_window_->addViewport( camera_ );
00036 viewport_->setOverlaysEnabled( overlays_enabled_ );
00037 viewport_->setBackgroundColour( background_color_ );
00038
00039 setCameraAspectRatio();
00040 }
00041
00042
00043 Ogre::Viewport* QtOgreRenderWindow::getViewport () const
00044 {
00045 assert( viewport_ );
00046 return viewport_;
00047 }
00048
00049 void QtOgreRenderWindow::setCamera( Ogre::Camera* camera )
00050 {
00051 camera_ = camera;
00052 if( viewport_ )
00053 {
00054 viewport_->setCamera( camera );
00055 }
00056
00057 setCameraAspectRatio();
00058
00059 update();
00060 }
00061
00062 void QtOgreRenderWindow::setOverlaysEnabled( bool overlays_enabled )
00063 {
00064 overlays_enabled_ = overlays_enabled;
00065 if( viewport_ )
00066 {
00067 viewport_->setOverlaysEnabled( overlays_enabled );
00068 }
00069 }
00070
00071 void QtOgreRenderWindow::setBackgroundColor( Ogre::ColourValue background_color )
00072 {
00073 background_color_ = background_color;
00074 if( viewport_ )
00075 {
00076 viewport_->setBackgroundColour( background_color );
00077 }
00078 }
00079
00080 void QtOgreRenderWindow::setCameraAspectRatio()
00081 {
00082 if ( camera_ )
00083 {
00084 camera_->setAspectRatio( Ogre::Real( width() ) / Ogre::Real( height() ) );
00085
00086 if ( camera_->getProjectionType() == Ogre::PT_ORTHOGRAPHIC )
00087 {
00088 Ogre::Matrix4 proj;
00089 buildScaledOrthoMatrix( proj,
00090 -width() / ortho_scale_ / 2, width() / ortho_scale_ / 2,
00091 -height() / ortho_scale_ / 2, height() / ortho_scale_ / 2,
00092 camera_->getNearClipDistance(), camera_->getFarClipDistance() );
00093 camera_->setCustomProjectionMatrix(true, proj);
00094 }
00095 }
00096 }
00097
00098 void QtOgreRenderWindow::setOrthoScale( float scale )
00099 {
00100 ortho_scale_ = scale;
00101
00102 setCameraAspectRatio();
00103 }
00104
00105 void QtOgreRenderWindow::setPreRenderCallback( boost::function<void ()> func )
00106 {
00107 pre_render_callback_ = func;
00108 }
00109
00110 void QtOgreRenderWindow::setPostRenderCallback( boost::function<void ()> func )
00111 {
00112 post_render_callback_ = func;
00113 }
00114
00115
00116 void QtOgreRenderWindow::paintEvent( QPaintEvent* e )
00117 {
00118 if( auto_render_ && render_window_ )
00119 {
00120 if( pre_render_callback_ )
00121 {
00122 pre_render_callback_();
00123 }
00124
00125 if( ogre_root_->_fireFrameStarted() )
00126 {
00127 #if (OGRE_VERSION_MAJOR >= 1 && OGRE_VERSION_MINOR >= 6)
00128 ogre_root_->_fireFrameRenderingQueued();
00129 #endif
00130
00131 render_window_->update();
00132
00133 ogre_root_->_fireFrameEnded();
00134 }
00135
00136 if ( post_render_callback_ )
00137 {
00138 post_render_callback_();
00139 }
00140 }
00141 }
00142
00143
00144 void QtOgreRenderWindow::resizeEvent( QResizeEvent* event )
00145 {
00146 RenderWidget::resizeEvent( event );
00147
00148 if( render_window_ )
00149 {
00150 setCameraAspectRatio();
00151
00152 if( auto_render_ )
00153 {
00154 update();
00155 }
00156 }
00157 }
00158
00159 }