From 853e371d1e02e7cd491fffca11bab5814a6ac1d9 Mon Sep 17 00:00:00 2001
From: timoore <timoore>
Date: Thu, 20 Dec 2007 23:20:51 +0000
Subject: [PATCH] Fix for "falling through ground" problem

The LOD far range on the tile entry scenegraph node was initialized to
0. This meant that any traverals of active children that happened
before the tile manager updated the node would ignore the node
altogether. Among these is the groundcache traversal which was failing
at startup even though scenery was loaded.

Also added a function to dump scene graph nodes to files; very handy
in gdb.
---
 src/FDM/groundcache.cxx   | 10 ++++++----
 src/Main/renderer.cxx     |  6 ++++++
 src/Scenery/tileentry.cxx | 13 +++++++++++++
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/FDM/groundcache.cxx b/src/FDM/groundcache.cxx
index dfec899cf..13ed6ea2b 100644
--- a/src/FDM/groundcache.cxx
+++ b/src/FDM/groundcache.cxx
@@ -249,8 +249,8 @@ public:
     FGGroundCache::GroundProperty oldGp = mGroundProperty;
     if (!enterNode(geode))
       return;
-
-    for(unsigned i = 0; i < geode.getNumDrawables(); ++i)
+    unsigned int numDrawables = geode.getNumDrawables();
+    for(unsigned i = 0; i < numDrawables; ++i)
       fillWith(geode.getDrawable(i));
     sphIsec = oldSphIsec;
     mGroundProperty = oldGp;
@@ -438,12 +438,14 @@ public:
 
 FGGroundCache::FGGroundCache() :
   ground_radius(0.0),
+  _type(0),
+  _material(0),
   cache_ref_time(0.0),
   wire_id(0),
   reference_wgs84_point(SGVec3d(0, 0, 0)),
   reference_vehicle_radius(0.0),
-  found_ground(false),
-  _material(0)
+  down(0.0, 0.0, 0.0),
+  found_ground(false)
 {
 }
 
diff --git a/src/Main/renderer.cxx b/src/Main/renderer.cxx
index e4f0d5230..2ac532f6e 100644
--- a/src/Main/renderer.cxx
+++ b/src/Main/renderer.cxx
@@ -1077,5 +1077,11 @@ fgDumpTerrainBranchToFile(const char* filename)
                                  filename );
 }
 
+// For debugging
+bool
+fgDumpNodeToFile(osg::Node* node, const char* filename)
+{
+    return osgDB::writeNodeFile(*node, filename);
+}
 // end of renderer.cxx
     
diff --git a/src/Scenery/tileentry.cxx b/src/Scenery/tileentry.cxx
index 71c1ce258..bbec0ee6a 100644
--- a/src/Scenery/tileentry.cxx
+++ b/src/Scenery/tileentry.cxx
@@ -151,6 +151,10 @@ FGTileEntry::FGTileEntry ( const SGBucket& b )
     _node->setCullCallback(new TileCullCallback);
     tileFileName += ".stg";
     _node->setName(tileFileName);
+    // Give a default LOD range so that traversals that traverse
+    // active children (like the groundcache lookup) will work before
+    // tile manager has had a chance to update this node.
+    _node->setRange(0, 0.0, 10000.0);
 }
 
 
@@ -569,6 +573,11 @@ bool ReaderWriterSTG::acceptsExtension(const string& extension) const
             || osgDB::equalCaseInsensitive(extension, "stg"));   
 }
 
+//#define SLOW_PAGER 1
+#ifdef SLOW_PAGER
+#include <unistd.h>
+#endif
+
 osgDB::ReaderWriter::ReadResult
 ReaderWriterSTG::readNode(const string& fileName,
                           const osgDB::ReaderWriter::Options* options) const
@@ -589,6 +598,10 @@ ReaderWriterSTG::readNode(const string& fileName,
     osg::Node* result
         = FGTileEntry::loadTileByName(osgDB::getNameLessExtension(stgFileName),
                                       globals->get_fg_scenery());
+    // For debugging race conditions
+#ifdef SLOW_PAGER
+    sleep(5);
+#endif
     if (result)
         return result;           // Constructor converts to ReadResult
     else