partial_ogre_panel.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
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
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
36 #include <string>
37 #include <vector>
38 
39 namespace robot_nav_rviz_plugins
40 {
41 OgrePanel::PartialOgrePanel::PartialOgrePanel(Ogre::SceneManager& scene_manager,
42  Ogre::SceneNode& parent_scene_node,
43  const nav_core2::UIntBounds& bounds,
44  float resolution)
45  : scene_manager_(scene_manager), manual_object_(NULL), bounds_(bounds)
46 {
47  // Set up map material
48  static int material_count = 0;
49  std::stringstream material_name;
50  material_name << "NavGridMaterial" << material_count++;
51  material_ = Ogre::MaterialManager::getSingleton().getByName("rviz/Indexed8BitImage");
52  material_ = material_->clone(material_name.str());
53 
54  material_->setReceiveShadows(false);
55  material_->getTechnique(0)->setLightingEnabled(false);
56  material_->setDepthBias(-16.0f, 0.0f);
57  material_->setCullingMode(Ogre::CULL_NONE);
58  material_->setDepthWriteEnabled(false);
59 
60  static int map_count = 0;
61  std::stringstream object_name;
62  object_name << "NavGridObject" << map_count++;
63  manual_object_ = scene_manager_.createManualObject(object_name.str());
64 
65  static int node_count = 0;
66  std::stringstream node_name;
67  node_name << "NGNodeObject" << node_count++;
68 
69  scene_node_ = parent_scene_node.createChildSceneNode(node_name.str());
70  scene_node_->attachObject(manual_object_);
71 
72  /*
73  * Divide the rectangular panel into two large triangles for rendering in Ogre
74  * o-----o
75  * | /|
76  * | / |
77  * | / |
78  * | / |
79  * |/ |
80  * o-----o
81  */
82  manual_object_->begin(material_->getName(), Ogre::RenderOperation::OT_TRIANGLE_LIST);
83 
84  // First triangle - Bottom left
85  manual_object_->position(0.0f, 0.0f, 0.0f);
86  manual_object_->textureCoord(0.0f, 0.0f);
87  manual_object_->normal(0.0f, 0.0f, 1.0f);
88 
89  // First triangle - Top right
90  manual_object_->position(1.0f, 1.0f, 0.0f);
91  manual_object_->textureCoord(1.0f, 1.0f);
92  manual_object_->normal(0.0f, 0.0f, 1.0f);
93 
94  // First triangle - Top left
95  manual_object_->position(0.0f, 1.0f, 0.0f);
96  manual_object_->textureCoord(0.0f, 1.0f);
97  manual_object_->normal(0.0f, 0.0f, 1.0f);
98 
99  // Second triangle - Bottom left
100  manual_object_->position(0.0f, 0.0f, 0.0f);
101  manual_object_->textureCoord(0.0f, 0.0f);
102  manual_object_->normal(0.0f, 0.0f, 1.0f);
103 
104  // Second triangle - Bottom right
105  manual_object_->position(1.0f, 0.0f, 0.0f);
106  manual_object_->textureCoord(1.0f, 0.0f);
107  manual_object_->normal(0.0f, 0.0f, 1.0f);
108 
109  // Second triangle - Top right
110  manual_object_->position(1.0f, 1.0f, 0.0f);
111  manual_object_->textureCoord(1.0f, 1.0f);
112  manual_object_->normal(0.0f, 0.0f, 1.0f);
113 
114  manual_object_->end();
115 
116  scene_node_->setPosition(bounds_.getMinX() * resolution, bounds_.getMinY() * resolution, 0);
117  scene_node_->setScale(bounds_.getWidth() * resolution, bounds_.getHeight() * resolution, 1.0);
118 
119  // Set default data (crucial so that it fails on construction if too big)
120  std::vector<unsigned char> pixels;
121  pixels.resize(bounds_.getWidth() * bounds_.getHeight());
122  updateData(pixels);
123 
124  // don't show map until the plugin is actually enabled
125  manual_object_->setVisible(false);
126 }
127 
129 {
130  manual_object_->detachFromParent();
131  scene_manager_.destroyManualObject(manual_object_);
132 }
133 
134 void OgrePanel::PartialOgrePanel::updateData(std::vector<unsigned char>& pixels)
135 {
136  Ogre::DataStreamPtr pixel_stream;
137  pixel_stream.bind(new Ogre::MemoryDataStream(&pixels[0], pixels.size()));
138 
139  if (!texture_.isNull())
140  {
141  Ogre::TextureManager::getSingleton().remove(texture_->getName());
142  texture_.setNull();
143  }
144 
145  static int tex_count = 0;
146  std::stringstream texture_name;
147  texture_name << "MapTexture" << tex_count++;
148  texture_ = Ogre::TextureManager::getSingleton().loadRawData(texture_name.str(),
149  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
150  pixel_stream, bounds_.getWidth(), bounds_.getHeight(), Ogre::PF_L8, Ogre::TEX_TYPE_2D,
151  0);
152 
153  setTexture(texture_->getName(), 0);
154  manual_object_->setVisible(true);
155 }
156 
158 {
159  if (manual_object_)
160  manual_object_->setVisible(false);
161 
162  if (!texture_.isNull())
163  {
164  Ogre::TextureManager::getSingleton().remove(texture_->getName());
165  texture_.setNull();
166  }
167 }
168 
169 void OgrePanel::PartialOgrePanel::setTexture(const std::string& texture_name, int index)
170 {
171  Ogre::Pass* pass = material_->getTechnique(0)->getPass(0);
172  Ogre::TextureUnitState* tex_unit = NULL;
173  if (pass->getNumTextureUnitStates() > index)
174  {
175  tex_unit = pass->getTextureUnitState(index);
176  }
177  else
178  {
179  tex_unit = pass->createTextureUnitState();
180  }
181  tex_unit->setTextureName(texture_name);
182  tex_unit->setTextureFiltering(Ogre::TFO_NONE);
183 }
184 
185 void OgrePanel::PartialOgrePanel::updateAlphaRendering(Ogre::SceneBlendType scene_blending, bool depth_write, int group,
186  Ogre::Renderable::Visitor* alpha_setter)
187 {
188  material_->setSceneBlending(scene_blending);
189  material_->setDepthWriteEnabled(depth_write);
190  if (manual_object_)
191  {
192  manual_object_->visitRenderables(alpha_setter);
193  manual_object_->setRenderQueueGroup(group);
194  }
195 }
196 
197 } // namespace robot_nav_rviz_plugins
#define NULL
f
unsigned int getMinX() const
unsigned int getMinY() const
void updateAlphaRendering(Ogre::SceneBlendType scene_blending, bool depth_write, int group, Ogre::Renderable::Visitor *alpha_setter)
PartialOgrePanel(Ogre::SceneManager &scene_manager, Ogre::SceneNode &parent_scene_node, const nav_core2::UIntBounds &bounds, float resolution)
void setTexture(const std::string &texture_name, int index)
void updateData(std::vector< unsigned char > &pixels)
Update the data for this Partial Panel.
Several reusable pieces for displaying polygons.
unsigned int getHeight() const
unsigned int getWidth() const


robot_nav_rviz_plugins
Author(s):
autogenerated on Sun Jan 10 2021 04:08:58