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 <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; // the entity is initialized after the vertex data was specified
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 } // namespace rviz
00186 


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28