qt_ogre_render_window.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 #include "qt_ogre_render_window.h"
00030 #include "orthographic.h"
00031 #include "render_system.h"
00032 
00033 #include <OGRE/OgreRoot.h>
00034 #include <OGRE/OgreViewport.h>
00035 #include <OGRE/OgreCamera.h>
00036 #include <OGRE/OgreRenderWindow.h>
00037 #include <OGRE/OgreStringConverter.h>
00038 #include <OGRE/OgreGpuProgramManager.h>
00039 
00040 #include <ros/console.h>
00041 #include <ros/assert.h>
00042 
00043 #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
00044 #include <stdlib.h>
00045 #endif
00046 
00047 namespace rviz
00048 {
00049 
00050 QtOgreRenderWindow::QtOgreRenderWindow( QWidget* parent )
00051   : RenderWidget( RenderSystem::get(), parent )
00052   , viewport_( 0 )
00053   , ogre_root_( RenderSystem::get()->root() )
00054   , ortho_scale_( 1.0f )
00055   , auto_render_( true )
00056   , camera_( 0 )
00057   , overlays_enabled_( true ) // matches the default of Ogre::Viewport.
00058   , background_color_( Ogre::ColourValue::Black ) // matches the default of Ogre::Viewport.
00059 {
00060   render_window_->setVisible(true);
00061   render_window_->setAutoUpdated(true);
00062 
00063   viewport_ = render_window_->addViewport( camera_ );
00064   viewport_->setOverlaysEnabled( overlays_enabled_ );
00065   viewport_->setBackgroundColour( background_color_ );
00066 
00067   setCameraAspectRatio();
00068 }
00069 
00070 //------------------------------------------------------------------------------
00071 Ogre::Viewport* QtOgreRenderWindow::getViewport () const
00072 {
00073   return viewport_;
00074 }
00075 
00076 void QtOgreRenderWindow::setCamera( Ogre::Camera* camera )
00077 {
00078   camera_ = camera;
00079   viewport_->setCamera( camera );
00080 
00081   setCameraAspectRatio();
00082 
00083   update();
00084 }
00085 
00086 void QtOgreRenderWindow::setOverlaysEnabled( bool overlays_enabled )
00087 {
00088   overlays_enabled_ = overlays_enabled;
00089   viewport_->setOverlaysEnabled( overlays_enabled );
00090 }
00091 
00092 void QtOgreRenderWindow::setBackgroundColor( Ogre::ColourValue background_color )
00093 {
00094   background_color_ = background_color;
00095   viewport_->setBackgroundColour( background_color );
00096 }
00097 
00098 void QtOgreRenderWindow::setCameraAspectRatio()
00099 {
00100   if ( camera_ )
00101   {
00102     camera_->setAspectRatio( Ogre::Real( width() ) / Ogre::Real( height() ) );
00103 
00104     if ( camera_->getProjectionType() == Ogre::PT_ORTHOGRAPHIC )
00105     {
00106       Ogre::Matrix4 proj;
00107       buildScaledOrthoMatrix( proj,
00108                               -width() / ortho_scale_ / 2, width() / ortho_scale_ / 2,
00109                               -height() / ortho_scale_ / 2, height() / ortho_scale_ / 2,
00110                               camera_->getNearClipDistance(), camera_->getFarClipDistance() );
00111       camera_->setCustomProjectionMatrix(true, proj);
00112     }
00113   }
00114 }
00115 
00116 void QtOgreRenderWindow::setOrthoScale( float scale )
00117 {
00118   ortho_scale_ = scale;
00119 
00120   setCameraAspectRatio();
00121 }
00122 
00123 void QtOgreRenderWindow::setPreRenderCallback( boost::function<void ()> func )
00124 {
00125   pre_render_callback_ = func;
00126 }
00127 
00128 void QtOgreRenderWindow::setPostRenderCallback( boost::function<void ()> func )
00129 {
00130   post_render_callback_ = func;
00131 }
00132 
00133 //------------------------------------------------------------------------------
00134 void QtOgreRenderWindow::paintEvent( QPaintEvent* e )
00135 {
00136   if( auto_render_ && render_window_ )
00137   {
00138     if( pre_render_callback_ )
00139     {
00140       pre_render_callback_();
00141     }
00142 
00143     if( ogre_root_->_fireFrameStarted() )
00144     {
00145 #if (OGRE_VERSION_MAJOR >= 1 && OGRE_VERSION_MINOR >= 6)
00146       ogre_root_->_fireFrameRenderingQueued();
00147 #endif
00148 
00149       render_window_->update();
00150 
00151       ogre_root_->_fireFrameEnded();
00152     }
00153 
00154     if ( post_render_callback_ )
00155     {
00156       post_render_callback_();
00157     }
00158   }
00159 }
00160 
00161 //------------------------------------------------------------------------------
00162 void QtOgreRenderWindow::resizeEvent( QResizeEvent* event )
00163 {
00164   RenderWidget::resizeEvent( event );
00165 
00166   if( render_window_ )
00167   {
00168     setCameraAspectRatio();
00169 
00170     if( auto_render_ )
00171     {
00172       update();
00173     }
00174   }
00175 }
00176 
00177 } // namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:35