Hi Cedric,

For some reason Outlook webmail wrapped the cpp file into a binary format.

I have included a new version of the demo file with the timings fixed. It uses: const osg::FrameStamp* fs = nv->getFrameStamp(); and fs->getSimulationTime(); to calculate the current time. (Just like osgAmimation)

Kind Regards,
- Laurence


Cedric Pinson wrote:
<div class="moz-text-flowed" style="font-family: -moz-fixed">Hi Laurence,

I dont know what the winmail.dat in attachement is, i am not on windows. I will try tomorrow your main.cpp in previous mail and with the svn to be sure osgAnimation works fine with the svn.

About time in callback you can read the code of some updatecallback in osgAmimation. It will give you an example how to manage time.

Cheers,
Cedric

Muller, L.Y.L. wrote:
Hi Cedric,

hmm for some reason the post of Guy wasn't added to the right thread in the maillist ( http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/2009-March/025337.html ) I tried out his suggestion and got something like a self-deleting callback working (Demo file is attached). I just need to figure out how to measure the time between the callback calls. For now it just increments the 'time' value with a fixed number. It would ofcourse be nicer if I could use an exciting class that handles the scheduling/timeline.


About your request, I do not know how to create a stacktrace here with Visual Studio 2005, but that would require the debug libraries of OSG right? I could try stepping through the code with the debugger later this week and see where it locks up/crashes. I also noticed that there are some other issues with the (animation?) demos. After building the OSG 2.9.1 package (svn), I tried out a few of the animation demos. Running the demos is not a problem, but if you try to close them, it usually result in a crash. I'm not sure if its the osgAnimation part thats causing it or the viewer.

Kind regards,
- Laurence
P.S.: Sorry about the previous reply, I made a mistake while attaching the file... ;\


-----Original Message-----
From: Cedric Pinson [mailto:[email protected]]
Sent: Mon 3/23/2009 4:27 PM
To: OpenSceneGraph Users
Subject: Re: [osg-users] Animating vertices in a Geode
Hi Laurence, Before i dig into can you give the stacktrace to have an idea where it crashes ?

Cheers,
Cedric

Laurence Muller wrote:
Hi Cedric,

I tried to create a simple project based on the 'osganimationmorph' demo as Robert Osfield suggested.

Instead of loading the geometries from a *.osg file, I define two new geometries (both quads but on a different position). These geometries are added as a drawable to the MorphGeometry. However, for some reason OSG doesn't allows me to swap the old geom0 and geom1 geometry objects with the ones I create. The application crashes when you try to run it.

Any suggestions?

Kind regards,
- Laurence


Cedric Pinson wrote:
<div class="moz-text-flowed" style="font-family: -moz-fixed">Hi Laurence,

You can easily do it with morphgeometry:
- define two geometry with morphgeometry
- use an custom update callback that use an ease motion to control the ratio of geometry to use

Would be great to make an example with it, it will help others users.

Cheers,
Cedric

Laurence Muller wrote:
Hi,

In the application I am working on, I am trying to create a smooth (transition) animation on the vertices inside an object.

Example:
I have included an illustration to make my problem a bit more clear. The current scene contains one geode with a geometry attached. The geometry is filled with 7 quads (4 vertices per quad) and is illustrated as red dots in the picture. (The 'edges' are stored in a separate geode)

The application allows users to select a specific layout (radial, force based, etc) for the quads. In the current implementation this change happens immediately. (It updates the vertex array with new values and uses the dirty() call on the vertex array).

On the OSG wiki page I found some information about animating objects in the scene graph. http://www.openscenegraph.org/projects/osg/wiki/Community/NodeKits/osgAnimation
http://www.robertpenner.com/easing/easing_demo.html

However, it seems like this will only work on geode level (by modifying the Matrix or PositionAttitudeTransformation). Another problem with this method is that it seems that you need to create a predefined animation.

