]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/renderer.cxx
Improve timing statistics
[flightgear.git] / src / Main / renderer.cxx
index 5e9dba5a4d389aeb642a6235f18509f789289ed5..d1fa367cfb09434aa53cd840046a527d3bd49b42 100644 (file)
 
 #include <simgear/compiler.h>
 
+#include <algorithm>
+#include <iostream>
+#include <map>
+#include <vector>
+#include <typeinfo>
+
 #include <osg/ref_ptr>
 #include <osg/AlphaFunc>
 #include <osg/BlendFunc>
 #include <osg/Camera>
 #include <osg/CullFace>
+#include <osg/CullStack>
 #include <osg/Depth>
 #include <osg/Fog>
 #include <osg/Group>
@@ -47,6 +54,7 @@
 #include <osg/Notify>
 #include <osg/PolygonMode>
 #include <osg/PolygonOffset>
+#include <osg/Program>
 #include <osg/Version>
 #include <osg/TexEnv>
 
@@ -56,7 +64,6 @@
 #include <osgDB/WriteFile>
 
 #include <simgear/math/SGMath.hxx>
-#include <simgear/screen/extensions.hxx>
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/scene/model/animation.hxx>
 #include <simgear/scene/model/placement.hxx>
 
 #include <Time/light.hxx>
 #include <Time/light.hxx>
-#include <Aircraft/aircraft.hxx>
 #include <Cockpit/panel.hxx>
-#include <Cockpit/cockpit.hxx>
-#include <Cockpit/hud.hxx>
 #include <Model/panelnode.hxx>
 #include <Model/modelmgr.hxx>
 #include <Model/acmodel.hxx>
 #include <Scenery/scenery.hxx>
 #include <Scenery/redout.hxx>
-#include <Scenery/tilemgr.hxx>
 #include <GUI/new_gui.hxx>
-#include <Instrumentation/instrument_mgr.hxx>
 #include <Instrumentation/HUD/HUD.hxx>
 #include <Environment/precipitation_mgr.hxx>
 
-#include <Include/general.hxx>
 #include "splash.hxx"
 #include "renderer.hxx"
 #include "main.hxx"
 #include "CameraGroup.hxx"
 #include "FGEventHandler.hxx"
 #include <Main/viewer.hxx>
+#include <Main/viewmgr.hxx>
 
 using namespace osg;
 using namespace simgear;
