Index: /osgVisual/trunk/CMakeLists.txt
===================================================================
--- /osgVisual/trunk/CMakeLists.txt	(revision 297)
+++ /osgVisual/trunk/CMakeLists.txt	(revision 298)
@@ -18,5 +18,5 @@
 #################################################################################################
 
-cmake_minimum_required(VERSION 2.8)
+cmake_minimum_required(VERSION 3.0)
 
 
@@ -305,4 +305,6 @@
 	include/util/visual_util.h
 	src/util/visual_util.cpp
+	include/util/terrainQuery.h
+	src/util/terrainQuery.cpp
 	# Draw 2D
 	include/draw2D/visual_draw2D.h
Index: /osgVisual/trunk/bin/osgVisualConfig.xml
===================================================================
--- /osgVisual/trunk/bin/osgVisualConfig.xml	(revision 297)
+++ /osgVisual/trunk/bin/osgVisualConfig.xml	(revision 298)
@@ -2,5 +2,5 @@
 <osgvisualconfiguration>
   <module name="distortion" enabled="no">
-    <distortion channelname="center" renderimplementation="fbo" width="2048" height="2048" useshader="yes" hdr="yes" usetexturerectangle="no"></distortion>
+    <distortion channelname="right" renderimplementation="fbo" width="1920" height="1200" useshader="yes" hdr="no" usetexturerectangle="no"></distortion>
   </module>
   <module name="sky_silverlining" enabled="yes"></module>
@@ -15,5 +15,6 @@
   
   <scenery>
-    <terrain filename="D:/OpenSceneGraph/VPB-Testdatensatz/DB_Small/database.ive.terrainmod" filename2="J:\BRD1m_MUC0.25m_srtmEU_BM\terrain.ive.terrainmod" filename3="axes.osg.10000000.scale"></terrain>
+    <!-- <terrain filename="D:/OpenSceneGraph/VPB-Testdatensatz/DB_Small/database.ive.terrainmod" filename2="H:\BRD1m_MUC0.25m_srtmEU_BM\terrain.ive" filename3="axes.osg.10000000.scale"></terrain>-->
+    <terrain filename="D:/OpenSceneGraph/VPB-Testdatensatz/DB_Small/database.ive.terrainmod" filename2="H:\BRD1m_MUC0.25m_srtmEU_BM\terrain.ive"></terrain>
     <animationpath filename="airport_muc.path"></animationpath>
     <models>
