1 | |
---|
2 | |
---|
3 | #include <bedslope.h> |
---|
4 | |
---|
5 | #include <osg/BoundingBox> |
---|
6 | #include <osg/Texture2D> |
---|
7 | #include <osgDB/Registry> |
---|
8 | #include <osgDB/ReadFile> |
---|
9 | #include <osg/TexEnv> |
---|
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 | |
---|
18 | // bedslope geometry as triangles with shared vertices |
---|
19 | _geom = new osg::Geometry; |
---|
20 | |
---|
21 | // geometry from sww file |
---|
22 | osg::Vec3Array* vertices = sww->getBedslopeVertexArray().get(); |
---|
23 | osg::UIntArray* indices = sww->getBedslopeIndexArray().get(); |
---|
24 | osg::Vec3Array* normals = sww->getBedslopeNormalArray().get(); |
---|
25 | unsigned int nindices = sww->getNumberOfBedslopeIndices(); |
---|
26 | |
---|
27 | _geom->setUseDisplayList(false); |
---|
28 | _geom->setVertexArray( vertices ); |
---|
29 | _geom->setVertexIndices( indices ); |
---|
30 | _geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, nindices )); |
---|
31 | _geom->setNormalArray( normals ); |
---|
32 | _geom->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE ); |
---|
33 | |
---|
34 | |
---|
35 | // geometry bounding box |
---|
36 | osg::BoundingBox bbox = _geom->getBound(); |
---|
37 | |
---|
38 | // bedslope texture coords scaled to range [0,1] |
---|
39 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
---|
40 | osg::Vec3 v; |
---|
41 | float xrange = bbox.xMax() - bbox.xMin(); |
---|
42 | float yrange = bbox.yMax() - bbox.yMin(); |
---|
43 | for( unsigned int i=0; i < vertices->size(); i++ ) |
---|
44 | { |
---|
45 | v = vertices->at(i); |
---|
46 | texcoords->push_back( osg::Vec2( (v.x()-bbox.xMin())/xrange, (v.y()-bbox.yMin())/yrange) ); |
---|
47 | } |
---|
48 | _geom->setTexCoordArray( 0, texcoords ); |
---|
49 | _geom->setTexCoordIndices( 0, indices ); |
---|
50 | |
---|
51 | // bedslope texture |
---|
52 | osg::Texture2D* texture = new osg::Texture2D; |
---|
53 | texture->setDataVariance(osg::Object::DYNAMIC); |
---|
54 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
---|
55 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); |
---|
56 | texture->setImage(osgDB::readImageFile("bedslope.jpg")); |
---|
57 | |
---|
58 | |
---|
59 | // bedslope colour |
---|
60 | //osg::UByte4Array* colors = new osg::UByte4Array; |
---|
61 | //colors->push_back(osg::UByte4(DEF_BEDSLOPE_COLOUR)); |
---|
62 | //_geom->setColorArray(colors); |
---|
63 | //_geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
---|
64 | |
---|
65 | // material properties |
---|
66 | _material = new osg::Material; |
---|
67 | _material->setDiffuse(osg::Material::FRONT, osg::Vec4(DEF_BEDSLOPE_COLOUR)); |
---|
68 | _material->setSpecular(osg::Material::FRONT, osg::Vec4(1,0,0,1)); |
---|
69 | _material->setShininess(osg::Material::FRONT, 128); |
---|
70 | |
---|
71 | |
---|
72 | _stateset = new osg::StateSet; |
---|
73 | _stateset->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON ); |
---|
74 | _stateset->setMode( GL_BLEND, osg::StateAttribute::ON ); |
---|
75 | _stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON ); |
---|
76 | _stateset->setAttribute(_material); |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | _node = new osg::Geode; |
---|
82 | _node->setName("bedslope"); |
---|
83 | _node->addDrawable(_geom); |
---|
84 | _node->setStateSet(_stateset); |
---|
85 | } |
---|
86 | |
---|