#include #include #include #include #define DEF_BEDSLOPE_COLOUR 145, 122, 59, 255 // R, G, B, Alpha (brown) // constructor BedSlope::BedSlope(SWWReader* sww) { // persistent _sww = sww; // bedslope geometry as triangles with shared vertices _geom = new osg::Geometry; // geometry from sww file osg::Vec3Array* vertices = _sww->getBedslopeVertexArray().get(); _geom->setUseDisplayList( true ); _geom->setVertexArray( vertices ); _geom->addPrimitiveSet( _sww->getBedslopeIndexArray().get() ); _geom->setTexCoordArray( 0, _sww->getBedslopeTextureCoords().get() ); osg::UByte4Array* color = new osg::UByte4Array(1); (*color)[0] = osg::UByte4( DEF_BEDSLOPE_COLOUR ); _geom->setColorArray( color ); _geom->setColorBinding( osg::Geometry::BIND_OVERALL ); // Calculate per-vertex normals osgUtil::SmoothingVisitor* visitor = new osgUtil::SmoothingVisitor(); visitor->smooth( *_geom ); // bedslope texture _texture = true; osg::Texture2D* texture = new osg::Texture2D; texture->setDataVariance( osg::Object::DYNAMIC ); texture->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT ); texture->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT ); texture->setImage( _sww->getBedslopeTexture() ); // material _material = new osg::Material(); _material->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4(0.2, 0.15, 0.06, 1.0) ); _material->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4(0.6, 0.5, 0.2, 1.0) ); _material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); _material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); _material->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); _material->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0) ); _material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); _material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); // state _stateset = new osg::StateSet; _stateset->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON ); _stateset->setAttributeAndModes( _material, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); _stateset->setMode( GL_BLEND, osg::StateAttribute::ON ); _stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON ); _node = new osg::Geode; _node->setName("bedslope"); _node->addDrawable(_geom); _node->setStateSet(_stateset); } void BedSlope::toggleTexture() { if ( _texture ) { _stateset->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::OFF ); _texture = false; } else { _stateset->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::ON ); _texture = true; } }