/* WaterSurface class An OpenSceneGraph viewer for pyVolution .sww files. copyright (C) 2004 Geoscience Australia */ #include #include #include #include #include #include #include #include #include #include #include #define DEF_ALPHA_THRESHOLD 0.01 // constructor WaterSurface::WaterSurface(SWWReader* sww) { // persistent _sww = sww; _node = new osg::Geode; _geom = new osg::Geometry; _stateset = new osg::StateSet; // construct local scenegraph hierarchy _node->setName("watersurface"); _node->addDrawable(_geom); _node->setStateSet(_stateset); _geom->setUseDisplayList( false ); // initial geometry setTimeStep(0); // environment map osg::Texture2D* texture = new osg::Texture2D; texture->setDataVariance(osg::Object::DYNAMIC); texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); texture->setImage(osgDB::readImageFile("envmap.jpg")); _stateset->setTextureAttributeAndModes( 1, texture, osg::StateAttribute::ON ); _stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON ); // surface transparency osg::BlendFunc* osgBlendFunc = new osg::BlendFunc(); osgBlendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); _stateset->setAttribute(osgBlendFunc); _stateset->setMode(GL_BLEND, osg::StateAttribute::ON); _stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); // discard pixels with an alpha value below threshold osg::AlphaFunc* alphaFunc = new osg::AlphaFunc; alphaFunc->setFunction( osg::AlphaFunc::GREATER, DEF_ALPHA_THRESHOLD ); _stateset->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON ); // automatically generate texture coords osg::TexGen* texgen = new osg::TexGen; texgen->setMode( osg::TexGen::SPHERE_MAP ); osg::TexEnv* texenv = new osg::TexEnv; texenv->setMode( osg::TexEnv::DECAL ); texenv->setColor( osg::Vec4(0.6f,0.6f,0.6f,0.2f) ); _stateset->setTextureAttributeAndModes( 1, texgen, osg::StateAttribute::ON ); _stateset->setTextureAttribute( 1, texenv ); } WaterSurface::~WaterSurface() { } void WaterSurface::setTimeStep(unsigned int ts) { // delete if exists if( _geom->getNumPrimitiveSets() ) _geom->removePrimitiveSet(0); // reference counting does actual delete // local reference to raw height field data osg::ref_ptr vertices = _sww->getStageVertexArray(ts); osg::ref_ptr vertexnormals = _sww->getStageVertexNormalArray(); osg::ref_ptr colors = _sww->getStageColorArray(); // geometry _geom->setVertexArray( vertices.get() ); _geom->addPrimitiveSet( _sww->getBedslopeIndexArray().get() ); // per vertex colors (we only modulate the alpha for transparency) _geom->setColorArray( colors.get() ); _geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); // normals _geom->setNormalArray( vertexnormals.get() ); _geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); // osgUtil::TriStripVisitor* optimize = new osgUtil::TriStripVisitor(); // optimize->stripify( *_geom ); } void WaterSurface::toggleWireframe() { osg::PolygonMode* polyModeObj = dynamic_cast(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); if (!polyModeObj) { polyModeObj = new osg::PolygonMode; _stateset->setAttribute(polyModeObj); } switch( polyModeObj->getMode(osg::PolygonMode::FRONT_AND_BACK) ) { case osg::PolygonMode::FILL : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE); break; case osg::PolygonMode::LINE : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL); break; case osg::PolygonMode::POINT : break; } }