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 rviz
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 = "rviz_cone.mesh";
00052     break;
00053 
00054   case Cube:
00055     mesh_name = "rviz_cube.mesh";
00056     break;
00057 
00058   case Cylinder:
00059     mesh_name = "rviz_cylinder.mesh";
00060     break;
00061 
00062   case Sphere:
00063     mesh_name = "rviz_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 << "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   material_->unload();
00114   Ogre::MaterialManager::getSingleton().remove(material_->getName());
00115 }
00116 
00117 void Shape::setColor(const Ogre::ColourValue& c)
00118 {
00119   material_->getTechnique(0)->setAmbient( c * 0.5 );
00120   material_->getTechnique(0)->setDiffuse( c );
00121 
00122   if ( c.a < 0.9998 )
00123   {
00124     material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00125     material_->getTechnique(0)->setDepthWriteEnabled( false );
00126   }
00127   else
00128   {
00129     material_->getTechnique(0)->setSceneBlending( Ogre::SBT_REPLACE );
00130     material_->getTechnique(0)->setDepthWriteEnabled( true );
00131   }
00132 }
00133 
00134 void Shape::setColor( float r, float g, float b, float a )
00135 {
00136   setColor(Ogre::ColourValue(r, g, b, a));
00137 }
00138 
00139 void Shape::setOffset( const Ogre::Vector3& offset )
00140 {
00141   offset_node_->setPosition( offset );
00142 }
00143 
00144 void Shape::setPosition( const Ogre::Vector3& position )
00145 {
00146   scene_node_->setPosition( position );
00147 }
00148 
00149 void Shape::setOrientation( const Ogre::Quaternion& orientation )
00150 {
00151   scene_node_->setOrientation( orientation );
00152 }
00153 
00154 void Shape::setScale( const Ogre::Vector3& scale )
00155 {
00156   scene_node_->setScale( scale );
00157 }
00158 
00159 const Ogre::Vector3& Shape::getPosition()
00160 {
00161   return scene_node_->getPosition();
00162 }
00163 
00164 const Ogre::Quaternion& Shape::getOrientation()
00165 {
00166   return scene_node_->getOrientation();
00167 }
00168 
00169 void Shape::setUserData( const Ogre::Any& data )
00170 {
00171   entity_->setUserAny( data );
00172 }
00173 
00174 } // namespace rviz
00175 


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:36