Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "grid.h"
00031 #include "billboard_line.h"
00032
00033 #include <OgreSceneManager.h>
00034 #include <OgreSceneNode.h>
00035 #include <OgreVector3.h>
00036 #include <OgreQuaternion.h>
00037 #include <OgreManualObject.h>
00038 #include <OgreMaterialManager.h>
00039 #include <OgreTechnique.h>
00040
00041 #include <sstream>
00042
00043 namespace rviz
00044 {
00045
00046 Grid::Grid( Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node, Style style, uint32_t cell_count, float cell_length, float line_width, const Ogre::ColourValue& color )
00047 : scene_manager_( scene_manager )
00048 , style_(style)
00049 , cell_count_(cell_count)
00050 , cell_length_(cell_length)
00051 , line_width_(line_width)
00052 , height_(0)
00053 , color_(color)
00054 {
00055 static uint32_t gridCount = 0;
00056 std::stringstream ss;
00057 ss << "Grid" << gridCount++;
00058
00059 manual_object_ = scene_manager_->createManualObject( ss.str() );
00060
00061 if ( !parent_node )
00062 {
00063 parent_node = scene_manager_->getRootSceneNode();
00064 }
00065
00066 scene_node_ = parent_node->createChildSceneNode();
00067 scene_node_->attachObject( manual_object_ );
00068
00069 billboard_line_ = new BillboardLine(scene_manager, scene_node_);
00070
00071 ss << "Material";
00072 material_ = Ogre::MaterialManager::getSingleton().create( ss.str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
00073 material_->setReceiveShadows(false);
00074 material_->getTechnique(0)->setLightingEnabled(false);
00075
00076 setColor(color_);
00077 }
00078
00079 Grid::~Grid()
00080 {
00081 delete billboard_line_;
00082
00083 scene_manager_->destroySceneNode( scene_node_->getName() );
00084 scene_manager_->destroyManualObject( manual_object_ );
00085
00086 material_->unload();
00087 }
00088
00089 void Grid::setCellCount(uint32_t count)
00090 {
00091 cell_count_ = count;
00092
00093 create();
00094 }
00095
00096 void Grid::setCellLength(float len)
00097 {
00098 cell_length_ = len;
00099
00100 create();
00101 }
00102
00103 void Grid::setLineWidth(float width)
00104 {
00105 line_width_ = width;
00106
00107 create();
00108 }
00109
00110 void Grid::setColor(const Ogre::ColourValue& color)
00111 {
00112 color_ = color;
00113
00114 if ( color_.a < 0.9998 )
00115 {
00116 material_->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00117 material_->setDepthWriteEnabled( false );
00118 }
00119 else
00120 {
00121 material_->setSceneBlending( Ogre::SBT_REPLACE );
00122 material_->setDepthWriteEnabled( true );
00123 }
00124
00125 create();
00126 }
00127
00128 void Grid::setStyle(Style style)
00129 {
00130 style_ = style;
00131
00132 create();
00133 }
00134
00135 void Grid::setHeight(uint32_t height)
00136 {
00137 height_ = height;
00138
00139 create();
00140 }
00141
00142 void Grid::create()
00143 {
00144 manual_object_->clear();
00145 billboard_line_->clear();
00146
00147 float extent = (cell_length_*((double)cell_count_))/2;
00148
00149 if (style_ == Billboards)
00150 {
00151 billboard_line_->setColor(color_.r, color_.g, color_.b, color_.a);
00152 billboard_line_->setLineWidth(line_width_);
00153 billboard_line_->setMaxPointsPerLine(2);
00154 billboard_line_->setNumLines((cell_count_+1) * 2 * (height_ + 1)
00155 + ((cell_count_ + 1) * (cell_count_ + 1)) * height_);
00156 }
00157 else
00158 {
00159 manual_object_->estimateVertexCount( cell_count_ * 4 * (height_ + 1) + ((cell_count_ + 1) * (cell_count_ + 1) * height_));
00160 manual_object_->begin( material_->getName(), Ogre::RenderOperation::OT_LINE_LIST );
00161 }
00162
00163 for (uint32_t h = 0; h <= height_; ++h)
00164 {
00165 float h_real = (height_ / 2.0f - (float)h) * cell_length_;
00166 for( uint32_t i = 0; i <= cell_count_; i++ )
00167 {
00168 float inc = extent - ( i * cell_length_ );
00169
00170 Ogre::Vector3 p1(inc, h_real, -extent);
00171 Ogre::Vector3 p2(inc, h_real, extent);
00172 Ogre::Vector3 p3(-extent, h_real, inc);
00173 Ogre::Vector3 p4(extent, h_real, inc);
00174
00175 if (style_ == Billboards)
00176 {
00177 if (h != 0 || i != 0)
00178 {
00179 billboard_line_->newLine();
00180 }
00181
00182 billboard_line_->addPoint(p1);
00183 billboard_line_->addPoint(p2);
00184
00185 billboard_line_->newLine();
00186
00187 billboard_line_->addPoint(p3);
00188 billboard_line_->addPoint(p4);
00189 }
00190 else
00191 {
00192 manual_object_->position(p1);
00193 manual_object_->colour( color_ );
00194 manual_object_->position(p2);
00195 manual_object_->colour( color_ );
00196
00197 manual_object_->position(p3);
00198 manual_object_->colour( color_ );
00199 manual_object_->position(p4);
00200 manual_object_->colour( color_ );
00201 }
00202 }
00203 }
00204
00205 if (height_ > 0)
00206 {
00207 for (uint32_t x = 0; x <= cell_count_; ++x)
00208 {
00209 for (uint32_t z = 0; z <= cell_count_; ++z)
00210 {
00211 float x_real = extent - x * cell_length_;
00212 float z_real = extent - z * cell_length_;
00213
00214 float y_top = (height_ / 2.0f) * cell_length_;
00215 float y_bottom = -y_top;
00216
00217 if (style_ == Billboards)
00218 {
00219 billboard_line_->newLine();
00220
00221 billboard_line_->addPoint( Ogre::Vector3(x_real, y_bottom, z_real) );
00222 billboard_line_->addPoint( Ogre::Vector3(x_real, y_top, z_real) );
00223 }
00224 else
00225 {
00226 manual_object_->position( x_real, y_bottom, z_real );
00227 manual_object_->colour( color_ );
00228 manual_object_->position(x_real, y_top, z_real);
00229 manual_object_->colour( color_ );
00230 }
00231 }
00232 }
00233 }
00234
00235 if (style_ == Lines)
00236 {
00237 manual_object_->end();
00238 }
00239 }
00240
00241 void Grid::setUserData( const Ogre::Any& data )
00242 {
00243 manual_object_->setUserAny( data );
00244 }
00245
00246 }