From 4f4669cbe6a8d7ab4f3d2bff2a21e3a6134101f5 Mon Sep 17 00:00:00 2001
From: till busch <buti@bux.at>
Date: Mon, 31 Mar 2008 13:30:58 +0200
Subject: [PATCH] add an elevation-util v0.0.2

---
 configure.ac                  |    1 +
 src/Main/Makefile.am          |    2 +-
 utils/Makefile.am             |    4 +-
 utils/elevation/Makefile.am   |   81 ++++++++++++++++++
 utils/elevation/elevation.cxx |  189 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 274 insertions(+), 3 deletions(-)
 create mode 100644 utils/elevation/Makefile.am
 create mode 100644 utils/elevation/README
 create mode 100644 utils/elevation/elevation.cxx

diff --git a/configure.ac b/configure.ac
index 14514d8..fec8483 100644
--- a/configure.ac
+++ b/configure.ac
@@ -690,6 +690,7 @@ AC_CONFIG_FILES([ \
 	utils/GPSsmooth/Makefile \
 	utils/fgadmin/Makefile
 	utils/fgadmin/src/Makefile \
+	utils/elevation/Makefile \
 	utils/js_server/Makefile \
 	utils/Modeller/Makefile \
 	utils/TerraSync/Makefile \
diff --git a/src/Main/Makefile.am b/src/Main/Makefile.am
index 3991083..661c8c8 100644
--- a/src/Main/Makefile.am
+++ b/src/Main/Makefile.am
@@ -95,7 +95,7 @@ fgfs_LDADD = \
 	$(top_builddir)/src/Sound/libSound.a \
 	$(top_builddir)/src/Airports/libAirports.a \
 	$(MPLAYER_LIBS) \
-        $(top_builddir)/src/AIModel/libAIModel.a \
+	$(top_builddir)/src/AIModel/libAIModel.a \
 	$(top_builddir)/src/Systems/libSystems.a \
 	$(top_builddir)/src/Time/libTime.a \
 	$(top_builddir)/src/Traffic/libTraffic.a \
diff --git a/utils/Makefile.am b/utils/Makefile.am
index e73dcc1..ba9918a 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -1,4 +1,4 @@
-DIST_SUBDIRS = GPSsmooth TerraSync Modeller js_server fgadmin xmlgrep
+DIST_SUBDIRS = GPSsmooth TerraSync Modeller js_server fgadmin xmlgrep elevation
 
-SUBDIRS = GPSsmooth TerraSync Modeller js_server
+SUBDIRS = GPSsmooth TerraSync Modeller js_server elevation
 
diff --git a/utils/elevation/Makefile.am b/utils/elevation/Makefile.am
new file mode 100644
index 0000000..90ec958
--- /dev/null
+++ b/utils/elevation/Makefile.am
@@ -0,0 +1,81 @@
+EXTRA_DIST = README
+
+bin_PROGRAMS = elevation
+
+elevation_SOURCES = elevation.cxx
+
+#elevation_LDADD = -lplibnet -lplibul $(network_LIBS)
+
+MPLAYER_LIBS = $(top_builddir)/src/MultiPlayer/libMultiPlayer.a
+
+if ENABLE_SP_FDM
+SP_FDM_LIBS = $(top_builddir)/src/FDM/SP/libSPFDM.a
+else
+SP_FDM_LIBS = 
+endif
+
+if WITH_THREADS
+THREAD_LIBS = -lsgthreads $(thread_LIBS)
+else
+THREAD_LIBS =
+endif
+
+INCLUDES = -I$(top_srcdir)/src/Scenery/ -I$(top_srcdir)/src/ -I$(top_srcdir)/
+
+JSBSIM_LIBS = \
+	$(top_builddir)/src/FDM/JSBSim/libJSBSim.a \
+        $(top_builddir)/src/FDM/JSBSim/initialization/libInit.a \
+        $(top_builddir)/src/FDM/JSBSim/models/libModels.a \
+        $(top_builddir)/src/FDM/JSBSim/models/flight_control/libFlightControl.a\
+        $(top_builddir)/src/FDM/JSBSim/models/atmosphere/libAtmosphere.a \
+        $(top_builddir)/src/FDM/JSBSim/models/propulsion/libPropulsion.a \
+        $(top_builddir)/src/FDM/JSBSim/input_output/libInputOutput.a \
+        $(top_builddir)/src/FDM/JSBSim/math/libMath.a
+
+elevation_LDADD = \
+	$(top_builddir)/src/Main/libMain.a \
+	$(top_builddir)/src/Aircraft/libAircraft.a \
+	$(top_builddir)/src/ATC/libATC.a \
+	$(top_builddir)/src/Cockpit/libCockpit.a \
+	$(top_builddir)/src/Cockpit/built_in/libBuilt_in.a \
+	$(top_builddir)/src/FDM/libFlight.a \
+	$(top_builddir)/src/FDM/Balloon/libBalloon.a \
+	$(top_builddir)/src/FDM/ExternalNet/libExternalNet.a \
+	$(top_builddir)/src/FDM/ExternalPipe/libExternalPipe.a \
+	$(JSBSIM_LIBS) \
+	$(top_builddir)/src/FDM/YASim/libYASim.a \
+	$(top_builddir)/src/FDM/LaRCsim/libLaRCsim.a \
+	$(top_builddir)/src/FDM/UIUCModel/libUIUCModel.a \
+	$(SP_FDM_LIBS) \
+	$(top_builddir)/src/GUI/libGUI.a \
+	$(top_builddir)/src/Autopilot/libAutopilot.a \
+	$(top_builddir)/src/Input/libInput.a \
+	$(top_builddir)/src/Instrumentation/KLN89/libKLN89.a \
+	$(top_builddir)/src/Instrumentation/libInstrumentation.a \
+	$(top_builddir)/src/Instrumentation/HUD/libHUD.a \
+	$(top_builddir)/src/Model/libModel.a \
+	$(top_builddir)/src/Network/libNetwork.a \
+	$(top_builddir)/src/Navaids/libNavaids.a \
+	$(top_builddir)/src/Scenery/libScenery.a \
+	$(top_builddir)/src/Scripting/libScripting.a \
+	$(top_builddir)/src/Sound/libSound.a \
+	$(top_builddir)/src/Airports/libAirports.a \
+	$(MPLAYER_LIBS) \
+	$(top_builddir)/src/AIModel/libAIModel.a \
+	$(top_builddir)/src/Systems/libSystems.a \
+	$(top_builddir)/src/Time/libTime.a \
+	$(top_builddir)/src/Traffic/libTraffic.a \
+	$(top_builddir)/src/Environment/libEnvironment.a \
+	-lsgroute -lsgsky -lsgsound -lsgephem -lsgmaterial -lsgtgdb -lsgmodel \
+	-lsgutil -lsgtiming -lsgio -lsgscreen -lsgmath -lsgbucket -lsgprops \
+	-lsgdebug -lsgmagvar -lsgmisc -lsgnasal -lsgxml -lsgsound -lsgserial \
+	-lsgstructure -lsgenvironment \
+	-lplibpuaux -lplibpu -lplibfnt -lplibjs -lplibnet \
+	-lplibsg -lplibul \
+	$(OSG_LIBS) \
+	$(THREAD_LIBS) \
+	$(network_LIBS) \
+	-lz \
+	$(opengl_LIBS) \
+	$(openal_LIBS)
+
diff --git a/utils/elevation/README b/utils/elevation/README
new file mode 100644
index 0000000..e69de29
diff --git a/utils/elevation/elevation.cxx b/utils/elevation/elevation.cxx
new file mode 100644
index 0000000..e50eed3
--- /dev/null
+++ b/utils/elevation/elevation.cxx
@@ -0,0 +1,189 @@
+#include <config.h>
+#include <simgear/scene/util/SGSceneFeatures.hxx>
+#include <simgear/scene/material/matlib.hxx>
+#include <simgear/scene/model/modellib.hxx>
+#include <simgear/scene/material/mat.hxx>
+#include <simgear/scene/tgdb/userdata.hxx>
+#include <Main/globals.hxx>
+#include <Scenery/tilemgr.hxx>
+#include <Scenery/scenery.hxx>
+#include <AIModel/AIMultiplayer.hxx>
+#include <Main/renderer.hxx>
+
+extern FGGlobals *globals;
+
+int _bootstrap_OSInit=0;
+char *homedir = ::getenv( "HOME" );
+char *hostname = ::getenv( "HOSTNAME" );
+bool free_hostname = false;
+SGLocation loc;
+
+static void update( void ) {
+    static int state=0;
+    static int id=0;
+    static int tile=0;
+    static SGGeod g;
+    static double lon=0.0,lat=0.0;
+    static int cnt=0;
+    //cerr << "update state=" << state << endl;
+
+    if(!state)
+    {
+        state=1;
+        cin >> id >> ws >> lat >> ws >> lon >> ws >> tile;
+        if(cin.eof())
+            globals->get_renderer()->getViewer()->setDone(true);
+
+        loc.setPosition(lon, lat, SG_MAX_ELEVATION_M*SG_METER_TO_FEET);
+//        loc.setPosition(lon, lat, 200.0);
+        g=SGGeod::fromDegM(lon, lat, 0);
+        SGGeod g2(SGGeod::fromDegM(lon, lat, 5000));
+        SGVec3f pos = SGVec3f::fromGeod(g);
+        SGVec3f pos2 = SGVec3f::fromGeod(g2);
+
+
+
+        //cerr << "pos " << pos[0] << " " << pos[1] << " " << pos[2] << endl;
+        //osg::Matrix m= g.makeSimulationFrame();
+        //osg::Matrix m= g.makeZUpFrameRelative();
+        /*
+        double *p=loc.get_absolute_view_pos();
+        float *up=loc.get_world_up();
+        cerr << "up " << up[0] << " " << up[1] << " " << up[2] << endl;
+        cerr << " " << p[0] << " " << p[1] << " " << p[2] << endl;
+        //globals->get_renderer()->getManipulator()->setPosition(osg::Vec3d(p[0],p[1],p[2]));
+        */
+
+        osg::Vec3d eye, center, up;
+        double distance=1000;
+        float *south=loc.get_surface_south();
+        up=osg::Vec3(south[0],south[1],south[2])*-1.0;
+        up.normalize();
+
+        osg::Matrix mm;//=globals->get_renderer()->getManipulator()->getMatrix();
+        //mm.makeLookAt(pos2.osg(), pos.osg(), m.getRotate().asVec3());
+        center[0]=0.0001;
+        center[1]=0.0001;
+        center[2]=0.0001;
+        mm.makeLookAt(pos2.osg(), pos.osg(), up);
+/*
+        distance=(pos2.osg()-pos.osg()).length();
+        mm.getLookAt(eye, center, up, distance);
+        cerr << "eye " << eye[0] << " " << eye[1] << " " << eye[2] << " ";
+        cerr << "center " << center[0] << " " << center[1] << " " << center[2] << " ";
+        cerr << "up " << up[0] << " " << up[1] << " " << up[2] << " ";
+        cerr << "distance " << distance << endl;
+*/
+        globals->get_renderer()->getManipulator()->setByInverseMatrix(mm);
+        return;
+    }
+
+        globals->get_tile_mgr()->update(&loc, 1000.0);
+    if(state)
+    {
+        cnt++;
+
+//        if(globals->get_scenery()->scenery_available(lat, lon, 100.0))
+        if(globals->get_tile_mgr()->scenery_available(lat, lon, 100.0))
+        {
+            double alt=0;
+            const SGMaterial *mat;
+            globals->get_scenery()->get_elevation_m(lat, lon, 9000, alt, &mat);
+            //if(mat)
+            //    cout << lon << " " << lat << " " << alt << " " << mat->get_names().front() << endl;
+            //else
+                cout << id << " " << lat << " " << lon << " " << alt << " " << tile << endl;
+            cnt=0;
+            state=0;
+        }
+        else
+        {
+            if(cnt > 10000)
+            {
+                cerr << "no scenery for 10000 frames\n";
+                cerr << id << " " << lat << " " << lon << " " << tile << endl;
+                state=0;
+                cnt=0;
+                globals->get_renderer()->getViewer()->setDone(true);
+            }
+        }
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    logbuf::set_log_classes(SG_ALL);
+    logbuf::set_log_priority(SG_WARN);
+
+    globals = new FGGlobals;
+    globals->set_fg_root("/usr/local/share/FlightGear");
+    globals->set_fg_scenery("/usr/local/share/FlightGear/Scenery:/usr/local/share/FlightGear/Scenery-Terrasync");
+
+//    SGConfigureDirectionalLights( use_point_sprites, enhanced_lighting,
+//                                  distance_attenuation );
+
+    SGSceneFeatures::instance()->setEnablePointSpriteLights(false);
+    SGSceneFeatures::instance()->setEnableDistanceAttenuationLights(false);
+
+    sgUserDataInit( globals->get_props() );
+    globals->set_matlib( new SGMaterialLib );
+    fgSetDouble("/environment/visibility-m", 30000.0);
+    fgSetString("/sim/startup/season", "summer");
+    string season = fgGetString("/sim/startup/season", "summer");
+    fgSetBool("/sim/rendering/random-objects", false);
+    fgSetBool("/sim/rendering/random-vegetation", false);
+    SGPath mpath( globals->get_fg_root() );
+    mpath.append( "materials.xml" );
+    if(!globals->get_matlib()->load(globals->get_fg_root(), mpath.str(), season.c_str(), globals->get_props()))
+        SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
+
+    globals->set_scenery( new FGScenery );
+    simgear::SGModelLib::init(globals->get_fg_root());
+
+    globals->set_tile_mgr(new FGTileMgr);
+    globals->get_scenery()->init();
+    globals->get_scenery()->bind();
+    osgViewer::Viewer viewer;
+
+    osg::Matrix projection;
+    //projection.makePerspective(60.0f, 300.0f/768.0f, 0.1f, 1000.0f);
+    projection.makePerspective(30.0f, 1.0f, 100.0f, 300.0f);
+
+    viewer.getCamera()->setProjectionMatrix(projection);
+    viewer.getCamera()->setClearColor(osg::Vec4(0.5, 0.6, 1.0, 1.0));
+
+//    viewer.getCamera()->setCullingMode(osg::CullSettings::NO_CULLING);
+//    GLbitfield inheritanceMask = osg::CullSettings::ALL_VARIABLES;
+//    inheritanceMask &= ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE;
+//    inheritanceMask &= ~osg::CullSettings::CULLING_MODE;
+//    viewer.getCamera()->setInheritanceMask(inheritanceMask);
+//    viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
+    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
+
+
+
+    viewer.setDatabasePager(globals->get_scenery()->getPagerSingleton());
+    viewer.setSceneData(globals->get_scenery()->get_scene_graph());
+    viewer.setCameraManipulator(globals->get_renderer()->getManipulator());
+
+    globals->get_renderer()->setViewer(&viewer);
+    globals->get_renderer()->splashinit();
+
+    globals->get_tile_mgr()->init();
+    // lon, lat, alt_ft
+
+    fgRegisterIdleHandler(update);
+
+    viewer.setUpViewInWindow(0, 0, 400, 400);
+    viewer.run();
+
+    //delete globals->get_renderer();
+    delete globals->get_matlib();
+    delete globals->get_tile_mgr();
+    delete globals->get_scenery();
+
+//    delete globals;
+
+    return 0;
+}
+
-- 
1.5.2.5


