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 Ogre::Entity* Shape::createEntity(const std::string& name, Type type, Ogre::SceneManager* scene_manager)
46 {
47  if (type == Mesh)
48  return nullptr; // the entity is initialized after the vertex data was specified
49 
50  std::string mesh_name;
51  switch (type)
52  {
53  case Cone:
54  mesh_name = "rviz_cone.mesh";
55  break;
56 
57  case Cube:
58  mesh_name = "rviz_cube.mesh";
59  break;
60 
61  case Cylinder:
62  mesh_name = "rviz_cylinder.mesh";
63  break;
64 
65  case Sphere:
66  mesh_name = "rviz_sphere.mesh";
67  break;
68 
69  default:
70  ROS_BREAK();
71  }
72 
73  return scene_manager->createEntity(name, mesh_name);
74 }
75 
76 Shape::Shape(Type type, Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node)
77  : Object(scene_manager), type_(type)
78 {
79  static uint32_t count = 0;
80  std::stringstream ss;
81  ss << "Shape" << count++;
82 
83  entity_ = createEntity(ss.str(), type, scene_manager);
84 
85  if (!parent_node)
86  {
87  parent_node = scene_manager_->getRootSceneNode();
88  }
89 
90  scene_node_ = parent_node->createChildSceneNode();
91  offset_node_ = scene_node_->createChildSceneNode();
92  if (entity_)
93  offset_node_->attachObject(entity_);
94 
95  ss << "Material";
96  material_name_ = ss.str();
97  material_ = Ogre::MaterialManager::getSingleton().create(material_name_, ROS_PACKAGE_NAME);
98  material_->setReceiveShadows(false);
99  material_->getTechnique(0)->setLightingEnabled(true);
100  material_->getTechnique(0)->setAmbient(0.5, 0.5, 0.5);
101 
102  if (entity_)
103  entity_->setMaterialName(material_name_);
104 
105 #if (OGRE_VERSION_MAJOR <= 1 && OGRE_VERSION_MINOR <= 4)
106  if (entity_)
107  entity_->setNormaliseNormals(true);
108 #endif
109 }
110 
112 {
113  scene_manager_->destroySceneNode(scene_node_->getName());
114  scene_manager_->destroySceneNode(offset_node_->getName());
115 
116  if (entity_)
117  scene_manager_->destroyEntity(entity_);
118 
119  material_->unload();
120  Ogre::MaterialManager::getSingleton().remove(material_->getName());
121 }
122 
123 void Shape::setColor(const Ogre::ColourValue& c)
124 {
125  material_->getTechnique(0)->setAmbient(c * 0.5);
126  material_->getTechnique(0)->setDiffuse(c);
127 
128  if (c.a < 0.9998)
129  {
130  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
131  material_->getTechnique(0)->setDepthWriteEnabled(false);
132  }
133  else
134  {
135  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_REPLACE);
136  material_->getTechnique(0)->setDepthWriteEnabled(true);
137  }
138 }
139 
140 void Shape::setColor(float r, float g, float b, float a)
141 {
142  setColor(Ogre::ColourValue(r, g, b, a));
143 }
144 
145 void Shape::setOffset(const Ogre::Vector3& offset)
146 {
147  offset_node_->setPosition(offset);
148 }
149 
150 void Shape::setPosition(const Ogre::Vector3& position)
151 {
152  scene_node_->setPosition(position);
153 }
154 
155 void Shape::setOrientation(const Ogre::Quaternion& orientation)
156 {
157  scene_node_->setOrientation(orientation);
158 }
159 
160 void Shape::setScale(const Ogre::Vector3& scale)
161 {
162  scene_node_->setScale(scale);
163 }
164 
165 const Ogre::Vector3& Shape::getPosition()
166 {
167  return scene_node_->getPosition();
168 }
169 
170 const Ogre::Quaternion& Shape::getOrientation()
171 {
172  return scene_node_->getOrientation();
173 }
174 
175 void Shape::setUserData(const Ogre::Any& data)
176 {
177  if (entity_)
178  entity_->getUserObjectBindings().setUserAny(data);
179  else
180  ROS_ERROR("Shape not yet fully constructed. Cannot set user data. Did you add triangles to the mesh "
181  "already?");
182 }
183 
184 } // namespace rviz
Ogre::SceneNode * offset_node_
Definition: shape.h:125
static Ogre::Entity * createEntity(const std::string &name, Type shape_type, Ogre::SceneManager *scene_manager)
Definition: shape.cpp:45
void setOrientation(const Ogre::Quaternion &orientation) override
Set the orientation of the object.
Definition: shape.cpp:155
Ogre::Entity * entity_
Definition: shape.h:126
void setScale(const Ogre::Vector3 &scale) override
Set the scale of the object. Always relative to the identity orientation of the object.
Definition: shape.cpp:160
std::string material_name_
Definition: shape.h:128
Ogre::MaterialPtr material_
Definition: shape.h:127
void setUserData(const Ogre::Any &data) override
Sets user data on all ogre objects we own.
Definition: shape.cpp:175
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:140
Base class for visible objects, providing a minimal generic interface.
Definition: object.h:50
const Ogre::Quaternion & getOrientation() override
Get the local orientation of this object.
Definition: shape.cpp:170
Shape(Type shape_type, Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node=nullptr)
Constructor.
Definition: shape.cpp:76
const Ogre::Vector3 & getPosition() override
Get the local position of this object.
Definition: shape.cpp:165
Ogre::SceneNode * scene_node_
Definition: shape.h:124
Type type_
Definition: shape.h:130
void setPosition(const Ogre::Vector3 &position) override
Set the position of this object.
Definition: shape.cpp:150
Ogre::SceneManager * scene_manager_
Ogre scene manager this object is part of.
Definition: object.h:106
void setOffset(const Ogre::Vector3 &offset)
Set the offset for this shape.
Definition: shape.cpp:145
#define ROS_BREAK()
#define ROS_ERROR(...)
~Shape() override
Definition: shape.cpp:111


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25