| 1 | #include <osg/Geode> |
|---|
| 2 | #include <osg/ShapeDrawable> |
|---|
| 3 | #include <osg/Material> |
|---|
| 4 | #include <osg/Texture2D> |
|---|
| 5 | #include <osg/Billboard> |
|---|
| 6 | #include <osg/AlphaFunc> |
|---|
| 7 | #include <osg/BlendFunc> |
|---|
| 8 | #include <osg/StateSet> |
|---|
| 9 | #include <osg/Geometry> |
|---|
| 10 | #include <osg/MatrixTransform> |
|---|
| 11 | #include <osg/Switch> |
|---|
| 12 | |
|---|
| 13 | #include <osgProducer/Viewer> |
|---|
| 14 | |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | #include <osgUtil/IntersectVisitor> |
|---|
| 17 | #include <osgUtil/SmoothingVisitor> |
|---|
| 18 | |
|---|
| 19 | #include <osg/Math> |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #include "../osghangglide/terrain_coords.h" |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | class ForestTechniqueManager : public osg::Referenced |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | |
|---|
| 29 | ForestTechniqueManager() {} |
|---|
| 30 | |
|---|
| 31 | class Tree : public osg::Referenced |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | Tree(): |
|---|
| 36 | _color(255,255,255,255), |
|---|
| 37 | _width(1.0f), |
|---|
| 38 | _height(1.0f), |
|---|
| 39 | _type(0) {} |
|---|
| 40 | |
|---|
| 41 | Tree(const osg::Vec3& position, const osg::UByte4& color, float width, float height, unsigned int type): |
|---|
| 42 | _position(position), |
|---|
| 43 | _color(color), |
|---|
| 44 | _width(width), |
|---|
| 45 | _height(height), |
|---|
| 46 | _type(type) {} |
|---|
| 47 | |
|---|
| 48 | osg::Vec3 _position; |
|---|
| 49 | osg::UByte4 _color; |
|---|
| 50 | float _width; |
|---|
| 51 | float _height; |
|---|
| 52 | unsigned int _type; |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | typedef std::vector< osg::ref_ptr<Tree> > TreeList; |
|---|
| 56 | |
|---|
| 57 | class Cell : public osg::Referenced |
|---|
| 58 | { |
|---|
| 59 | public: |
|---|
| 60 | typedef std::vector< osg::ref_ptr<Cell> > CellList; |
|---|
| 61 | |
|---|
| 62 | Cell():_parent(0) {} |
|---|
| 63 | Cell(osg::BoundingBox& bb):_parent(0), _bb(bb) {} |
|---|
| 64 | |
|---|
| 65 | void addCell(Cell* cell) { cell->_parent=this; _cells.push_back(cell); } |
|---|
| 66 | |
|---|
| 67 | void addTree(Tree* tree) { _trees.push_back(tree); } |
|---|
| 68 | |
|---|
| 69 | void addTrees(const TreeList& trees) { _trees.insert(_trees.end(),trees.begin(),trees.end()); } |
|---|
| 70 | |
|---|
| 71 | void computeBound(); |
|---|
| 72 | |
|---|
| 73 | bool contains(const osg::Vec3& position) const { return _bb.contains(position); } |
|---|
| 74 | |
|---|
| 75 | bool divide(unsigned int maxNumTreesPerCell=10); |
|---|
| 76 | |
|---|
| 77 | bool devide(bool xAxis, bool yAxis, bool zAxis); |
|---|
| 78 | |
|---|
| 79 | void bin(); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | Cell* _parent; |
|---|
| 83 | osg::BoundingBox _bb; |
|---|
| 84 | CellList _cells; |
|---|
| 85 | TreeList _trees; |
|---|
| 86 | |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } |
|---|
| 90 | int random(int min,int max) { return min + (int)((float)(max-min)*(float)rand()/(float)RAND_MAX); } |
|---|
| 91 | |
|---|
| 92 | osg::Geode* createTerrain(const osg::Vec3& origin, const osg::Vec3& size); |
|---|
| 93 | |
|---|
| 94 | void createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees); |
|---|
| 95 | |
|---|
| 96 | osg::Geometry* createSprite( float w, float h, osg::UByte4 color ); |
|---|
| 97 | |
|---|
| 98 | osg::Geometry* createOrthogonalQuads( const osg::Vec3& pos, float w, float h, osg::UByte4 color ); |
|---|
| 99 | |
|---|
| 100 | osg::Node* createBillboardGraph(Cell* cell,osg::StateSet* stateset); |
|---|
| 101 | |
|---|
| 102 | osg::Node* createXGraph(Cell* cell,osg::StateSet* stateset); |
|---|
| 103 | |
|---|
| 104 | osg::Node* createTransformGraph(Cell* cell,osg::StateSet* stateset); |
|---|
| 105 | |
|---|
| 106 | osg::Node* createScene(); |
|---|
| 107 | |
|---|
| 108 | void advanceToNextTechnique(int delta=1) |
|---|
| 109 | { |
|---|
| 110 | if (_techniqueSwitch.valid()) |
|---|
| 111 | { |
|---|
| 112 | _currentTechnique = (_currentTechnique + delta)%_techniqueSwitch->getNumChildren(); |
|---|
| 113 | _techniqueSwitch->setSingleChildOn(_currentTechnique); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | osg::ref_ptr<osg::Switch> _techniqueSwitch; |
|---|
| 118 | int _currentTechnique; |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | }; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | class TechniqueEventHandler : public osgGA::GUIEventHandler, public osg::NodeCallback |
|---|
| 125 | { |
|---|
| 126 | public: |
|---|
| 127 | |
|---|
| 128 | TechniqueEventHandler(ForestTechniqueManager* ttm=0) { _ForestTechniqueManager = ttm; } |
|---|
| 129 | |
|---|
| 130 | META_Object(osgforestApp,TechniqueEventHandler); |
|---|
| 131 | |
|---|
| 132 | virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); } |
|---|
| 133 | |
|---|
| 134 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); |
|---|
| 135 | |
|---|
| 136 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 137 | |
|---|
| 138 | protected: |
|---|
| 139 | |
|---|
| 140 | ~TechniqueEventHandler() {} |
|---|
| 141 | |
|---|
| 142 | TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {} |
|---|
| 143 | |
|---|
| 144 | osg::ref_ptr<ForestTechniqueManager> _ForestTechniqueManager; |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
|---|
| 150 | { |
|---|
| 151 | switch(ea.getEventType()) |
|---|
| 152 | { |
|---|
| 153 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 154 | { |
|---|
| 155 | if (ea.getKey()=='n' || |
|---|
| 156 | ea.getKey()==osgGA::GUIEventAdapter::KEY_Right || |
|---|
| 157 | ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right) |
|---|
| 158 | { |
|---|
| 159 | _ForestTechniqueManager->advanceToNextTechnique(1); |
|---|
| 160 | return true; |
|---|
| 161 | } |
|---|
| 162 | else if (ea.getKey()=='p' || |
|---|
| 163 | ea.getKey()==osgGA::GUIEventAdapter::KEY_Left || |
|---|
| 164 | ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left) |
|---|
| 165 | { |
|---|
| 166 | _ForestTechniqueManager->advanceToNextTechnique(-1); |
|---|
| 167 | return true; |
|---|
| 168 | } |
|---|
| 169 | return false; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | default: |
|---|
| 173 | return false; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 178 | { |
|---|
| 179 | usage.addKeyboardMouseBinding("n or Left Arrow","Advance to next technique"); |
|---|
| 180 | usage.addKeyboardMouseBinding("p or Right Array","Move to previous technique"); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | void ForestTechniqueManager::Cell::computeBound() |
|---|
| 185 | { |
|---|
| 186 | _bb.init(); |
|---|
| 187 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 188 | citr!=_cells.end(); |
|---|
| 189 | ++citr) |
|---|
| 190 | { |
|---|
| 191 | (*citr)->computeBound(); |
|---|
| 192 | _bb.expandBy((*citr)->_bb); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | for(TreeList::iterator titr=_trees.begin(); |
|---|
| 196 | titr!=_trees.end(); |
|---|
| 197 | ++titr) |
|---|
| 198 | { |
|---|
| 199 | _bb.expandBy((*titr)->_position); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | bool ForestTechniqueManager::Cell::divide(unsigned int maxNumTreesPerCell) |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | if (_trees.size()<=maxNumTreesPerCell) return false; |
|---|
| 207 | |
|---|
| 208 | computeBound(); |
|---|
| 209 | |
|---|
| 210 | float radius = _bb.radius(); |
|---|
| 211 | float divide_distance = radius*0.7f; |
|---|
| 212 | if (devide((_bb.xMax()-_bb.xMin())>divide_distance,(_bb.yMax()-_bb.yMin())>divide_distance,(_bb.zMax()-_bb.zMin())>divide_distance)) |
|---|
| 213 | { |
|---|
| 214 | |
|---|
| 215 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 216 | citr!=_cells.end(); |
|---|
| 217 | ++citr) |
|---|
| 218 | { |
|---|
| 219 | (*citr)->divide(maxNumTreesPerCell); |
|---|
| 220 | } |
|---|
| 221 | return true; |
|---|
| 222 | } |
|---|
| 223 | else |
|---|
| 224 | { |
|---|
| 225 | return false; |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | bool ForestTechniqueManager::Cell::devide(bool xAxis, bool yAxis, bool zAxis) |
|---|
| 230 | { |
|---|
| 231 | if (!(xAxis || yAxis || zAxis)) return false; |
|---|
| 232 | |
|---|
| 233 | if (_cells.empty()) |
|---|
| 234 | _cells.push_back(new Cell(_bb)); |
|---|
| 235 | |
|---|
| 236 | if (xAxis) |
|---|
| 237 | { |
|---|
| 238 | unsigned int numCellsToDivide=_cells.size(); |
|---|
| 239 | for(unsigned int i=0;i<numCellsToDivide;++i) |
|---|
| 240 | { |
|---|
| 241 | Cell* orig_cell = _cells[i].get(); |
|---|
| 242 | Cell* new_cell = new Cell(orig_cell->_bb); |
|---|
| 243 | |
|---|
| 244 | float xCenter = (orig_cell->_bb.xMin()+orig_cell->_bb.xMax())*0.5f; |
|---|
| 245 | orig_cell->_bb.xMax() = xCenter; |
|---|
| 246 | new_cell->_bb.xMin() = xCenter; |
|---|
| 247 | |
|---|
| 248 | _cells.push_back(new_cell); |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | if (yAxis) |
|---|
| 253 | { |
|---|
| 254 | unsigned int numCellsToDivide=_cells.size(); |
|---|
| 255 | for(unsigned int i=0;i<numCellsToDivide;++i) |
|---|
| 256 | { |
|---|
| 257 | Cell* orig_cell = _cells[i].get(); |
|---|
| 258 | Cell* new_cell = new Cell(orig_cell->_bb); |
|---|
| 259 | |
|---|
| 260 | float yCenter = (orig_cell->_bb.yMin()+orig_cell->_bb.yMax())*0.5f; |
|---|
| 261 | orig_cell->_bb.yMax() = yCenter; |
|---|
| 262 | new_cell->_bb.yMin() = yCenter; |
|---|
| 263 | |
|---|
| 264 | _cells.push_back(new_cell); |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | if (zAxis) |
|---|
| 269 | { |
|---|
| 270 | unsigned int numCellsToDivide=_cells.size(); |
|---|
| 271 | for(unsigned int i=0;i<numCellsToDivide;++i) |
|---|
| 272 | { |
|---|
| 273 | Cell* orig_cell = _cells[i].get(); |
|---|
| 274 | Cell* new_cell = new Cell(orig_cell->_bb); |
|---|
| 275 | |
|---|
| 276 | float zCenter = (orig_cell->_bb.zMin()+orig_cell->_bb.zMax())*0.5f; |
|---|
| 277 | orig_cell->_bb.zMax() = zCenter; |
|---|
| 278 | new_cell->_bb.zMin() = zCenter; |
|---|
| 279 | |
|---|
| 280 | _cells.push_back(new_cell); |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | bin(); |
|---|
| 285 | |
|---|
| 286 | return true; |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | void ForestTechniqueManager::Cell::bin() |
|---|
| 291 | { |
|---|
| 292 | |
|---|
| 293 | TreeList treesNotAssigned; |
|---|
| 294 | for(TreeList::iterator titr=_trees.begin(); |
|---|
| 295 | titr!=_trees.end(); |
|---|
| 296 | ++titr) |
|---|
| 297 | { |
|---|
| 298 | Tree* tree = titr->get(); |
|---|
| 299 | bool assigned = false; |
|---|
| 300 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 301 | citr!=_cells.end() && !assigned; |
|---|
| 302 | ++citr) |
|---|
| 303 | { |
|---|
| 304 | if ((*citr)->contains(tree->_position)) |
|---|
| 305 | { |
|---|
| 306 | (*citr)->addTree(tree); |
|---|
| 307 | assigned = true; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | if (!assigned) treesNotAssigned.push_back(tree); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | _trees.swap(treesNotAssigned); |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | CellList cellsNotEmpty; |
|---|
| 319 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 320 | citr!=_cells.end(); |
|---|
| 321 | ++citr) |
|---|
| 322 | { |
|---|
| 323 | if (!((*citr)->_trees.empty())) |
|---|
| 324 | { |
|---|
| 325 | cellsNotEmpty.push_back(*citr); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | _cells.swap(cellsNotEmpty); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | osg::Geode* ForestTechniqueManager::createTerrain(const osg::Vec3& origin, const osg::Vec3& size) |
|---|
| 334 | { |
|---|
| 335 | osg::Geode* geode = new osg::Geode(); |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 341 | |
|---|
| 342 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 343 | if (image) |
|---|
| 344 | { |
|---|
| 345 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 346 | texture->setImage(image); |
|---|
| 347 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | geode->setStateSet( stateset ); |
|---|
| 351 | |
|---|
| 352 | unsigned int numColumns = 38; |
|---|
| 353 | unsigned int numRows = 39; |
|---|
| 354 | unsigned int r; |
|---|
| 355 | unsigned int c; |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | float min_z = FLT_MAX; |
|---|
| 359 | float max_z = -FLT_MAX; |
|---|
| 360 | for(r=0;r<numRows;++r) |
|---|
| 361 | { |
|---|
| 362 | for(c=0;c<numColumns;++c) |
|---|
| 363 | { |
|---|
| 364 | min_z = osg::minimum(min_z,vertex[r+c*numRows][2]); |
|---|
| 365 | max_z = osg::maximum(max_z,vertex[r+c*numRows][2]); |
|---|
| 366 | } |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | float scale_z = size.z()/(max_z-min_z); |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | bool createGrid = false; |
|---|
| 373 | if (createGrid) |
|---|
| 374 | { |
|---|
| 375 | |
|---|
| 376 | osg::Grid* grid = new osg::Grid; |
|---|
| 377 | grid->allocateGrid(numColumns,numRows); |
|---|
| 378 | grid->setOrigin(origin); |
|---|
| 379 | grid->setXInterval(size.x()/(float)(numColumns-1)); |
|---|
| 380 | grid->setYInterval(size.y()/(float)(numRows-1)); |
|---|
| 381 | |
|---|
| 382 | for(r=0;r<numRows;++r) |
|---|
| 383 | { |
|---|
| 384 | for(c=0;c<numColumns;++c) |
|---|
| 385 | { |
|---|
| 386 | grid->setHeight(c,r,(vertex[r+c*numRows][2]-min_z)*scale_z); |
|---|
| 387 | } |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | geode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 391 | } |
|---|
| 392 | else |
|---|
| 393 | { |
|---|
| 394 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 395 | |
|---|
| 396 | osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows)); |
|---|
| 397 | osg::Vec2Array& t = *(new osg::Vec2Array(numColumns*numRows)); |
|---|
| 398 | osg::UByte4Array& color = *(new osg::UByte4Array(1)); |
|---|
| 399 | |
|---|
| 400 | color[0].set(255,255,255,255); |
|---|
| 401 | |
|---|
| 402 | float rowCoordDelta = size.y()/(float)(numRows-1); |
|---|
| 403 | float columnCoordDelta = size.x()/(float)(numColumns-1); |
|---|
| 404 | |
|---|
| 405 | float rowTexDelta = 1.0f/(float)(numRows-1); |
|---|
| 406 | float columnTexDelta = 1.0f/(float)(numColumns-1); |
|---|
| 407 | |
|---|
| 408 | osg::Vec3 pos = origin; |
|---|
| 409 | osg::Vec2 tex(0.0f,0.0f); |
|---|
| 410 | int vi=0; |
|---|
| 411 | for(r=0;r<numRows;++r) |
|---|
| 412 | { |
|---|
| 413 | pos.x() = origin.x(); |
|---|
| 414 | tex.x() = 0.0f; |
|---|
| 415 | for(c=0;c<numColumns;++c) |
|---|
| 416 | { |
|---|
| 417 | v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z); |
|---|
| 418 | t[vi].set(tex.x(),tex.y()); |
|---|
| 419 | pos.x()+=columnCoordDelta; |
|---|
| 420 | tex.x()+=columnTexDelta; |
|---|
| 421 | ++vi; |
|---|
| 422 | } |
|---|
| 423 | pos.y() += rowCoordDelta; |
|---|
| 424 | tex.y() += rowTexDelta; |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | geometry->setVertexArray(&v); |
|---|
| 428 | geometry->setColorArray(&color); |
|---|
| 429 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 430 | geometry->setTexCoordArray(0,&t); |
|---|
| 431 | |
|---|
| 432 | for(r=0;r<numRows-1;++r) |
|---|
| 433 | { |
|---|
| 434 | osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns)); |
|---|
| 435 | geometry->addPrimitiveSet(&drawElements); |
|---|
| 436 | int ei=0; |
|---|
| 437 | for(c=0;c<numColumns;++c) |
|---|
| 438 | { |
|---|
| 439 | drawElements[ei++] = (r+1)*numColumns+c; |
|---|
| 440 | drawElements[ei++] = (r)*numColumns+c; |
|---|
| 441 | } |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | geode->addDrawable(geometry); |
|---|
| 445 | |
|---|
| 446 | osgUtil::SmoothingVisitor sv; |
|---|
| 447 | sv.smooth(*geometry); |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | return geode; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees) |
|---|
| 454 | { |
|---|
| 455 | |
|---|
| 456 | float max_TreeHeight = sqrtf(size.length2()/(float)numTreesToCreate); |
|---|
| 457 | float max_TreeWidth = max_TreeHeight*0.5f; |
|---|
| 458 | |
|---|
| 459 | float min_TreeHeight = max_TreeHeight*0.3f; |
|---|
| 460 | float min_TreeWidth = min_TreeHeight*0.5f; |
|---|
| 461 | |
|---|
| 462 | trees.reserve(trees.size()+numTreesToCreate); |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | for(unsigned int i=0;i<numTreesToCreate;++i) |
|---|
| 466 | { |
|---|
| 467 | Tree* tree = new Tree; |
|---|
| 468 | tree->_position.set(random(origin.x(),origin.x()+size.x()),random(origin.y(),origin.y()+size.y()),origin.z()); |
|---|
| 469 | tree->_color.set(random(128,255),random(128,255),random(128,255),255); |
|---|
| 470 | tree->_width = random(min_TreeWidth,max_TreeWidth); |
|---|
| 471 | tree->_height = random(min_TreeHeight,max_TreeHeight); |
|---|
| 472 | tree->_type = 0; |
|---|
| 473 | |
|---|
| 474 | if (terrain) |
|---|
| 475 | { |
|---|
| 476 | osgUtil::IntersectVisitor iv; |
|---|
| 477 | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
|---|
| 478 | |
|---|
| 479 | segDown->set(tree->_position,tree->_position+osg::Vec3(0.0f,0.0f,size.z())); |
|---|
| 480 | iv.addLineSegment(segDown.get()); |
|---|
| 481 | |
|---|
| 482 | terrain->accept(iv); |
|---|
| 483 | |
|---|
| 484 | if (iv.hits()) |
|---|
| 485 | { |
|---|
| 486 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
|---|
| 487 | if (!hitList.empty()) |
|---|
| 488 | { |
|---|
| 489 | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
|---|
| 490 | osg::Vec3 np = hitList.front().getWorldIntersectNormal(); |
|---|
| 491 | tree->_position = ip; |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | trees.push_back(tree); |
|---|
| 497 | } |
|---|
| 498 | } |
|---|
| 499 | |
|---|
| 500 | osg::Geometry* ForestTechniqueManager::createSprite( float w, float h, osg::UByte4 color ) |
|---|
| 501 | { |
|---|
| 502 | |
|---|
| 503 | osg::Vec3Array& v = *(new osg::Vec3Array(4)); |
|---|
| 504 | osg::Vec2Array& t = *(new osg::Vec2Array(4)); |
|---|
| 505 | osg::UByte4Array& c = *(new osg::UByte4Array(1)); |
|---|
| 506 | |
|---|
| 507 | v[0].set(-w*0.5f,0.0f,0.0f); |
|---|
| 508 | v[1].set( w*0.5f,0.0f,0.0f); |
|---|
| 509 | v[2].set( w*0.5f,0.0f,h); |
|---|
| 510 | v[3].set(-w*0.5f,0.0f,h); |
|---|
| 511 | |
|---|
| 512 | c[0] = color; |
|---|
| 513 | |
|---|
| 514 | t[0].set(0.0f,0.0f); |
|---|
| 515 | t[1].set(1.0f,0.0f); |
|---|
| 516 | t[2].set(1.0f,1.0f); |
|---|
| 517 | t[3].set(0.0f,1.0f); |
|---|
| 518 | |
|---|
| 519 | osg::Geometry *geom = new osg::Geometry; |
|---|
| 520 | |
|---|
| 521 | geom->setVertexArray( &v ); |
|---|
| 522 | |
|---|
| 523 | geom->setTexCoordArray( 0, &t ); |
|---|
| 524 | |
|---|
| 525 | geom->setColorArray( &c ); |
|---|
| 526 | geom->setColorBinding( osg::Geometry::BIND_OVERALL ); |
|---|
| 527 | |
|---|
| 528 | geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4) ); |
|---|
| 529 | |
|---|
| 530 | return geom; |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | osg::Geometry* ForestTechniqueManager::createOrthogonalQuads( const osg::Vec3& pos, float w, float h, osg::UByte4 color ) |
|---|
| 534 | { |
|---|
| 535 | |
|---|
| 536 | osg::Vec3Array& v = *(new osg::Vec3Array(8)); |
|---|
| 537 | osg::Vec2Array& t = *(new osg::Vec2Array(8)); |
|---|
| 538 | osg::UByte4Array& c = *(new osg::UByte4Array(1)); |
|---|
| 539 | |
|---|
| 540 | float rotation = random(0.0f,osg::PI/2.0f); |
|---|
| 541 | float sw = sinf(rotation)*w*0.5f; |
|---|
| 542 | float cw = cosf(rotation)*w*0.5f; |
|---|
| 543 | |
|---|
| 544 | v[0].set(pos.x()-sw,pos.y()-cw,pos.z()+0.0f); |
|---|
| 545 | v[1].set(pos.x()+sw,pos.y()+cw,pos.z()+0.0f); |
|---|
| 546 | v[2].set(pos.x()+sw,pos.y()+cw,pos.z()+h); |
|---|
| 547 | v[3].set(pos.x()-sw,pos.y()-cw,pos.z()+h); |
|---|
| 548 | |
|---|
| 549 | v[4].set(pos.x()-cw,pos.y()+sw,pos.z()+0.0f); |
|---|
| 550 | v[5].set(pos.x()+cw,pos.y()-sw,pos.z()+0.0f); |
|---|
| 551 | v[6].set(pos.x()+cw,pos.y()-sw,pos.z()+h); |
|---|
| 552 | v[7].set(pos.x()-cw,pos.y()+sw,pos.z()+h); |
|---|
| 553 | |
|---|
| 554 | c[0] = color; |
|---|
| 555 | |
|---|
| 556 | t[0].set(0.0f,0.0f); |
|---|
| 557 | t[1].set(1.0f,0.0f); |
|---|
| 558 | t[2].set(1.0f,1.0f); |
|---|
| 559 | t[3].set(0.0f,1.0f); |
|---|
| 560 | |
|---|
| 561 | t[4].set(0.0f,0.0f); |
|---|
| 562 | t[5].set(1.0f,0.0f); |
|---|
| 563 | t[6].set(1.0f,1.0f); |
|---|
| 564 | t[7].set(0.0f,1.0f); |
|---|
| 565 | |
|---|
| 566 | osg::Geometry *geom = new osg::Geometry; |
|---|
| 567 | |
|---|
| 568 | geom->setVertexArray( &v ); |
|---|
| 569 | |
|---|
| 570 | geom->setTexCoordArray( 0, &t ); |
|---|
| 571 | |
|---|
| 572 | geom->setColorArray( &c ); |
|---|
| 573 | geom->setColorBinding( osg::Geometry::BIND_OVERALL ); |
|---|
| 574 | |
|---|
| 575 | geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8) ); |
|---|
| 576 | |
|---|
| 577 | return geom; |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | osg::Node* ForestTechniqueManager::createBillboardGraph(Cell* cell,osg::StateSet* stateset) |
|---|
| 581 | { |
|---|
| 582 | bool needGroup = !(cell->_cells.empty()); |
|---|
| 583 | bool needBillboard = !(cell->_trees.empty()); |
|---|
| 584 | |
|---|
| 585 | osg::Billboard* billboard = 0; |
|---|
| 586 | osg::Group* group = 0; |
|---|
| 587 | |
|---|
| 588 | if (needBillboard) |
|---|
| 589 | { |
|---|
| 590 | billboard = new osg::Billboard; |
|---|
| 591 | billboard->setStateSet(stateset); |
|---|
| 592 | for(TreeList::iterator itr=cell->_trees.begin(); |
|---|
| 593 | itr!=cell->_trees.end(); |
|---|
| 594 | ++itr) |
|---|
| 595 | { |
|---|
| 596 | Tree& tree = **itr; |
|---|
| 597 | billboard->addDrawable(createSprite(tree._width,tree._height,tree._color),tree._position); |
|---|
| 598 | } |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | if (needGroup) |
|---|
| 602 | { |
|---|
| 603 | group = new osg::Group; |
|---|
| 604 | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
|---|
| 605 | itr!=cell->_cells.end(); |
|---|
| 606 | ++itr) |
|---|
| 607 | { |
|---|
| 608 | group->addChild(createBillboardGraph(itr->get(),stateset)); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | if (billboard) group->addChild(billboard); |
|---|
| 612 | |
|---|
| 613 | } |
|---|
| 614 | if (group) return group; |
|---|
| 615 | else return billboard; |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | osg::Node* ForestTechniqueManager::createXGraph(Cell* cell,osg::StateSet* stateset) |
|---|
| 619 | { |
|---|
| 620 | bool needGroup = !(cell->_cells.empty()); |
|---|
| 621 | bool needTrees = !(cell->_trees.empty()); |
|---|
| 622 | |
|---|
| 623 | osg::Geode* geode = 0; |
|---|
| 624 | osg::Group* group = 0; |
|---|
| 625 | |
|---|
| 626 | if (needTrees) |
|---|
| 627 | { |
|---|
| 628 | geode = new osg::Geode; |
|---|
| 629 | geode->setStateSet(stateset); |
|---|
| 630 | |
|---|
| 631 | for(TreeList::iterator itr=cell->_trees.begin(); |
|---|
| 632 | itr!=cell->_trees.end(); |
|---|
| 633 | ++itr) |
|---|
| 634 | { |
|---|
| 635 | Tree& tree = **itr; |
|---|
| 636 | geode->addDrawable(createOrthogonalQuads(tree._position,tree._width,tree._height,tree._color)); |
|---|
| 637 | } |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | if (needGroup) |
|---|
| 641 | { |
|---|
| 642 | group = new osg::Group; |
|---|
| 643 | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
|---|
| 644 | itr!=cell->_cells.end(); |
|---|
| 645 | ++itr) |
|---|
| 646 | { |
|---|
| 647 | group->addChild(createXGraph(itr->get(),stateset)); |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | if (geode) group->addChild(geode); |
|---|
| 651 | |
|---|
| 652 | } |
|---|
| 653 | if (group) return group; |
|---|
| 654 | else return geode; |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | osg::Node* ForestTechniqueManager::createTransformGraph(Cell* cell,osg::StateSet* stateset) |
|---|
| 658 | { |
|---|
| 659 | bool needGroup = !(cell->_cells.empty()); |
|---|
| 660 | bool needTrees = !(cell->_trees.empty()); |
|---|
| 661 | |
|---|
| 662 | osg::Group* transform_group = 0; |
|---|
| 663 | osg::Group* group = 0; |
|---|
| 664 | |
|---|
| 665 | if (needTrees) |
|---|
| 666 | { |
|---|
| 667 | transform_group = new osg::Group; |
|---|
| 668 | |
|---|
| 669 | osg::Geometry* geometry = createOrthogonalQuads(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f,osg::UByte4(255,255,255,255)); |
|---|
| 670 | |
|---|
| 671 | for(TreeList::iterator itr=cell->_trees.begin(); |
|---|
| 672 | itr!=cell->_trees.end(); |
|---|
| 673 | ++itr) |
|---|
| 674 | { |
|---|
| 675 | Tree& tree = **itr; |
|---|
| 676 | osg::MatrixTransform* transform = new osg::MatrixTransform; |
|---|
| 677 | transform->setMatrix(osg::Matrix::scale(tree._width,tree._width,tree._height)*osg::Matrix::translate(tree._position)); |
|---|
| 678 | |
|---|
| 679 | osg::Geode* geode = new osg::Geode; |
|---|
| 680 | geode->setStateSet(stateset); |
|---|
| 681 | geode->addDrawable(geometry); |
|---|
| 682 | transform->addChild(geode); |
|---|
| 683 | transform_group->addChild(transform); |
|---|
| 684 | } |
|---|
| 685 | } |
|---|
| 686 | |
|---|
| 687 | if (needGroup) |
|---|
| 688 | { |
|---|
| 689 | group = new osg::Group; |
|---|
| 690 | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
|---|
| 691 | itr!=cell->_cells.end(); |
|---|
| 692 | ++itr) |
|---|
| 693 | { |
|---|
| 694 | group->addChild(createTransformGraph(itr->get(),stateset)); |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | if (transform_group) group->addChild(transform_group); |
|---|
| 698 | |
|---|
| 699 | } |
|---|
| 700 | if (group) return group; |
|---|
| 701 | else return transform_group; |
|---|
| 702 | } |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | osg::Node* ForestTechniqueManager::createScene() |
|---|
| 706 | { |
|---|
| 707 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 708 | osg::Vec3 size(1000.0f,1000.0f,200.0f); |
|---|
| 709 | unsigned int numTreesToCreates = 10000; |
|---|
| 710 | |
|---|
| 711 | std::cout<<"Creating terrain..."; |
|---|
| 712 | osg::ref_ptr<osg::Node> terrain = createTerrain(origin,size); |
|---|
| 713 | std::cout<<"done."<<std::endl; |
|---|
| 714 | |
|---|
| 715 | std::cout<<"Creating tree locations...";std::cout.flush(); |
|---|
| 716 | TreeList trees; |
|---|
| 717 | createTreeList(terrain.get(),origin,size,numTreesToCreates,trees); |
|---|
| 718 | std::cout<<"done."<<std::endl; |
|---|
| 719 | |
|---|
| 720 | std::cout<<"Creating cell subdivision..."; |
|---|
| 721 | osg::ref_ptr<Cell> cell = new Cell; |
|---|
| 722 | cell->addTrees(trees); |
|---|
| 723 | cell->divide(); |
|---|
| 724 | std::cout<<"done."<<std::endl; |
|---|
| 725 | |
|---|
| 726 | |
|---|
| 727 | osg::Texture2D *tex = new osg::Texture2D; |
|---|
| 728 | tex->setImage(osgDB::readImageFile("Images/tree0.rgba")); |
|---|
| 729 | |
|---|
| 730 | osg::StateSet *dstate = new osg::StateSet; |
|---|
| 731 | { |
|---|
| 732 | dstate->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); |
|---|
| 733 | dstate->setTextureAttribute(0, new osg::TexEnv ); |
|---|
| 734 | |
|---|
| 735 | dstate->setAttributeAndModes( new osg::BlendFunc, osg::StateAttribute::ON ); |
|---|
| 736 | |
|---|
| 737 | osg::AlphaFunc* alphaFunc = new osg::AlphaFunc; |
|---|
| 738 | alphaFunc->setFunction(osg::AlphaFunc::GEQUAL,0.05f); |
|---|
| 739 | dstate->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON ); |
|---|
| 740 | |
|---|
| 741 | dstate->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
|---|
| 742 | |
|---|
| 743 | dstate->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | _techniqueSwitch = new osg::Switch; |
|---|
| 748 | |
|---|
| 749 | std::cout<<"Creating billboard based forest..."; |
|---|
| 750 | _techniqueSwitch->addChild(createBillboardGraph(cell.get(),dstate)); |
|---|
| 751 | std::cout<<"done."<<std::endl; |
|---|
| 752 | |
|---|
| 753 | std::cout<<"Creating double quad based forest..."; |
|---|
| 754 | _techniqueSwitch->addChild(createXGraph(cell.get(),dstate)); |
|---|
| 755 | std::cout<<"done."<<std::endl; |
|---|
| 756 | |
|---|
| 757 | std::cout<<"Creating transform based forest..."; |
|---|
| 758 | _techniqueSwitch->addChild(createTransformGraph(cell.get(),dstate)); |
|---|
| 759 | std::cout<<"done."<<std::endl; |
|---|
| 760 | |
|---|
| 761 | _currentTechnique = 0; |
|---|
| 762 | _techniqueSwitch->setSingleChildOn(_currentTechnique); |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | osg::Group* scene = new osg::Group; |
|---|
| 766 | |
|---|
| 767 | scene->addChild(terrain.get()); |
|---|
| 768 | scene->addChild(_techniqueSwitch.get()); |
|---|
| 769 | |
|---|
| 770 | return scene; |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | int main( int argc, char **argv ) |
|---|
| 774 | { |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 778 | |
|---|
| 779 | |
|---|
| 780 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates the osg::Shape classes."); |
|---|
| 781 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 782 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | osgProducer::Viewer viewer(arguments); |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 789 | |
|---|
| 790 | osg::ref_ptr<ForestTechniqueManager> ttm = new ForestTechniqueManager; |
|---|
| 791 | |
|---|
| 792 | viewer.getEventHandlerList().push_front(new TechniqueEventHandler(ttm.get())); |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 799 | { |
|---|
| 800 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 801 | return 1; |
|---|
| 802 | } |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | if (arguments.errors()) |
|---|
| 809 | { |
|---|
| 810 | arguments.writeErrorMessages(std::cout); |
|---|
| 811 | return 1; |
|---|
| 812 | } |
|---|
| 813 | |
|---|
| 814 | osg::Node* node = ttm->createScene(); |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | viewer.setSceneData( node ); |
|---|
| 818 | |
|---|
| 819 | |
|---|
| 820 | viewer.realize(); |
|---|
| 821 | |
|---|
| 822 | while( !viewer.done() ) |
|---|
| 823 | { |
|---|
| 824 | |
|---|
| 825 | viewer.sync(); |
|---|
| 826 | |
|---|
| 827 | |
|---|
| 828 | |
|---|
| 829 | viewer.update(); |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | viewer.frame(); |
|---|
| 833 | |
|---|
| 834 | } |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | viewer.sync(); |
|---|
| 838 | |
|---|
| 839 | return 0; |
|---|
| 840 | } |
|---|