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 #include "mesh_resource_marker.h"
00031
00032 #include "marker_selection_handler.h"
00033 #include "rviz/default_plugin/marker_display.h"
00034 #include "rviz/selection/selection_manager.h"
00035
00036 #include "rviz/display_context.h"
00037 #include "rviz/mesh_loader.h"
00038 #include "marker_display.h"
00039
00040 #include <OGRE/OgreSceneNode.h>
00041 #include <OGRE/OgreSceneManager.h>
00042 #include <OGRE/OgreEntity.h>
00043 #include <OGRE/OgreSubEntity.h>
00044 #include <OGRE/OgreMaterialManager.h>
00045 #include <OGRE/OgreTextureManager.h>
00046
00047 namespace rviz
00048 {
00049
00050 MeshResourceMarker::MeshResourceMarker(MarkerDisplay* owner, DisplayContext* context, Ogre::SceneNode* parent_node)
00051 : MarkerBase(owner, context, parent_node)
00052 , entity_(0)
00053 {
00054 }
00055
00056 MeshResourceMarker::~MeshResourceMarker()
00057 {
00058 reset();
00059 }
00060
00061 void MeshResourceMarker::reset()
00062 {
00063
00064 if (entity_)
00065 {
00066 context_->getSceneManager()->destroyEntity( entity_ );
00067 entity_ = 0;
00068 }
00069
00070
00071 S_MaterialPtr::iterator it;
00072 for ( it = materials_.begin(); it!=materials_.end(); it++ )
00073 {
00074 Ogre::MaterialPtr material = *it;
00075 if (!material.isNull())
00076 {
00077 material->unload();
00078 Ogre::MaterialManager::getSingleton().remove(material->getName());
00079 }
00080 }
00081 materials_.clear();
00082 }
00083
00084 void MeshResourceMarker::onNewMessage(const MarkerConstPtr& old_message, const MarkerConstPtr& new_message)
00085 {
00086 ROS_ASSERT(new_message->type == visualization_msgs::Marker::MESH_RESOURCE);
00087
00088 bool need_color = false;
00089
00090 scene_node_->setVisible(false);
00091
00092 if( !entity_ ||
00093 old_message->mesh_resource != new_message->mesh_resource ||
00094 old_message->mesh_use_embedded_materials != new_message->mesh_use_embedded_materials )
00095 {
00096 reset();
00097
00098 if (new_message->mesh_resource.empty())
00099 {
00100 return;
00101 }
00102
00103 if (loadMeshFromResource(new_message->mesh_resource).isNull())
00104 {
00105 std::stringstream ss;
00106 ss << "Mesh resource marker [" << getStringID() << "] could not load [" << new_message->mesh_resource << "]";
00107 if ( owner_ )
00108 {
00109 owner_->setMarkerStatus(getID(), StatusProperty::Error, ss.str());
00110 }
00111 ROS_DEBUG("%s", ss.str().c_str());
00112 return;
00113 }
00114
00115 static uint32_t count = 0;
00116 std::stringstream ss;
00117 ss << "mesh_resource_marker_" << count++;
00118 std::string id = ss.str();
00119 entity_ = context_->getSceneManager()->createEntity(id, new_message->mesh_resource);
00120 scene_node_->attachObject(entity_);
00121 need_color = true;
00122
00123
00124 ss << "Material";
00125 Ogre::MaterialPtr default_material = Ogre::MaterialManager::getSingleton().create( ss.str(), ROS_PACKAGE_NAME );
00126 default_material->setReceiveShadows(false);
00127 default_material->getTechnique(0)->setLightingEnabled(true);
00128 default_material->getTechnique(0)->setAmbient( 0.5, 0.5, 0.5 );
00129 materials_.insert( default_material );
00130
00131 if ( new_message->mesh_use_embedded_materials )
00132 {
00133
00134 S_MaterialPtr materials = getMaterials();
00135
00136 S_MaterialPtr::iterator it;
00137 for ( it = materials.begin(); it!=materials.end(); it++ )
00138 {
00139 if( (*it)->getName() != "BaseWhiteNoLighting" )
00140 {
00141 Ogre::MaterialPtr new_material = (*it)->clone( id + (*it)->getName() );
00142 materials_.insert( new_material );
00143 }
00144 }
00145
00146
00147 for (uint32_t i = 0; i < entity_->getNumSubEntities(); ++i)
00148 {
00149 std::string mat_name = entity_->getSubEntity(i)->getMaterialName();
00150 if( mat_name != "BaseWhiteNoLighting" )
00151 {
00152 entity_->getSubEntity(i)->setMaterialName( id + mat_name );
00153 }
00154 else
00155 {
00156
00157
00158
00159
00160 entity_->getSubEntity(i)->setMaterial( default_material );
00161 }
00162 }
00163 }
00164 else
00165 {
00166 entity_->setMaterial( default_material );
00167 }
00168
00169 handler_.reset( new MarkerSelectionHandler( this, MarkerID( new_message->ns, new_message->id ), context_ ));
00170 handler_->addTrackedObject( entity_ );
00171 }
00172
00173 if( need_color ||
00174 old_message->color.r != new_message->color.r ||
00175 old_message->color.g != new_message->color.g ||
00176 old_message->color.b != new_message->color.b ||
00177 old_message->color.a != new_message->color.a )
00178 {
00179 float r = new_message->color.r;
00180 float g = new_message->color.g;
00181 float b = new_message->color.b;
00182 float a = new_message->color.a;
00183
00184
00185
00186
00187
00188
00189
00190 if( new_message->mesh_use_embedded_materials && r == 0 && g == 0 && b == 0 && a == 0 )
00191 {
00192 r = 1; g = 1; b = 1; a = 1;
00193 }
00194
00195 Ogre::SceneBlendType blending;
00196 bool depth_write;
00197
00198 if ( a < 0.9998 )
00199 {
00200 blending = Ogre::SBT_TRANSPARENT_ALPHA;
00201 depth_write = false;
00202 }
00203 else
00204 {
00205 blending = Ogre::SBT_REPLACE;
00206 depth_write = true;
00207 }
00208
00209 S_MaterialPtr::iterator it;
00210 for( it = materials_.begin(); it != materials_.end(); it++ )
00211 {
00212 Ogre::Technique* technique = (*it)->getTechnique( 0 );
00213
00214 technique->setAmbient( r*0.5, g*0.5, b*0.5 );
00215 technique->setDiffuse( r, g, b, a );
00216 technique->setSceneBlending( blending );
00217 technique->setDepthWriteEnabled( depth_write );
00218 technique->setLightingEnabled( true );
00219 }
00220 }
00221
00222 Ogre::Vector3 pos, scale;
00223 Ogre::Quaternion orient;
00224 transform(new_message, pos, orient, scale);
00225
00226 scene_node_->setVisible(true);
00227 setPosition(pos);
00228 setOrientation(orient);
00229
00230 scene_node_->setScale(scale);
00231 }
00232
00233 S_MaterialPtr MeshResourceMarker::getMaterials()
00234 {
00235 S_MaterialPtr materials;
00236 if( entity_ )
00237 {
00238 extractMaterials( entity_, materials );
00239 }
00240 return materials;
00241 }
00242
00243 }
00244