| 1 | #include <osg/AlphaFunc> |
|---|
| 2 | #include <osg/Billboard> |
|---|
| 3 | #include <osg/BlendFunc> |
|---|
| 4 | #include <osg/Depth> |
|---|
| 5 | #include <osg/Geode> |
|---|
| 6 | #include <osg/Geometry> |
|---|
| 7 | #include <osg/Material> |
|---|
| 8 | #include <osg/Math> |
|---|
| 9 | #include <osg/MatrixTransform> |
|---|
| 10 | #include <osg/PolygonOffset> |
|---|
| 11 | #include <osg/Projection> |
|---|
| 12 | #include <osg/ShapeDrawable> |
|---|
| 13 | #include <osg/StateSet> |
|---|
| 14 | #include <osg/Switch> |
|---|
| 15 | #include <osg/Texture2D> |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <osgDB/FileUtils> |
|---|
| 19 | |
|---|
| 20 | #include <osgUtil/IntersectVisitor> |
|---|
| 21 | #include <osgUtil/SmoothingVisitor> |
|---|
| 22 | |
|---|
| 23 | #include <osgText/Text> |
|---|
| 24 | |
|---|
| 25 | #include <osgProducer/Viewer> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | #include "../osghangglide/terrain_coords.h" |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | class ForestTechniqueManager : public osg::Referenced |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | ForestTechniqueManager() {} |
|---|
| 36 | |
|---|
| 37 | class Tree : public osg::Referenced |
|---|
| 38 | { |
|---|
| 39 | public: |
|---|
| 40 | |
|---|
| 41 | Tree(): |
|---|
| 42 | _color(255,255,255,255), |
|---|
| 43 | _width(1.0f), |
|---|
| 44 | _height(1.0f), |
|---|
| 45 | _type(0) {} |
|---|
| 46 | |
|---|
| 47 | Tree(const osg::Vec3& position, const osg::Vec4ub& color, float width, float height, unsigned int type): |
|---|
| 48 | _position(position), |
|---|
| 49 | _color(color), |
|---|
| 50 | _width(width), |
|---|
| 51 | _height(height), |
|---|
| 52 | _type(type) {} |
|---|
| 53 | |
|---|
| 54 | osg::Vec3 _position; |
|---|
| 55 | osg::Vec4ub _color; |
|---|
| 56 | float _width; |
|---|
| 57 | float _height; |
|---|
| 58 | unsigned int _type; |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | typedef std::vector< osg::ref_ptr<Tree> > TreeList; |
|---|
| 62 | |
|---|
| 63 | class Cell : public osg::Referenced |
|---|
| 64 | { |
|---|
| 65 | public: |
|---|
| 66 | typedef std::vector< osg::ref_ptr<Cell> > CellList; |
|---|
| 67 | |
|---|
| 68 | Cell():_parent(0) {} |
|---|
| 69 | Cell(osg::BoundingBox& bb):_parent(0), _bb(bb) {} |
|---|
| 70 | |
|---|
| 71 | void addCell(Cell* cell) { cell->_parent=this; _cells.push_back(cell); } |
|---|
| 72 | |
|---|
| 73 | void addTree(Tree* tree) { _trees.push_back(tree); } |
|---|
| 74 | |
|---|
| 75 | void addTrees(const TreeList& trees) { _trees.insert(_trees.end(),trees.begin(),trees.end()); } |
|---|
| 76 | |
|---|
| 77 | void computeBound(); |
|---|
| 78 | |
|---|
| 79 | bool contains(const osg::Vec3& position) const { return _bb.contains(position); } |
|---|
| 80 | |
|---|
| 81 | bool divide(unsigned int maxNumTreesPerCell=10); |
|---|
| 82 | |
|---|
| 83 | bool devide(bool xAxis, bool yAxis, bool zAxis); |
|---|
| 84 | |
|---|
| 85 | void bin(); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | Cell* _parent; |
|---|
| 89 | osg::BoundingBox _bb; |
|---|
| 90 | CellList _cells; |
|---|
| 91 | TreeList _trees; |
|---|
| 92 | |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } |
|---|
| 96 | int random(int min,int max) { return min + (int)((float)(max-min)*(float)rand()/(float)RAND_MAX); } |
|---|
| 97 | |
|---|
| 98 | osg::Geode* createTerrain(const osg::Vec3& origin, const osg::Vec3& size); |
|---|
| 99 | |
|---|
| 100 | void createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees); |
|---|
| 101 | |
|---|
| 102 | osg::Geometry* createSprite( float w, float h, osg::Vec4ub color ); |
|---|
| 103 | |
|---|
| 104 | osg::Geometry* createOrthogonalQuads( const osg::Vec3& pos, float w, float h, osg::Vec4ub color ); |
|---|
| 105 | osg::Geometry* createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h ); |
|---|
| 106 | |
|---|
| 107 | osg::Node* createBillboardGraph(Cell* cell,osg::StateSet* stateset); |
|---|
| 108 | |
|---|
| 109 | osg::Node* createXGraph(Cell* cell,osg::StateSet* stateset); |
|---|
| 110 | |
|---|
| 111 | osg::Node* createTransformGraph(Cell* cell,osg::StateSet* stateset); |
|---|
| 112 | |
|---|
| 113 | osg::Node* createShaderGraph(Cell* cell,osg::StateSet* stateset); |
|---|
| 114 | |
|---|
| 115 | osg::Node* createHUDWithText(const std::string& text); |
|---|
| 116 | |
|---|
| 117 | osg::Node* createScene(unsigned int numTreesToCreates); |
|---|
| 118 | |
|---|
| 119 | void advanceToNextTechnique(int delta=1) |
|---|
| 120 | { |
|---|
| 121 | if (_techniqueSwitch.valid()) |
|---|
| 122 | { |
|---|
| 123 | _currentTechnique += delta; |
|---|
| 124 | if (_currentTechnique<0) |
|---|
| 125 | _currentTechnique = _techniqueSwitch->getNumChildren()-1; |
|---|
| 126 | if (_currentTechnique>=(int)_techniqueSwitch->getNumChildren()) |
|---|
| 127 | _currentTechnique = 0; |
|---|
| 128 | _techniqueSwitch->setSingleChildOn(_currentTechnique); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | osg::ref_ptr<osg::Switch> _techniqueSwitch; |
|---|
| 133 | int _currentTechnique; |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | class TechniqueEventHandler : public osgGA::GUIEventHandler |
|---|
| 140 | { |
|---|
| 141 | public: |
|---|
| 142 | |
|---|
| 143 | TechniqueEventHandler(ForestTechniqueManager* ttm=0) { _ForestTechniqueManager = ttm; } |
|---|
| 144 | |
|---|
| 145 | META_Object(osgforestApp,TechniqueEventHandler); |
|---|
| 146 | |
|---|
| 147 | virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); } |
|---|
| 148 | |
|---|
| 149 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*); |
|---|
| 150 | |
|---|
| 151 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 152 | |
|---|
| 153 | protected: |
|---|
| 154 | |
|---|
| 155 | ~TechniqueEventHandler() {} |
|---|
| 156 | |
|---|
| 157 | TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {} |
|---|
| 158 | |
|---|
| 159 | osg::ref_ptr<ForestTechniqueManager> _ForestTechniqueManager; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*) |
|---|
| 165 | { |
|---|
| 166 | switch(ea.getEventType()) |
|---|
| 167 | { |
|---|
| 168 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 169 | { |
|---|
| 170 | if (ea.getKey()=='n' || |
|---|
| 171 | ea.getKey()==osgGA::GUIEventAdapter::KEY_Right || |
|---|
| 172 | ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right) |
|---|
| 173 | { |
|---|
| 174 | _ForestTechniqueManager->advanceToNextTechnique(1); |
|---|
| 175 | return true; |
|---|
| 176 | } |
|---|
| 177 | else if (ea.getKey()=='p' || |
|---|
| 178 | ea.getKey()==osgGA::GUIEventAdapter::KEY_Left || |
|---|
| 179 | ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left) |
|---|
| 180 | { |
|---|
| 181 | _ForestTechniqueManager->advanceToNextTechnique(-1); |
|---|
| 182 | return true; |
|---|
| 183 | } |
|---|
| 184 | return false; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | default: |
|---|
| 188 | return false; |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 193 | { |
|---|
| 194 | usage.addKeyboardMouseBinding("n or Left Arrow","Advance to next technique"); |
|---|
| 195 | usage.addKeyboardMouseBinding("p or Right Array","Move to previous technique"); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | void ForestTechniqueManager::Cell::computeBound() |
|---|
| 200 | { |
|---|
| 201 | _bb.init(); |
|---|
| 202 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 203 | citr!=_cells.end(); |
|---|
| 204 | ++citr) |
|---|
| 205 | { |
|---|
| 206 | (*citr)->computeBound(); |
|---|
| 207 | _bb.expandBy((*citr)->_bb); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | for(TreeList::iterator titr=_trees.begin(); |
|---|
| 211 | titr!=_trees.end(); |
|---|
| 212 | ++titr) |
|---|
| 213 | { |
|---|
| 214 | _bb.expandBy((*titr)->_position); |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | bool ForestTechniqueManager::Cell::divide(unsigned int maxNumTreesPerCell) |
|---|
| 219 | { |
|---|
| 220 | |
|---|
| 221 | if (_trees.size()<=maxNumTreesPerCell) return false; |
|---|
| 222 | |
|---|
| 223 | computeBound(); |
|---|
| 224 | |
|---|
| 225 | float radius = _bb.radius(); |
|---|
| 226 | float divide_distance = radius*0.7f; |
|---|
| 227 | if (devide((_bb.xMax()-_bb.xMin())>divide_distance,(_bb.yMax()-_bb.yMin())>divide_distance,(_bb.zMax()-_bb.zMin())>divide_distance)) |
|---|
| 228 | { |
|---|
| 229 | |
|---|
| 230 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 231 | citr!=_cells.end(); |
|---|
| 232 | ++citr) |
|---|
| 233 | { |
|---|
| 234 | (*citr)->divide(maxNumTreesPerCell); |
|---|
| 235 | } |
|---|
| 236 | return true; |
|---|
| 237 | } |
|---|
| 238 | else |
|---|
| 239 | { |
|---|
| 240 | return false; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | bool ForestTechniqueManager::Cell::devide(bool xAxis, bool yAxis, bool zAxis) |
|---|
| 245 | { |
|---|
| 246 | if (!(xAxis || yAxis || zAxis)) return false; |
|---|
| 247 | |
|---|
| 248 | if (_cells.empty()) |
|---|
| 249 | _cells.push_back(new Cell(_bb)); |
|---|
| 250 | |
|---|
| 251 | if (xAxis) |
|---|
| 252 | { |
|---|
| 253 | unsigned int numCellsToDivide=_cells.size(); |
|---|
| 254 | for(unsigned int i=0;i<numCellsToDivide;++i) |
|---|
| 255 | { |
|---|
| 256 | Cell* orig_cell = _cells[i].get(); |
|---|
| 257 | Cell* new_cell = new Cell(orig_cell->_bb); |
|---|
| 258 | |
|---|
| 259 | float xCenter = (orig_cell->_bb.xMin()+orig_cell->_bb.xMax())*0.5f; |
|---|
| 260 | orig_cell->_bb.xMax() = xCenter; |
|---|
| 261 | new_cell->_bb.xMin() = xCenter; |
|---|
| 262 | |
|---|
| 263 | _cells.push_back(new_cell); |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | if (yAxis) |
|---|
| 268 | { |
|---|
| 269 | unsigned int numCellsToDivide=_cells.size(); |
|---|
| 270 | for(unsigned int i=0;i<numCellsToDivide;++i) |
|---|
| 271 | { |
|---|
| 272 | Cell* orig_cell = _cells[i].get(); |
|---|
| 273 | Cell* new_cell = new Cell(orig_cell->_bb); |
|---|
| 274 | |
|---|
| 275 | float yCenter = (orig_cell->_bb.yMin()+orig_cell->_bb.yMax())*0.5f; |
|---|
| 276 | orig_cell->_bb.yMax() = yCenter; |
|---|
| 277 | new_cell->_bb.yMin() = yCenter; |
|---|
| 278 | |
|---|
| 279 | _cells.push_back(new_cell); |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | if (zAxis) |
|---|
| 284 | { |
|---|
| 285 | unsigned int numCellsToDivide=_cells.size(); |
|---|
| 286 | for(unsigned int i=0;i<numCellsToDivide;++i) |
|---|
| 287 | { |
|---|
| 288 | Cell* orig_cell = _cells[i].get(); |
|---|
| 289 | Cell* new_cell = new Cell(orig_cell->_bb); |
|---|
| 290 | |
|---|
| 291 | float zCenter = (orig_cell->_bb.zMin()+orig_cell->_bb.zMax())*0.5f; |
|---|
| 292 | orig_cell->_bb.zMax() = zCenter; |
|---|
| 293 | new_cell->_bb.zMin() = zCenter; |
|---|
| 294 | |
|---|
| 295 | _cells.push_back(new_cell); |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | bin(); |
|---|
| 300 | |
|---|
| 301 | return true; |
|---|
| 302 | |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | void ForestTechniqueManager::Cell::bin() |
|---|
| 306 | { |
|---|
| 307 | |
|---|
| 308 | TreeList treesNotAssigned; |
|---|
| 309 | for(TreeList::iterator titr=_trees.begin(); |
|---|
| 310 | titr!=_trees.end(); |
|---|
| 311 | ++titr) |
|---|
| 312 | { |
|---|
| 313 | Tree* tree = titr->get(); |
|---|
| 314 | bool assigned = false; |
|---|
| 315 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 316 | citr!=_cells.end() && !assigned; |
|---|
| 317 | ++citr) |
|---|
| 318 | { |
|---|
| 319 | if ((*citr)->contains(tree->_position)) |
|---|
| 320 | { |
|---|
| 321 | (*citr)->addTree(tree); |
|---|
| 322 | assigned = true; |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | if (!assigned) treesNotAssigned.push_back(tree); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | _trees.swap(treesNotAssigned); |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | CellList cellsNotEmpty; |
|---|
| 334 | for(CellList::iterator citr=_cells.begin(); |
|---|
| 335 | citr!=_cells.end(); |
|---|
| 336 | ++citr) |
|---|
| 337 | { |
|---|
| 338 | if (!((*citr)->_trees.empty())) |
|---|
| 339 | { |
|---|
| 340 | cellsNotEmpty.push_back(*citr); |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | _cells.swap(cellsNotEmpty); |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | osg::Geode* ForestTechniqueManager::createTerrain(const osg::Vec3& origin, const osg::Vec3& size) |
|---|
| 349 | { |
|---|
| 350 | osg::Geode* geode = new osg::Geode(); |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 356 | |
|---|
| 357 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 358 | if (image) |
|---|
| 359 | { |
|---|
| 360 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 361 | texture->setImage(image); |
|---|
| 362 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | geode->setStateSet( stateset ); |
|---|
| 366 | |
|---|
| 367 | unsigned int numColumns = 38; |
|---|
| 368 | unsigned int numRows = 39; |
|---|
| 369 | unsigned int r; |
|---|
| 370 | unsigned int c; |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | float min_z = FLT_MAX; |
|---|
| 374 | float max_z = -FLT_MAX; |
|---|
| 375 | for(r=0;r<numRows;++r) |
|---|
| 376 | { |
|---|
| 377 | for(c=0;c<numColumns;++c) |
|---|
| 378 | { |
|---|
| 379 | min_z = osg::minimum(min_z,vertex[r+c*numRows][2]); |
|---|
| 380 | max_z = osg::maximum(max_z,vertex[r+c*numRows][2]); |
|---|
| 381 | } |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | float scale_z = size.z()/(max_z-min_z); |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | bool createGrid = false; |
|---|
| 388 | if (createGrid) |
|---|
| 389 | { |
|---|
| 390 | |
|---|
| 391 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 392 | grid->allocate(numColumns,numRows); |
|---|
| 393 | grid->setOrigin(origin); |
|---|
| 394 | grid->setXInterval(size.x()/(float)(numColumns-1)); |
|---|
| 395 | grid->setYInterval(size.y()/(float)(numRows-1)); |
|---|
| 396 | |
|---|
| 397 | for(r=0;r<numRows;++r) |
|---|
| 398 | { |
|---|
| 399 | for(c=0;c<numColumns;++c) |
|---|
| 400 | { |
|---|
| 401 | grid->setHeight(c,r,(vertex[r+c*numRows][2]-min_z)*scale_z); |
|---|
| 402 | } |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | geode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 406 | } |
|---|
| 407 | else |
|---|
| 408 | { |
|---|
| 409 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 410 | |
|---|
| 411 | osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows)); |
|---|
| 412 | osg::Vec2Array& t = *(new osg::Vec2Array(numColumns*numRows)); |
|---|
| 413 | osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1)); |
|---|
| 414 | |
|---|
| 415 | color[0].set(255,255,255,255); |
|---|
| 416 | |
|---|
| 417 | float rowCoordDelta = size.y()/(float)(numRows-1); |
|---|
| 418 | float columnCoordDelta = size.x()/(float)(numColumns-1); |
|---|
| 419 | |
|---|
| 420 | float rowTexDelta = 1.0f/(float)(numRows-1); |
|---|
| 421 | float columnTexDelta = 1.0f/(float)(numColumns-1); |
|---|
| 422 | |
|---|
| 423 | osg::Vec3 pos = origin; |
|---|
| 424 | osg::Vec2 tex(0.0f,0.0f); |
|---|
| 425 | int vi=0; |
|---|
| 426 | for(r=0;r<numRows;++r) |
|---|
| 427 | { |
|---|
| 428 | pos.x() = origin.x(); |
|---|
| 429 | tex.x() = 0.0f; |
|---|
| 430 | for(c=0;c<numColumns;++c) |
|---|
| 431 | { |
|---|
| 432 | v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z); |
|---|
| 433 | t[vi].set(tex.x(),tex.y()); |
|---|
| 434 | pos.x()+=columnCoordDelta; |
|---|
| 435 | tex.x()+=columnTexDelta; |
|---|
| 436 | ++vi; |
|---|
| 437 | } |
|---|
| 438 | pos.y() += rowCoordDelta; |
|---|
| 439 | tex.y() += rowTexDelta; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | geometry->setVertexArray(&v); |
|---|
| 443 | geometry->setColorArray(&color); |
|---|
| 444 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 445 | geometry->setTexCoordArray(0,&t); |
|---|
| 446 | |
|---|
| 447 | for(r=0;r<numRows-1;++r) |
|---|
| 448 | { |
|---|
| 449 | osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns)); |
|---|
| 450 | geometry->addPrimitiveSet(&drawElements); |
|---|
| 451 | int ei=0; |
|---|
| 452 | for(c=0;c<numColumns;++c) |
|---|
| 453 | { |
|---|
| 454 | drawElements[ei++] = (r+1)*numColumns+c; |
|---|
| 455 | drawElements[ei++] = (r)*numColumns+c; |
|---|
| 456 | } |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | geode->addDrawable(geometry); |
|---|
| 460 | |
|---|
| 461 | osgUtil::SmoothingVisitor sv; |
|---|
| 462 | sv.smooth(*geometry); |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | return geode; |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees) |
|---|
| 469 | { |
|---|
| 470 | |
|---|
| 471 | float max_TreeHeight = sqrtf(size.length2()/(float)numTreesToCreate); |
|---|
| 472 | float max_TreeWidth = max_TreeHeight*0.5f; |
|---|
| 473 | |
|---|
| 474 | float min_TreeHeight = max_TreeHeight*0.3f; |
|---|
| 475 | float min_TreeWidth = min_TreeHeight*0.5f; |
|---|
| 476 | |
|---|
| 477 | trees.reserve(trees.size()+numTreesToCreate); |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | for(unsigned int i=0;i<numTreesToCreate;++i) |
|---|
| 481 | { |
|---|
| 482 | Tree* tree = new Tree; |
|---|
| 483 | tree->_position.set(random(origin.x(),origin.x()+size.x()),random(origin.y(),origin.y()+size.y()),origin.z()); |
|---|
| 484 | tree->_color.set(random(128,255),random(128,255),random(128,255),255); |
|---|
| 485 | tree->_width = random(min_TreeWidth,max_TreeWidth); |
|---|
| 486 | tree->_height = random(min_TreeHeight,max_TreeHeight); |
|---|
| 487 | tree->_type = 0; |
|---|
| 488 | |
|---|
| 489 | if (terrain) |
|---|
| 490 | { |
|---|
| 491 | osgUtil::IntersectVisitor iv; |
|---|
| 492 | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
|---|
| 493 | |
|---|
| 494 | segDown->set(tree->_position,tree->_position+osg::Vec3(0.0f,0.0f,size.z())); |
|---|
| 495 | iv.addLineSegment(segDown.get()); |
|---|
| 496 | |
|---|
| 497 | terrain->accept(iv); |
|---|
| 498 | |
|---|
| 499 | if (iv.hits()) |
|---|
| 500 | { |
|---|
| 501 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
|---|
| 502 | if (!hitList.empty()) |
|---|
| 503 | { |
|---|
| 504 | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
|---|
| 505 | osg::Vec3 np = hitList.front().getWorldIntersectNormal(); |
|---|
| 506 | tree->_position = ip; |
|---|
| 507 | } |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | trees.push_back(tree); |
|---|
| 512 | } |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | osg::Geometry* ForestTechniqueManager::createSprite( float w, float h, osg::Vec4ub color ) |
|---|
| 516 | { |
|---|
| 517 | |
|---|
| 518 | osg::Vec3Array& v = *(new osg::Vec3Array(4)); |
|---|
| 519 | osg::Vec2Array& t = *(new osg::Vec2Array(4)); |
|---|
| 520 | osg::Vec4ubArray& c = *(new osg::Vec4ubArray(1)); |
|---|
| 521 | |
|---|
| 522 | v[0].set(-w*0.5f,0.0f,0.0f); |
|---|
| 523 | v[1].set( w*0.5f,0.0f,0.0f); |
|---|
| 524 | v[2].set( w*0.5f,0.0f,h); |
|---|
| 525 | v[3].set(-w*0.5f,0.0f,h); |
|---|
| 526 | |
|---|
| 527 | c[0] = color; |
|---|
| 528 | |
|---|
| 529 | t[0].set(0.0f,0.0f); |
|---|
| 530 | t[1].set(1.0f,0.0f); |
|---|
| 531 | t[2].set(1.0f,1.0f); |
|---|
| 532 | t[3].set(0.0f,1.0f); |
|---|
| 533 | |
|---|
| 534 | osg::Geometry *geom = new osg::Geometry; |
|---|
| 535 | |
|---|
| 536 | geom->setVertexArray( &v ); |
|---|
| 537 | |
|---|
| 538 | geom->setTexCoordArray( 0, &t ); |
|---|
| 539 | |
|---|
| 540 | geom->setColorArray( &c ); |
|---|
| 541 | geom->setColorBinding( osg::Geometry::BIND_OVERALL ); |
|---|
| 542 | |
|---|
| 543 | geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4) ); |
|---|
| 544 | |
|---|
| 545 | return geom; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | osg::Geometry* ForestTechniqueManager::createOrthogonalQuads( const osg::Vec3& pos, float w, float h, osg::Vec4ub color ) |
|---|
| 549 | { |
|---|
| 550 | |
|---|
| 551 | osg::Vec3Array& v = *(new osg::Vec3Array(8)); |
|---|
| 552 | osg::Vec2Array& t = *(new osg::Vec2Array(8)); |
|---|
| 553 | osg::Vec4ubArray& c = *(new osg::Vec4ubArray(1)); |
|---|
| 554 | |
|---|
| 555 | float rotation = random(0.0f,osg::PI/2.0f); |
|---|
| 556 | float sw = sinf(rotation)*w*0.5f; |
|---|
| 557 | float cw = cosf(rotation)*w*0.5f; |
|---|
| 558 | |
|---|
| 559 | v[0].set(pos.x()-sw,pos.y()-cw,pos.z()+0.0f); |
|---|
| 560 | v[1].set(pos.x()+sw,pos.y()+cw,pos.z()+0.0f); |
|---|
| 561 | v[2].set(pos.x()+sw,pos.y()+cw,pos.z()+h); |
|---|
| 562 | v[3].set(pos.x()-sw,pos.y()-cw,pos.z()+h); |
|---|
| 563 | |
|---|
| 564 | v[4].set(pos.x()-cw,pos.y()+sw,pos.z()+0.0f); |
|---|
| 565 | v[5].set(pos.x()+cw,pos.y()-sw,pos.z()+0.0f); |
|---|
| 566 | v[6].set(pos.x()+cw,pos.y()-sw,pos.z()+h); |
|---|
| 567 | v[7].set(pos.x()-cw,pos.y()+sw,pos.z()+h); |
|---|
| 568 | |
|---|
| 569 | c[0] = color; |
|---|
| 570 | |
|---|
| 571 | t[0].set(0.0f,0.0f); |
|---|
| 572 | t[1].set(1.0f,0.0f); |
|---|
| 573 | t[2].set(1.0f,1.0f); |
|---|
| 574 | t[3].set(0.0f,1.0f); |
|---|
| 575 | |
|---|
| 576 | t[4].set(0.0f,0.0f); |
|---|
| 577 | t[5].set(1.0f,0.0f); |
|---|
| 578 | t[6].set(1.0f,1.0f); |
|---|
| 579 | t[7].set(0.0f,1.0f); |
|---|
| 580 | |
|---|
| 581 | osg::Geometry *geom = new osg::Geometry; |
|---|
| 582 | |
|---|
| 583 | geom->setVertexArray( &v ); |
|---|
| 584 | |
|---|
| 585 | geom->setTexCoordArray( 0, &t ); |
|---|
| 586 | |
|---|
| 587 | geom->setColorArray( &c ); |
|---|
| 588 | geom->setColorBinding( osg::Geometry::BIND_OVERALL ); |
|---|
| 589 | |
|---|
| 590 | geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8) ); |
|---|
| 591 | |
|---|
| 592 | return geom; |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | osg::Node* ForestTechniqueManager::createBillboardGraph(Cell* cell,osg::StateSet* stateset) |
|---|
| 596 | { |
|---|
| 597 | bool needGroup = !(cell->_cells.empty()); |
|---|
| 598 | bool needBillboard = !(cell->_trees.empty()); |
|---|
| 599 | |
|---|
| 600 | osg::Billboard* billboard = 0; |
|---|
| 601 | osg::Group* group = 0; |
|---|
| 602 | |
|---|
| 603 | if (needBillboard) |
|---|
| 604 | { |
|---|
| 605 | billboard = new osg::Billboard; |
|---|
| 606 | billboard->setStateSet(stateset); |
|---|
| 607 | for(TreeList::iterator itr=cell->_trees.begin(); |
|---|
| 608 | itr!=cell->_trees.end(); |
|---|
| 609 | ++itr) |
|---|
| 610 | { |
|---|
| 611 | Tree& tree = **itr; |
|---|
| 612 | billboard->addDrawable(createSprite(tree._width,tree._height,tree._color),tree._position); |
|---|
| 613 | } |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | if (needGroup) |
|---|
| 617 | { |
|---|
| 618 | group = new osg::Group; |
|---|
| 619 | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
|---|
| 620 | itr!=cell->_cells.end(); |
|---|
| 621 | ++itr) |
|---|
| 622 | { |
|---|
| 623 | group->addChild(createBillboardGraph(itr->get(),stateset)); |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | if (billboard) group->addChild(billboard); |
|---|
| 627 | |
|---|
| 628 | } |
|---|
| 629 | if (group) return group; |
|---|
| 630 | else return billboard; |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | osg::Node* ForestTechniqueManager::createXGraph(Cell* cell,osg::StateSet* stateset) |
|---|
| 634 | { |
|---|
| 635 | bool needGroup = !(cell->_cells.empty()); |
|---|
| 636 | bool needTrees = !(cell->_trees.empty()); |
|---|
| 637 | |
|---|
| 638 | osg::Geode* geode = 0; |
|---|
| 639 | osg::Group* group = 0; |
|---|
| 640 | |
|---|
| 641 | if (needTrees) |
|---|
| 642 | { |
|---|
| 643 | geode = new osg::Geode; |
|---|
| 644 | geode->setStateSet(stateset); |
|---|
| 645 | |
|---|
| 646 | for(TreeList::iterator itr=cell->_trees.begin(); |
|---|
| 647 | itr!=cell->_trees.end(); |
|---|
| 648 | ++itr) |
|---|
| 649 | { |
|---|
| 650 | Tree& tree = **itr; |
|---|
| 651 | geode->addDrawable(createOrthogonalQuads(tree._position,tree._width,tree._height,tree._color)); |
|---|
| 652 | } |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | if (needGroup) |
|---|
| 656 | { |
|---|
| 657 | group = new osg::Group; |
|---|
| 658 | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
|---|
| 659 | itr!=cell->_cells.end(); |
|---|
| 660 | ++itr) |
|---|
| 661 | { |
|---|
| 662 | group->addChild(createXGraph(itr->get(),stateset)); |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | if (geode) group->addChild(geode); |
|---|
| 666 | |
|---|
| 667 | } |
|---|
| 668 | if (group) return group; |
|---|
| 669 | else return geode; |
|---|
| 670 | } |
|---|
| 671 | |
|---|
| 672 | osg::Node* ForestTechniqueManager::createTransformGraph(Cell* cell,osg::StateSet* stateset) |
|---|
| 673 | { |
|---|
| 674 | bool needGroup = !(cell->_cells.empty()); |
|---|
| 675 | bool needTrees = !(cell->_trees.empty()); |
|---|
| 676 | |
|---|
| 677 | osg::Group* transform_group = 0; |
|---|
| 678 | osg::Group* group = 0; |
|---|
| 679 | |
|---|
| 680 | if (needTrees) |
|---|
| 681 | { |
|---|
| 682 | transform_group = new osg::Group; |
|---|
| 683 | |
|---|
| 684 | osg::Geometry* geometry = createOrthogonalQuads(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f,osg::Vec4ub(255,255,255,255)); |
|---|
| 685 | |
|---|
| 686 | for(TreeList::iterator itr=cell->_trees.begin(); |
|---|
| 687 | itr!=cell->_trees.end(); |
|---|
| 688 | ++itr) |
|---|
| 689 | { |
|---|
| 690 | Tree& tree = **itr; |
|---|
| 691 | osg::MatrixTransform* transform = new osg::MatrixTransform; |
|---|
| 692 | transform->setMatrix(osg::Matrix::scale(tree._width,tree._width,tree._height)*osg::Matrix::translate(tree._position)); |
|---|
| 693 | |
|---|
| 694 | osg::Geode* geode = new osg::Geode; |
|---|
| 695 | geode->setStateSet(stateset); |
|---|
| 696 | geode->addDrawable(geometry); |
|---|
| 697 | transform->addChild(geode); |
|---|
| 698 | transform_group->addChild(transform); |
|---|
| 699 | } |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | if (needGroup) |
|---|
| 703 | { |
|---|
| 704 | group = new osg::Group; |
|---|
| 705 | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
|---|
| 706 | itr!=cell->_cells.end(); |
|---|
| 707 | ++itr) |
|---|
| 708 | { |
|---|
| 709 | group->addChild(createTransformGraph(itr->get(),stateset)); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | if (transform_group) group->addChild(transform_group); |
|---|
| 713 | |
|---|
| 714 | } |
|---|
| 715 | if (group) return group; |
|---|
| 716 | else return transform_group; |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | osg::Geometry* ForestTechniqueManager::createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h) |
|---|
| 720 | { |
|---|
| 721 | |
|---|
| 722 | osg::Vec3Array& v = *(new osg::Vec3Array(8)); |
|---|
| 723 | osg::Vec2Array& t = *(new osg::Vec2Array(8)); |
|---|
| 724 | |
|---|
| 725 | float rotation = random(0.0f,osg::PI/2.0f); |
|---|
| 726 | float sw = sinf(rotation)*w*0.5f; |
|---|
| 727 | float cw = cosf(rotation)*w*0.5f; |
|---|
| 728 | |
|---|
| 729 | v[0].set(pos.x()-sw,pos.y()-cw,pos.z()+0.0f); |
|---|
| 730 | v[1].set(pos.x()+sw,pos.y()+cw,pos.z()+0.0f); |
|---|
| 731 | v[2].set(pos.x()+sw,pos.y()+cw,pos.z()+h); |
|---|
| 732 | v[3].set(pos.x()-sw,pos.y()-cw,pos.z()+h); |
|---|
| 733 | |
|---|
| 734 | v[4].set(pos.x()-cw,pos.y()+sw,pos.z()+0.0f); |
|---|
| 735 | v[5].set(pos.x()+cw,pos.y()-sw,pos.z()+0.0f); |
|---|
| 736 | v[6].set(pos.x()+cw,pos.y()-sw,pos.z()+h); |
|---|
| 737 | v[7].set(pos.x()-cw,pos.y()+sw,pos.z()+h); |
|---|
| 738 | |
|---|
| 739 | t[0].set(0.0f,0.0f); |
|---|
| 740 | t[1].set(1.0f,0.0f); |
|---|
| 741 | t[2].set(1.0f,1.0f); |
|---|
| 742 | t[3].set(0.0f,1.0f); |
|---|
| 743 | |
|---|
| 744 | t[4].set(0.0f,0.0f); |
|---|
| 745 | t[5].set(1.0f,0.0f); |
|---|
| 746 | t[6].set(1.0f,1.0f); |
|---|
| 747 | t[7].set(0.0f,1.0f); |
|---|
| 748 | |
|---|
| 749 | osg::Geometry *geom = new osg::Geometry; |
|---|
| 750 | |
|---|
| 751 | geom->setVertexArray( &v ); |
|---|
| 752 | |
|---|
| 753 | geom->setTexCoordArray( 0, &t ); |
|---|
| 754 | |
|---|
| 755 | geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8) ); |
|---|
| 756 | |
|---|
| 757 | return geom; |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | class ShaderGeometry : public osg::Drawable |
|---|
| 761 | { |
|---|
| 762 | public: |
|---|
| 763 | ShaderGeometry() { setUseDisplayList(false); } |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | ShaderGeometry(const ShaderGeometry& ShaderGeometry,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): |
|---|
| 767 | osg::Drawable(ShaderGeometry,copyop) {} |
|---|
| 768 | |
|---|
| 769 | META_Object(osg,ShaderGeometry) |
|---|
| 770 | |
|---|
| 771 | typedef std::vector<osg::Vec4> PositionSizeList; |
|---|
| 772 | |
|---|
| 773 | virtual void drawImplementation(osg::State& state) const |
|---|
| 774 | { |
|---|
| 775 | for(PositionSizeList::const_iterator itr = _trees.begin(); |
|---|
| 776 | itr != _trees.end(); |
|---|
| 777 | ++itr) |
|---|
| 778 | { |
|---|
| 779 | glColor4fv(itr->ptr()); |
|---|
| 780 | _geometry->draw(state); |
|---|
| 781 | } |
|---|
| 782 | } |
|---|
| 783 | |
|---|
| 784 | virtual osg::BoundingBox computeBound() const |
|---|
| 785 | { |
|---|
| 786 | osg::BoundingBox geom_box = _geometry->getBound(); |
|---|
| 787 | osg::BoundingBox bb; |
|---|
| 788 | for(PositionSizeList::const_iterator itr = _trees.begin(); |
|---|
| 789 | itr != _trees.end(); |
|---|
| 790 | ++itr) |
|---|
| 791 | { |
|---|
| 792 | bb.expandBy(geom_box.corner(0)*(*itr)[3] + |
|---|
| 793 | osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] )); |
|---|
| 794 | bb.expandBy(geom_box.corner(7)*(*itr)[3] + |
|---|
| 795 | osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] )); |
|---|
| 796 | } |
|---|
| 797 | return bb; |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | void setGeometry(osg::Geometry* geometry) |
|---|
| 801 | { |
|---|
| 802 | _geometry = geometry; |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | void addTree(ForestTechniqueManager::Tree& tree) |
|---|
| 806 | { |
|---|
| 807 | _trees.push_back(osg::Vec4(tree._position.x(), tree._position.y(), tree._position.z(), tree._height)); |
|---|
| 808 | } |
|---|
| 809 | |
|---|
| 810 | osg::ref_ptr<osg::Geometry> _geometry; |
|---|
| 811 | |
|---|
| 812 | PositionSizeList _trees; |
|---|
| 813 | |
|---|
| 814 | protected: |
|---|
| 815 | |
|---|
| 816 | virtual ~ShaderGeometry() {} |
|---|
| 817 | |
|---|
| 818 | }; |
|---|
| 819 | |
|---|
| 820 | osg::Geometry* shared_geometry = 0; |
|---|
| 821 | |
|---|
| 822 | osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* stateset) |
|---|
| 823 | { |
|---|
| 824 | if (shared_geometry==0) |
|---|
| 825 | { |
|---|
| 826 | shared_geometry = createOrthogonalQuadsNoColor(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f); |
|---|
| 827 | |
|---|
| 828 | } |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | bool needGroup = !(cell->_cells.empty()); |
|---|
| 832 | bool needTrees = !(cell->_trees.empty()); |
|---|
| 833 | |
|---|
| 834 | osg::Geode* geode = 0; |
|---|
| 835 | osg::Group* group = 0; |
|---|
| 836 | |
|---|
| 837 | if (needTrees) |
|---|
| 838 | { |
|---|
| 839 | geode = new osg::Geode; |
|---|
| 840 | |
|---|
| 841 | ShaderGeometry* shader_geometry = new ShaderGeometry; |
|---|
| 842 | shader_geometry->setGeometry(shared_geometry); |
|---|
| 843 | |
|---|
| 844 | |
|---|
| 845 | for(TreeList::iterator itr=cell->_trees.begin(); |
|---|
| 846 | itr!=cell->_trees.end(); |
|---|
| 847 | ++itr) |
|---|
| 848 | { |
|---|
| 849 | Tree& tree = **itr; |
|---|
| 850 | shader_geometry->addTree(tree); |
|---|
| 851 | |
|---|
| 852 | } |
|---|
| 853 | |
|---|
| 854 | geode->setStateSet(stateset); |
|---|
| 855 | geode->addDrawable(shader_geometry); |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | if (needGroup) |
|---|
| 859 | { |
|---|
| 860 | group = new osg::Group; |
|---|
| 861 | for(Cell::CellList::iterator itr=cell->_cells.begin(); |
|---|
| 862 | itr!=cell->_cells.end(); |
|---|
| 863 | ++itr) |
|---|
| 864 | { |
|---|
| 865 | group->addChild(createShaderGraph(itr->get(),stateset)); |
|---|
| 866 | } |
|---|
| 867 | |
|---|
| 868 | if (geode) group->addChild(geode); |
|---|
| 869 | |
|---|
| 870 | } |
|---|
| 871 | if (group) return group; |
|---|
| 872 | else return geode; |
|---|
| 873 | } |
|---|
| 874 | |
|---|
| 875 | osg::Node* ForestTechniqueManager::createHUDWithText(const std::string& str) |
|---|
| 876 | { |
|---|
| 877 | osg::Geode* geode = new osg::Geode(); |
|---|
| 878 | |
|---|
| 879 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 880 | |
|---|
| 881 | |
|---|
| 882 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 883 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 884 | |
|---|
| 885 | |
|---|
| 886 | |
|---|
| 887 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 888 | stateset->setRenderBinDetails(11,"RenderBin"); |
|---|
| 889 | |
|---|
| 890 | osg::Vec3 position(150.0f,800.0f,0.0f); |
|---|
| 891 | osg::Vec3 delta(0.0f,-120.0f,0.0f); |
|---|
| 892 | |
|---|
| 893 | { |
|---|
| 894 | osgText::Text* text = new osgText::Text; |
|---|
| 895 | geode->addDrawable( text ); |
|---|
| 896 | |
|---|
| 897 | text->setFont(timesFont); |
|---|
| 898 | text->setPosition(position); |
|---|
| 899 | text->setText(str); |
|---|
| 900 | |
|---|
| 901 | position += delta; |
|---|
| 902 | } |
|---|
| 903 | |
|---|
| 904 | |
|---|
| 905 | |
|---|
| 906 | osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; |
|---|
| 907 | modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 908 | modelview_abs->setMatrix(osg::Matrix::identity()); |
|---|
| 909 | modelview_abs->addChild(geode); |
|---|
| 910 | |
|---|
| 911 | osg::Projection* projection = new osg::Projection; |
|---|
| 912 | projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 913 | projection->addChild(modelview_abs); |
|---|
| 914 | |
|---|
| 915 | return projection; |
|---|
| 916 | } |
|---|
| 917 | |
|---|
| 918 | osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) |
|---|
| 919 | { |
|---|
| 920 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 921 | osg::Vec3 size(1000.0f,1000.0f,200.0f); |
|---|
| 922 | |
|---|
| 923 | std::cout<<"Creating terrain..."; |
|---|
| 924 | osg::ref_ptr<osg::Node> terrain = createTerrain(origin,size); |
|---|
| 925 | std::cout<<"done."<<std::endl; |
|---|
| 926 | |
|---|
| 927 | std::cout<<"Creating tree locations...";std::cout.flush(); |
|---|
| 928 | TreeList trees; |
|---|
| 929 | createTreeList(terrain.get(),origin,size,numTreesToCreates,trees); |
|---|
| 930 | std::cout<<"done."<<std::endl; |
|---|
| 931 | |
|---|
| 932 | std::cout<<"Creating cell subdivision..."; |
|---|
| 933 | osg::ref_ptr<Cell> cell = new Cell; |
|---|
| 934 | cell->addTrees(trees); |
|---|
| 935 | cell->divide(); |
|---|
| 936 | std::cout<<"done."<<std::endl; |
|---|
| 937 | |
|---|
| 938 | |
|---|
| 939 | osg::Texture2D *tex = new osg::Texture2D; |
|---|
| 940 | tex->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP ); |
|---|
| 941 | tex->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP ); |
|---|
| 942 | tex->setImage(osgDB::readImageFile("Images/tree0.rgba")); |
|---|
| 943 | |
|---|
| 944 | osg::StateSet *dstate = new osg::StateSet; |
|---|
| 945 | { |
|---|
| 946 | dstate->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); |
|---|
| 947 | dstate->setTextureAttribute(0, new osg::TexEnv ); |
|---|
| 948 | |
|---|
| 949 | dstate->setAttributeAndModes( new osg::BlendFunc, osg::StateAttribute::ON ); |
|---|
| 950 | |
|---|
| 951 | osg::AlphaFunc* alphaFunc = new osg::AlphaFunc; |
|---|
| 952 | alphaFunc->setFunction(osg::AlphaFunc::GEQUAL,0.05f); |
|---|
| 953 | dstate->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON ); |
|---|
| 954 | |
|---|
| 955 | dstate->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
|---|
| 956 | |
|---|
| 957 | dstate->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); |
|---|
| 958 | } |
|---|
| 959 | |
|---|
| 960 | |
|---|
| 961 | _techniqueSwitch = new osg::Switch; |
|---|
| 962 | |
|---|
| 963 | { |
|---|
| 964 | std::cout<<"Creating billboard based forest..."; |
|---|
| 965 | osg::Group* group = new osg::Group; |
|---|
| 966 | group->addChild(createBillboardGraph(cell.get(),dstate)); |
|---|
| 967 | group->addChild(createHUDWithText("Using osg::Billboard's to create a forest\n\nPress left cursor key to select OpenGL shader based forest\nPress right cursor key to select double quad based forest")); |
|---|
| 968 | _techniqueSwitch->addChild(group); |
|---|
| 969 | std::cout<<"done."<<std::endl; |
|---|
| 970 | } |
|---|
| 971 | |
|---|
| 972 | { |
|---|
| 973 | std::cout<<"Creating billboard based forest..."; |
|---|
| 974 | osg::Group* group = new osg::Group; |
|---|
| 975 | group->addChild(createXGraph(cell.get(),dstate)); |
|---|
| 976 | group->addChild(createHUDWithText("Using double quads to create a forest\n\nPress left cursor key to select osg::Billboard based forest\nPress right cursor key to select osg::MatrixTransform based forest\n")); |
|---|
| 977 | _techniqueSwitch->addChild(group); |
|---|
| 978 | std::cout<<"done."<<std::endl; |
|---|
| 979 | } |
|---|
| 980 | |
|---|
| 981 | { |
|---|
| 982 | std::cout<<"Creating billboard based forest..."; |
|---|
| 983 | osg::Group* group = new osg::Group; |
|---|
| 984 | group->addChild(createTransformGraph(cell.get(),dstate)); |
|---|
| 985 | group->addChild(createHUDWithText("Using osg::MatrixTransform's to create a forest\n\nPress left cursor key to select double quad based forest\nPress right cursor key to select OpenGL shder based forest")); |
|---|
| 986 | _techniqueSwitch->addChild(group); |
|---|
| 987 | std::cout<<"done."<<std::endl; |
|---|
| 988 | } |
|---|
| 989 | |
|---|
| 990 | { |
|---|
| 991 | osg::Group* group = new osg::Group; |
|---|
| 992 | |
|---|
| 993 | |
|---|
| 994 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 995 | |
|---|
| 996 | stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); |
|---|
| 997 | stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); |
|---|
| 998 | |
|---|
| 999 | { |
|---|
| 1000 | osg::Program* program = new osg::Program; |
|---|
| 1001 | stateset->setAttribute(program); |
|---|
| 1002 | |
|---|
| 1003 | #if 1 |
|---|
| 1004 | |
|---|
| 1005 | |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | char vertexShaderSource[] = |
|---|
| 1009 | "varying vec2 texcoord;\n" |
|---|
| 1010 | "\n" |
|---|
| 1011 | "void main(void)\n" |
|---|
| 1012 | "{\n" |
|---|
| 1013 | " vec3 position = gl_Vertex.xyz * gl_Color.w + gl_Color.xyz;\n" |
|---|
| 1014 | " gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);\n" |
|---|
| 1015 | " gl_FrontColor = vec4(1.0,1.0,1.0,1.0);\n" |
|---|
| 1016 | " texcoord = gl_MultiTexCoord0.st;\n" |
|---|
| 1017 | "}\n"; |
|---|
| 1018 | |
|---|
| 1019 | |
|---|
| 1020 | |
|---|
| 1021 | |
|---|
| 1022 | char fragmentShaderSource[] = |
|---|
| 1023 | "uniform sampler2D baseTexture; \n" |
|---|
| 1024 | "varying vec2 texcoord; \n" |
|---|
| 1025 | "\n" |
|---|
| 1026 | "void main(void) \n" |
|---|
| 1027 | "{ \n" |
|---|
| 1028 | " gl_FragColor = texture2D( baseTexture, texcoord); \n" |
|---|
| 1029 | "}\n"; |
|---|
| 1030 | |
|---|
| 1031 | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource); |
|---|
| 1032 | program->addShader(vertex_shader); |
|---|
| 1033 | |
|---|
| 1034 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); |
|---|
| 1035 | program->addShader(fragment_shader); |
|---|
| 1036 | |
|---|
| 1037 | #else |
|---|
| 1038 | |
|---|
| 1039 | |
|---|
| 1040 | program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("shaders/forest.vert"))); |
|---|
| 1041 | program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("shaders/forest.frag"))); |
|---|
| 1042 | |
|---|
| 1043 | #endif |
|---|
| 1044 | |
|---|
| 1045 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); |
|---|
| 1046 | stateset->addUniform(baseTextureSampler); |
|---|
| 1047 | } |
|---|
| 1048 | |
|---|
| 1049 | std::cout<<"Creating billboard based forest..."; |
|---|
| 1050 | group->addChild(createShaderGraph(cell.get(),stateset)); |
|---|
| 1051 | group->addChild(createHUDWithText("Using OpenGL Shader to create a forest\n\nPress left cursor key to select osg::MatrixTransform based forest\nPress right cursor key to select osg::Billboard based forest")); |
|---|
| 1052 | _techniqueSwitch->addChild(group); |
|---|
| 1053 | std::cout<<"done."<<std::endl; |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | _currentTechnique = 0; |
|---|
| 1057 | _techniqueSwitch->setSingleChildOn(_currentTechnique); |
|---|
| 1058 | |
|---|
| 1059 | |
|---|
| 1060 | osg::Group* scene = new osg::Group; |
|---|
| 1061 | |
|---|
| 1062 | scene->addChild(terrain.get()); |
|---|
| 1063 | scene->addChild(_techniqueSwitch.get()); |
|---|
| 1064 | |
|---|
| 1065 | return scene; |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | int main( int argc, char **argv ) |
|---|
| 1069 | { |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 1073 | |
|---|
| 1074 | |
|---|
| 1075 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates the osg::Shape classes."); |
|---|
| 1076 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 1077 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 1078 | arguments.getApplicationUsage()->addCommandLineOption("--trees <number>","Set the number of trees to create"); |
|---|
| 1079 | |
|---|
| 1080 | |
|---|
| 1081 | osgProducer::Viewer viewer(arguments); |
|---|
| 1082 | |
|---|
| 1083 | float numTreesToCreates = 10000; |
|---|
| 1084 | arguments.read("--trees",numTreesToCreates); |
|---|
| 1085 | |
|---|
| 1086 | |
|---|
| 1087 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 1088 | |
|---|
| 1089 | osg::ref_ptr<ForestTechniqueManager> ttm = new ForestTechniqueManager; |
|---|
| 1090 | |
|---|
| 1091 | viewer.getEventHandlerList().push_front(new TechniqueEventHandler(ttm.get())); |
|---|
| 1092 | |
|---|
| 1093 | |
|---|
| 1094 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 1095 | |
|---|
| 1096 | |
|---|
| 1097 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 1098 | { |
|---|
| 1099 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 1100 | return 1; |
|---|
| 1101 | } |
|---|
| 1102 | |
|---|
| 1103 | |
|---|
| 1104 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 1105 | |
|---|
| 1106 | |
|---|
| 1107 | if (arguments.errors()) |
|---|
| 1108 | { |
|---|
| 1109 | arguments.writeErrorMessages(std::cout); |
|---|
| 1110 | return 1; |
|---|
| 1111 | } |
|---|
| 1112 | |
|---|
| 1113 | osg::Node* node = ttm->createScene((unsigned int)numTreesToCreates); |
|---|
| 1114 | |
|---|
| 1115 | |
|---|
| 1116 | viewer.setSceneData( node ); |
|---|
| 1117 | |
|---|
| 1118 | |
|---|
| 1119 | viewer.realize(); |
|---|
| 1120 | |
|---|
| 1121 | while( !viewer.done() ) |
|---|
| 1122 | { |
|---|
| 1123 | |
|---|
| 1124 | viewer.sync(); |
|---|
| 1125 | |
|---|
| 1126 | |
|---|
| 1127 | |
|---|
| 1128 | viewer.update(); |
|---|
| 1129 | |
|---|
| 1130 | |
|---|
| 1131 | viewer.frame(); |
|---|
| 1132 | |
|---|
| 1133 | } |
|---|
| 1134 | |
|---|
| 1135 | |
|---|
| 1136 | viewer.sync(); |
|---|
| 1137 | |
|---|
| 1138 | |
|---|
| 1139 | viewer.cleanup_frame(); |
|---|
| 1140 | |
|---|
| 1141 | |
|---|
| 1142 | viewer.sync(); |
|---|
| 1143 | |
|---|
| 1144 | return 0; |
|---|
| 1145 | } |
|---|