source: Swollen/swollen/watersurface.cpp @ 45

Last change on this file since 45 was 45, checked in by darran, 20 years ago

Major changes as per Robert's suggestions

  • removed all culling from swwreader
  • DrawElements? for primitive set improves speed
  • per vertex alpha (vec4 color)

Also,

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