$search
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 <wx/wx.h> 00032 #include <wx/timer.h> 00033 00034 #include "ogre_tools/wx_ogre_render_window.h" 00035 #include "ogre_tools/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/OgreSceneManager.h> 00043 #include <OGRE/OgreViewport.h> 00044 #include <OGRE/OgreRectangle2D.h> 00045 #include <OGRE/OgreMaterial.h> 00046 #include <OGRE/OgreMaterialManager.h> 00047 #include <OGRE/OgreTextureUnitState.h> 00048 00049 #ifdef __WXMAC__ 00050 #include <ApplicationServices/ApplicationServices.h> 00051 #endif 00052 00053 using namespace ogre_tools; 00054 using namespace rviz; 00055 00056 class MyFrame : public wxFrame 00057 { 00058 public: 00059 MyFrame(wxWindow* parent) 00060 : wxFrame(parent, wxID_ANY, wxT("rviz Image Viewer"), wxDefaultPosition, wxSize(800,600), wxDEFAULT_FRAME_STYLE) 00061 , timer_(this) 00062 { 00063 ogre_tools::initializeOgre(); 00064 00065 root_ = Ogre::Root::getSingletonPtr(); 00066 00067 try 00068 { 00069 scene_manager_ = root_->createSceneManager( Ogre::ST_GENERIC, "TestSceneManager" ); 00070 00071 render_window_ = new ogre_tools::wxOgreRenderWindow( root_, this ); 00072 render_window_->setAutoRender(false); 00073 render_window_->SetSize( this->GetSize() ); 00074 00075 ogre_tools::V_string paths; 00076 paths.push_back(ros::package::getPath(ROS_PACKAGE_NAME) + "/ogre_media/textures"); 00077 ogre_tools::initializeResources(paths); 00078 00079 camera_ = scene_manager_->createCamera("Camera"); 00080 00081 render_window_->getViewport()->setCamera( camera_ ); 00082 00083 std::string resolved_image = nh_.resolveName("image"); 00084 if (resolved_image == "/image") 00085 { 00086 ROS_WARN("image topic has not been remapped"); 00087 } 00088 00089 std::stringstream title; 00090 title << "rviz Image Viewer [" << resolved_image << "]"; 00091 SetTitle(wxString::FromAscii(title.str().c_str())); 00092 00093 texture_ = new ROSImageTexture(nh_); 00094 texture_->setTopic("image"); 00095 00096 Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create( "Material", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME ); 00097 material->setCullingMode(Ogre::CULL_NONE); 00098 material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(true); 00099 material->getTechnique(0)->setLightingEnabled(false); 00100 Ogre::TextureUnitState* tu = material->getTechnique(0)->getPass(0)->createTextureUnitState(); 00101 tu->setTextureName(texture_->getTexture()->getName()); 00102 tu->setTextureFiltering( Ogre::TFO_NONE ); 00103 00104 Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true); 00105 rect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f); 00106 rect->setMaterial(material->getName()); 00107 rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1); 00108 Ogre::AxisAlignedBox aabb; 00109 aabb.setInfinite(); 00110 rect->setBoundingBox(aabb); 00111 00112 Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode(); 00113 node->attachObject(rect); 00114 node->setVisible(true); 00115 } 00116 catch ( Ogre::Exception& e ) 00117 { 00118 printf( "Fatal error: %s\n", e.what() ); 00119 exit(1); 00120 } 00121 00122 Connect(timer_.GetId(), wxEVT_TIMER, wxTimerEventHandler(MyFrame::onTimer), NULL, this); 00123 timer_.Start(33); 00124 00125 render_window_->Refresh(); 00126 } 00127 00128 void onTimer(wxTimerEvent&) 00129 { 00130 ros::spinOnce(); 00131 00132 static bool first = true; 00133 try 00134 { 00135 if (texture_->update()) 00136 { 00137 if (first) 00138 { 00139 first = false; 00140 00141 render_window_->SetSize(texture_->getWidth(), texture_->getHeight()); 00142 Fit(); 00143 } 00144 } 00145 00146 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 ~MyFrame() 00160 { 00161 delete texture_; 00162 render_window_->Destroy(); 00163 delete root_; 00164 } 00165 00166 private: 00167 00168 Ogre::Root* root_; 00169 Ogre::SceneManager* scene_manager_; 00170 ogre_tools::wxOgreRenderWindow* render_window_; 00171 Ogre::Camera* camera_; 00172 ROSImageTexture* texture_; 00173 wxTimer timer_; 00174 00175 ros::NodeHandle nh_; 00176 }; 00177 00178 // our normal wxApp-derived class, as usual 00179 class MyApp : public wxApp 00180 { 00181 public: 00182 00183 bool OnInit() 00184 { 00185 #ifdef __WXMAC__ 00186 ProcessSerialNumber PSN; 00187 GetCurrentProcess(&PSN); 00188 TransformProcessType(&PSN,kProcessTransformToForegroundApplication); 00189 SetFrontProcess(&PSN); 00190 #endif 00191 00192 // create our own copy of argv, with regular char*s. 00193 char** local_argv = new char*[ argc ]; 00194 for ( int i = 0; i < argc; ++i ) 00195 { 00196 local_argv[ i ] = strdup( wxString( argv[ i ] ).mb_str() ); 00197 } 00198 00199 ros::init(argc, local_argv, "rviz_image_view", ros::init_options::AnonymousName); 00200 00201 wxFrame* frame = new MyFrame(NULL); 00202 SetTopWindow(frame); 00203 frame->Show(); 00204 return true; 00205 } 00206 00207 int OnExit() 00208 { 00209 ogre_tools::cleanupOgre(); 00210 return 0; 00211 } 00212 }; 00213 00214 DECLARE_APP(MyApp); 00215 IMPLEMENT_APP(MyApp);