Changeset 8074

Show
Ignore:
Timestamp:
04/11/08 12:16:38
Author:
robert
Message:

From Bob Kuehne, "1) add simplifier command "--simplifier .5" to reduce complexity
2) complementarily add a "--overallNormal" to replace
per-vert/per-facet normals with an overall. simplifier doesn't work

in certain cases without less complex normals. this gets that done.
3) add env var output with full verbose output so people realize it's
active when the app is run - i see this all the time in training where
people run osgconv, with unintended data transformations due to
osgUtil:;Optimzer, for example"

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenSceneGraph/trunk/applications/osgconv/osgconv.cpp

    r7648 r8074  
    66#include <osg/Notify> 
    77#include <osg/Vec3> 
     8#include <osg/ProxyNode> 
    89#include <osg/Geometry> 
    910#include <osg/Texture2D> 
     
    1920 
    2021#include <osgUtil/Optimizer> 
     22#include <osgUtil/Simplifier> 
    2123#include <osgUtil/SmoothingVisitor> 
    2224 
     
    7375    private: 
    7476        osg::ref_ptr<osg::GraphicsContext> _gc; 
     77}; 
     78 
     79class DefaultNormalsGeometryVisitor 
     80    : public osg::NodeVisitor 
     81{ 
     82public: 
     83 
     84    DefaultNormalsGeometryVisitor() 
     85        : osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN ) { 
     86    } 
     87 
     88    virtual void apply( osg::Geode & geode ) 
     89    { 
     90        for( unsigned int ii = 0; ii < geode.getNumDrawables(); ++ii ) 
     91        { 
     92            osg::ref_ptr< osg::Geometry > geometry = dynamic_cast< osg::Geometry * >( geode.getDrawable( ii ) ); 
     93            if( geometry.valid() ) 
     94            { 
     95                osg::ref_ptr< osg::Vec3Array > newnormals = new osg::Vec3Array; 
     96                newnormals->push_back( osg::Z_AXIS ); 
     97                geometry->setNormalArray( newnormals.get() ); 
     98                geometry->setNormalBinding( osg::Geometry::BIND_OVERALL ); 
     99            } 
     100        } 
     101    } 
     102 
     103    virtual void apply( osg::Node & node ) 
     104    { 
     105        traverse( node ); 
     106    } 
     107 
    75108}; 
    76109 
     
    370403        osg::notify(osg::NOTICE) << msg << std::endl; 
    371404    } 
     405 
     406    // basic usage 
    372407    osg::notify(osg::NOTICE)<< std::endl; 
    373408    osg::notify(osg::NOTICE)<<"usage:"<< std::endl; 
    374409    osg::notify(osg::NOTICE)<<"    " << prog << " [options] infile1 [infile2 ...] outfile"<< std::endl; 
    375410    osg::notify(osg::NOTICE)<< std::endl; 
     411 
     412    // print env options - especially since optimizer is always _on_ 
     413    osg::notify(osg::NOTICE)<<"environment:" << std::endl; 
     414    osg::ApplicationUsage::UsageMap um = osg::ApplicationUsage::instance()->getEnvironmentalVariables(); 
     415    std::string envstring; 
     416    osg::ApplicationUsage::instance()->getFormattedString( envstring, um ); 
     417    osg::notify(osg::NOTICE)<<envstring << std::endl; 
     418 
     419    // print tool options 
    376420    osg::notify(osg::NOTICE)<<"options:"<< std::endl; 
    377421    osg::notify(osg::NOTICE)<<"    -O option          - ReaderWriter option"<< std::endl; 
     
    431475                              "                         absolute position in world space\n" 
    432476                              << std::endl; 
     477    osg::notify(osg::NOTICE)<<"    --simplify n       - Run simplifier on prior to output. Argument must be a" << std::endl 
     478                            <<"                         normalized value for the resultant percentage reduction." << std::endl 
     479                            <<"                         Example: --simplify .5 will produce an 50 reduced model." << std::endl 
     480                            << std::endl; 
    433481    osg::notify(osg::NOTICE)<<"    -s scale           - Scale size of model.  Scale argument must be the \n" 
    434482                              "                         following :\n" 
     
    443491    osg::notify(osg::NOTICE)<<"    --addMissingColors - Adding a white color value to all geometry that don't have\n" 
    444492                              "                         their own color values (--addMissingColours also accepted)."<< std::endl; 
     493    osg::notify(osg::NOTICE)<<"    --overallNormal    - Replace normals with a single overall normal."<< std::endl; 
    445494 
    446495} 
     
    548597    } 
    549598 
     599    float simplifyPercent = 1.0; 
     600    bool do_simplify = false; 
     601    while ( arguments.read( "--simplify",str ) ) 
     602    { 
     603        float nsimp = 1.0; 
     604        if( sscanf( str.c_str(), "%f", 
     605                &nsimp ) != 1 ) 
     606        { 
     607            usage( argv[0], "Scale argument format incorrect." ); 
     608            return 1; 
     609        } 
     610        std::cout << str << " " << nsimp << std::endl; 
     611        simplifyPercent = nsimp; 
     612        osg::notify( osg::INFO ) << "Simplifying with percentage: " << simplifyPercent << std::endl; 
     613        do_simplify = true; 
     614    } 
     615 
    550616    while (arguments.read("-t",str)) 
    551617    { 
     
    587653    while(arguments.read("--addMissingColours") || arguments.read("--addMissingColors")) { addMissingColours = true; } 
    588654 
     655    bool do_overallNormal = false; 
     656    while(arguments.read("--overallNormal") || arguments.read("--overallNormal")) { do_overallNormal = true; } 
     657 
    589658    // any option left unread are converted into errors to write out later. 
    590659    arguments.reportRemainingOptionsAsUnrecognized(); 
     
    672741            } 
    673742        } 
    674          
     743 
     744        // scrub normals 
     745        if ( do_overallNormal ) 
     746        { 
     747            DefaultNormalsGeometryVisitor dngv; 
     748            root->accept( dngv ); 
     749        } 
     750 
     751        // apply any user-specified simplification 
     752        if ( do_simplify ) 
     753        { 
     754            osgUtil::Simplifier simple; 
     755            simple.setSmoothing( smooth ); 
     756            osg::notify( osg::ALWAYS ) << " smoothing: " << smooth << std::endl; 
     757            simple.setSampleRatio( simplifyPercent ); 
     758            root->accept( simple ); 
     759        } 
     760 
    675761        osgDB::ReaderWriter::WriteResult result = osgDB::Registry::instance()->writeNode(*root,fileNameOut,osgDB::Registry::instance()->getOptions()); 
    676762        if (result.success())