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