Index: /osgVisual/trunk/include/util/TerrainQuery.h
===================================================================
--- /osgVisual/trunk/include/util/TerrainQuery.h	(revision 298)
+++ /osgVisual/trunk/include/util/TerrainQuery.h	(revision 298)
@@ -0,0 +1,129 @@
+/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
+ *
+ * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under 
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
+ * (at your option) any later version.  The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ *
+ * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
+ * You have to aquire licenses for all used proprietary modules.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * OpenSceneGraph Public License for more details.
+*/
+
+#ifndef OSGSIM_HEIGHTABOVETERRAIN
+#define OSGSIM_HEIGHTABOVETERRAIN 1
+
+#include <osgUtil/IntersectionVisitor>
+
+// include so we can get access to the DatabaseCacheReadCallback
+#include <osgSim/LineOfSight>
+
+namespace osgSim {
+
+/** Helper class for setting up and acquiring height above terrain intersections with terrain.
+  * By default assigns a osgSim::DatabaseCacheReadCallback that enables automatic loading
+  * of external PagedLOD tiles to ensure that the highest level of detail is used in intersections.
+  * This automatic loading of tiles is done by the intersection traversal that is done within
+  * the computeIntersections(..) method, so can result in long intersection times when external
+  * tiles have to be loaded.
+  * The external loading of tiles can be disabled by removing the read callback, this is done by
+  * calling the setDatabaseCacheReadCallback(DatabaseCacheReadCallback*) method with a value of 0.*/
+class OSGSIM_EXPORT HeightAboveTerrain
+{
+    public :
+
+    
+        HeightAboveTerrain(bool loadHighestLOD = true);
+        
+    
+        /** Clear the internal HAT List so it contains no height above terrain tests.*/
+        void clear();
+        
+        /** Add a height above terrain test point in the CoordinateFrame.*/
+        unsigned int addPoint(const osg::Vec3d& point);
+
+        /** Get the number of height above terrain tests.*/
+        unsigned int getNumPoints() const { return _HATList.size(); }
+        
+        /** Set the source point of single height above terrain test.*/
+        void setPoint(unsigned int i, const osg::Vec3d& point) { _HATList[i]._point = point; }
+
+        /** Get the source point of single height above terrain test.*/
+        const osg::Vec3d& getPoint(unsigned int i) const { return _HATList[i]._point; }
+
+        /** Get the intersection height for a single height above terrain test.
+          * Note, you must call computeIntersections(..) before you can query the HeightAboveTerrain. 
+          * If no intersections are found then height returned will be the height above mean sea level. */
+        double getHeightAboveTerrain(unsigned int i) const  { return _HATList[i]._hat; }
+
+		/** Get the intersection height for a single height of terrain test.
+          * Note, you must call computeIntersections(..) before you can query the HeightOfTerrain. 
+          * If no intersections are found then height returned will be the height of mean sea level. */
+        double getHeightOfTerrain(unsigned int i) const  { return _HATList[i]._hot; }
+
+        /** Set the lowest height that the should be tested for.
+          * Defaults to -1000, i.e. 1000m below mean sea level. */
+        void setLowestHeight(double lowestHeight) { _lowestHeight = lowestHeight; }
+
+        /** Get the lowest height that the should be tested for.*/
+        double getLowestHeight() const { return _lowestHeight; }
+        
+        /** Compute the HAT intersections with the specified scene graph.
+          * The results are all stored in the form of a single height above terrain value per HAT test.
+          * Note, if the topmost node is a CoordinateSystemNode then the input points are assumed to be geocentric,
+          * with the up vector defined by the EllipsoidModel attached to the CoordinateSystemNode.
+          * If the topmost node is not a CoordinateSystemNode then a local coordinates frame is assumed, with a local up vector. */
+        void computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask=0xffffffff);
+
+        /** Compute the vertical distance between the specified scene graph and a single HAT point. */
+        static double computeHeightAboveTerrain(osg::Node* scene, const osg::Vec3d& point, bool loadHighestLOD = true, osg::Node::NodeMask traversalMask=0xffffffff);
+        
+		/** Compute the vertical position of the scene at a single HOT point. */
+        static double computeHeightOfTerrain(osg::Node* scene, const osg::Vec3d& point, bool loadHighestLOD = true, osg::Node::NodeMask traversalMask=0xffffffff);
+
+        
+        /** Clear the database cache.*/
+        void clearDatabaseCache() { if (_dcrc.valid()) _dcrc->clearDatabaseCache(); }
+
+        /** Set the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.
+          * Note, if you have multiple LineOfSight or HeightAboveTerrain objects in use at one time then you should share a single
+          * DatabaseCacheReadCallback between all of them. */
+        void setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc);
+
+        /** Get the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.*/
+        DatabaseCacheReadCallback* getDatabaseCacheReadCallback() { return _dcrc.get(); }
+        
+    protected :
+    
+        struct HAT
+        {
+            HAT(const osg::Vec3d& point):
+                _point(point),
+                _hat(0.0),
+				_hot(0.0) {}
+                
+            osg::Vec3d      _point;
+            double          _hat;
+			double          _hot;
+        };
+        
+        typedef std::vector<HAT> HATList;
+        
+
+        double                                  _lowestHeight;
+        HATList                                 _HATList;
+
+        
+        osg::ref_ptr<DatabaseCacheReadCallback> _dcrc;
+        osgUtil::IntersectionVisitor            _intersectionVisitor;
+
+
+};
+
+}
+
+#endif
Index: /osgVisual/trunk/resources/distortion/shader.frag
===================================================================
--- /osgVisual/trunk/resources/distortion/shader.frag	(revision 297)
+++ /osgVisual/trunk/resources/distortion/shader.frag	(revision 298)
@@ -12,7 +12,14 @@
     blendColor = texture2D(textureBlend, gl_TexCoord[0].st);
 