@@ -138,6 +140,7 @@ public:
   {
     // Dynamic stuff, do not store geometry
     setUseDisplayList(false);
+    setDataVariance(Object::DYNAMIC);
 
     osg::StateSet* stateSet = getOrCreateStateSet();
     stateSet->setRenderBinDetails(1001, "RenderBin");
@@ -183,6 +186,7 @@ public:
   {
     // Dynamic stuff, do not store geometry
     setUseDisplayList(false);
+    setDataVariance(Object::DYNAMIC);
 
     osg::StateSet* stateSet = getOrCreateStateSet();
     stateSet->setRenderBinDetails(1000, "RenderBin");
@@ -207,10 +211,7 @@ public:
     glPushAttrib(GL_ALL_ATTRIB_BITS);
     glPushClientAttrib(~0u);
 
-    fgCockpitUpdate(&state);
-
-    FGInstrumentMgr *instr = static_cast<FGInstrumentMgr*>(globals->get_subsystem("instrumentation"));
-    HUD *hud = static_cast<HUD*>(instr->get_subsystem("hud"));
+    HUD *hud = static_cast<HUD*>(globals->get_subsystem("hud"));
     hud->draw(state);
 
     // update the panel subsystem
@@ -552,22 +553,22 @@ FGRenderer::init( void )
     // as the rest of the scene. This may not be true in practice.
     mRealRoot->addChild(thesky->getCloudRoot());
     mRealRoot->addChild(FGCreateRedoutNode());
+    // Attach empty program to the scene root so that shader programs
+    // don't leak into state sets (effects) that shouldn't have one.
+    stateSet = mRealRoot->getOrCreateStateSet();
+    stateSet->setAttributeAndModes(new osg::Program, osg::StateAttribute::ON);
 }
 
 
 // Update all Visuals (redraws anything graphics related)
 void
 FGRenderer::update( bool refresh_camera_settings ) {
-    bool scenery_loaded = fgGetBool("sim/sceneryloaded")
+    bool scenery_loaded = fgGetBool("sim/sceneryloaded", false)
                           || fgGetBool("sim/sceneryloaded-override");
     osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
-    if ( idle_state < 1000 || !scenery_loaded ) {
-        fgSetDouble("/sim/startup/splash-alpha", 1.0);
-
-        // Keep resetting sim time while the sim is initializing
-        // the splash screen is now in the scenegraph
-        globals->set_sim_time_sec( 0.0 );
-        return;
+    if (!scenery_loaded) {
+      fgSetDouble("/sim/startup/splash-alpha", 1.0);
+      return;
     }
 
     // Fade out the splash screen over the first three seconds.
@@ -623,9 +624,8 @@ FGRenderer::update( bool refresh_camera_settings ) {
     double visibility_meters = fgGetDouble("/environment/visibility-m");
     thesky->set_visibility(visibility_meters);
 
-    thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
-                        ( global_multi_loop * fgGetInt("/sim/speed-up") )
-                        / (double)fgGetInt("/sim/model-hz") );
+    double altitude_m = fgGetDouble("/position/altitude-ft") * SG_FEET_TO_METER;
+    thesky->modify_vis( altitude_m, 0.0 /* time factor, now unused */);
 
     // update the sky dome
     if ( skyblend ) {
@@ -651,72 +651,28 @@ FGRenderer::update( bool refresh_camera_settings ) {
         }
 
         SGSkyState sstate;
-
-        SGVec3d viewPos = current__view->getViewPosition();
-        sstate.view_pos  = toVec3f(viewPos);
-        SGGeod geodViewPos = SGGeod::fromCart(viewPos);
-        SGGeod geodZeroViewPos = SGGeod::fromGeodM(geodViewPos, 0);
-        sstate.zero_elev = toVec3f(SGVec3d::fromGeod(geodZeroViewPos));
-        SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos);
-        sstate.view_up   = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
-        sstate.lon       = geodViewPos.getLongitudeRad();
-        sstate.lat       = geodViewPos.getLatitudeRad();
-        sstate.alt       = geodViewPos.getElevationM();
+        sstate.pos       = current__view->getViewPosition();
+        sstate.pos_geod  = current__view->getPosition();
+        sstate.ori       = current__view->getViewOrientation();
         sstate.spin      = l->get_sun_rotation();
         sstate.gst       = globals->get_time_params()->getGst();
         sstate.sun_dist  = 50000.0 * sun_horiz_eff;
         sstate.moon_dist = 40000.0 * moon_horiz_eff;
         sstate.sun_angle = l->get_sun_angle();
 
-
-        /*
-         SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
-         << l->sky_color()[0] << " "
-         << l->sky_color()[1] << " "
-         << l->sky_color()[2] << " "
-         << l->sky_color()[3] );
-        SG_LOG( SG_GENERAL, SG_BULK, "    fog = "
-         << l->fog_color()[0] << " "
-         << l->fog_color()[1] << " "
-         << l->fog_color()[2] << " "
-         << l->fog_color()[3] );
-        SG_LOG( SG_GENERAL, SG_BULK,
-                "    sun_angle = " << l->sun_angle
-         << "    moon_angle = " << l->moon_angle );
-        */
-
         SGSkyColor scolor;
-
         scolor.sky_color   = SGVec3f(l->sky_color().data());
         scolor.adj_sky_color = SGVec3f(l->adj_sky_color().data());
         scolor.fog_color   = SGVec3f(l->adj_fog_color().data());
         scolor.cloud_color = SGVec3f(l->cloud_color().data());
         scolor.sun_angle   = l->get_sun_angle();
         scolor.moon_angle  = l->get_moon_angle();
-
+  
+        double delta_time_sec = fgGetDouble("/sim/time/delta-sec");
         thesky->reposition( sstate, *globals->get_ephem(), delta_time_sec );
         thesky->repaint( scolor, *globals->get_ephem() );
 
-        /*
-        SG_LOG( SG_GENERAL, SG_BULK,
-                "thesky->reposition( view_pos = " << view_pos[0] << " "
-         << view_pos[1] << " " << view_pos[2] );
-        SG_LOG( SG_GENERAL, SG_BULK,
-                "    zero_elev = " << zero_elev[0] << " "
-         << zero_elev[1] << " " << zero_elev[2]
-         << " lon = " << cur_fdm_state->get_Longitude()
-         << " lat = " << cur_fdm_state->get_Latitude() );
-        SG_LOG( SG_GENERAL, SG_BULK,
-                "    sun_rot = " << l->get_sun_rotation
-         << " gst = " << SGTime::cur_time_params->getGst() );
-        SG_LOG( SG_GENERAL, SG_BULK,
-             "    sun ra = " << globals->get_ephem()->getSunRightAscension()
-          << " sun dec = " << globals->get_ephem()->getSunDeclination()
-          << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
-          << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
-        */
-
-        //OSGFIXME
+            //OSGFIXME
 //         shadows->setupShadows(
 //           current__view->getLongitude_deg(),
 //           current__view->getLatitude_deg(),
@@ -873,5 +829,207 @@ fgDumpNodeToFile(osg::Node* node, const char* filename)
 {
     return osgDB::writeNodeFile(*node, filename);
 }
+
+namespace flightgear
+{
+using namespace osg;
+
+class VisibleSceneInfoVistor : public NodeVisitor, CullStack
+{
+public:
+    VisibleSceneInfoVistor()
+        : NodeVisitor(CULL_VISITOR, TRAVERSE_ACTIVE_CHILDREN)
+    {
+        setCullingMode(CullSettings::SMALL_FEATURE_CULLING
+                       | CullSettings::VIEW_FRUSTUM_CULLING);
+        setComputeNearFarMode(CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
+    }
+
+    VisibleSceneInfoVistor(const VisibleSceneInfoVistor& rhs)
+    {
+    }
+
+    META_NodeVisitor("flightgear","VisibleSceneInfoVistor")
+
+    typedef std::map<const std::string,int> InfoMap;
+
+    void getNodeInfo(Node* node)
+    {
+        const char* typeName = typeid(*node).name();
+        classInfo[typeName]++;
+        const std::string& nodeName = node->getName();
+        if (!nodeName.empty())
+            nodeInfo[nodeName]++;
+    }
+
+    void dumpInfo()
+    {
+        using namespace std;
+        typedef vector<InfoMap::iterator> FreqVector;
+        cout << "class info:\n";
+        FreqVector classes;
+        for (InfoMap::iterator itr = classInfo.begin(), end = classInfo.end();
+             itr != end;
+             ++itr)
+            classes.push_back(itr);
+        sort(classes.begin(), classes.end(), freqComp);
+        for (FreqVector::iterator itr = classes.begin(), end = classes.end();
+             itr != end;
+             ++itr) {
+            cout << (*itr)->first << " " << (*itr)->second << "\n";
+        }
+        cout << "\nnode info:\n";
+        FreqVector nodes;
+        for (InfoMap::iterator itr = nodeInfo.begin(), end = nodeInfo.end();
+             itr != end;
+             ++itr)
+            nodes.push_back(itr);
+
+        sort (nodes.begin(), nodes.end(), freqComp);
+        for (FreqVector::iterator itr = nodes.begin(), end = nodes.end();
+             itr != end;
+             ++itr) {
+            cout << (*itr)->first << " " << (*itr)->second << "\n";
+        }
+        cout << endl;
+    }
+    
+    void doTraversal(Camera* camera, Node* root, Viewport* viewport)
+    {
+        ref_ptr<RefMatrix> projection
+            = createOrReuseMatrix(camera->getProjectionMatrix());
+        ref_ptr<RefMatrix> mv = createOrReuseMatrix(camera->getViewMatrix());
+        if (!viewport)
+            viewport = camera->getViewport();
+        if (viewport)
+            pushViewport(viewport);
+        pushProjectionMatrix(projection.get());
+        pushModelViewMatrix(mv.get(), Transform::ABSOLUTE_RF);
+        root->accept(*this);
+        popModelViewMatrix();
+        popProjectionMatrix();
+        if (viewport)
+            popViewport();
+        dumpInfo();
+    }
+
+    void apply(Node& node)
+    {
+        if (isCulled(node))
+            return;
+        pushCurrentMask();
+        getNodeInfo(&node);
+        traverse(node);
+        popCurrentMask();
+    }
+    void apply(Group& node)
+    {
+        if (isCulled(node))
+            return;
+        pushCurrentMask();
+        getNodeInfo(&node);
+        traverse(node);
+        popCurrentMask();
+    }
+
+    void apply(Transform& node)
+    {
+        if (isCulled(node))
+            return;
+        pushCurrentMask();
+        ref_ptr<RefMatrix> matrix = createOrReuseMatrix(*getModelViewMatrix());
+        node.computeLocalToWorldMatrix(*matrix,this);
+        pushModelViewMatrix(matrix.get(), node.getReferenceFrame());
+        getNodeInfo(&node);
+        traverse(node);
+        popModelViewMatrix();
+        popCurrentMask();
+    }
+
+    void apply(Camera& camera)
+    {
+        // Save current cull settings
+        CullSettings saved_cull_settings(*this);
+
+        // set cull settings from this Camera
+        setCullSettings(camera);
+        // inherit the settings from above
+        inheritCullSettings(saved_cull_settings, camera.getInheritanceMask());
+
+        // set the cull mask.
+        unsigned int savedTraversalMask = getTraversalMask();
+        bool mustSetCullMask = (camera.getInheritanceMask()
+                                & osg::CullSettings::CULL_MASK) == 0;
+        if (mustSetCullMask)
+            setTraversalMask(camera.getCullMask());
+
+        osg::RefMatrix* projection = 0;
+        osg::RefMatrix* modelview = 0;
+
+        if (camera.getReferenceFrame()==osg::Transform::RELATIVE_RF) {
+            if (camera.getTransformOrder()==osg::Camera::POST_MULTIPLY) {
+                projection = createOrReuseMatrix(*getProjectionMatrix()
+                                                 *camera.getProjectionMatrix());
+                modelview = createOrReuseMatrix(*getModelViewMatrix()
+                                                * camera.getViewMatrix());
+            }
+            else {              // pre multiply 
+                projection = createOrReuseMatrix(camera.getProjectionMatrix()
+                                                 * (*getProjectionMatrix()));
+                modelview = createOrReuseMatrix(camera.getViewMatrix()
+                                                * (*getModelViewMatrix()));
+            }
+        } else {
+            // an absolute reference frame
+            projection = createOrReuseMatrix(camera.getProjectionMatrix());
+            modelview = createOrReuseMatrix(camera.getViewMatrix());
+        }
+        if (camera.getViewport())
+            pushViewport(camera.getViewport());
+
+        pushProjectionMatrix(projection);
+        pushModelViewMatrix(modelview, camera.getReferenceFrame());    
+
+        traverse(camera);
+    
+        // restore the previous model view matrix.
+        popModelViewMatrix();
+
+        // restore the previous model view matrix.
+        popProjectionMatrix();
+
+        if (camera.getViewport()) popViewport();
+
+        // restore the previous traversal mask settings
+        if (mustSetCullMask)
+            setTraversalMask(savedTraversalMask);
+
+        // restore the previous cull settings
+        setCullSettings(saved_cull_settings);
+    }
+
+protected:
+    // sort in reverse
+    static bool freqComp(const InfoMap::iterator& lhs, const InfoMap::iterator& rhs)
+    {
+        return lhs->second > rhs->second;
+    }
+    InfoMap classInfo;
+    InfoMap nodeInfo;
+};
+
+bool printVisibleSceneInfo(FGRenderer* renderer)
+{
+    osgViewer::Viewer* viewer = renderer->getViewer();
+    VisibleSceneInfoVistor vsv;
+    Viewport* vp = 0;
+    if (!viewer->getCamera()->getViewport() && viewer->getNumSlaves() > 0) {
+        const View::Slave& slave = viewer->getSlave(0);
+        vp = slave._camera->getViewport();
+    }
+    vsv.doTraversal(viewer->getCamera(), viewer->getSceneData(), vp);
+    return true;
+}
+}
 // end of renderer.cxx