Hi I was following some tutorials from the Quick Start Guide when I found some 
pretty strange behavior (I assume its a BUG) when saving .osg files.
Below is a code snipped which
1) Creates a Group, puts a osg::Material on it and sets its flag to OVERRIDE. I 
set the material diffuse to red.
2) The 'axes.osg" 3d file is loaded and attached a to 2 MatrixTransforms
3) Each of the 2 MatrixTransform receives also a osg::Material. One gets a 
black diffuse color and the other is specified as setColorMode(AMBIENT_DIFFUSE).

So the correct behavior would be that the OVERRIDE in the Group makes all of 
the children RED.
And this is what happens as soon as you run the example.
BUT.
I also storre the whole database using writeNodeFile("Lighting.osg") as an .osg 
file.
Unfortunately when viewing the osg file "osgviewer Lighting.osg" the result are 
not the same. Apparently the OVERRIDE flag on the material attribute is not 
saved
and the children are not red anymore, but maintain their own materials.
Strangely enough if you save the file to .ive instead of .osg everything works 
fine.
I think doeas this also when setting other Attributes. It seams the .osg file 
format can not save correctly the whole database state.

Below is the code
Dimi

 
// Viewer Example, A minimal OSG viewer 
 #include <osgDB/WriteFile> 
#include <osg/Notify> 
#include <osgViewer/Viewer> 
#include <osgDB/ReadFile> 
#include <osg/MatrixTransform> 
#include <osg/Geode> 
#include <osg/Geometry> 
#include <osg/StateSet> 
#include <osg/StateAttribute> 
#include <osg/CullFace> 
#include <osg/Point> 
#include <osg/Light> 
#include <osg/LightSource> 
#include <osg/Material> 
#include <osg/PolygonMode> 
#include <osg/Notify> 
 
int 
main( int, char ** ) 
{ 
    // Create a Viewer. 
    osgViewer::Viewer viewer; 
 
    // ROOT NODE CREATE A RED MATERIAL
    osg::ref_ptr<osg::Group> root = new osg::Group; 
    { 
        osg::StateSet* state2 = root->getOrCreateStateSet(); 
        
        osg::ref_ptr<osg::Material> mat2 = new osg::Material; 
        mat2->setDiffuse( osg::Material::FRONT, 
            osg::Vec4( 1.f, 0.f, 0.f, 1.f ) ); 
        mat2->setSpecular( osg::Material::FRONT, 
            osg::Vec4( 1.f, 0.f, 1.f, 1.f ) ); 
        mat2->setShininess( osg::Material::FRONT, 128.f ); 
        state2->setAttributeAndModes( mat2.get() , osg::StateAttribute::ON | 
osg::StateAttribute::OVERRIDE);
    }

    //CREATE 2 CHILDREN AND APPLY LOCAL MATERIALS
    osg::ref_ptr<osg::Node> lozenge = osgDB::readNodeFile( "axes.osg" ); 
    if (!lozenge.valid()) 
    { 
        osg::notify( osg::FATAL ) << "Unable to load data file. Exiting." << 
std::endl; 
        return 0; 
    } 
    { 
        osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform; 
        osg::Matrix m; 
        m.makeTranslate( osg::Vec3( -4.f, -1.f, 1.f ) ); 
        mt->setMatrix( m ); 
 
        osg::StateSet* state = mt->getOrCreateStateSet(); 
        osg::ref_ptr<osg::Material> mat = new osg::Material; 
        mat->setDiffuse( osg::Material::FRONT, 
            osg::Vec4( 0.f, 0.f, 0.f, 1.f ) ); 
        mat->setSpecular( osg::Material::FRONT, 
            osg::Vec4( 1.f, 1.f, 1.f, 1.f ) ); 
        mat->setShininess( osg::Material::FRONT, 128.f ); 
        state->setAttribute( mat.get());

        mt->addChild( lozenge.get() ); 
        root->addChild( mt.get() ); 
    } 
 
    { 
        osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform; 
        osg::Matrix m; 
        m.makeTranslate( osg::Vec3( 4.f, -1.f, 1.f ) ); 
        mt->setMatrix( m ); 
 
        osg::StateSet* state = mt->getOrCreateStateSet(); 
        osg::ref_ptr<osg::Material> mat = new osg::Material; 
        // Just use the object's primary color for ambient and 
        //   diffuse (uses the OpenGL color material feature). 
        mat->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE ); 
        state->setAttribute( mat.get() ); 
 
        mt->addChild( lozenge.get() ); 
        root->addChild( mt.get() ); 
    }

    viewer.setSceneData (root.get());
  
    // SAVE THE SCENE FILE to an external file
    std::string out( "Lighting.osg" ); 
    if ( !(osgDB::writeNodeFile( *(root.get()), out )) ) 
    { 
        osg::notify(osg::FATAL) << "Failed in osgDB::writeNodeFile()." << 
std::endl; 
        return 1; 
    } 
    osg::notify(osg::ALWAYS) << "Successfully wrote \"" << out << "\". Execute 
\"osgviewer " << out << "\" to view." << std::endl;
 
    // Display, and main loop. 
    return viewer.run(); 
} 


      
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to