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