From 454ccad3b9cc24ddc9c8e587a861b90098e64b97 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 5 Sep 2011 12:27:20 +0000 Subject: [PATCH] Replaced use of ShapeDrawable with osgTerrain for the terrain tile --- examples/osgshadow/CMakeLists.txt | 2 +- examples/osgshadow/IslandScene.cpp | 49 ++++++++++++++++-------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/examples/osgshadow/CMakeLists.txt b/examples/osgshadow/CMakeLists.txt index 73f38c7de..174bea91a 100644 --- a/examples/osgshadow/CMakeLists.txt +++ b/examples/osgshadow/CMakeLists.txt @@ -3,6 +3,6 @@ SET(TARGET_SRC osgshadow.cpp IslandScene.cpp ) SET(TARGET_H terrain_coords.h IslandScene.h ) -SET(TARGET_ADDED_LIBRARIES osgShadow ) +SET(TARGET_ADDED_LIBRARIES osgShadow osgTerrain) #### end var setup ### SETUP_EXAMPLE(osgshadow) diff --git a/examples/osgshadow/IslandScene.cpp b/examples/osgshadow/IslandScene.cpp index 3f10b8935..7c8719d02 100644 --- a/examples/osgshadow/IslandScene.cpp +++ b/examples/osgshadow/IslandScene.cpp @@ -11,6 +11,8 @@ #include #include +#include + static unsigned int heightTexture[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00010101, 0x02000000, 0x00020809, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000001, 0x00000000, 0x00000000, 0x00000000, @@ -469,19 +471,15 @@ osg::Node* createIsland(const osg::Vec3& center = osg::Vec3( 0,0,0 ), float radi { float height = 1000; - osg::Geode* geode = new osg::Geode; + osg::ref_ptr group = new osg::Group; - // set up the texture of the base. - osg::StateSet* stateset = new osg::StateSet(); - geode->setStateSet( stateset ); - - osg::Image * heightMap = new osg::Image(); + osg::ref_ptr heightMap = new osg::Image(); heightMap->setImage( 64, 64, 1, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, orderBytes( heightTexture, sizeof( heightTexture ) ), osg::Image::NO_DELETE ); - osg::Image* colorMap = NULL; // osgDB::readImageFile("Images/colorMap.png"); + osg::ref_ptr colorMap = NULL; // osgDB::readImageFile("Images/colorMap.png"); if ( !colorMap ) { struct colorElevation @@ -527,15 +525,6 @@ osg::Node* createIsland(const osg::Vec3& center = osg::Vec3( 0,0,0 ), float radi } } - if (colorMap) - { - osg::Texture2D* texture = new osg::Texture2D; - texture->setImage(colorMap); - stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); - } - - stateset->setAttributeAndModes( new osg::CullFace(), osg::StateAttribute::ON ); - osg::HeightField* grid = new osg::HeightField; grid->allocate(heightMap->s(),heightMap->t()); grid->setOrigin( center - osg::Vec3( radius, radius, 0 ) ); @@ -549,17 +538,33 @@ osg::Node* createIsland(const osg::Vec3& center = osg::Vec3( 0,0,0 ), float radi grid->setHeight( c, r, height * exp( *heightMap->data(c,r) / 255.f ) / exp( 1.0 ) ); } } - osg::ShapeDrawable * sd = new osg::ShapeDrawable(grid); - sd->setColor( osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) ); - geode->addDrawable(sd); + osg::ref_ptr terrainTile = new osgTerrain::TerrainTile; - osg::Group* group = new osg::Group; - group->addChild(geode); + osg::ref_ptr locator = new osgTerrain::Locator; + locator->setCoordinateSystemType(osgTerrain::Locator::PROJECTED); + locator->setTransformAsExtents(center.x()-radius,center.y()-radius,center.x()+radius,center.y()+radius); + terrainTile->setLocator(locator.get()); + + osg::ref_ptr hfl = new osgTerrain::HeightFieldLayer; + hfl->setHeightField(grid); + hfl->setLocator(locator.get()); + terrainTile->setElevationLayer(hfl.get()); + + osg::ref_ptr imageLayer = new osgTerrain::ImageLayer; + imageLayer->setImage(colorMap.get()); + imageLayer->setLocator(locator.get()); + terrainTile->setColorLayer(0, imageLayer.get()); + + osg::StateSet* stateset = terrainTile->getOrCreateStateSet(); + stateset->setAttributeAndModes( new osg::CullFace(), osg::StateAttribute::ON ); + + group->addChild(terrainTile.get()); + group->addChild( createObjects( grid, 1 ) ); - return group; + return group.release(); } //////////////////////////////////////////////////////////////////////////////// namespace ModelFour {