-    texCoord.s = (distortColor.b + mod(floor(distortColor.r * 255.5), 16.0)) / 16.0;
-    texCoord.t = 1.0 - (distortColor.g + floor(distortColor.r * 255.5 / 16.0)) / 16.0;
+// modified 
+//    texCoord.s = (distortColor.b / 255.0 + mod(distortColor.r , 16.0)) / 16.0;
+//    texCoord.t = 1.0 - (distortColor.g / 255.0 + (distortColor.r / 16.0)) / 16.0;
+
+// Original
+texCoord.s = (distortColor.b + mod(floor(distortColor.r * 255.5), 16.0)) / 16.0;
+texCoord.t = 1.0 - (distortColor.g + floor(distortColor.r * 255.5 / 16.0)) / 16.0;
 
     gl_FragColor = texture2D(textureImage, texCoord.st) * blendColor;
 }
+
+
Index: /osgVisual/trunk/src/draw2D/visual_debug_hud.cpp
===================================================================
--- /osgVisual/trunk/src/draw2D/visual_debug_hud.cpp	(revision 297)
+++ /osgVisual/trunk/src/draw2D/visual_debug_hud.cpp	(revision 298)
@@ -16,4 +16,5 @@
 
 #include <visual_debug_hud.h>
+#include <osgSim/HeightAboveTerrain>
 
 using namespace osgVisual;
@@ -141,6 +142,14 @@
 
 	util::getWGS84ofCamera( sceneCamera, csn, lat, lon, alt ); 
-	util::queryHeightAboveTerrainInWGS84(hat, csn, lat, lon, alt);
-	util::queryHeightOfTerrain(hot, csn, lat, lon);
+	//util::queryHeightAboveTerrainInWGS84(hat, csn, lat, lon, alt);
+	//util::queryHeightOfTerrain(hot, csn, lat, lon);
+
+	double x = 0;
+	double y = 0;
+	double z = 0;
+
+	util::getXYZofCamera( sceneCamera, x, y, z ); 
+	hat = osgSim::HeightAboveTerrain::computeHeightAboveTerrain(csn, osg::Vec3d(x,y,z), false);
+	hot = osgSim::HeightAboveTerrain::computeHeightOfTerrain(csn, osg::Vec3d(x,y,z), false);
 
 
Index: /osgVisual/trunk/src/sky_Silverlining/skySilverLining_skyDrawable.cpp
===================================================================
--- /osgVisual/trunk/src/sky_Silverlining/skySilverLining_skyDrawable.cpp	(revision 297)
+++ /osgVisual/trunk/src/sky_Silverlining/skySilverLining_skyDrawable.cpp	(revision 298)
@@ -80,13 +80,4 @@
         //direction = view * direction;
         direction.normalize();
-		OSG_NOTIFY( osg::ALWAYS) << "X: "<<direction.x()<<"  Y: "<<direction.y()<<"  Z:"<<direction.z()<< std::endl;
-		
-double cx,cy,cz;
-util::getXYZofCamera(_view->getCamera(), cx, cy, cz);
-osg::Vec3d up(cx, cy, cz);
-//up.normalize();
-//util::AddCylinderBetweenPoints(osg::Vec3d(cx,cy,cz), direction*15000000 , 5000.0, osg::Vec4d(1.0, 1.0, 0.0, 1 ), sceneRoot);
-util::AddCylinderBetweenPoints(osg::Vec3d(0,0,0), direction*15000000 , 5000.0, osg::Vec4d(0.0, 1.0, 1.0, 1 ), sceneRoot);
-
 
         light->setAmbient(ambient);
