source: Swollen/swollen/bedslope.cpp @ 72

Last change on this file since 72 was 72, checked in by darran, 20 years ago
  • work in progress
File size: 2.9 KB
Line 
1
2
3#include <bedslope.h>
4
5#include <osg/Texture>
6#include <osg/Texture2D>
7
8#include <osgUtil/SmoothingVisitor>
9
10
11#define DEF_BEDSLOPE_COLOUR     145, 122, 59, 255     // R, G, B, Alpha (brown)
12
13
14// constructor
15BedSlope::BedSlope(SWWReader* sww)
16{
17    // persistent
18    _sww = sww;
19
20    // bedslope geometry as triangles with shared vertices
21    _geom = new osg::Geometry;
22
23    // geometry from sww file
24    osg::Vec3Array* vertices = _sww->getBedslopeVertexArray().get();
25
26    _geom->setUseDisplayList( true );
27    _geom->setVertexArray( vertices );
28    _geom->addPrimitiveSet( _sww->getBedslopeIndexArray().get() );
29    _geom->setTexCoordArray( 0, _sww->getBedslopeTextureCoords().get() );
30
31    osg::UByte4Array* color = new osg::UByte4Array(1);
32    (*color)[0] = osg::UByte4( DEF_BEDSLOPE_COLOUR );
33    _geom->setColorArray( color );
34    _geom->setColorBinding( osg::Geometry::BIND_OVERALL );
35
36    // Calculate per-vertex normals
37    osgUtil::SmoothingVisitor* visitor = new osgUtil::SmoothingVisitor();
38    visitor->smooth( *_geom );
39
40    // bedslope texture
41    _texture = true;
42    osg::Texture2D* texture = new osg::Texture2D;
43    texture->setDataVariance( osg::Object::DYNAMIC );
44    texture->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT );
45    texture->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT );
46    texture->setImage( _sww->getBedslopeTexture() );
47
48    // material
49    _material = new osg::Material();
50    _material->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4(0.2, 0.15, 0.06, 1.0) );
51    _material->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4(0.6, 0.5, 0.2, 1.0) );
52    _material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) );
53    _material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) );
54
55    _material->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) );
56    _material->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0) );
57    _material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) );
58    _material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0) );
59
60
61    // state
62    _stateset = new osg::StateSet;
63    _stateset->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON );
64    _stateset->setAttributeAndModes( _material, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
65    _stateset->setMode( GL_BLEND, osg::StateAttribute::ON );
66    _stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON );
67
68    _node = new osg::Geode;
69    _node->setName("bedslope");
70    _node->addDrawable(_geom);
71    _node->setStateSet(_stateset);
72
73}
74
75
76void BedSlope::toggleTexture()
77{
78
79    if ( _texture )
80    {
81        _stateset->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::OFF );
82        _texture = false;
83    }
84    else
85    { 
86        _stateset->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::ON );
87        _texture = true;
88    }
89
90}
Note: See TracBrowser for help on using the repository browser.