shape.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "shape.h"
31 #include <ros/assert.h>
32 
33 #include <OgreSceneManager.h>
34 #include <OgreSceneNode.h>
35 #include <OgreVector3.h>
36 #include <OgreQuaternion.h>
37 #include <OgreEntity.h>
38 #include <OgreMaterialManager.h>
39 #include <OgreTextureManager.h>
40 #include <OgreTechnique.h>
41 #include <stdint.h>
42 
43 namespace rviz
44 {
45 
46 Ogre::Entity* Shape::createEntity(const std::string& name, Type type, Ogre::SceneManager* scene_manager)
47 {
48  if (type == Mesh)
49  return NULL; // the entity is initialized after the vertex data was specified
50 
51  std::string mesh_name;
52  switch (type)
53  {
54  case Cone:
55  mesh_name = "rviz_cone.mesh";
56  break;
57 
58  case Cube:
59  mesh_name = "rviz_cube.mesh";
60  break;
61 
62  case Cylinder:
63  mesh_name = "rviz_cylinder.mesh";
64  break;
65 
66  case Sphere:
67  mesh_name = "rviz_sphere.mesh";
68  break;
69 
70  default:
71  ROS_BREAK();
72  }
73 
74  return scene_manager->createEntity(name, mesh_name);
75 }
76 
77 Shape::Shape( Type type, Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node )
78 : Object( scene_manager )
79 , type_(type)
80 {
81  static uint32_t count = 0;
82  std::stringstream ss;
83  ss << "Shape" << count++;
84 
85  entity_ = createEntity(ss.str(), type, scene_manager);
86 
87  if ( !parent_node )
88  {
89  parent_node = scene_manager_->getRootSceneNode();
90  }
91 
92  scene_node_ = parent_node->createChildSceneNode();
93  offset_node_ = scene_node_->createChildSceneNode();
94  if (entity_)
95  offset_node_->attachObject( entity_ );
96 
97  ss << "Material";
98  material_name_ = ss.str();
99  material_ = Ogre::MaterialManager::getSingleton().create( material_name_, ROS_PACKAGE_NAME );
100  material_->setReceiveShadows(false);
101  material_->getTechnique(0)->setLightingEnabled(true);
102  material_->getTechnique(0)->setAmbient( 0.5, 0.5, 0.5 );
103 
104  if (entity_)
105  entity_->setMaterialName(material_name_);
106 
107 #if (OGRE_VERSION_MAJOR <= 1 && OGRE_VERSION_MINOR <= 4)
108  if (entity_)
109  entity_->setNormaliseNormals(true);
110 #endif
111 }
112 
114 {
115  scene_manager_->destroySceneNode( scene_node_->getName() );
116  scene_manager_->destroySceneNode( offset_node_->getName() );
117 
118  if (entity_)
119  scene_manager_->destroyEntity( entity_ );
120 
121  material_->unload();
122  Ogre::MaterialManager::getSingleton().remove(material_->getName());
123 }
124 
125 void Shape::setColor(const Ogre::ColourValue& c)
126 {
127  material_->getTechnique(0)->setAmbient( c * 0.5 );
128  material_->getTechnique(0)->setDiffuse( c );
129 
130  if ( c.a < 0.9998 )
131  {
132  material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
133  material_->getTechnique(0)->setDepthWriteEnabled( false );
134  }
135  else
136  {
137  material_->getTechnique(0)->setSceneBlending( Ogre::SBT_REPLACE );
138  material_->getTechnique(0)->setDepthWriteEnabled( true );
139  }
140 }
141 
142 void Shape::setColor( float r, float g, float b, float a )
143 {
144  setColor(Ogre::ColourValue(r, g, b, a));
145 }
146 
147 void Shape::setOffset( const Ogre::Vector3& offset )
148 {
149  offset_node_->setPosition( offset );
150 }
151 
152 void Shape::setPosition( const Ogre::Vector3& position )
153 {
154  scene_node_->setPosition( position );
155 }
156 
157 void Shape::setOrientation( const Ogre::Quaternion& orientation )
158 {
159  scene_node_->setOrientation( orientation );
160 }
161 
162 void Shape::setScale( const Ogre::Vector3& scale )
163 {
164  scene_node_->setScale( scale );
165 }
166 
167 const Ogre::Vector3& Shape::getPosition()
168 {
169  return scene_node_->getPosition();
170 }
171 
172 const Ogre::Quaternion& Shape::getOrientation()
173 {
174  return scene_node_->getOrientation();
175 }
176 
177 void Shape::setUserData( const Ogre::Any& data )
178 {
179  if (entity_)
180  entity_->getUserObjectBindings().setUserAny( data );
181  else
182  ROS_ERROR("Shape not yet fully constructed. Cannot set user data. Did you add triangles to the mesh already?");
183 }
184 
185 } // namespace rviz
186 
virtual void setOrientation(const Ogre::Quaternion &orientation)
Set the orientation of the object.
Definition: shape.cpp:157
#define NULL
Definition: global.h:37
virtual const Ogre::Vector3 & getPosition()
Get the local position of this object.
Definition: shape.cpp:167
Ogre::SceneNode * offset_node_
Definition: shape.h:112
static Ogre::Entity * createEntity(const std::string &name, Type shape_type, Ogre::SceneManager *scene_manager)
Definition: shape.cpp:46
Ogre::Entity * entity_
Definition: shape.h:113
virtual const Ogre::Quaternion & getOrientation()
Get the local orientation of this object.
Definition: shape.cpp:172
void setUserData(const Ogre::Any &data)
Sets user data on all ogre objects we own.
Definition: shape.cpp:177
std::string material_name_
Definition: shape.h:115
Ogre::MaterialPtr material_
Definition: shape.h:114
Shape(Type shape_type, Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node=NULL)
Constructor.
Definition: shape.cpp:77
virtual void setColor(float r, float g, float b, float a)
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:142
Base class for visible objects, providing a minimal generic interface.
Definition: object.h:49
Ogre::SceneNode * scene_node_
Definition: shape.h:111
virtual void setPosition(const Ogre::Vector3 &position)
Set the position of this object.
Definition: shape.cpp:152
virtual ~Shape()
Definition: shape.cpp:113
Type type_
Definition: shape.h:117
Ogre::SceneManager * scene_manager_
Ogre scene manager this object is part of.
Definition: object.h:103
void setOffset(const Ogre::Vector3 &offset)
Set the offset for this shape.
Definition: shape.cpp:147
#define ROS_BREAK()
#define ROS_ERROR(...)
virtual void setScale(const Ogre::Vector3 &scale)
Set the scale of the object. Always relative to the identity orientation of the object.
Definition: shape.cpp:162


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51