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