[6] | 1 | |
---|
| 2 | |
---|
| 3 | #include <bedslope.h> |
---|
| 4 | |
---|
[72] | 5 | #include <osg/Texture> |
---|
[6] | 6 | #include <osg/Texture2D> |
---|
[72] | 7 | |
---|
[64] | 8 | #include <osgUtil/SmoothingVisitor> |
---|
[6] | 9 | |
---|
[64] | 10 | |
---|
[6] | 11 | #define DEF_BEDSLOPE_COLOUR 145, 122, 59, 255 // R, G, B, Alpha (brown) |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | // constructor |
---|
| 15 | BedSlope::BedSlope(SWWReader* sww) |
---|
| 16 | { |
---|
[45] | 17 | // persistent |
---|
| 18 | _sww = sww; |
---|
| 19 | |
---|
[6] | 20 | // bedslope geometry as triangles with shared vertices |
---|
| 21 | _geom = new osg::Geometry; |
---|
| 22 | |
---|
| 23 | // geometry from sww file |
---|
[45] | 24 | osg::Vec3Array* vertices = _sww->getBedslopeVertexArray().get(); |
---|
[6] | 25 | |
---|
[72] | 26 | _geom->setUseDisplayList( true ); |
---|
[6] | 27 | _geom->setVertexArray( vertices ); |
---|
[45] | 28 | _geom->addPrimitiveSet( _sww->getBedslopeIndexArray().get() ); |
---|
| 29 | _geom->setTexCoordArray( 0, _sww->getBedslopeTextureCoords().get() ); |
---|
[6] | 30 | |
---|
[64] | 31 | osg::UByte4Array* color = new osg::UByte4Array(1); |
---|
[72] | 32 | (*color)[0] = osg::UByte4( DEF_BEDSLOPE_COLOUR ); |
---|
[64] | 33 | _geom->setColorArray( color ); |
---|
| 34 | _geom->setColorBinding( osg::Geometry::BIND_OVERALL ); |
---|
[6] | 35 | |
---|
[64] | 36 | // Calculate per-vertex normals |
---|
| 37 | osgUtil::SmoothingVisitor* visitor = new osgUtil::SmoothingVisitor(); |
---|
| 38 | visitor->smooth( *_geom ); |
---|
| 39 | |
---|
[6] | 40 | // bedslope texture |
---|
[72] | 41 | _texture = true; |
---|
[6] | 42 | osg::Texture2D* texture = new osg::Texture2D; |
---|
[72] | 43 | texture->setDataVariance( osg::Object::DYNAMIC ); |
---|
| 44 | texture->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT ); |
---|
| 45 | texture->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT ); |
---|
[62] | 46 | texture->setImage( _sww->getBedslopeTexture() ); |
---|
[64] | 47 | |
---|
[65] | 48 | // material |
---|
| 49 | _material = new osg::Material(); |
---|
[72] | 50 | _material->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4(0.2, 0.15, 0.06, 1.0) ); |
---|
| 51 | _material->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4(0.6, 0.5, 0.2, 1.0) ); |
---|
| 52 | _material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); |
---|
| 53 | _material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); |
---|
| 54 | |
---|
| 55 | _material->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); |
---|
[65] | 56 | _material->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0) ); |
---|
[72] | 57 | _material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); |
---|
[65] | 58 | _material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) ); |
---|
| 59 | |
---|
| 60 | |
---|
[64] | 61 | // state |
---|
[6] | 62 | _stateset = new osg::StateSet; |
---|
[65] | 63 | _stateset->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON ); |
---|
[72] | 64 | _stateset->setAttributeAndModes( _material, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); |
---|
[6] | 65 | _stateset->setMode( GL_BLEND, osg::StateAttribute::ON ); |
---|
| 66 | _stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON ); |
---|
| 67 | |
---|
| 68 | _node = new osg::Geode; |
---|
| 69 | _node->setName("bedslope"); |
---|
| 70 | _node->addDrawable(_geom); |
---|
| 71 | _node->setStateSet(_stateset); |
---|
[64] | 72 | |
---|
[6] | 73 | } |
---|
| 74 | |
---|
[72] | 75 | |
---|
| 76 | void BedSlope::toggleTexture() |
---|
| 77 | { |
---|
| 78 | |
---|
| 79 | if ( _texture ) |
---|
| 80 | { |
---|
| 81 | _stateset->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::OFF ); |
---|
| 82 | _texture = false; |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | _stateset->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::ON ); |
---|
| 87 | _texture = true; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | } |
---|