Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <QtGlobal>
00032 #include <QTimer>
00033
00034 #include "rviz/ogre_helpers/qt_ogre_render_window.h"
00035 #include "rviz/ogre_helpers/initialization.h"
00036 #include "rviz/image/ros_image_texture.h"
00037
00038 #include "ros/ros.h"
00039 #include <ros/package.h>
00040
00041 #include <OGRE/OgreRoot.h>
00042 #include <OGRE/OgreRenderWindow.h>
00043 #include <OGRE/OgreSceneManager.h>
00044 #include <OGRE/OgreViewport.h>
00045 #include <OGRE/OgreRectangle2D.h>
00046 #include <OGRE/OgreMaterial.h>
00047 #include <OGRE/OgreMaterialManager.h>
00048 #include <OGRE/OgreTextureUnitState.h>
00049
00050 #include "image_view.h"
00051
00052 using namespace rviz;
00053 using namespace rviz;
00054
00055 ImageView::ImageView( QWidget* parent )
00056 : QtOgreRenderWindow( parent )
00057 {
00058 setAutoRender(false);
00059 scene_manager_ = ogre_root_->createSceneManager( Ogre::ST_GENERIC, "TestSceneManager" );
00060 }
00061
00062 ImageView::~ImageView()
00063 {
00064 delete texture_;
00065 }
00066
00067 void ImageView::showEvent( QShowEvent* event )
00068 {
00069 QtOgreRenderWindow::showEvent( event );
00070
00071 V_string paths;
00072 paths.push_back(ros::package::getPath(ROS_PACKAGE_NAME) + "/ogre_media/textures");
00073 initializeResources(paths);
00074
00075 setCamera( scene_manager_->createCamera( "Camera" ));
00076
00077 std::string resolved_image = nh_.resolveName("image");
00078 if( resolved_image == "/image" )
00079 {
00080 ROS_WARN("image topic has not been remapped");
00081 }
00082
00083 std::stringstream title;
00084 title << "rviz Image Viewer [" << resolved_image << "]";
00085 setWindowTitle( QString::fromStdString( title.str() ));
00086
00087 texture_ = new ROSImageTexture(nh_);
00088 texture_->setTopic("image");
00089
00090 Ogre::MaterialPtr material =
00091 Ogre::MaterialManager::getSingleton().create( "Material",
00092 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
00093 material->setCullingMode(Ogre::CULL_NONE);
00094 material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(true);
00095 material->getTechnique(0)->setLightingEnabled(false);
00096 Ogre::TextureUnitState* tu = material->getTechnique(0)->getPass(0)->createTextureUnitState();
00097 tu->setTextureName(texture_->getTexture()->getName());
00098 tu->setTextureFiltering( Ogre::TFO_NONE );
00099
00100 Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true);
00101 rect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
00102 rect->setMaterial(material->getName());
00103 rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
00104 Ogre::AxisAlignedBox aabb;
00105 aabb.setInfinite();
00106 rect->setBoundingBox(aabb);
00107
00108 Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode();
00109 node->attachObject(rect);
00110 node->setVisible(true);
00111
00112 QTimer* timer = new QTimer( this );
00113 connect( timer, SIGNAL( timeout() ), this, SLOT( onTimer() ));
00114 timer->start( 33 );
00115 }
00116
00117 void ImageView::onTimer()
00118 {
00119 ros::spinOnce();
00120
00121 static bool first = true;
00122 try
00123 {
00124 if( texture_->update() )
00125 {
00126 if( first )
00127 {
00128 first = false;
00129
00130 resize( texture_->getWidth(), texture_->getHeight() );
00131 }
00132 }
00133
00134 ogre_root_->renderOneFrame();
00135 }
00136 catch( UnsupportedImageEncoding& e )
00137 {
00138 ROS_ERROR("%s", e.what());
00139 }
00140
00141 if( !nh_.ok() )
00142 {
00143 close();
00144 }
00145 }