Index: /osgVisual/trunk/src/util/TerrainQuery.cpp
===================================================================
--- /osgVisual/trunk/src/util/TerrainQuery.cpp	(revision 298)
+++ /osgVisual/trunk/src/util/TerrainQuery.cpp	(revision 298)
@@ -0,0 +1,150 @@
+/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
+ *
+ * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under 
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
+ * (at your option) any later version.  The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ *
+ * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
+ * You have to aquire licenses for all used proprietary modules.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * OpenSceneGraph Public License for more details.
+*/
+
+#include <osg/CoordinateSystemNode>
+#include <terrainQuery.h>
+
+#include <osg/Notify>
+#include <osgUtil/LineSegmentIntersector>
+
+using namespace osgSim;
+
+HeightAboveTerrain::HeightAboveTerrain(bool loadHighestLOD)
+{
+    _lowestHeight = -1000.0;
+    
+	if(loadHighestLOD)
+		setDatabaseCacheReadCallback(new DatabaseCacheReadCallback);
+}
+
+void HeightAboveTerrain::clear()
+{
+    _HATList.clear();
+}
+
+unsigned int HeightAboveTerrain::addPoint(const osg::Vec3d& point)
+{
+    unsigned int index = _HATList.size();
+    _HATList.push_back(HAT(point));
+    return index;
+}
+
+void HeightAboveTerrain::computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask)
+{
+    osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(scene);
+    osg::EllipsoidModel* em = csn ? csn->getEllipsoidModel() : 0;
+
+    osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
+
+    for(HATList::iterator itr = _HATList.begin();
+        itr != _HATList.end();
+        ++itr)
+    {
+        if (em)
+        {
+        
+            osg::Vec3d start = itr->_point;
+            osg::Vec3d upVector = em->computeLocalUpVector(start.x(), start.y(), start.z());
+
+            double latitude, longitude, height;
+            em->convertXYZToLatLongHeight(start.x(), start.y(), start.z(), latitude, longitude, height);
+            osg::Vec3d end = start - upVector * (height - _lowestHeight);            
+            
+            itr->_hat = height;
+
+            OSG_NOTICE<<"lat = "<<latitude<<" longitude = "<<longitude<<" height = "<<height<<std::endl;
+
+            osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end);
+            intersectorGroup->addIntersector( intersector.get() );
+        }
+        else
+        {
+            osg::Vec3d start = itr->_point;
+            osg::Vec3d upVector (0.0, 0.0, 1.0);
+            
+            double height = start.z();
+            osg::Vec3d end = start - upVector * (height - _lowestHeight);            
+
+            itr->_hat = height;
+
+            osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector( start, end);
+            intersectorGroup->addIntersector( intersector.get() );
+        }
+    }
+    
+    _intersectionVisitor.reset();
+    _intersectionVisitor.setTraversalMask(traversalMask);
+    _intersectionVisitor.setIntersector( intersectorGroup.get() );
+    
+    scene->accept(_intersectionVisitor);
+    
+    unsigned int index = 0;
+    osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
+    for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
+        intersector_itr != intersectors.end();
+        ++intersector_itr, ++index)
+    {
+        osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get());
+        if (lsi)
+        {
+            osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections();
+            if (!intersections.empty())
+            {
+                const osgUtil::LineSegmentIntersector::Intersection& intersection = *intersections.begin();
+                osg::Vec3d intersectionPoint = intersection.matrix.valid() ? intersection.localIntersectionPoint * (*intersection.matrix) :
+                                               intersection.localIntersectionPoint;
+                // HAT
+				_HATList[index]._hat = (_HATList[index]._point - intersectionPoint).length();
+
+				// HOT
+				if (em)
+				{
+					double latitude, longitude, height;
+					em->convertXYZToLatLongHeight(intersectionPoint.x(), intersectionPoint.y(), intersectionPoint.z(), latitude, longitude, height);
+					_HATList[index]._hot = height;
+				}
+				else
+				{
+					_HATList[index]._hot = intersectionPoint.z();
+				}
+
+            }
+        }
+    }
+    
+}
+
+double HeightAboveTerrain::computeHeightAboveTerrain(osg::Node* scene, const osg::Vec3d& point, bool loadHighestLOD, osg::Node::NodeMask traversalMask)
+{
+    HeightAboveTerrain hat(loadHighestLOD);
+    unsigned int index = hat.addPoint(point);
+    hat.computeIntersections(scene, traversalMask);
+    return hat.getHeightAboveTerrain(index);
+}
+
+double HeightAboveTerrain::computeHeightOfTerrain(osg::Node* scene, const osg::Vec3d& point, bool loadHighestLOD, osg::Node::NodeMask traversalMask)
+{
+	HeightAboveTerrain hat(loadHighestLOD);
+    unsigned int index = hat.addPoint(point);
+    hat.computeIntersections(scene, traversalMask);
+    return hat.getHeightOfTerrain(index);
+}
+
+void HeightAboveTerrain::setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc)
+{
+    _dcrc = dcrc;
+    _intersectionVisitor.setReadCallback(dcrc);
+}
