source: Swollen/swollen/watersurface.cpp @ 71

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