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
00054 ImageView::ImageView( QWidget* parent )
00055 : QtOgreRenderWindow( parent )
00056 , texture_it_(nh_)
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();
00088
00089 try
00090 {
00091 texture_->clear();
00092
00093 texture_sub_.reset(new image_transport::SubscriberFilter());
00094 texture_sub_->subscribe(texture_it_, "image", 1, image_transport::TransportHints("raw"));
00095 texture_sub_->registerCallback(boost::bind(&ImageView::textureCallback, this, _1));
00096 }
00097 catch (ros::Exception& e)
00098 {
00099 ROS_ERROR("%s", (std::string("Error subscribing: ") + e.what()).c_str());
00100 }
00101
00102 Ogre::MaterialPtr material =
00103 Ogre::MaterialManager::getSingleton().create( "Material",
00104 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
00105 material->setCullingMode(Ogre::CULL_NONE);
00106 material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(true);
00107 material->getTechnique(0)->setLightingEnabled(false);
00108 Ogre::TextureUnitState* tu = material->getTechnique(0)->getPass(0)->createTextureUnitState();
00109 tu->setTextureName(texture_->getTexture()->getName());
00110 tu->setTextureFiltering( Ogre::TFO_NONE );
00111
00112 Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true);
00113 rect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
00114 rect->setMaterial(material->getName());
00115 rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
00116 Ogre::AxisAlignedBox aabb;
00117 aabb.setInfinite();
00118 rect->setBoundingBox(aabb);
00119
00120 Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode();
00121 node->attachObject(rect);
00122 node->setVisible(true);
00123
00124 QTimer* timer = new QTimer( this );
00125 connect( timer, SIGNAL( timeout() ), this, SLOT( onTimer() ));
00126 timer->start( 33 );
00127 }
00128
00129 void ImageView::onTimer()
00130 {
00131 ros::spinOnce();
00132
00133 static bool first = true;
00134 try
00135 {
00136 if( texture_->update() )
00137 {
00138 if( first )
00139 {
00140 first = false;
00141
00142 resize( texture_->getWidth(), texture_->getHeight() );
00143 }
00144 }
00145
00146 ogre_root_->renderOneFrame();
00147 }
00148 catch( UnsupportedImageEncoding& e )
00149 {
00150 ROS_ERROR("%s", e.what());
00151 }
00152
00153 if( !nh_.ok() )
00154 {
00155 close();
00156 }
00157 }
00158
00159 void ImageView::textureCallback(const sensor_msgs::Image::ConstPtr& msg)
00160 {
00161 if (texture_) {
00162 texture_->addMessage(msg);
00163 }
00164 }