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