[6] | 1 | /* |
---|
| 2 | WaterSurface class |
---|
| 3 | |
---|
| 4 | An OpenSceneGraph viewer for pyVolution .sww files. |
---|
| 5 | copyright (C) 2004 Geoscience Australia |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #include <watersurface.h> |
---|
| 10 | #include <osg/BlendFunc> |
---|
| 11 | #include <osg/PolygonMode> |
---|
| 12 | #include <osg/ShapeDrawable> |
---|
| 13 | #include <osg/Texture2D> |
---|
| 14 | #include <osg/TexEnv> |
---|
| 15 | #include <osg/TexGen> |
---|
| 16 | |
---|
| 17 | #include <osgDB/Registry> |
---|
| 18 | #include <osgDB/ReadFile> |
---|
| 19 | |
---|
| 20 | |
---|
[13] | 21 | #define DEF_WATER_COLOUR 0, 0, 0.5, 1 // R, G, B, Alpha (blue) |
---|
| 22 | #define DEF_WATER_TRANSPARENCY 0.1 // 0=opaque, 1=transparent |
---|
| 23 | #define DEF_ZOFFSET 0.01 |
---|
[6] | 24 | |
---|
| 25 | // constructor |
---|
| 26 | WaterSurface::WaterSurface(SWWReader* sww) |
---|
| 27 | { |
---|
| 28 | // persistent |
---|
| 29 | _sww = sww; |
---|
| 30 | _node = new osg::Geode; |
---|
| 31 | _geom = new osg::Geometry; |
---|
| 32 | _stateset = new osg::StateSet; |
---|
| 33 | _material = new osg::Material; |
---|
[13] | 34 | _zoffset = DEF_ZOFFSET; |
---|
[6] | 35 | |
---|
| 36 | // construct local scenegraph hierarchy |
---|
| 37 | _node->setName("watersurface"); |
---|
| 38 | _node->addDrawable(_geom); |
---|
| 39 | _node->setStateSet(_stateset); |
---|
| 40 | _stateset->setAttribute(_material); |
---|
| 41 | |
---|
| 42 | // why??? |
---|
| 43 | _geom->setUseDisplayList(false); |
---|
| 44 | |
---|
| 45 | // initial geometry |
---|
[36] | 46 | setTimeStep(0); |
---|
[6] | 47 | |
---|
| 48 | _material->setDiffuse(osg::Material::FRONT, osg::Vec4(DEF_WATER_COLOUR)); |
---|
| 49 | _material->setSpecular(osg::Material::FRONT, osg::Vec4(1,1,1,1)); |
---|
| 50 | _material->setTransparency(osg::Material::FRONT_AND_BACK, DEF_WATER_TRANSPARENCY); |
---|
| 51 | _material->setShininess(osg::Material::FRONT, 128); |
---|
| 52 | |
---|
| 53 | // environment map |
---|
| 54 | osg::Texture2D* texture = new osg::Texture2D; |
---|
| 55 | texture->setDataVariance(osg::Object::DYNAMIC); |
---|
| 56 | texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
---|
| 57 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
---|
| 58 | texture->setImage(osgDB::readImageFile("envmap.jpg")); |
---|
| 59 | _stateset->setTextureAttributeAndModes( 1, texture, osg::StateAttribute::ON ); |
---|
| 60 | _stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
| 61 | |
---|
| 62 | // surface transparency |
---|
| 63 | osg::BlendFunc* osgBlendFunc = new osg::BlendFunc(); |
---|
| 64 | osgBlendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); |
---|
| 65 | _stateset->setAttribute(osgBlendFunc); |
---|
| 66 | _stateset->setMode(GL_BLEND, osg::StateAttribute::ON); |
---|
| 67 | _stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
---|
[11] | 68 | |
---|
[6] | 69 | // automatically generate texture coords |
---|
| 70 | osg::TexGen* texgen = new osg::TexGen; |
---|
| 71 | texgen->setMode( osg::TexGen::SPHERE_MAP ); |
---|
| 72 | |
---|
| 73 | osg::TexEnv* texenv = new osg::TexEnv; |
---|
| 74 | texenv->setMode( osg::TexEnv::DECAL ); |
---|
| 75 | texenv->setColor( osg::Vec4(0.6f,0.6f,0.6f,0.2f) ); |
---|
| 76 | _stateset->setTextureAttributeAndModes( 1, texgen, osg::StateAttribute::ON ); |
---|
| 77 | _stateset->setTextureAttribute( 1, texenv ); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | WaterSurface::~WaterSurface() |
---|
| 83 | { |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | void WaterSurface::setTimeStep(unsigned int ts) |
---|
| 89 | { |
---|
| 90 | // delete if exists |
---|
| 91 | if( _geom->getNumPrimitiveSets() ) |
---|
| 92 | _geom->removePrimitiveSet(0); // reference counting actually does the delete |
---|
| 93 | |
---|
| 94 | // local reference to raw height field data |
---|
| 95 | osg::ref_ptr<osg::Vec3Array> vertices = _sww->getStageVertexArray(ts); |
---|
| 96 | osg::ref_ptr<osg::UIntArray> indices = _sww->getStageIndexArray(); |
---|
| 97 | osg::ref_ptr<osg::Vec3Array> normals = _sww->getStagePrimitiveNormalArray(); |
---|
| 98 | osg::ref_ptr<osg::Vec3Array> vertexnormals = _sww->getStageVertexNormalArray(); |
---|
| 99 | |
---|
[11] | 100 | |
---|
[13] | 101 | // vertical (z) offset to eliminate potential zbuffer problems with bedslope |
---|
[11] | 102 | for( unsigned int iv=0; iv < vertices.get()->size(); iv++ ) |
---|
[13] | 103 | vertices.get()->at(iv) += osg::Vec3f( 0.0, 0.0, _zoffset ); |
---|
[11] | 104 | |
---|
[6] | 105 | _geom->setVertexArray( vertices.get() ); |
---|
| 106 | _geom->setVertexIndices( indices.get() ); |
---|
| 107 | _geom->addPrimitiveSet( new osg::DrawArrays( |
---|
| 108 | osg::PrimitiveSet::TRIANGLES, 0, _sww->getNumberOfStageIndices() ) ); |
---|
| 109 | |
---|
| 110 | //_geom->setNormalArray( normals.get() ); |
---|
| 111 | //_geom->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE ); |
---|
| 112 | |
---|
| 113 | _geom->setNormalArray( vertexnormals.get() ); |
---|
| 114 | _geom->setNormalIndices( indices.get() ); |
---|
| 115 | _geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
| 116 | |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | void WaterSurface::toggleWireframe() |
---|
| 121 | { |
---|
| 122 | osg::PolygonMode* polyModeObj = |
---|
| 123 | dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); |
---|
| 124 | |
---|
| 125 | if (!polyModeObj) |
---|
| 126 | { |
---|
| 127 | polyModeObj = new osg::PolygonMode; |
---|
| 128 | _stateset->setAttribute(polyModeObj); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | switch( polyModeObj->getMode(osg::PolygonMode::FRONT_AND_BACK) ) |
---|
| 132 | { |
---|
| 133 | |
---|
| 134 | case osg::PolygonMode::FILL : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, |
---|
| 135 | osg::PolygonMode::LINE); break; |
---|
| 136 | case osg::PolygonMode::LINE : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, |
---|
| 137 | osg::PolygonMode::FILL); break; |
---|
| 138 | case osg::PolygonMode::POINT : break; |
---|
| 139 | } |
---|
| 140 | } |
---|