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 "mesh_shape.h"
00031
00032 #include <OgreMeshManager.h>
00033 #include <OgreSceneManager.h>
00034 #include <OgreSceneNode.h>
00035 #include <OgreEntity.h>
00036 #include <OgreMaterialManager.h>
00037 #include <OgreManualObject.h>
00038
00039 #include <ros/console.h>
00040 #include <boost/lexical_cast.hpp>
00041
00042 namespace rviz
00043 {
00044
00045 MeshShape::MeshShape(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node )
00046 : Shape( Shape::Mesh, scene_manager, parent_node )
00047 , started_(false)
00048 {
00049 static uint32_t count = 0;
00050 manual_object_ = scene_manager->createManualObject("MeshShape_ManualObject" + boost::lexical_cast<std::string>(count++));
00051 material_->setCullingMode(Ogre::CULL_NONE);
00052 }
00053
00054 MeshShape::~MeshShape()
00055 {
00056 clear();
00057 scene_manager_->destroyManualObject(manual_object_);
00058 }
00059
00060 void MeshShape::estimateVertexCount(size_t vcount)
00061 {
00062 if (entity_ == NULL && started_ == false)
00063 manual_object_->estimateVertexCount(vcount);
00064 }
00065
00066 void MeshShape::beginTriangles()
00067 {
00068 if (!started_ && entity_)
00069 {
00070 ROS_WARN("Cannot modify mesh once construction is complete");
00071 return;
00072 }
00073
00074 if (!started_)
00075 {
00076 started_ = true;
00077 manual_object_->begin(material_name_, Ogre::RenderOperation::OT_TRIANGLE_LIST);
00078 }
00079 }
00080
00081 void MeshShape::addVertex(const Ogre::Vector3& position)
00082 {
00083 beginTriangles();
00084 manual_object_->position(position);
00085 }
00086
00087 void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal)
00088 {
00089 beginTriangles();
00090 manual_object_->position(position);
00091 manual_object_->normal(normal);
00092 }
00093
00094 void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal, const Ogre::ColourValue &color)
00095 {
00096 beginTriangles();
00097 manual_object_->position(position);
00098 manual_object_->normal(normal);
00099 manual_object_->colour(color);
00100 }
00101
00102 void MeshShape::addNormal(const Ogre::Vector3& normal)
00103 {
00104 manual_object_->normal(normal);
00105 }
00106
00107 void MeshShape::addColor(const Ogre::ColourValue &color)
00108 {
00109 manual_object_->colour(color);
00110 }
00111
00112 void MeshShape::addTriangle(unsigned int v1, unsigned int v2, unsigned int v3)
00113 {
00114 manual_object_->triangle(v1, v2, v3);
00115 }
00116
00117 void MeshShape::endTriangles()
00118 {
00119 if (started_)
00120 {
00121 started_ = false;
00122 manual_object_->end();
00123 static uint32_t count = 0;
00124 std::string name = "ConvertedMeshShape@" + boost::lexical_cast<std::string>(count++);
00125 manual_object_->convertToMesh(name);
00126 entity_ = scene_manager_->createEntity(name);
00127 if (entity_)
00128 {
00129 entity_->setMaterialName(material_name_);
00130 offset_node_->attachObject(entity_);
00131 }
00132 else
00133 ROS_ERROR("Unable to construct triangle mesh");
00134 }
00135 else
00136 ROS_ERROR("No triangles added");
00137 }
00138
00139 void MeshShape::clear()
00140 {
00141 if (entity_)
00142 {
00143 entity_->detachFromParent();
00144 Ogre::MeshManager::getSingleton().remove(entity_->getMesh()->getName());
00145 scene_manager_->destroyEntity( entity_ );
00146 entity_ = NULL;
00147 }
00148 manual_object_->clear();
00149 started_ = false;
00150 }
00151
00152 }
00153