| 34 | | // class to create the forest and manage the movement between various techniques. |
| 35 | | class ForestTechniqueManager : public osg::Referenced |
| 36 | | { |
| 37 | | public: |
| 38 | | |
| 39 | | ForestTechniqueManager() {} |
| 40 | | |
| 41 | | class Tree : public osg::Referenced |
| 42 | | { |
| 43 | | public: |
| 44 | | |
| 45 | | Tree(): |
| 46 | | _color(255,255,255,255), |
| 47 | | _width(1.0f), |
| 48 | | _height(1.0f), |
| 49 | | _type(0) {} |
| 50 | | |
| 51 | | Tree(const osg::Vec3& position, const osg::Vec4ub& color, float width, float height, unsigned int type): |
| 52 | | _position(position), |
| 53 | | _color(color), |
| 54 | | _width(width), |
| 55 | | _height(height), |
| 56 | | _type(type) {} |
| 57 | | |
| 58 | | osg::Vec3 _position; |
| 59 | | osg::Vec4ub _color; |
| 60 | | float _width; |
| 61 | | float _height; |
| 62 | | unsigned int _type; |
| 63 | | }; |
| 64 | | |
| 65 | | typedef std::vector< osg::ref_ptr<Tree> > TreeList; |
| 66 | | |
| 67 | | class Cell : public osg::Referenced |
| 68 | | { |
| 69 | | public: |
| 70 | | typedef std::vector< osg::ref_ptr<Cell> > CellList; |
| 71 | | |
| 72 | | Cell():_parent(0) {} |
| 73 | | Cell(osg::BoundingBox& bb):_parent(0), _bb(bb) {} |
| 74 | | |
| 75 | | void addCell(Cell* cell) { cell->_parent=this; _cells.push_back(cell); } |
| 76 | | |
| 77 | | void addTree(Tree* tree) { _trees.push_back(tree); } |
| 78 | | |
| 79 | | void addTrees(const TreeList& trees) { _trees.insert(_trees.end(),trees.begin(),trees.end()); } |
| 80 | | |
| 81 | | void computeBound(); |
| 82 | | |
| 83 | | bool contains(const osg::Vec3& position) const { return _bb.contains(position); } |
| 84 | | |
| 85 | | bool divide(unsigned int maxNumTreesPerCell=10); |
| 86 | | |
| 87 | | bool devide(bool xAxis, bool yAxis, bool zAxis); |
| 88 | | |
| 89 | | void bin(); |
| 90 | | |
| 91 | | |
| 92 | | Cell* _parent; |
| 93 | | osg::BoundingBox _bb; |
| 94 | | CellList _cells; |
| 95 | | TreeList _trees; |
| 96 | | |
| 97 | | }; |
| 98 | | |
| 99 | | float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } |
| 100 | | int random(int min,int max) { return min + (int)((float)(max-min)*(float)rand()/(float)RAND_MAX); } |
| 101 | | |
| 102 | | osg::Geode* createTerrain(const osg::Vec3& origin, const osg::Vec3& size); |
| 103 | | |
| 104 | | void createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees); |
| 105 | | |
| 106 | | osg::Geometry* createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h ); |
| 107 | | |
| 108 | | osg::Node* createShaderGraph(Cell* cell,osg::StateSet* stateset); |
| 109 | | |
| 110 | | osg::Node* createScene(unsigned int numTreesToCreates); |
| 111 | | |
| 112 | | |
| 113 | | }; |
| 114 | | |
| 115 | | // event handler to capture keyboard events and use them to advance the technique used for rendering |
| 116 | | void ForestTechniqueManager::Cell::computeBound() |
| 117 | | { |
| 118 | | _bb.init(); |
| 119 | | for(CellList::iterator citr=_cells.begin(); |
| 120 | | citr!=_cells.end(); |
| 121 | | ++citr) |
| 122 | | { |
| 123 | | (*citr)->computeBound(); |
| 124 | | _bb.expandBy((*citr)->_bb); |
| 125 | | } |
| 126 | | |
| 127 | | for(TreeList::iterator titr=_trees.begin(); |
| 128 | | titr!=_trees.end(); |
| 129 | | ++titr) |
| 130 | | { |
| 131 | | _bb.expandBy((*titr)->_position); |
| 132 | | } |
| 133 | | } |
| 134 | | |
| 135 | | bool ForestTechniqueManager::Cell::divide(unsigned int maxNumTreesPerCell) |
| 136 | | { |
| 137 | | |
| 138 | | if (_trees.size()<=maxNumTreesPerCell) return false; |
| 139 | | |
| 140 | | computeBound(); |
| 141 | | |
| 142 | | float radius = _bb.radius(); |
| 143 | | float divide_distance = radius*0.7f; |
| 144 | | if (devide((_bb.xMax()-_bb.xMin())>divide_distance,(_bb.yMax()-_bb.yMin())>divide_distance,(_bb.zMax()-_bb.zMin())>divide_distance)) |
| 145 | | { |
| 146 | | // recusively divide the new cells till maxNumTreesPerCell is met. |
| 147 | | for(CellList::iterator citr=_cells.begin(); |
| 148 | | citr!=_cells.end(); |
| 149 | | ++citr) |
| 150 | | { |
| 151 | | (*citr)->divide(maxNumTreesPerCell); |
| 152 | | } |
| 153 | | return true; |
| 154 | | } |
| 155 | | else |
| 156 | | { |
| 157 | | return false; |
| 158 | | } |
| 159 | | } |
| 160 | | |
| 161 | | bool ForestTechniqueManager::Cell::devide(bool xAxis, bool yAxis, bool zAxis) |
| 162 | | { |
| 163 | | if (!(xAxis || yAxis || zAxis)) return false; |
| 164 | | |
| 165 | | if (_cells.empty()) |
| 166 | | _cells.push_back(new Cell(_bb)); |
| 167 | | |
| 168 | | if (xAxis) |
| 169 | | { |
| 170 | | unsigned int numCellsToDivide=_cells.size(); |
| 171 | | for(unsigned int i=0;i<numCellsToDivide;++i) |
| 172 | | { |
| 173 | | Cell* orig_cell = _cells[i].get(); |
| 174 | | Cell* new_cell = new Cell(orig_cell->_bb); |
| 175 | | |
| 176 | | float xCenter = (orig_cell->_bb.xMin()+orig_cell->_bb.xMax())*0.5f; |
| 177 | | orig_cell->_bb.xMax() = xCenter; |
| 178 | | new_cell->_bb.xMin() = xCenter; |
| 179 | | |
| 180 | | _cells.push_back(new_cell); |
| 181 | | } |
| 182 | | } |
| 183 | | |
| 184 | | if (yAxis) |
| 185 | | { |
| 186 | | unsigned int numCellsToDivide=_cells.size(); |
| 187 | | for(unsigned int i=0;i<numCellsToDivide;++i) |
| 188 | | { |
| 189 | | Cell* orig_cell = _cells[i].get(); |
| 190 | | Cell* new_cell = new Cell(orig_cell->_bb); |
| 191 | | |
| 192 | | float yCenter = (orig_cell->_bb.yMin()+orig_cell->_bb.yMax())*0.5f; |
| 193 | | orig_cell->_bb.yMax() = yCenter; |
| 194 | | new_cell->_bb.yMin() = yCenter; |
| 195 | | |
| 196 | | _cells.push_back(new_cell); |
| 197 | | } |
| 198 | | } |
| 199 | | |
| 200 | | if (zAxis) |
| 201 | | { |
| 202 | | unsigned int numCellsToDivide=_cells.size(); |
| 203 | | for(unsigned int i=0;i<numCellsToDivide;++i) |
| 204 | | { |
| 205 | | Cell* orig_cell = _cells[i].get(); |
| 206 | | Cell* new_cell = new Cell(orig_cell->_bb); |
| 207 | | |
| 208 | | float zCenter = (orig_cell->_bb.zMin()+orig_cell->_bb.zMax())*0.5f; |
| 209 | | orig_cell->_bb.zMax() = zCenter; |
| 210 | | new_cell->_bb.zMin() = zCenter; |
| 211 | | |
| 212 | | _cells.push_back(new_cell); |
| 213 | | } |
| 214 | | } |
| 215 | | |
| 216 | | bin(); |
| 217 | | |
| 218 | | return true; |
| 219 | | |
| 220 | | } |
| 221 | | |
| 222 | | void ForestTechniqueManager::Cell::bin() |
| 223 | | { |
| 224 | | // put trees in apprpriate cells. |
| 225 | | TreeList treesNotAssigned; |
| 226 | | for(TreeList::iterator titr=_trees.begin(); |
| 227 | | titr!=_trees.end(); |
| 228 | | ++titr) |
| 229 | | { |
| 230 | | Tree* tree = titr->get(); |
| 231 | | bool assigned = false; |
| 232 | | for(CellList::iterator citr=_cells.begin(); |
| 233 | | citr!=_cells.end() && !assigned; |
| 234 | | ++citr) |
| 235 | | { |
| 236 | | if ((*citr)->contains(tree->_position)) |
| 237 | | { |
| 238 | | (*citr)->addTree(tree); |
| 239 | | assigned = true; |
| 240 | | } |
| 241 | | } |
| 242 | | if (!assigned) treesNotAssigned.push_back(tree); |
| 243 | | } |
| 244 | | |
| 245 | | // put the unassigned trees back into the original local tree list. |
| 246 | | _trees.swap(treesNotAssigned); |
| 247 | | |
| 248 | | |
| 249 | | // prune empty cells. |
| 250 | | CellList cellsNotEmpty; |
| 251 | | for(CellList::iterator citr=_cells.begin(); |
| 252 | | citr!=_cells.end(); |
| 253 | | ++citr) |
| 254 | | { |
| 255 | | if (!((*citr)->_trees.empty())) |
| 256 | | { |
| 257 | | cellsNotEmpty.push_back(*citr); |
| 258 | | } |
| 259 | | } |
| 260 | | _cells.swap(cellsNotEmpty); |
| 261 | | |
| 262 | | |
| 263 | | } |
| 264 | | |
| 265 | | |
| 266 | | void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees) |
| 267 | | { |
| 268 | | |
| 269 | | float max_TreeHeight = sqrtf(size.length2()/(float)numTreesToCreate); |
| 270 | | float max_TreeWidth = max_TreeHeight*0.5f; |
| 271 | | |
| 272 | | float min_TreeHeight = max_TreeHeight*0.3f; |
| 273 | | float min_TreeWidth = min_TreeHeight*0.5f; |
| 274 | | |
| 275 | | trees.reserve(trees.size()+numTreesToCreate); |
| 276 | | |
| 277 | | |
| 278 | | for(unsigned int i=0;i<numTreesToCreate;++i) |
| 279 | | { |
| 280 | | Tree* tree = new Tree; |
| 281 | | tree->_position.set(random(origin.x(),origin.x()+size.x()),random(origin.y(),origin.y()+size.y()),origin.z()); |
| 282 | | tree->_color.set(random(128,255),random(128,255),random(128,255),255); |
| 283 | | tree->_width = random(min_TreeWidth,max_TreeWidth); |
| 284 | | tree->_height = random(min_TreeHeight,max_TreeHeight); |
| 285 | | tree->_type = 0; |
| 286 | | |
| 287 | | if (terrain) |
| 288 | | { |
| 289 | | osgUtil::IntersectVisitor iv; |
| 290 | | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
| 291 | | |
| 292 | | segDown->set(tree->_position,tree->_position+osg::Vec3(0.0f,0.0f,size.z())); |
| 293 | | iv.addLineSegment(segDown.get()); |
| 294 | | |
| 295 | | terrain->accept(iv); |
| 296 | | |
| 297 | | if (iv.hits()) |
| 298 | | { |
| 299 | | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
| 300 | | if (!hitList.empty()) |
| 301 | | { |
| 302 | | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
| 303 | | osg::Vec3 np = hitList.front().getWorldIntersectNormal(); |
| 304 | | tree->_position = ip; |
| 305 | | } |
| 306 | | } |
| 307 | | } |
| 308 | | |
| 309 | | trees.push_back(tree); |
| 310 | | } |
| 311 | | } |
| 312 | | |
| 313 | | osg::Geometry* ForestTechniqueManager::createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h) |
| 314 | | { |
| 315 | | // set up the coords |
| 316 | | osg::Vec3Array& v = *(new osg::Vec3Array(8)); |
| 317 | | osg::Vec2Array& t = *(new osg::Vec2Array(8)); |
| 318 | | |
| 319 | | float rotation = random(0.0f,osg::PI/2.0f); |
| 320 | | float sw = sinf(rotation)*w*0.5f; |
| 321 | | float cw = cosf(rotation)*w*0.5f; |
| 322 | | |
| 323 | | v[0].set(pos.x()-sw,pos.y()-cw,pos.z()+0.0f); |
| 324 | | v[1].set(pos.x()+sw,pos.y()+cw,pos.z()+0.0f); |
| 325 | | v[2].set(pos.x()+sw,pos.y()+cw,pos.z()+h); |
| 326 | | v[3].set(pos.x()-sw,pos.y()-cw,pos.z()+h); |
| 327 | | |
| 328 | | v[4].set(pos.x()-cw,pos.y()+sw,pos.z()+0.0f); |
| 329 | | v[5].set(pos.x()+cw,pos.y()-sw,pos.z()+0.0f); |
| 330 | | v[6].set(pos.x()+cw,pos.y()-sw,pos.z()+h); |
| 331 | | v[7].set(pos.x()-cw,pos.y()+sw,pos.z()+h); |
| 332 | | |
| 333 | | t[0].set(0.0f,0.0f); |
| 334 | | t[1].set(1.0f,0.0f); |
| 335 | | t[2].set(1.0f,1.0f); |
| 336 | | t[3].set(0.0f,1.0f); |
| 337 | | |
| 338 | | t[4].set(0.0f,0.0f); |
| 339 | | t[5].set(1.0f,0.0f); |
| 340 | | t[6].set(1.0f,1.0f); |
| 341 | | t[7].set(0.0f,1.0f); |
| 342 | | |
| 343 | | osg::Geometry *geom = new osg::Geometry; |
| 344 | | |
| 345 | | geom->setVertexArray( &v ); |
| 346 | | |
| 347 | | geom->setTexCoordArray( 0, &t ); |
| 348 | | |
| 349 | | geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8) ); |
| 350 | | |
| 351 | | return geom; |
| 352 | | } |
| 353 | | |
| 354 | | class ShaderGeometry : public osg::Drawable |
| 355 | | { |
| 356 | | public: |
| 357 | | ShaderGeometry() { setUseDisplayList(false); } |
| 358 | | |
| 359 | | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
| 360 | | ShaderGeometry(const ShaderGeometry& ShaderGeometry,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): |
| 361 | | osg::Drawable(ShaderGeometry,copyop) {} |
| 362 | | |
| 363 | | META_Object(osg,ShaderGeometry) |
| 364 | | |
| 365 | | typedef std::vector<osg::Vec4> PositionSizeList; |
| 366 | | |
| 367 | | virtual void drawImplementation(osg::RenderInfo& renderInfo) const |
| 368 | | { |
| 369 | | for(PositionSizeList::const_iterator itr = _trees.begin(); |
| 370 | | itr != _trees.end(); |
| 371 | | ++itr) |
| 372 | | { |
| 373 | | glColor4fv(itr->ptr()); |
| 374 | | _geometry->draw(renderInfo); |
| 375 | | } |
| 376 | | } |
| 377 | | |
| 378 | | virtual osg::BoundingBox computeBound() const |
| 379 | | { |
| 380 | | osg::BoundingBox geom_box = _geometry->getBound(); |
| 381 | | osg::BoundingBox bb; |
| 382 | | for(PositionSizeList::const_iterator itr = _trees.begin(); |
| 383 | | itr != _trees.end(); |
| 384 | | ++itr) |
| 385 | | { |
| 386 | | bb.expandBy(geom_box.corner(0)*(*itr)[3] + |
| 387 | | osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] )); |
| 388 | | bb.expandBy(geom_box.corner(7)*(*itr)[3] + |
| 389 | | osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] )); |
| 390 | | } |
| 391 | | return bb; |
| 392 | | } |
| 393 | | |
| 394 | | void setGeometry(osg::Geometry* geometry) |
| 395 | | { |
| 396 | | _geometry = geometry; |
| 397 | | } |
| 398 | | |
| 399 | | void addTree(ForestTechniqueManager::Tree& tree) |
| 400 | | { |
| 401 | | _trees.push_back(osg::Vec4(tree._position.x(), tree._position.y(), tree._position.z(), tree._height)); |
| 402 | | } |
| 403 | | |
| 404 | | osg::ref_ptr<osg::Geometry> _geometry; |
| 405 | | |
| 406 | | PositionSizeList _trees; |
| 407 | | |
| 408 | | protected: |
| 409 | | |
| 410 | | virtual ~ShaderGeometry() {} |
| 411 | | |
| 412 | | }; |
| 413 | | |
| 414 | | osg::Geometry* shared_geometry = 0; |
| 415 | | |
| 416 | | osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* stateset) |
| 417 | | { |
| 418 | | if (shared_geometry==0) |
| 419 | | { |
| 420 | | shared_geometry = createOrthogonalQuadsNoColor(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f); |
| 421 | | //shared_geometry->setUseDisplayList(false); |
| 422 | | } |
| 423 | | |
| 424 | | |
| 425 | | bool needGroup = !(cell->_cells.empty()); |
| 426 | | bool needTrees = !(cell->_trees.empty()); |
| 427 | | |
| 428 | | osg::Geode* geode = 0; |
| 429 | | osg::Group* group = 0; |
| 430 | | |
| 431 | | if (needTrees) |
| 432 | | { |
| 433 | | geode = new osg::Geode; |
| 434 | | |
| 435 | | ShaderGeometry* shader_geometry = new ShaderGeometry; |
| 436 | | shader_geometry->setGeometry(shared_geometry); |
| 437 | | |
| 438 | | |
| 439 | | for(TreeList::iterator itr=cell->_trees.begin(); |
| 440 | | itr!=cell->_trees.end(); |
| 441 | | ++itr) |
| 442 | | { |
| 443 | | Tree& tree = **itr; |
| 444 | | shader_geometry->addTree(tree); |
| 445 | | |
| 446 | | } |
| 447 | | |
| 448 | | geode->setStateSet(stateset); |
| 449 | | geode->addDrawable(shader_geometry); |
| 450 | | } |
| 451 | | |
| 452 | | if (needGroup) |
| 453 | | { |
| 454 | | group = new osg::Group; |
| 455 | | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
| 456 | | itr!=cell->_cells.end(); |
| 457 | | ++itr) |
| 458 | | { |
| 459 | | group->addChild(createShaderGraph(itr->get(),stateset)); |
| 460 | | } |
| 461 | | |
| 462 | | if (geode) group->addChild(geode); |
| 463 | | |
| 464 | | } |
| 465 | | if (group) return group; |
| 466 | | else return geode; |
| 467 | | } |
| 468 | | |
| 469 | | osg::Node* ForestTechniqueManager::createScene(unsigned int /*numTreesToCreates*/) |
| | 34 | osg::Node* createScene() |