qt_ogre_render_window.cpp
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 ogre_tools
00020 {
00021 
00022 QtOgreRenderWindow::QtOgreRenderWindow( RenderSystem* render_system, QWidget* parent )
00023   : RenderWidget( render_system, parent )
00024   , viewport_( 0 )
00025   , ogre_root_( render_system_->root() )
00026   , ortho_scale_( 1.0f )
00027   , auto_render_( true )
00028   , camera_( 0 )
00029   , overlays_enabled_( true ) // matches the default of Ogre::Viewport.
00030   , background_color_( Ogre::ColourValue::Black ) // matches the default of Ogre::Viewport.
00031 {
00032 }
00033 
00034 //------------------------------------------------------------------------------
00035 Ogre::Viewport* QtOgreRenderWindow::getViewport () const
00036 {
00037   assert( viewport_ );
00038   return viewport_;
00039 }
00040 
00041 void QtOgreRenderWindow::setCamera( Ogre::Camera* camera )
00042 {
00043   camera_ = camera;
00044   if( viewport_ )
00045   {
00046     viewport_->setCamera( camera );
00047   }
00048 
00049   setCameraAspectRatio();
00050 
00051   update();
00052 }
00053 
00054 void QtOgreRenderWindow::setOverlaysEnabled( bool overlays_enabled )
00055 {
00056   overlays_enabled_ = overlays_enabled;
00057   if( viewport_ )
00058   {
00059     viewport_->setOverlaysEnabled( overlays_enabled );
00060   }
00061 }
00062 
00063 void QtOgreRenderWindow::setBackgroundColor( Ogre::ColourValue background_color )
00064 {
00065   background_color_ = background_color;
00066   if( viewport_ )
00067   {
00068     viewport_->setBackgroundColour( background_color );
00069   }
00070 }
00071 
00072 void QtOgreRenderWindow::setCameraAspectRatio()
00073 {
00074   if ( camera_ )
00075   {
00076     camera_->setAspectRatio( Ogre::Real( width() ) / Ogre::Real( height() ) );
00077 
00078     if ( camera_->getProjectionType() == Ogre::PT_ORTHOGRAPHIC )
00079     {
00080       Ogre::Matrix4 proj;
00081       buildScaledOrthoMatrix( proj,
00082                               -width() / ortho_scale_ / 2, width() / ortho_scale_ / 2,
00083                               -height() / ortho_scale_ / 2, height() / ortho_scale_ / 2,
00084                               camera_->getNearClipDistance(), camera_->getFarClipDistance() );
00085       camera_->setCustomProjectionMatrix(true, proj);
00086     }
00087   }
00088 }
00089 
00090 void QtOgreRenderWindow::setOrthoScale( float scale )
00091 {
00092   ortho_scale_ = scale;
00093 
00094   setCameraAspectRatio();
00095 }
00096 
00097 void QtOgreRenderWindow::setPreRenderCallback( boost::function<void ()> func )
00098 {
00099   pre_render_callback_ = func;
00100 }
00101 
00102 void QtOgreRenderWindow::setPostRenderCallback( boost::function<void ()> func )
00103 {
00104   post_render_callback_ = func;
00105 }
00106 
00107 //------------------------------------------------------------------------------
00108 void QtOgreRenderWindow::paintEvent( QPaintEvent* e )
00109 {
00110   if( auto_render_ && render_window_ )
00111   {
00112     if( pre_render_callback_ )
00113     {
00114       pre_render_callback_();
00115     }
00116 
00117     if( ogre_root_->_fireFrameStarted() )
00118     {
00119 #if (OGRE_VERSION_MAJOR >= 1 && OGRE_VERSION_MINOR >= 6)
00120       ogre_root_->_fireFrameRenderingQueued();
00121 #endif
00122 
00123       render_window_->update();
00124 
00125       ogre_root_->_fireFrameEnded();
00126     }
00127 
00128     if ( post_render_callback_ )
00129     {
00130       post_render_callback_();
00131     }
00132   }
00133 }
00134 
00135 //------------------------------------------------------------------------------
00136 void QtOgreRenderWindow::resizeEvent( QResizeEvent* event )
00137 {
00138   RenderWidget::resizeEvent( event );
00139 
00140   if( render_window_ )
00141   {
00142     setCameraAspectRatio();
00143 
00144     if( auto_render_ )
00145     {
00146       update();
00147     }
00148   }
00149 }
00150 
00151 void QtOgreRenderWindow::showEvent( QShowEvent *event )
00152 {
00153   RenderWidget::showEvent( event );
00154 
00155   render_window_->setVisible(true);
00156   render_window_->setAutoUpdated(true);
00157 
00158   viewport_ = render_window_->addViewport( camera_ );
00159   viewport_->setOverlaysEnabled( overlays_enabled_ );
00160   viewport_->setBackgroundColour( background_color_ );
00161 
00162   setCameraAspectRatio();
00163 }
00164 
00166 //
00167 // bool QtOgreRenderWindow::Reparent(wxWindowBase* new_parent)
00168 // {
00169 //   // Dear god I hate GTK.  Because gtk_widget_reparent() does not work properly (https://netfiles.uiuc.edu/rvmorale/www/gtk-faq-es/x639.html),
00170 //   // When a window is reparented in wx the drawable client area has to be destroyed and re-created, causing its XID to change.  This causes both:
00171 //   // 1) Any window that was its child (like the Ogre render window's) is destroyed
00172 //   // 2) The XID changes
00173 //   // The following seems to be the only thing that actually works when trying to work around this:
00174 //   // 1) Reparent the render window's window to the root window
00175 //   // 2) Allow wx to reparent the window
00176 //   // 3) Reparent the render window's window to the new parent window
00177 // #if defined(__WXGTK__)
00178 //   Window win;
00179 //   Display* disp;
00180 // 
00181 //   if (render_window_)
00182 //   {
00183 //     render_window_->getCustomAttribute("WINDOW", &win);
00184 //     render_window_->getCustomAttribute("XDISPLAY", &disp);
00185 // 
00186 //     wxWindow* top = wxTheApp->GetTopWindow();
00187 //     GtkPizza* pizza = GTK_PIZZA(top->m_wxwindow);
00188 //     Window parent = GDK_WINDOW_XWINDOW(pizza->bin_window);
00189 // 
00190 //     XSync(disp, False);
00191 //     XReparentWindow(disp, win, parent, 0, 0);
00192 //     XSync(disp, False);
00193 //   }
00194 // #endif
00195 // 
00196 //   bool ret = wxControl::Reparent(new_parent);
00197 // 
00198 // #if defined(__WXGTK__)
00199 //   if (render_window_)
00200 //   {
00201 //     XSync(disp, False);
00202 // 
00203 //     gtk_widget_set_double_buffered(m_wxwindow, FALSE);
00204 //     if (!GTK_WIDGET_REALIZED(m_wxwindow))
00205 //     {
00206 //       gtk_widget_realize(m_wxwindow);
00207 //     }
00208 // 
00209 //     GtkPizza* pizza = GTK_PIZZA(m_wxwindow);
00210 // 
00211 //     XSync(GDK_WINDOW_XDISPLAY(m_wxwindow->window), False);
00212 //     XSync(GDK_WINDOW_XDISPLAY(pizza->bin_window), False);
00213 // 
00214 //     GdkWindow* gdkWin = pizza->bin_window;
00215 //     Window parent = GDK_WINDOW_XWINDOW(gdkWin);
00216 //     XReparentWindow(disp, win, parent, 0, 0);
00217 // 
00218 // //    XSync(disp, False);
00219 // 
00220 //     XMapWindow(disp, win);
00221 // 
00222 //     XSync(disp, False);
00223 //   }
00224 // #endif
00225 // 
00226 //   return ret;
00227 // }
00228 
00229 } // namespace ogre_tools


ogre_tools_qt
Author(s): Josh Faust
autogenerated on Fri Dec 6 2013 20:56:42