osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthDrivers/engine_osgterrain/DynamicLODScaleCallback

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003 * Copyright 2008-2010 Pelican Mapping
00004 * http://osgearth.org
00005 *
00006 * osgEarth is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU Lesser General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * This program is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU Lesser General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU Lesser General Public License
00017 * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 
00020 #ifndef OSGEARTH_ENGINE_OSGTERRAIN_DYNAMIC_LOD_SCALE_CALLBACK_H
00021 #define OSGEARTH_ENGINE_OSGTERRAIN_DYNAMIC_LOD_SCALE_CALLBACK_H 1
00022 
00023 #include <osg/NodeCallback>
00024 #include <osg/CullStack>
00025 
00032 struct DynamicLODScaleCallback : public osg::NodeCallback 
00033 {
00034     DynamicLODScaleCallback( float fallOff ) : _fallOff(fallOff) { }
00035 
00036     void operator()( osg::Node* node, osg::NodeVisitor* nv )
00037     {
00038         osg::CullStack* cs = dynamic_cast<osg::CullStack*>(nv);
00039         if ( cs )
00040         {
00041             osg::LOD* lod = static_cast<osg::LOD*>( node );
00042             osg::Vec3 center = lod->getCenter(); 
00043 
00044             osg::Vec3 eye = nv->getEyePoint();
00045             osg::Vec3 eyeVec = eye; eyeVec.normalize();
00046             float has = osg::clampAbove( eye.length() - 6356752.3142f, 0.0f );
00047             float centerToEye = nv->getDistanceToViewPoint(center, false);
00048             float bsToEye = centerToEye - lod->getChild(0)->getBound().radius();
00049 
00050             float scaleAdj = 1.0f;
00051             if ( bsToEye > has )
00052             {
00053                 float denom = osg::maximum(0.1f, (1.0f/_fallOff)) * 10000.0f;
00054                 scaleAdj = osg::clampBetween( log10f(bsToEye/denom), 1.0f, 3.0f );
00055                 
00056                 //OE_INFO << LC 
00057                 //    << std::fixed
00058                 //    << "centerToEye=" << centerToEye 
00059                 //    << ", bsToEye=" << bsToEye
00060                 //    << ", scaleAdj=" << scaleAdj
00061                 //    << std::endl;
00062             }
00063 
00064             float lodScale = cs->getLODScale();
00065             cs->setLODScale( lodScale * scaleAdj );
00066             traverse( node, nv );
00067             cs->setLODScale( lodScale );
00068         }
00069         else
00070         {
00071             traverse( node, nv );
00072         }
00073     }
00074 
00075     float _fallOff;
00076 };
00077 
00078 #endif //OSGEARTH_ENGINE_OSGTERRAIN_DYNAMIC_LOD_SCALE_CALLBACK_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines