| | 15 | |
| | 16 | // for the grid data.. |
| | 17 | #include "../osghangglide/terrain_coords.h" |
| | 18 | |
| | 19 | osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y) |
| | 20 | { |
| | 21 | osgUtil::IntersectVisitor iv; |
| | 22 | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
| | 23 | |
| | 24 | const osg::BoundingSphere& bs = subgraph->getBound(); |
| | 25 | float zMax = bs.center().z()+bs.radius(); |
| | 26 | float zMin = bs.center().z()-bs.radius(); |
| | 27 | |
| | 28 | segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax)); |
| | 29 | iv.addLineSegment(segDown.get()); |
| | 30 | |
| | 31 | subgraph->accept(iv); |
| | 32 | |
| | 33 | if (iv.hits()) |
| | 34 | { |
| | 35 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
| | 36 | if (!hitList.empty()) |
| | 37 | { |
| | 38 | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
| | 39 | return ip; |
| | 40 | } |
| | 41 | } |
| | 42 | |
| | 43 | return osg::Vec3(x,y,0.0f); |
| | 44 | } |
| | 54 | osg::Geode* terrainGeode = new osg::Geode; |
| | 55 | { |
| | 56 | osg::StateSet* stateset = new osg::StateSet(); |
| | 57 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
| | 58 | if (image) |
| | 59 | { |
| | 60 | osg::Texture2D* texture = new osg::Texture2D; |
| | 61 | texture->setImage(image); |
| | 62 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
| | 63 | } |
| | 64 | |
| | 65 | terrainGeode->setStateSet( stateset ); |
| | 66 | |
| | 67 | float size = 1000; // 10km; |
| | 68 | float scale = size/39.0f; // 10km; |
| | 69 | float z_scale = scale*3.0f; |
| | 70 | |
| | 71 | osg::HeightField* grid = new osg::HeightField; |
| | 72 | grid->allocateGrid(38,39); |
| | 73 | grid->setXInterval(scale); |
| | 74 | grid->setYInterval(scale); |
| | 75 | |
| | 76 | for(unsigned int r=0;r<39;++r) |
| | 77 | { |
| | 78 | for(unsigned int c=0;c<38;++c) |
| | 79 | { |
| | 80 | grid->setHeight(c,r,z_scale*vertex[r+c*39][2]); |
| | 81 | } |
| | 82 | } |
| | 83 | terrainGeode->addDrawable(new osg::ShapeDrawable(grid)); |
| | 84 | |
| | 85 | root->addChild(terrainGeode); |
| | 86 | } |
| | 87 | |
| | 88 | |
| | 89 | |
| | 90 | osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform; |
| | 91 | positionEffects->setPosition(computeTerrainIntersection(terrainGeode,100.0f,100.0f)); |
| | 92 | root->addChild(positionEffects); |
| | 93 | |