image_view.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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 
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 }


rviz
Author(s): Dave Hershberger, Josh Faust
autogenerated on Mon Jan 6 2014 11:54:32