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 | #define DEF_WATER_COLOUR 0, 0, 0.5, 1 // R, G, B, Alpha (blue) |
---|
25 | #define DEF_WATER_TRANSPARENCY 0.1 // 0=opaque, 1=transparent |
---|
26 | #define DEF_ZOFFSET 0.01 |
---|
27 | |
---|
28 | // constructor |
---|
29 | WaterSurface::WaterSurface(SWWReader* sww) |
---|
30 | { |
---|
31 | // persistent |
---|
32 | _sww = sww; |
---|
33 | _node = new osg::Geode; |
---|
34 | _geom = new osg::Geometry; |
---|
35 | _stateset = new osg::StateSet; |
---|
36 | _material = new osg::Material; |
---|
37 | _zoffset = DEF_ZOFFSET; |
---|
38 | |
---|
39 | // construct local scenegraph hierarchy |
---|
40 | _node->setName("watersurface"); |
---|
41 | _node->addDrawable(_geom); |
---|
42 | _node->setStateSet(_stateset); |
---|
43 | //_stateset->setAttribute(_material); |
---|
44 | |
---|
45 | _geom->setUseDisplayList(true); |
---|
46 | |
---|
47 | // initial geometry |
---|
48 | setTimeStep(0); |
---|
49 | |
---|
50 | //_material->setDiffuse(osg::Material::FRONT, osg::Vec4(DEF_WATER_COLOUR)); |
---|
51 | //_material->setSpecular(osg::Material::FRONT, osg::Vec4(1,1,1,1)); |
---|
52 | //_material->setTransparency(osg::Material::FRONT_AND_BACK, DEF_WATER_TRANSPARENCY); |
---|
53 | //_material->setShininess(osg::Material::FRONT, 128); |
---|
54 | |
---|
55 | // environment map |
---|
56 | osg::Texture2D* texture = new osg::Texture2D; |
---|
57 | texture->setDataVariance(osg::Object::DYNAMIC); |
---|
58 | texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
---|
59 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
---|
60 | texture->setImage(osgDB::readImageFile("envmap.jpg")); |
---|
61 | //_stateset->setTextureAttributeAndModes( 1, texture, osg::StateAttribute::ON ); |
---|
62 | _stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
63 | |
---|
64 | // surface transparency |
---|
65 | //osg::BlendFunc* osgBlendFunc = new osg::BlendFunc(); |
---|
66 | //osgBlendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); |
---|
67 | //_stateset->setAttribute(osgBlendFunc); |
---|
68 | //_stateset->setMode(GL_BLEND, osg::StateAttribute::ON); |
---|
69 | |
---|
70 | osg::AlphaFunc* alphaFunc = new osg::AlphaFunc; |
---|
71 | //alphaFunc->setFunction(osg::AlphaFunc::GEQUAL,0.1f); |
---|
72 | alphaFunc->setFunction(osg::AlphaFunc::ALWAYS); |
---|
73 | _stateset->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON ); |
---|
74 | |
---|
75 | _stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
---|
76 | |
---|
77 | |
---|
78 | // automatically generate texture coords |
---|
79 | osg::TexGen* texgen = new osg::TexGen; |
---|
80 | texgen->setMode( osg::TexGen::SPHERE_MAP ); |
---|
81 | |
---|
82 | osg::TexEnv* texenv = new osg::TexEnv; |
---|
83 | texenv->setMode( osg::TexEnv::DECAL ); |
---|
84 | texenv->setColor( osg::Vec4(0.6f,0.6f,0.6f,0.2f) ); |
---|
85 | //_stateset->setTextureAttributeAndModes( 1, texgen, osg::StateAttribute::ON ); |
---|
86 | //_stateset->setTextureAttribute( 1, texenv ); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | WaterSurface::~WaterSurface() |
---|
92 | { |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | void WaterSurface::setTimeStep(unsigned int ts) |
---|
98 | { |
---|
99 | // delete if exists |
---|
100 | if( _geom->getNumPrimitiveSets() ) |
---|
101 | _geom->removePrimitiveSet(0); // reference counting actually does the delete |
---|
102 | |
---|
103 | // local reference to raw height field data |
---|
104 | osg::ref_ptr<osg::Vec3Array> vertices = _sww->getStageVertexArray(ts); |
---|
105 | osg::ref_ptr<osg::UIntArray> indices = _sww->getStageIndexArray(); |
---|
106 | osg::ref_ptr<osg::Vec3Array> normals = _sww->getStagePrimitiveNormalArray(); |
---|
107 | osg::ref_ptr<osg::Vec3Array> vertexnormals = _sww->getStageVertexNormalArray(); |
---|
108 | osg::ref_ptr<osg::Vec4Array> colors = _sww->getStageColorArray(); |
---|
109 | |
---|
110 | // vertical (z) offset to eliminate potential zbuffer problems with bedslope |
---|
111 | osg::UIntArray* colorindices = new osg::UIntArray(vertices.get()->size()); |
---|
112 | for( unsigned int iv=0; iv < vertices.get()->size(); iv++ ) |
---|
113 | { |
---|
114 | vertices.get()->at(iv) += osg::Vec3f( 0.0, 0.0, _zoffset ); |
---|
115 | colorindices->at(iv) = iv; |
---|
116 | } |
---|
117 | |
---|
118 | // geometry |
---|
119 | _geom->setVertexArray( vertices.get() ); |
---|
120 | _geom->setVertexIndices( indices.get() ); |
---|
121 | _geom->addPrimitiveSet( new osg::DrawArrays( |
---|
122 | osg::PrimitiveSet::TRIANGLES, 0, _sww->getNumberOfStageIndices() ) ); |
---|
123 | |
---|
124 | // per vertex colors (using only alpha) |
---|
125 | _geom->setColorArray( colors.get() ); |
---|
126 | _geom->setColorIndices( colorindices ); |
---|
127 | _geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
128 | |
---|
129 | // normals |
---|
130 | _geom->setNormalArray( vertexnormals.get() ); |
---|
131 | _geom->setNormalIndices( indices.get() ); |
---|
132 | _geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
133 | |
---|
134 | // osgUtil::TriStripVisitor* optimize = new osgUtil::TriStripVisitor(); |
---|
135 | // optimize->stripify( *_geom ); |
---|
136 | |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | void WaterSurface::toggleWireframe() |
---|
141 | { |
---|
142 | osg::PolygonMode* polyModeObj = |
---|
143 | dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); |
---|
144 | |
---|
145 | if (!polyModeObj) |
---|
146 | { |
---|
147 | polyModeObj = new osg::PolygonMode; |
---|
148 | _stateset->setAttribute(polyModeObj); |
---|
149 | } |
---|
150 | |
---|
151 | switch( polyModeObj->getMode(osg::PolygonMode::FRONT_AND_BACK) ) |
---|
152 | { |
---|
153 | |
---|
154 | case osg::PolygonMode::FILL : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, |
---|
155 | osg::PolygonMode::LINE); break; |
---|
156 | case osg::PolygonMode::LINE : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, |
---|
157 | osg::PolygonMode::FILL); break; |
---|
158 | case osg::PolygonMode::POINT : break; |
---|
159 | } |
---|
160 | } |
---|