Hi,
I am following the Callback osg Tutorial ( Basic Tutorial on Tutorials
page ). I exchanged "main" to write an osg file, instead of creating a
viewer, but no "UpdateCallbacks" get written into my file.
How can I get "UpdateCallbacks" written into an osg file ?
Will such a file be properly displayed with the standard osgviewer ?
If not, how do I get Callbacks to work in a viewer written in an osg file ?
Two reference files are pasted bellow.
First my c++ code, I have commented out the original "main", at the end
and added a writeFile main.
Second, a reference osg ( Animation ) file exported from Blender with
"UpdateCallbacks", works properly only with the osganimationviewer.
Thanks.
Cheers, searching for the Pivot of my Soul, Peter Particle !
C++ code:
// Callback Example, Using an update callback to modify the scene graph
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
#include <osg/Camera>
#include <osg/NodeCallback>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osg/Notify>
#include <osg/Geode>
#include <osg/Geometry>
// Create a Quad Plane
osg::ref_ptr< osg::Node > createSceneGraph()
{
osg::ref_ptr< osg::Group > grpRoot = new osg::Group() ;
osg::ref_ptr< osg::Geode > geode = new osg::Geode() ;
grpRoot -> addChild( geode.get() ) ;
osg::ref_ptr< osg::Geometry > geom = new osg::Geometry() ;
geode -> addDrawable( geom.get() ) ;
osg::ref_ptr< osg::Vec3Array > vrts = new osg::Vec3Array() ;
geom -> setVertexArray( vrts.get() ) ;
vrts -> push_back( osg::Vec3( -1.f , -1.f , 0.f ) ) ;
vrts -> push_back( osg::Vec3( 1.f , -1.f , 0.f ) ) ;
vrts -> push_back( osg::Vec3( 1.f , 1.f , 0.f ) ) ;
vrts -> push_back( osg::Vec3( -1.f , 1.f , 0.f ) ) ;
osg::ref_ptr< osg::Vec4Array > cols = new osg::Vec4Array() ;
geom -> setColorArray( cols.get() ) ;
geom -> setColorBinding( osg::Geometry::BIND_PER_VERTEX ) ;
cols -> push_back( osg::Vec4( 1.f , 0.f , 0.f , 0.f ) ) ;
cols -> push_back( osg::Vec4( 1.f , 1.f , 0.f , 1.f ) ) ;
cols -> push_back( osg::Vec4( 0.f , 1.f , 0.f , 0.f ) ) ;
cols -> push_back( osg::Vec4( 0.f , 0.f , 1.f , 0.f ) ) ;
osg::ref_ptr< osg::Vec3Array > nrms = new osg::Vec3Array() ;
geom -> setNormalArray( nrms.get() ) ;
geom -> setNormalBinding( osg::Geometry::BIND_OVERALL ) ;
nrms -> push_back( osg::Vec3( 0.f , 0.f , -1.f ) ) ;
geom -> addPrimitiveSet( new osg::DrawArrays(
osg::PrimitiveSet::QUADS , 0 , 4 ) ) ;
osg::StateSet * state = geom -> getOrCreateStateSet() ;
state -> setMode( GL_LIGHTING , osg::StateAttribute::OFF ) ;
return grpRoot.get() ;
}
class RotateCallback : public osg::NodeCallback
{
public :
RotateCallback() : _angle( 0.0 ) {}
virtual void operator()( osg::Node * node , osg::NodeVisitor *
nodeVisitor )
{
osg::MatrixTransform * transformLeft = dynamic_cast<
osg::MatrixTransform * >( node ) ;
osg::Matrix matrixRotate , matrixTranslate ;
matrixTranslate.makeTranslate( -2.0 , 0.0 , 0.0 ) ;
matrixRotate.makeRotate( _angle, osg::Vec3( 0.0 , 0.0 , 1.0 ) ) ;
transformLeft -> setMatrix( matrixRotate * matrixTranslate ) ;
_angle += 0.01 ;
traverse( node , nodeVisitor ) ;
}
protected :
double _angle ;
} ;
osg::Node * createScene()
{
osg::ref_ptr< osg::Node > plane = createSceneGraph() ;
plane -> setDataVariance( osg::Object::STATIC ) ;
osg::ref_ptr< osg::MatrixTransform > transformLeft = new
osg::MatrixTransform ;
transformLeft -> setName( "Left Plane DYNAMIC" ) ;
transformLeft -> setDataVariance( osg::Object::DYNAMIC ) ;
transformLeft -> setUpdateCallback( new RotateCallback ) ;
osg::Matrix matrixTranslate ;
matrixTranslate.makeTranslate( -2.0f , 0.0f , 0.0f ) ;
transformLeft -> setMatrix( matrixTranslate ) ;
transformLeft -> addChild( plane.get() ) ;
osg::ref_ptr< osg::MatrixTransform > transformRght = new
osg::MatrixTransform ;
transformRght -> setName( "Right Plane STATIC" ) ;
transformRght -> setDataVariance( osg::Object::STATIC ) ;
matrixTranslate.makeTranslate( 2.0f , 0.0f , 0.0f ) ;
transformRght -> setMatrix( matrixTranslate ) ;
transformRght -> addChild( plane.get() ) ;
osg::ref_ptr< osg::Group > grpRoot = new osg::Group ;
grpRoot -> setName( "Root Node" ) ;
grpRoot -> setInitialBound( osg::BoundingSphere( osg::Vec3( 0 , 0 , 6
) , 4 ) ) ;
grpRoot -> setDataVariance( osg::Object::STATIC ) ;
grpRoot -> addChild( transformLeft.get() ) ;
grpRoot -> addChild( transformRght.get() ) ;
return( grpRoot.release() ) ;
}
/*
int main( int, char ** )
{
osgViewer::Viewer viewer ;
viewer.setSceneData( createScene() ) ;
if ( !viewer.getSceneData())
return( 1 ) ;
viewer.getCamera() -> setClearColor( osg::Vec4( 0.0 , 0.0 , 0.0 , 1.0
) ) ;
return( viewer.run() ) ;
}
*/
#include <osgDB/WriteFile>
using std::endl ;
int main( int , char ** )
{
osg::ref_ptr< osg::Node > root = createScene() ;
if ( !root.valid() )
osg::notify( osg::FATAL ) << "Failed in createSceneGraph() !" <<
endl ;
bool result = osgDB::writeNodeFile( * ( root.get() ) ,
"Callback.osg" ) ;
if ( !result )
osg::notify( osg::FATAL ) << "Failed in osgDB::writeNodeFile() !"
<< endl ;
}
Blender osgAnimation code:
Group {
UniqueID uniqid_Group_0
name "grpRoot"
cullingActive TRUE
UpdateCallbacks {
osgAnimation::BasicAnimationManager {
UniqueID uniqid_BasicAnimationManager_0
num_animations 1
osgAnimation::Animation {
UniqueID uniqid_Animation_0
name "animPlane"
num_channels 3
Channel {
name "position"
target "Plane"
Keyframes "Vec3" 5 {
key 0.0000 1.0000 0.0000 1.0000
key 1.0000 1.0000 0.0000 -1.0000
key 2.0000 -1.0000 0.0000 -1.0000
key 3.0000 -1.0000 0.0000 1.0000
key 4.0000 1.0000 0.0000 1.0000
}
}
Channel {
name "euler"
target "Plane"
Keyframes "Vec3" 5 {
key 0.0000 0.0000 0.0000 0.0000
key 1.0000 3.1416 0.0000 0.0000
key 2.0000 3.1416 0.0000 3.1416
key 3.0000 6.2832 0.0000 3.1416
key 4.0000 6.2832 0.0000 6.2832
}
}
Channel {
name "scale"
target "Plane"
Keyframes "Vec3" 1 {
key 0.0000 0.5000 0.5000 0.5000
}
}
}
}
}
num_children 1
MatrixTransform {
UniqueID uniqid_MatrixTransform_0
name "Plane"
cullingActive TRUE
UpdateCallbacks {
osgAnimation::UpdateTransform {
UniqueID uniqid_UpdateTransform_0
name "Plane"
}
}
Matrix {
1.0000 0.0000 0.0000 0.0000
0.0000 1.0000 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000
}
num_children 1
Geode {
UniqueID uniqid_Geode_0
name "GeodePlane"
cullingActive TRUE
num_drawables 1
Geometry {
StateSet {
rendering_hint DEFAULT_BIN
renderBinMode INHERIT
GL_LIGHTING OFF
}
useDisplayList TRUE
useVertexBufferObjects FALSE
PrimitiveSets 1
{
DrawArrays QUADS 0 4
}
VertexArray Vec3Array 4
{
-1 0 -1
1 0 -1
1 0 1
-1 0 1
}
NormalBinding OVERALL
NormalArray Vec3Array 1
{
0 -1 0
}
ColorBinding PER_VERTEX
ColorArray Vec4Array 4
{
1 0 0 0
1 1 0 1
0 1 0 0
0 0 1 0
}
}
}
}
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org