In my case the new position of a vertex will depend on the current position (and the animation only needs to be used once).

Question:
- Is there a way to use the osgAnimation functions on the vertex array and is it possible to use it with the transition methods from the EaseMotion demo?

Kind regards,
- Laurence

------------------------------------------
Laurence Muller
Website/Blog/Portfolio:
http://www.multigesture.net/


------------------------------------------------------------------------

------------------------------------------------------------------------

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

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

------------------------------------------------------------------------

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
/*
        Name: Laurence Muller
        Date: 24 - 03 - 2009
        Description: Animation example using osgAnimation/EaseMotion and a 
custom (self-deleting) callback
*/

#include <iostream>

#include <osg/Geode>
#include <osg/Geometry>
#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgAnimation/EaseMotion>

// Create a custom (geode) callback
class AnimationUpdatePostionCallback : public osg::NodeCallback
{
        public: 
                AnimationUpdatePostionCallback(osg::Geode* parent, float 
animation_time, osg::Vec3 pos);                
                ~AnimationUpdatePostionCallback() { printf("Destructor 
called\n"); }
                virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
                
                // Use EaseMotion animation!
                //osgAnimation::OutQuadFunction anim;   
                osgAnimation::OutBounceFunction anim;

                float start_time;       // Start time
                float cur_time;         // Current time (t-range is from 0 to 1)
                float res_time;         // "The time" calculated using the 
EaseMotion functions

        private:
                osg::Geode* parent;
                osg::Geometry* geom;

                osg::Vec3 new_pos;
                osg::ref_ptr<osg::Vec3Array> old_pos;
                osg::Vec3Array* vert_curpos;
                
                float animation_time;   // Total duration of the animation
};

// Parent Geode, Time to complete (in seconds), new (relative) position
AnimationUpdatePostionCallback::AnimationUpdatePostionCallback(osg::Geode* 
parent, float animation_time, osg::Vec3 pos)
{
        this->parent = parent;
        this->animation_time = animation_time;
        new_pos = pos;
        start_time = 0;
        cur_time = 0;
        res_time = 0;
        old_pos = new osg::Vec3Array;
        
        // Find the geometry (first drawable in this example)
        geom = dynamic_cast<osg::Geometry*>(parent->getDrawable(0));
        if( geom != NULL )
        {
                // Get pointer to the vertex list
                vert_curpos = 
dynamic_cast<osg::Vec3Array*>(geom->getVertexArray());            
                if( vert_curpos == NULL )
                {
                        printf("[Error] Could not find vertex array\n");
                        parent->removeUpdateCallback(this);
                        return;
                }                       
                else
                {                       
                        // Store current position of the quad
                        old_pos->push_back( vert_curpos->at(0) );
                        old_pos->push_back( vert_curpos->at(1) );
                        old_pos->push_back( vert_curpos->at(2) );
                        old_pos->push_back( vert_curpos->at(3) );
                }
        }
        else
        {
                printf("[Error] Could not find drawable\n");
                parent->removeUpdateCallback(this);
                return;
        }
}

void AnimationUpdatePostionCallback::operator()(osg::Node* node, 
osg::NodeVisitor* nv)
{
        const osg::FrameStamp* fs = nv->getFrameStamp();

// Get the current time
        if(start_time == 0)
                start_time = fs->getSimulationTime();   
        else    
                cur_time = (fs->getSimulationTime() - start_time) / 
animation_time;

// Calculate the 'result time'
        if(cur_time >= 1.0)     
                res_time = 1.0; // Animation is done... move the vertex to its 
final destination        
        else
                anim.getValueAt(cur_time, res_time);    // Animation is still 
running... set the vertex position (EaseMotion)
        
// Set new position
        vert_curpos->at(0) = old_pos->at(0) + osg::Vec3( new_pos.x() * 
res_time, new_pos.y() * res_time, new_pos.z() * res_time );
        vert_curpos->at(1) = old_pos->at(1) + osg::Vec3( new_pos.x() * 
res_time, new_pos.y() * res_time, new_pos.z() * res_time );
        vert_curpos->at(2) = old_pos->at(2) + osg::Vec3( new_pos.x() * 
res_time, new_pos.y() * res_time, new_pos.z() * res_time );
        vert_curpos->at(3) = old_pos->at(3) + osg::Vec3( new_pos.x() * 
res_time, new_pos.y() * res_time, new_pos.z() * res_time );              

// Inform geode / geometry of changes
        geom->dirtyDisplayList();
        geom->dirtyBound();
        //parent->dirtyBound();

// Animation is done, remove callback
        if(res_time == 1.0)
        {               
                printf("I'm done, bye!\nAnimation time: %f seconds\n", 
(fs->getSimulationTime() - start_time));         
                parent->removeUpdateCallback(this);
        }
}

int main (int argc, char* argv[])
{
    osg::ArgumentParser arguments(&argc, argv);
    osgViewer::Viewer viewer(arguments);
                        
// State A (Just a simple quad for now)
        osg::Geometry* geom0 = new osg::Geometry;
        osg::Vec3Array* verts0 = new osg::Vec3Array;
        osg::Vec4Array* colors0 = new osg::Vec4Array;

        geom0->setName("StateA");

        double x = 0, y = 0, w = 10, h = 10;
        verts0->push_back(osg::Vec3(x + w,      -0.01, y + h)); // top right
        verts0->push_back(osg::Vec3(x,          -0.01, y + h)); // top left
        verts0->push_back(osg::Vec3(x,          -0.01, y));     // bottom left  
                
        verts0->push_back(osg::Vec3(x + w,      -0.01, y));     // bottom right
                
        colors0->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
        colors0->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
        colors0->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
        colors0->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));

