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