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 "shape.h"
00031 #include <ros/assert.h>
00032
00033 #include <OgreSceneManager.h>
00034 #include <OgreSceneNode.h>
00035 #include <OgreVector3.h>
00036 #include <OgreQuaternion.h>
00037 #include <OgreEntity.h>
00038 #include <OgreMaterialManager.h>
00039 #include <OgreTextureManager.h>
00040 #include <OgreTechnique.h>
00041 #include <stdint.h>
00042
00043 namespace rviz
00044 {
00045
00046 Ogre::Entity* Shape::createEntity(const std::string& name, Type type, Ogre::SceneManager* scene_manager)
00047 {
00048 if (type == Mesh)
00049 return NULL;
00050
00051 std::string mesh_name;
00052 switch (type)
00053 {
00054 case Cone:
00055 mesh_name = "rviz_cone.mesh";
00056 break;
00057
00058 case Cube:
00059 mesh_name = "rviz_cube.mesh";
00060 break;
00061
00062 case Cylinder:
00063 mesh_name = "rviz_cylinder.mesh";
00064 break;
00065
00066 case Sphere:
00067 mesh_name = "rviz_sphere.mesh";
00068 break;
00069
00070 default:
00071 ROS_BREAK();
00072 }
00073
00074 return scene_manager->createEntity(name, mesh_name);
00075 }
00076
00077 Shape::Shape( Type type, Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node )
00078 : Object( scene_manager )
00079 , type_(type)
00080 {
00081 static uint32_t count = 0;
00082 std::stringstream ss;
00083 ss << "Shape" << count++;
00084
00085 entity_ = createEntity(ss.str(), type, scene_manager);
00086
00087 if ( !parent_node )
00088 {
00089 parent_node = scene_manager_->getRootSceneNode();
00090 }
00091
00092 scene_node_ = parent_node->createChildSceneNode();
00093 offset_node_ = scene_node_->createChildSceneNode();
00094 if (entity_)
00095 offset_node_->attachObject( entity_ );
00096
00097 ss << "Material";
00098 material_name_ = ss.str();
00099 material_ = Ogre::MaterialManager::getSingleton().create( material_name_, ROS_PACKAGE_NAME );
00100 material_->setReceiveShadows(false);
00101 material_->getTechnique(0)->setLightingEnabled(true);
00102 material_->getTechnique(0)->setAmbient( 0.5, 0.5, 0.5 );
00103
00104 if (entity_)
00105 entity_->setMaterialName(material_name_);
00106
00107 #if (OGRE_VERSION_MAJOR <= 1 && OGRE_VERSION_MINOR <= 4)
00108 if (entity_)
00109 entity_->setNormaliseNormals(true);
00110 #endif
00111 }
00112
00113 Shape::~Shape()
00114 {
00115 scene_manager_->destroySceneNode( scene_node_->getName() );
00116 scene_manager_->destroySceneNode( offset_node_->getName() );
00117
00118 if (entity_)
00119 scene_manager_->destroyEntity( entity_ );
00120
00121 material_->unload();
00122 Ogre::MaterialManager::getSingleton().remove(material_->getName());
00123 }
00124
00125 void Shape::setColor(const Ogre::ColourValue& c)
00126 {
00127 material_->getTechnique(0)->setAmbient( c * 0.5 );
00128 material_->getTechnique(0)->setDiffuse( c );
00129
00130 if ( c.a < 0.9998 )
00131 {
00132 material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00133 material_->getTechnique(0)->setDepthWriteEnabled( false );
00134 }
00135 else
00136 {
00137 material_->getTechnique(0)->setSceneBlending( Ogre::SBT_REPLACE );
00138 material_->getTechnique(0)->setDepthWriteEnabled( true );
00139 }
00140 }
00141
00142 void Shape::setColor( float r, float g, float b, float a )
00143 {
00144 setColor(Ogre::ColourValue(r, g, b, a));
00145 }
00146
00147 void Shape::setOffset( const Ogre::Vector3& offset )
00148 {
00149 offset_node_->setPosition( offset );
00150 }
00151
00152 void Shape::setPosition( const Ogre::Vector3& position )
00153 {
00154 scene_node_->setPosition( position );
00155 }
00156
00157 void Shape::setOrientation( const Ogre::Quaternion& orientation )
00158 {
00159 scene_node_->setOrientation( orientation );
00160 }
00161
00162 void Shape::setScale( const Ogre::Vector3& scale )
00163 {
00164 scene_node_->setScale( scale );
00165 }
00166
00167 const Ogre::Vector3& Shape::getPosition()
00168 {
00169 return scene_node_->getPosition();
00170 }
00171
00172 const Ogre::Quaternion& Shape::getOrientation()
00173 {
00174 return scene_node_->getOrientation();
00175 }
00176
00177 void Shape::setUserData( const Ogre::Any& data )
00178 {
00179 if (entity_)
00180 entity_->setUserAny( data );
00181 else
00182 ROS_ERROR("Shape not yet fully constructed. Cannot set user data. Did you add triangles to the mesh already?");
00183 }
00184
00185 }
00186