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/visualization_manager.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, VisualizationManager* manager, Ogre::SceneNode* parent_node)
00051 : MarkerBase(owner, manager, parent_node)
00052 , entity_(0)
00053 {
00054 if (parent_node)
00055 {
00056 scene_node_ = parent_node->createChildSceneNode();
00057 }
00058 else
00059 {
00060 scene_node_ = vis_manager_->getSceneManager()->getRootSceneNode()->createChildSceneNode();
00061 }
00062 }
00063
00064 MeshResourceMarker::~MeshResourceMarker()
00065 {
00066 vis_manager_->getSceneManager()->destroySceneNode(scene_node_->getName());
00067
00068 if (entity_)
00069 {
00070 vis_manager_->getSceneManager()->destroyEntity( entity_ );
00071 }
00072
00073 if (!material_.isNull())
00074 {
00075 for (size_t i = 0; i < material_->getNumTechniques(); ++i)
00076 {
00077 Ogre::Technique* t = material_->getTechnique(i);
00078
00079
00080 if (t->getSchemeName() == "Pick")
00081 {
00082 Ogre::TextureManager::getSingleton().remove(t->getPass(0)->getTextureUnitState(0)->getTextureName());
00083 }
00084 }
00085
00086 material_->unload();
00087 Ogre::MaterialManager::getSingleton().remove(material_->getName());
00088 }
00089 }
00090
00091 void MeshResourceMarker::onNewMessage(const MarkerConstPtr& old_message, const MarkerConstPtr& new_message)
00092 {
00093 ROS_ASSERT(new_message->type == visualization_msgs::Marker::MESH_RESOURCE);
00094
00095 scene_node_->setVisible(false);
00096
00097 if (!entity_ || old_message->mesh_resource != new_message->mesh_resource)
00098 {
00099 if (entity_)
00100 {
00101 vis_manager_->getSceneManager()->destroyEntity(entity_);
00102 entity_ = 0;
00103 }
00104
00105 if (new_message->mesh_resource.empty())
00106 {
00107 return;
00108 }
00109
00110 if (loadMeshFromResource(new_message->mesh_resource).isNull())
00111 {
00112 std::stringstream ss;
00113 ss << "Mesh resource marker [" << getStringID() << "] could not load [" << new_message->mesh_resource << "]";
00114 owner_->setMarkerStatus(getID(), status_levels::Error, ss.str());
00115 ROS_DEBUG("%s", ss.str().c_str());
00116 return;
00117 }
00118
00119 static uint32_t count = 0;
00120 std::stringstream ss;
00121 ss << "Mesh Resource Marker" << count++;
00122 entity_ = vis_manager_->getSceneManager()->createEntity(ss.str(), new_message->mesh_resource);
00123 scene_node_->attachObject(entity_);
00124
00125 if (material_.isNull())
00126 {
00127 ss << "Material";
00128 material_name_ = ss.str();
00129 material_ = Ogre::MaterialManager::getSingleton().create( material_name_, ROS_PACKAGE_NAME );
00130 material_->setReceiveShadows(false);
00131 material_->getTechnique(0)->setLightingEnabled(true);
00132 material_->getTechnique(0)->setAmbient( 0.5, 0.5, 0.5 );
00133 }
00134
00135 original_material_names_.clear();
00136 for (uint32_t i = 0; i < entity_->getNumSubEntities(); ++i)
00137 {
00138 std::string name = entity_->getSubEntity(i)->getMaterialName();
00139 ss << name;
00140 if (!Ogre::MaterialManager::getSingleton().resourceExists(ss.str()))
00141 {
00142 Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().getByName(name);
00143 mat->clone(ss.str());
00144 }
00145
00146 entity_->getSubEntity(i)->setMaterialName(ss.str());
00147 original_material_names_.push_back(ss.str());
00148 }
00149
00150 coll_ = vis_manager_->getSelectionManager()->createCollisionForEntity(entity_, SelectionHandlerPtr(new MarkerSelectionHandler(this, MarkerID(new_message->ns, new_message->id))), coll_);
00151 }
00152
00153 Ogre::Vector3 pos, scale;
00154 Ogre::Quaternion orient;
00155 transform(new_message, pos, orient, scale, false);
00156
00157 scene_node_->setVisible(true);
00158 scene_node_->setPosition(pos);
00159 scene_node_->setOrientation(orient);
00160 scene_node_->setScale(scale);
00161
00162 if (new_message->mesh_use_embedded_materials)
00163 {
00164 for (uint32_t i = 0; i < entity_->getNumSubEntities(); ++i)
00165 {
00166 entity_->getSubEntity(i)->setMaterialName(original_material_names_[i]);
00167 }
00168 }
00169 else
00170 {
00171 entity_->setMaterialName(material_name_);
00172 }
00173
00174 float r = new_message->color.r;
00175 float g = new_message->color.g;
00176 float b = new_message->color.b;
00177 float a = new_message->color.a;
00178 material_->getTechnique(0)->setAmbient( r*0.5, g*0.5, b*0.5 );
00179 material_->getTechnique(0)->setDiffuse( r, g, b, a );
00180
00181 if ( a < 0.9998 )
00182 {
00183 material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00184 material_->getTechnique(0)->setDepthWriteEnabled( false );
00185 }
00186 else
00187 {
00188 material_->getTechnique(0)->setSceneBlending( Ogre::SBT_REPLACE );
00189 material_->getTechnique(0)->setDepthWriteEnabled( true );
00190 }
00191 }
00192
00193 }
00194