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/AlphaFunc> |
---|
11 | #include <osg/BlendFunc> |
---|
12 | #include <osg/PolygonMode> |
---|
13 | #include <osg/ShapeDrawable> |
---|
14 | #include <osg/Texture2D> |
---|
15 | #include <osg/TexEnv> |
---|
16 | #include <osg/TexGen> |
---|
17 | |
---|
18 | #include <osgDB/Registry> |
---|
19 | #include <osgDB/ReadFile> |
---|
20 | |
---|
21 | #include <osgUtil/TriStripVisitor> |
---|
22 | |
---|
23 | |
---|
24 | // constructor |
---|
25 | WaterSurface::WaterSurface(SWWReader* sww) |
---|
26 | { |
---|
27 | // persistent |
---|
28 | _sww = sww; |
---|
29 | _node = new osg::Geode; |
---|
30 | _geom = new osg::Geometry; |
---|
31 | _stateset = new osg::StateSet; |
---|
32 | |
---|
33 | // construct local scenegraph hierarchy |
---|
34 | _node->setName("watersurface"); |
---|
35 | _node->addDrawable(_geom); |
---|
36 | _node->setStateSet(_stateset); |
---|
37 | |
---|
38 | _geom->setUseDisplayList( true ); |
---|
39 | |
---|
40 | // initial geometry |
---|
41 | setTimeStep(0); |
---|
42 | |
---|
43 | // environment map |
---|
44 | osg::Texture2D* texture = new osg::Texture2D; |
---|
45 | texture->setDataVariance(osg::Object::DYNAMIC); |
---|
46 | texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
---|
47 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
---|
48 | texture->setImage(osgDB::readImageFile("envmap.jpg")); |
---|
49 | _stateset->setTextureAttributeAndModes( 1, texture, osg::StateAttribute::ON ); |
---|
50 | _stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
51 | |
---|
52 | // surface transparency |
---|
53 | osg::BlendFunc* osgBlendFunc = new osg::BlendFunc(); |
---|
54 | osgBlendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); |
---|
55 | _stateset->setAttribute(osgBlendFunc); |
---|
56 | _stateset->setMode(GL_BLEND, osg::StateAttribute::ON); |
---|
57 | _stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
---|
58 | |
---|
59 | // discard pixels with an alpha value below threshold |
---|
60 | osg::AlphaFunc* alphaFunc = new osg::AlphaFunc; |
---|
61 | alphaFunc->setFunction( osg::AlphaFunc::GREATER, _sww->getAlphaMin() ); |
---|
62 | _stateset->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON ); |
---|
63 | |
---|
64 | // automatically generate texture coords |
---|
65 | osg::TexGen* texgen = new osg::TexGen; |
---|
66 | texgen->setMode( osg::TexGen::SPHERE_MAP ); |
---|
67 | |
---|
68 | osg::TexEnv* texenv = new osg::TexEnv; |
---|
69 | texenv->setMode( osg::TexEnv::DECAL ); |
---|
70 | texenv->setColor( osg::Vec4(0.6f,0.6f,0.6f,0.2f) ); |
---|
71 | _stateset->setTextureAttributeAndModes( 1, texgen, osg::StateAttribute::ON ); |
---|
72 | _stateset->setTextureAttribute( 1, texenv ); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | WaterSurface::~WaterSurface() |
---|
78 | { |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | void WaterSurface::setTimeStep(unsigned int ts) |
---|
84 | { |
---|
85 | // delete if exists |
---|
86 | if( _geom->getNumPrimitiveSets() ) |
---|
87 | _geom->removePrimitiveSet(0); // reference counting does actual delete |
---|
88 | |
---|
89 | // local reference to raw height field data |
---|
90 | osg::ref_ptr<osg::Vec3Array> vertices = _sww->getStageVertexArray(ts); |
---|
91 | osg::ref_ptr<osg::Vec3Array> vertexnormals = _sww->getStageVertexNormalArray(); |
---|
92 | osg::ref_ptr<osg::Vec4Array> colors = _sww->getStageColorArray(); |
---|
93 | |
---|
94 | // geometry |
---|
95 | _geom->setVertexArray( vertices.get() ); |
---|
96 | _geom->addPrimitiveSet( _sww->getBedslopeIndexArray().get() ); |
---|
97 | |
---|
98 | // per vertex colors (we only modulate the alpha for transparency) |
---|
99 | _geom->setColorArray( colors.get() ); |
---|
100 | _geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
101 | |
---|
102 | // normals |
---|
103 | _geom->setNormalArray( vertexnormals.get() ); |
---|
104 | _geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
105 | |
---|
106 | // osgUtil::TriStripVisitor* optimize = new osgUtil::TriStripVisitor(); |
---|
107 | // optimize->stripify( *_geom ); |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void WaterSurface::toggleWireframe() |
---|
113 | { |
---|
114 | osg::PolygonMode* polyModeObj = |
---|
115 | dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); |
---|
116 | |
---|
117 | if (!polyModeObj) |
---|
118 | { |
---|
119 | polyModeObj = new osg::PolygonMode; |
---|
120 | _stateset->setAttribute(polyModeObj); |
---|
121 | } |
---|
122 | |
---|
123 | switch( polyModeObj->getMode(osg::PolygonMode::FRONT_AND_BACK) ) |
---|
124 | { |
---|
125 | |
---|
126 | case osg::PolygonMode::FILL : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, |
---|
127 | osg::PolygonMode::LINE); break; |
---|
128 | case osg::PolygonMode::LINE : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, |
---|
129 | osg::PolygonMode::FILL); break; |
---|
130 | case osg::PolygonMode::POINT : break; |
---|
131 | } |
---|
132 | } |
---|