source: Swollen/swollen/watersurface.cpp @ 61

Last change on this file since 61 was 61, checked in by darran, 20 years ago
  • Added AlphaFunc? for threshold (-alphamin <float>) culling of pixels
  • Prettified Ole's source code modifications to swwreader ;-)
File size: 4.1 KB
Line 
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
25WaterSurface::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    // FIXME: if set to true, wireframe doesn't work - need to dirty to force update?
39    _geom->setUseDisplayList( false );
40
41    // initial geometry
42    setTimeStep(0);
43
44    // environment map
45    osg::Texture2D* texture = new osg::Texture2D;
46    texture->setDataVariance(osg::Object::DYNAMIC);
47    texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f));
48    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
49    texture->setImage(osgDB::readImageFile("envmap.jpg"));
50    _stateset->setTextureAttributeAndModes( 1, texture, osg::StateAttribute::ON );
51    _stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
52
53    // surface transparency
54    osg::BlendFunc* osgBlendFunc = new osg::BlendFunc();
55    osgBlendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
56    _stateset->setAttribute(osgBlendFunc);
57    _stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
58    _stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
59
60    // discard pixels with an alpha value below threshold
61    osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
62    alphaFunc->setFunction( osg::AlphaFunc::GREATER, _sww->getAlphaMin() );
63    _stateset->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON );
64
65    // automatically generate texture coords
66    osg::TexGen* texgen = new osg::TexGen;
67    texgen->setMode( osg::TexGen::SPHERE_MAP );
68
69    osg::TexEnv* texenv = new osg::TexEnv;
70    texenv->setMode( osg::TexEnv::DECAL );
71    texenv->setColor( osg::Vec4(0.6f,0.6f,0.6f,0.2f) );
72    _stateset->setTextureAttributeAndModes( 1, texgen, osg::StateAttribute::ON );
73    _stateset->setTextureAttribute( 1, texenv );
74}
75
76
77
78WaterSurface::~WaterSurface()
79{
80}
81
82
83
84void WaterSurface::setTimeStep(unsigned int ts)
85{
86    // delete if exists
87    if( _geom->getNumPrimitiveSets() )
88      _geom->removePrimitiveSet(0);  // reference counting does actual delete
89
90    // local reference to raw height field data
91    osg::ref_ptr<osg::Vec3Array> vertices = _sww->getStageVertexArray(ts);
92    osg::ref_ptr<osg::Vec3Array> vertexnormals = _sww->getStageVertexNormalArray();
93    osg::ref_ptr<osg::Vec4Array> colors = _sww->getStageColorArray();
94
95    // geometry
96    _geom->setVertexArray( vertices.get() );
97    _geom->addPrimitiveSet( _sww->getBedslopeIndexArray().get() );
98
99    // per vertex colors (we only modulate the alpha for transparency)
100    _geom->setColorArray( colors.get() );
101    _geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
102
103    // normals
104    _geom->setNormalArray( vertexnormals.get() );
105    _geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
106
107    // osgUtil::TriStripVisitor* optimize = new osgUtil::TriStripVisitor();
108    // optimize->stripify( *_geom );
109
110}
111
112
113void WaterSurface::toggleWireframe()
114{
115    osg::PolygonMode* polyModeObj = 
116        dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE));
117
118    if (!polyModeObj) 
119    {
120            polyModeObj = new osg::PolygonMode;
121            _stateset->setAttribute(polyModeObj);
122    }
123
124    switch( polyModeObj->getMode(osg::PolygonMode::FRONT_AND_BACK) )
125    {
126   
127      case osg::PolygonMode::FILL : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,
128                                                         osg::PolygonMode::LINE); break;
129      case osg::PolygonMode::LINE : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,
130                                                         osg::PolygonMode::FILL); break;
131      case osg::PolygonMode::POINT : break;
132    }
133}
Note: See TracBrowser for help on using the repository browser.