        geom0->setVertexArray(verts0);
        geom0->setColorArray(colors0);
        geom0->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 
        geom0->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 
4));

// State B
        osg::Geometry* geom1 = new osg::Geometry;
        osg::Vec3Array* verts1 = new osg::Vec3Array;
        osg::Vec4Array* colors1 = new osg::Vec4Array;

        geom1->setName("StateB");

        x = 50; y = 50; w = 10; h = 10;
        verts1->push_back(osg::Vec3(x + w,      0, y + h));     // top right
        verts1->push_back(osg::Vec3(x,          0, y + h));     // top left
        verts1->push_back(osg::Vec3(x,          0, y)); // bottom left          
        
        verts1->push_back(osg::Vec3(x + w,      0, y)); // bottom right
                
        colors1->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
        colors1->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
        colors1->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
        colors1->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));

        geom1->setVertexArray(verts1);
        geom1->setColorArray(colors1);
        geom1->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 
        geom1->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 
4));

// Create root/scene
    osg::Group* scene = new osg::Group;
        scene->getOrCreateStateSet()->setMode(GL_LIGHTING, 
osg::StateAttribute::OFF);
    
// Create geode for our 2 objects    
    osg::Geode* geode = new osg::Geode;    
        geode->addDrawable(geom0);
        geode->addDrawable(geom1);
        
// Schedule the animation       
        AnimationUpdatePostionCallback* one_time_use_callback = new 
AnimationUpdatePostionCallback(geode, 3.5, osg::Vec3(x, 0.0f, y));
        geode->addUpdateCallback(one_time_use_callback);
    scene->addChild(geode);

// Viewer settings
    viewer.addEventHandler(new osgViewer::StatsHandler());
    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
    viewer.addEventHandler(new 
osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
        viewer.setCameraManipulator(new osgGA::TrackballManipulator()); 

// let's run !
    viewer.setSceneData( scene );
        viewer.setUpViewInWindow(40, 40, 1024, 768, 0);
    viewer.realize();
    
    while (!viewer.done())
    {
        viewer.frame();
    }

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

Reply via email to