mesh_node.h
Go to the documentation of this file.
1 /*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013-2015, Timm Linder, Social Robotics Lab, University of Freiburg
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * * Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #ifndef MESH_NODE_H
32 #define MESH_NODE_H
33 
34 #include <rviz/mesh_loader.h>
36 
38 #include <rviz/render_panel.h> // hack to get camera position
39 
40 #include <OgreSceneManager.h>
41 #include <OgreSubEntity.h>
42 #include <OgreMaterialManager.h>
43 #include <OgreTextureManager.h>
44 #include <OgreTechnique.h>
45 #include <OgreRoot.h>
46 #include <OgreFrameListener.h>
47 
48 
50  class MeshNode : public Ogre::FrameListener {
51  public:
52  MeshNode(rviz::DisplayContext* displayContext, Ogre::SceneNode* parentNode, const std::string& meshResource, Ogre::Vector3 position = Ogre::Vector3::ZERO)
53  : m_sceneManager(displayContext->getSceneManager()), m_displayContext(displayContext), m_meshResource(meshResource)
54  {
55  m_cameraFacing = false;
56  m_sceneNode = parentNode->createChildSceneNode();
57  m_sceneNode->setVisible(false);
58 
59  // Load mesh
60  assert(!rviz::loadMeshFromResource(meshResource).isNull());
61 
62  // Create scene entity
63  std::stringstream ss;
64  static int counter = 0;
65  ss << "gender_symbol_" << counter++;
66  std::string id = ss.str();
67 
68  m_entity = m_sceneManager->createEntity(id, meshResource);
69  m_sceneNode->attachObject(m_entity);
70 
71  // set up material
72  ss << "Material";
73  Ogre::MaterialPtr default_material = Ogre::MaterialManager::getSingleton().create( ss.str(), "rviz" );
74  default_material->setReceiveShadows(false);
75  default_material->getTechnique(0)->setLightingEnabled(true);
76  default_material->getTechnique(0)->setAmbient( 0.5, 0.5, 0.5 );
77  m_materials.insert( default_material );
78  m_entity->setMaterial( default_material );
79 
80  setPosition(position);
81  setVisible(true);
82 
83  // For camera-facing meshes
84  Ogre::Root::getSingleton().addFrameListener(this);
85  }
86 
87  virtual ~MeshNode() {
88  Ogre::Root::getSingleton().removeFrameListener(this);
89  m_sceneManager->destroyEntity(m_entity);
90 
91  // destroy all the materials we've created
92  std::set<Ogre::MaterialPtr>::iterator it;
93  for(it = m_materials.begin(); it != m_materials.end(); it++ )
94  {
95  Ogre::MaterialPtr material = *it;
96  if (!material.isNull())
97  {
98  material->unload();
99  Ogre::MaterialManager::getSingleton().remove(material->getName());
100  }
101  }
102  m_materials.clear();
103  m_sceneManager->destroySceneNode(m_sceneNode->getName());
104  }
105 
106  void setOrientation(const Ogre::Quaternion& orientation) {
107  m_orientation = orientation;
108  }
109 
110  void setPosition(const Ogre::Vector3& position) {
111  m_sceneNode->setPosition(position);
112  }
113 
114  void setScale(const float scaleFactor) {
115  m_sceneNode->setScale(Ogre::Vector3(scaleFactor, scaleFactor, scaleFactor));
116  }
117 
118  void setVisible(bool visible) {
119  m_sceneNode->setVisible(visible, true);
120  }
121 
122  void setCameraFacing(bool cameraFacing) {
123  m_cameraFacing = cameraFacing;
124  }
125 
126  void setColor(const Ogre::ColourValue& c) {
127  Ogre::SceneBlendType blending;
128  bool depth_write;
129 
130  if ( c.a < 0.9998 )
131  {
132  blending = Ogre::SBT_TRANSPARENT_ALPHA;
133  depth_write = false;
134  }
135  else
136  {
137  blending = Ogre::SBT_REPLACE;
138  depth_write = true;
139  }
140 
141  std::set<Ogre::MaterialPtr>::iterator it;
142  for(it = m_materials.begin(); it != m_materials.end(); it++)
143  {
144  Ogre::Technique* technique = (*it)->getTechnique( 0 );
145 
146  technique->setAmbient( c.r*0.5, c.g*0.5, c.b*0.5 );
147  technique->setDiffuse( c.r, c.g, c.b, c.a );
148  technique->setSceneBlending( blending );
149  technique->setDepthWriteEnabled( depth_write );
150  technique->setLightingEnabled( true );
151  }
152  }
153 
154  const std::string& getMeshResource() const {
155  return m_meshResource;
156  }
157 
158  // We are using this FrameListener callback to orient the mesh towards the camera.
159  // Using a SceneManager::Listener and its preUpdateSceneGraph(SceneManager, Camera) method doesn't work because
160  // it is apparently never invoked by the Rviz render system.
161  virtual bool frameStarted(const Ogre::FrameEvent &evt)
162  {
163  Ogre::Quaternion cameraQuat;
164  if(m_cameraFacing) {
165  // Align with camera view direction
166  // FIXME: The following way of retrieving the camera and its position is a bit hacky, don't try this at home!
167  rviz::VisualizationManager* visualizationManager = dynamic_cast<rviz::VisualizationManager*>(m_displayContext);
168  assert(visualizationManager != NULL);
169  cameraQuat = visualizationManager->getRenderPanel()->getCamera()->getOrientation();
170  }
171  m_sceneNode->setOrientation(cameraQuat * m_orientation);
172  return true;
173  }
174 
175  private:
176  Ogre::SceneManager* m_sceneManager;
177  Ogre::SceneNode* m_sceneNode;
179 
180  Ogre::Quaternion m_orientation;
181  Ogre::Entity* m_entity;
182  std::set<Ogre::MaterialPtr> m_materials;
183  std::string m_meshResource;
185  };
186 
187 }
188 
189 #endif // MESH_NODE_H
#define NULL
void setPosition(const Ogre::Vector3 &position)
Definition: mesh_node.h:110
void setOrientation(const Ogre::Quaternion &orientation)
Definition: mesh_node.h:106
MeshNode(rviz::DisplayContext *displayContext, Ogre::SceneNode *parentNode, const std::string &meshResource, Ogre::Vector3 position=Ogre::Vector3::ZERO)
Definition: mesh_node.h:52
Ogre::MeshPtr loadMeshFromResource(const std::string &resource_path)
RenderPanel * getRenderPanel() const
void setColor(const Ogre::ColourValue &c)
Definition: mesh_node.h:126
std::set< Ogre::MaterialPtr > m_materials
Definition: mesh_node.h:182
void setScale(const float scaleFactor)
Definition: mesh_node.h:114
const std::string & getMeshResource() const
Definition: mesh_node.h:154
Ogre::Camera * getCamera() const
virtual bool frameStarted(const Ogre::FrameEvent &evt)
Definition: mesh_node.h:161
Ogre::SceneManager * m_sceneManager
Definition: mesh_node.h:176
rviz::DisplayContext * m_displayContext
Definition: mesh_node.h:178
void setCameraFacing(bool cameraFacing)
Definition: mesh_node.h:122


tuw_object_rviz
Author(s): Florian Beck
autogenerated on Mon Jun 10 2019 15:40:17