diff --git a/examples/osgforest/osgforest.cpp b/examples/osgforest/osgforest.cpp index 58f36517c..7bfa3f68d 100644 --- a/examples/osgforest/osgforest.cpp +++ b/examples/osgforest/osgforest.cpp @@ -85,9 +85,9 @@ public: float _height; unsigned int _type; }; - + typedef std::vector< osg::ref_ptr > TreeList; - + class Cell : public osg::Referenced { public: @@ -95,21 +95,21 @@ public: Cell():_parent(0) {} Cell(osg::BoundingBox& bb):_parent(0), _bb(bb) {} - + void addCell(Cell* cell) { cell->_parent=this; _cells.push_back(cell); } void addTree(Tree* tree) { _trees.push_back(tree); } - + void addTrees(const TreeList& trees) { _trees.insert(_trees.end(),trees.begin(),trees.end()); } - + void computeBound(); - + bool contains(const osg::Vec3& position) const { return _bb.contains(position); } - + bool divide(unsigned int maxNumTreesPerCell=10); - + bool divide(bool xAxis, bool yAxis, bool zAxis); - + void bin(); @@ -117,7 +117,7 @@ public: osg::BoundingBox _bb; CellList _cells; TreeList _trees; - + }; float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } @@ -139,11 +139,11 @@ public: osg::Node* createTransformGraph(Cell* cell,osg::StateSet* stateset); osg::Node* createShaderGraph(Cell* cell,osg::StateSet* stateset); - + osg::Node* createHUDWithText(const std::string& text); osg::Node* createScene(unsigned int numTreesToCreates); - + void advanceToNextTechnique(int delta=1) { if (_techniqueSwitch.valid()) @@ -156,10 +156,10 @@ public: _techniqueSwitch->setSingleChildOn(_currentTechnique); } } - + osg::ref_ptr _techniqueSwitch; int _currentTechnique; - + }; @@ -169,22 +169,22 @@ class TechniqueEventHandler : public osgGA::GUIEventHandler public: TechniqueEventHandler(ForestTechniqueManager* ttm=0) { _ForestTechniqueManager = ttm; } - + META_Object(osgforestApp,TechniqueEventHandler); virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*); - + virtual void getUsage(osg::ApplicationUsage& usage) const; protected: ~TechniqueEventHandler() {} - + TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {} - + osg::ref_ptr _ForestTechniqueManager; - + }; bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*) @@ -194,15 +194,15 @@ bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIAc case(osgGA::GUIEventAdapter::KEYDOWN): { if (ea.getKey()=='n' || - ea.getKey()==osgGA::GUIEventAdapter::KEY_Right || + ea.getKey()==osgGA::GUIEventAdapter::KEY_Right || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right) { _ForestTechniqueManager->advanceToNextTechnique(1); return true; } else if (ea.getKey()=='p' || - ea.getKey()==osgGA::GUIEventAdapter::KEY_Left || - ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left) + ea.getKey()==osgGA::GUIEventAdapter::KEY_Left || + ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left) { _ForestTechniqueManager->advanceToNextTechnique(-1); return true; @@ -260,11 +260,11 @@ bool ForestTechniqueManager::Cell::divide(unsigned int maxNumTreesPerCell) (*citr)->divide(maxNumTreesPerCell); } return true; - } - else - { - return false; - } + } + else + { + return false; + } } bool ForestTechniqueManager::Cell::divide(bool xAxis, bool yAxis, bool zAxis) @@ -329,7 +329,7 @@ bool ForestTechniqueManager::Cell::divide(bool xAxis, bool yAxis, bool zAxis) } void ForestTechniqueManager::Cell::bin() -{ +{ // put trees in appropriate cells. TreeList treesNotAssigned; for(TreeList::iterator titr=_trees.begin(); @@ -387,9 +387,9 @@ osg::Geode* ForestTechniqueManager::createTerrain(const osg::Vec3& origin, const texture->setImage(image); stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); } - + geode->setStateSet( stateset ); - + unsigned int numColumns = 38; unsigned int numRows = 39; unsigned int r; @@ -406,7 +406,7 @@ osg::Geode* ForestTechniqueManager::createTerrain(const osg::Vec3& origin, const max_z = osg::maximum(max_z,vertex[r+c*numRows][2]); } } - + float scale_z = size.z()/(max_z-min_z); @@ -427,22 +427,22 @@ osg::Geode* ForestTechniqueManager::createTerrain(const osg::Vec3& origin, const grid->setHeight(c,r,(vertex[r+c*numRows][2]-min_z)*scale_z); } } - + geode->addDrawable(new osg::ShapeDrawable(grid)); } else { osg::Geometry* geometry = new osg::Geometry; - + osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows)); osg::Vec2Array& t = *(new osg::Vec2Array(numColumns*numRows)); osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1)); - + color[0].set(255,255,255,255); float rowCoordDelta = size.y()/(float)(numRows-1); float columnCoordDelta = size.x()/(float)(numColumns-1); - + float rowTexDelta = 1.0f/(float)(numRows-1); float columnTexDelta = 1.0f/(float)(numColumns-1); @@ -464,12 +464,12 @@ osg::Geode* ForestTechniqueManager::createTerrain(const osg::Vec3& origin, const pos.y() += rowCoordDelta; tex.y() += rowTexDelta; } - + geometry->setVertexArray(&v); geometry->setColorArray(&color); geometry->setColorBinding(osg::Geometry::BIND_OVERALL); geometry->setTexCoordArray(0,&t); - + for(r=0;raddDrawable(geometry); - + osgUtil::SmoothingVisitor sv; sv.smooth(*geometry); } - + return geode; } @@ -496,7 +496,7 @@ void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& float max_TreeHeight = sqrtf(size.length2()/(float)numTreesToCreate); float max_TreeWidth = max_TreeHeight*0.5f; - + float min_TreeHeight = max_TreeHeight*0.3f; float min_TreeWidth = min_TreeHeight*0.5f; @@ -511,14 +511,14 @@ void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& tree->_width = random(min_TreeWidth,max_TreeWidth); tree->_height = random(min_TreeHeight,max_TreeHeight); tree->_type = 0; - + if (terrain) { - osg::ref_ptr intersector = + osg::ref_ptr intersector = new osgUtil::LineSegmentIntersector(tree->_position,tree->_position+osg::Vec3(0.0f,0.0f,size.z())); osgUtil::IntersectionVisitor iv(intersector.get()); - + terrain->accept(iv); if (intersector->containsIntersections()) @@ -533,7 +533,7 @@ void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& } } } - + trees.push_back(tree); } } @@ -577,7 +577,7 @@ osg::Geometry* ForestTechniqueManager::createOrthogonalQuads( const osg::Vec3& p osg::Vec3Array& v = *(new osg::Vec3Array(8)); osg::Vec2Array& t = *(new osg::Vec2Array(8)); osg::Vec4ubArray& c = *(new osg::Vec4ubArray(1)); - + float rotation = random(0.0f,osg::PI/2.0f); float sw = sinf(rotation)*w*0.5f; float cw = cosf(rotation)*w*0.5f; @@ -622,10 +622,10 @@ osg::Node* ForestTechniqueManager::createBillboardGraph(Cell* cell,osg::StateSet { bool needGroup = !(cell->_cells.empty()); bool needBillboard = !(cell->_trees.empty()); - + osg::Billboard* billboard = 0; osg::Group* group = 0; - + if (needBillboard) { billboard = new osg::Billboard; @@ -635,10 +635,10 @@ osg::Node* ForestTechniqueManager::createBillboardGraph(Cell* cell,osg::StateSet ++itr) { Tree& tree = **itr; - billboard->addDrawable(createSprite(tree._width,tree._height,tree._color),tree._position); + billboard->addDrawable(createSprite(tree._width,tree._height,tree._color),tree._position); } } - + if (needGroup) { group = new osg::Group; @@ -648,9 +648,9 @@ osg::Node* ForestTechniqueManager::createBillboardGraph(Cell* cell,osg::StateSet { group->addChild(createBillboardGraph(itr->get(),stateset)); } - + if (billboard) group->addChild(billboard); - + } if (group) return group; else return billboard; @@ -660,15 +660,15 @@ osg::Node* ForestTechniqueManager::createXGraph(Cell* cell,osg::StateSet* states { bool needGroup = !(cell->_cells.empty()); bool needTrees = !(cell->_trees.empty()); - + osg::Geode* geode = 0; osg::Group* group = 0; - + if (needTrees) { geode = new osg::Geode; geode->setStateSet(stateset); - + for(TreeList::iterator itr=cell->_trees.begin(); itr!=cell->_trees.end(); ++itr) @@ -677,7 +677,7 @@ osg::Node* ForestTechniqueManager::createXGraph(Cell* cell,osg::StateSet* states geode->addDrawable(createOrthogonalQuads(tree._position,tree._width,tree._height,tree._color)); } } - + if (needGroup) { group = new osg::Group; @@ -687,9 +687,9 @@ osg::Node* ForestTechniqueManager::createXGraph(Cell* cell,osg::StateSet* states { group->addChild(createXGraph(itr->get(),stateset)); } - + if (geode) group->addChild(geode); - + } if (group) return group; else return geode; @@ -699,16 +699,16 @@ osg::Node* ForestTechniqueManager::createTransformGraph(Cell* cell,osg::StateSet { bool needGroup = !(cell->_cells.empty()); bool needTrees = !(cell->_trees.empty()); - + osg::Group* transform_group = 0; osg::Group* group = 0; - + if (needTrees) { transform_group = new osg::Group; - + osg::Geometry* geometry = createOrthogonalQuads(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f,osg::Vec4ub(255,255,255,255)); - + for(TreeList::iterator itr=cell->_trees.begin(); itr!=cell->_trees.end(); ++itr) @@ -716,7 +716,7 @@ osg::Node* ForestTechniqueManager::createTransformGraph(Cell* cell,osg::StateSet Tree& tree = **itr; osg::MatrixTransform* transform = new osg::MatrixTransform; transform->setMatrix(osg::Matrix::scale(tree._width,tree._width,tree._height)*osg::Matrix::translate(tree._position)); - + osg::Geode* geode = new osg::Geode; geode->setStateSet(stateset); geode->addDrawable(geometry); @@ -724,7 +724,7 @@ osg::Node* ForestTechniqueManager::createTransformGraph(Cell* cell,osg::StateSet transform_group->addChild(transform); } } - + if (needGroup) { group = new osg::Group; @@ -734,9 +734,9 @@ osg::Node* ForestTechniqueManager::createTransformGraph(Cell* cell,osg::StateSet { group->addChild(createTransformGraph(itr->get(),stateset)); } - + if (transform_group) group->addChild(transform_group); - + } if (group) return group; else return transform_group; @@ -747,7 +747,7 @@ osg::Geometry* ForestTechniqueManager::createOrthogonalQuadsNoColor( const osg:: // set up the coords osg::Vec3Array& v = *(new osg::Vec3Array(8)); osg::Vec2Array& t = *(new osg::Vec2Array(8)); - + float rotation = random(0.0f,osg::PI/2.0f); float sw = sinf(rotation)*w*0.5f; float cw = cosf(rotation)*w*0.5f; @@ -795,7 +795,7 @@ class ShaderGeometry : public osg::Drawable META_Object(osg,ShaderGeometry) typedef std::vector PositionSizeList; - + virtual void drawImplementation(osg::RenderInfo& renderInfo) const { for(PositionSizeList::const_iterator itr = _trees.begin(); @@ -822,25 +822,25 @@ class ShaderGeometry : public osg::Drawable } return bb; } - + void setGeometry(osg::Geometry* geometry) { _geometry = geometry; } - + void addTree(ForestTechniqueManager::Tree& tree) { _trees.push_back(osg::Vec4(tree._position.x(), tree._position.y(), tree._position.z(), tree._height)); } - + osg::ref_ptr _geometry; PositionSizeList _trees; protected: - + virtual ~ShaderGeometry() {} - + }; osg::Geometry* shared_geometry = 0; @@ -856,18 +856,18 @@ osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* s bool needGroup = !(cell->_cells.empty()); bool needTrees = !(cell->_trees.empty()); - + osg::Geode* geode = 0; osg::Group* group = 0; - + if (needTrees) { geode = new osg::Geode; - + ShaderGeometry* shader_geometry = new ShaderGeometry; shader_geometry->setGeometry(shared_geometry); - - + + for(TreeList::iterator itr=cell->_trees.begin(); itr!=cell->_trees.end(); ++itr) @@ -880,7 +880,7 @@ osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* s geode->setStateSet(stateset); geode->addDrawable(shader_geometry); } - + if (needGroup) { group = new osg::Group; @@ -890,9 +890,9 @@ osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* s { group->addChild(createShaderGraph(itr->get(),stateset)); } - + if (geode) group->addChild(geode); - + } if (group) return group; else return geode; @@ -901,14 +901,14 @@ osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* s osg::Node* ForestTechniqueManager::createHUDWithText(const std::string& str) { osg::Geode* geode = new osg::Geode(); - + std::string timesFont("fonts/arial.ttf"); // turn lighting off for the text and disable depth test to ensure its always ontop. osg::StateSet* stateset = geode->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); - // or disable depth test, and make sure that the hud is drawn after everything + // or disable depth test, and make sure that the hud is drawn after everything // else so that it always appears ontop. stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); stateset->setRenderBinDetails(11,"RenderBin"); @@ -923,11 +923,11 @@ osg::Node* ForestTechniqueManager::createHUDWithText(const std::string& str) text->setFont(timesFont); text->setPosition(position); text->setText(str); - - position += delta; - } - + position += delta; + } + + // create the hud. osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF); @@ -949,26 +949,26 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) std::cout<<"Creating terrain..."; osg::ref_ptr terrain = createTerrain(origin,size); std::cout<<"done."< cell = new Cell; cell->addTrees(trees); cell->divide(); - std::cout<<"done."<setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP ); tex->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP ); tex->setImage(osgDB::readImageFile("Images/tree0.rgba")); osg::StateSet *dstate = new osg::StateSet; - { + { dstate->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); dstate->setTextureAttribute(0, new osg::TexEnv ); @@ -983,7 +983,7 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) dstate->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); } - + _techniqueSwitch = new osg::Switch; @@ -995,7 +995,7 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) _techniqueSwitch->addChild(group); std::cout<<"done."<addShader(fragment_shader); - + #else // get shaders from source @@ -1139,10 +1139,10 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) _currentTechnique = 0; _techniqueSwitch->setSingleChildOn(_currentTechnique); - + osg::Group* scene = new osg::Group; - + scene->addChild(terrain.get()); scene->addChild(_techniqueSwitch.get()); @@ -1154,15 +1154,15 @@ int main( int argc, char **argv ) // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); - + // construct the viewer. osgViewer::Viewer viewer(arguments); float numTreesToCreates = 10000; arguments.read("--trees",numTreesToCreates); - + osg::ref_ptr ttm = new ForestTechniqueManager; - + // add the stats handler viewer.addEventHandler(new osgViewer::StatsHandler);