]> git.mxchange.org Git - flightgear.git/commitdiff
Reduce AI/MP lags when removing models
authorThorstenB <brehmt@gmail.com>
Tue, 11 Oct 2011 19:43:24 +0000 (21:43 +0200)
committerThorstenB <brehmt@gmail.com>
Tue, 11 Oct 2011 19:43:24 +0000 (21:43 +0200)
Move load of removing OSG objects to the OSG pager thread

src/AIModel/AIBase.cxx
src/AIModel/AIBase.hxx

index f967995f611eda0d9635d3ad353eb9dab973cf73..b405d623ab9751f11659a919d4235203d4645242 100644 (file)
@@ -129,9 +129,7 @@ FGAIBase::FGAIBase(object_type ot, bool enableHot) :
 
 FGAIBase::~FGAIBase() {
     // Unregister that one at the scenery manager
-    if (globals->get_scenery()) {
-        globals->get_scenery()->get_scene_graph()->removeChild(aip.getSceneGraph());
-    }
+    removeModel();
 
     if (props) {
         SGPropertyNode* parent = props->getParent();
@@ -143,6 +141,29 @@ FGAIBase::~FGAIBase() {
     fp = 0;
 }
 
+/** Cleanly remove the model
+ * and let the scenery database pager do the clean-up work.
+ */
+void
+FGAIBase::removeModel()
+{
+    FGScenery* pSceneryManager = globals->get_scenery();
+    if (pSceneryManager)
+    {
+        osg::ref_ptr<osg::Object> temp = _model.get();
+        pSceneryManager->get_scene_graph()->removeChild(aip.getSceneGraph());
+        // withdraw from SGModelPlacement and drop own reference (unref)
+        aip.init( 0 );
+        _model = 0;
+        // pass it on to the pager, to be be deleted in the pager thread
+        pSceneryManager->getPagerSingleton()->queueDeleteRequest(temp);
+    }
+    else
+    {
+        SG_LOG(SG_AI, SG_ALERT, "AIBase: Could not unload model. Missing scenery manager!");
+    }
+}
+
 void FGAIBase::readFromScenario(SGPropertyNode* scFileNode)
 {
     if (!scFileNode)
@@ -248,6 +269,12 @@ bool FGAIBase::init(bool search_in_AI_path) {
 
     osg::Node * mdl = SGModelLib::loadPagedModel(f, props, new FGNasalModelData(props));
 
+    if (_model.valid())
+    {
+        // reinit, dump the old model
+        removeModel();
+    }
+
     _model = new osg::LOD;
     _model->setName("AI-model range animation node");
 
index 298d67cf0f0185709caf4653893dfffb02d03931..ed20dd869b6a9d65269ae87fbb171996e30983c7 100644 (file)
@@ -216,6 +216,8 @@ protected:
     void CalculateMach();
     double UpdateRadar(FGAIManager* manager);
 
+    void removeModel();
+
     static int _newAIModelID();
 
 private: