mesh_resource_marker.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 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 "mesh_resource_marker.h"
31 
35 
36 #include "rviz/display_context.h"
37 #include "rviz/mesh_loader.h"
38 #include "marker_display.h"
39 
40 #include <OgreSceneNode.h>
41 #include <OgreSceneManager.h>
42 #include <OgreEntity.h>
43 #include <OgreSubEntity.h>
44 #include <OgreMaterialManager.h>
45 #include <OgreTextureManager.h>
46 #include <OgreSharedPtr.h>
47 #include <OgreTechnique.h>
48 
49 namespace rviz
50 {
52  DisplayContext* context,
53  Ogre::SceneNode* parent_node)
54  : MarkerBase(owner, context, parent_node), entity_(nullptr)
55 {
56 }
57 
59 {
60  reset();
61 }
62 
64 {
65  // destroy entity
66  if (entity_)
67  {
68  context_->getSceneManager()->destroyEntity(entity_);
69  entity_ = nullptr;
70  }
71 
72 
73  // destroy all the materials we've created
74  S_MaterialPtr::iterator it;
75  for (it = materials_.begin(); it != materials_.end(); it++)
76  {
77  Ogre::MaterialPtr material = *it;
78  if (!material.isNull())
79  {
80  material->unload();
81  Ogre::MaterialManager::getSingleton().remove(material->getName());
82  }
83  }
84  materials_.clear();
85 }
86 
88  const MarkerConstPtr& new_message)
89 {
90  ROS_ASSERT(new_message->type == visualization_msgs::Marker::MESH_RESOURCE);
91 
92  // flag indicating if the mesh material color needs to be updated
93  bool update_color = false;
94 
95  scene_node_->setVisible(false);
96 
97  // Get the color information from the message
98  float r = new_message->color.r;
99  float g = new_message->color.g;
100  float b = new_message->color.b;
101  float a = new_message->color.a;
102 
103  if (!entity_ || old_message->mesh_resource != new_message->mesh_resource ||
104  old_message->mesh_use_embedded_materials != new_message->mesh_use_embedded_materials)
105  {
106  reset();
107 
108  if (new_message->mesh_resource.empty())
109  {
110  return;
111  }
112 
113  if (loadMeshFromResource(new_message->mesh_resource).isNull())
114  {
115  std::stringstream ss;
116  ss << "Mesh resource marker [" << getStringID() << "] could not load ["
117  << new_message->mesh_resource << "]";
118  if (owner_)
119  {
121  }
122  ROS_DEBUG("%s", ss.str().c_str());
123  return;
124  }
125 
126  static uint32_t count = 0;
127  std::stringstream ss;
128  ss << "mesh_resource_marker_" << count++;
129  std::string id = ss.str();
130  entity_ = context_->getSceneManager()->createEntity(id, new_message->mesh_resource);
131  scene_node_->attachObject(entity_);
132 
133  // create a default material for any sub-entities which don't have their own.
134  ss << "Material";
135  Ogre::MaterialPtr default_material =
136  Ogre::MaterialManager::getSingleton().create(ss.str(), ROS_PACKAGE_NAME);
137  default_material->setReceiveShadows(false);
138  default_material->getTechnique(0)->setLightingEnabled(true);
139  default_material->getTechnique(0)->setAmbient(0.5, 0.5, 0.5);
140  materials_.insert(default_material);
141 
142  if (new_message->mesh_use_embedded_materials)
143  {
144  // make clones of all embedded materials so selection works correctly
145  for (const Ogre::MaterialPtr& material : getMaterials())
146  {
147  if (material->getName() != "BaseWhiteNoLighting")
148  {
149  Ogre::MaterialPtr new_material = material->clone(id + material->getName());
150  // add a new pass to every custom material to perform the color tinting
151  Ogre::Pass* pass = new_material->getTechnique(0)->createPass();
152  pass->setAmbient(0.0f, 0.0f, 0.0f);
153  pass->setDiffuse(0.0f, 0.0f, 0.0f, 0.0f);
154  pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
155  pass->setDepthWriteEnabled(false);
156  pass->setLightingEnabled(true);
157  materials_.insert(new_material);
158  }
159  }
160 
161  // make sub-entities use cloned materials
162  for (uint32_t i = 0; i < entity_->getNumSubEntities(); ++i)
163  {
164  std::string mat_name = entity_->getSubEntity(i)->getMaterialName();
165  if (mat_name != "BaseWhiteNoLighting")
166  {
167  entity_->getSubEntity(i)->setMaterialName(id + mat_name);
168  }
169  else
170  {
171  // BaseWhiteNoLighting is the default material Ogre uses
172  // when it sees a mesh with no material. Here we replace
173  // that with our default_material which gets colored with
174  // new_message->color.
175  entity_->getSubEntity(i)->setMaterial(default_material);
176  }
177  }
178  }
179  else
180  {
181  entity_->setMaterial(default_material);
182  }
183 
184  update_color = !(new_message->mesh_use_embedded_materials && r == 0 && g == 0 && b == 0 && a == 0);
185 
186  handler_.reset(
187  new MarkerSelectionHandler(this, MarkerID(new_message->ns, new_message->id), context_));
188  handler_->addTrackedObject(entity_);
189  }
190  else
191  {
192  // underlying mesh resource has not changed but if the color has
193  // then we need to update the materials color
194  if (!old_message || old_message->color.r != r || old_message->color.g != g ||
195  old_message->color.b != b || old_message->color.a != a)
196  {
197  update_color = true;
198  }
199  }
200 
201  // update material color
202  if (update_color)
203  {
204  bool depth_write = a >= 0.9998;
205  Ogre::SceneBlendType blending = depth_write ? Ogre::SBT_REPLACE : Ogre::SBT_TRANSPARENT_ALPHA;
206  bool tinting = new_message->mesh_use_embedded_materials;
207 
208  for (const Ogre::MaterialPtr& material : materials_)
209  {
210  Ogre::Technique* technique = material->getTechnique(0);
211  Ogre::Pass* pass0 = technique->getPass(0);
212  Ogre::Pass* passT = technique->getPass(technique->getNumPasses() - 1);
213  if (tinting)
214  {
215  // modify material's original color to use given alpha value
216  Ogre::ColourValue color = pass0->getDiffuse();
217  color.a = a;
218  pass0->setDiffuse(color);
219  // tint by re-rendering with marker color
220  passT->setAmbient(r * 0.5f, g * 0.5f, b * 0.5f);
221  passT->setDiffuse(r, g, b, std::min(a, 0.5f));
222  }
223  else
224  {
225  pass0->setAmbient(r * 0.5f, g * 0.5f, b * 0.5f);
226  pass0->setDiffuse(r, g, b, a);
227  }
228 
229  pass0->setSceneBlending(blending);
230  pass0->setDepthWriteEnabled(depth_write);
231  pass0->setLightingEnabled(true);
232  }
233  }
234 
235  Ogre::Vector3 pos, scale;
236  Ogre::Quaternion orient;
237  transform(new_message, pos, orient, scale);
238 
239  scene_node_->setVisible(true);
240  setPosition(pos);
241  setOrientation(orient);
242 
243  scene_node_->setScale(scale);
244 }
245 
247 {
248  S_MaterialPtr materials;
249  if (entity_)
250  {
251  extractMaterials(entity_, materials);
252  }
253  return materials;
254 }
255 
256 } // namespace rviz
Ogre::SceneNode * scene_node_
Definition: marker_base.h:116
bool transform(const MarkerConstPtr &message, Ogre::Vector3 &pos, Ogre::Quaternion &orient, Ogre::Vector3 &scale)
Definition: marker_base.cpp:83
f
void setMarkerStatus(MarkerID id, StatusLevel level, const std::string &text)
MeshResourceMarker(MarkerDisplay *owner, DisplayContext *context, Ogre::SceneNode *parent_node)
std::string getStringID()
Definition: marker_base.h:84
virtual void setPosition(const Ogre::Vector3 &position)
Ogre::MeshPtr loadMeshFromResource(const std::string &resource_path)
MarkerDisplay * owner_
Definition: marker_base.h:113
void extractMaterials(Ogre::Entity *entity, S_MaterialPtr &materials)
std::pair< std::string, int32_t > MarkerID
S_MaterialPtr getMaterials() override
Pure-virtual base class for objects which give Display subclasses context in which to work...
boost::shared_ptr< MarkerSelectionHandler > handler_
Definition: marker_base.h:122
void onNewMessage(const MarkerConstPtr &old_message, const MarkerConstPtr &new_message) override
DisplayContext * context_
Definition: marker_base.h:114
MarkerID getID()
Definition: marker_base.h:80
visualization_msgs::Marker::ConstPtr MarkerConstPtr
Definition: marker_base.h:63
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
std::set< Ogre::MaterialPtr > S_MaterialPtr
Definition: marker_base.h:57
virtual void setOrientation(const Ogre::Quaternion &orientation)
#define ROS_ASSERT(cond)
r
Displays "markers" sent in by other ROS nodes on the "visualization_marker" topic.
#define ROS_DEBUG(...)


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