shape.cpp
Go to the documentation of this file.
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 #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     // hack hack hack, really need to do a shader-based way of picking, rather than
00117     // creating a texture for each object
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 } // namespace ogre_tools
00185 


ogre_tools_qt
Author(s): Josh Faust
autogenerated on Fri Dec 6 2013 20:56:42