An example of querying/modifying animations(osg::sequence) of an DtOsgArticulatedModel.
Each 'entity' is our numbered grid model (a row with 7 boxes): -The row has 7 boxes, each of which have a different type of animation. Starting animations in the model starting from Large box on the far left are as follows:
Each box's animation is basically a numbered sequence with the textures indicating the 'frame number'
Pressing key '1' tests DtOsgArticulatedModel::setAnimationFrame and DtOsgArticulatedModel::setAnimationSpeed methods using row 1 (the top row). The first time key '1' is pressed, all the boxes in the top row will be reset to frame 0 and the animation speed will be set to 0.5 fps (every 2 seconds). The second time it is pressed, the animations will be set to frame 5 and the animation speed set to 1.0 fps. All subsequent presses of the '1' key will set the animations to frame 10 and the animation speed to 5.0 fps.
Pressing key '2' tests DtOsgArticulatedModel::setAnimationRange() methods using row 2. The first time key '2' is pressed, the animation range is set to be from frame 0 to frame 9 (1-10). The second time it is pressed the range is set from frame 4 to frame 7 (5-8). The third time it is pressed the range is from from frame 1 to frame 3 (2-4). On the fourth key press the range is set from frame 4 to frame 4 (5 only). Subsequent presses have no effect.
Pressing key '3' tests DtOsgArticulatedModel::setAnimationState methods using row 3. The first, third, fifth, etc. presses of key '3' will pause the first four boxes and stop the last three. The second, fourth, sixth, etc. key press resumes the first four boxes and restarts the last three.
Pressing key '4' tests DtOsgArticulatedModel::animationCurrentFrame methods using row 4. This test reports the animation frame for each box in row 4 (to the console window).
Pressing key 5 will test any random code you wish to add for row 5. The example also outputs the animations it found in the articulated model with their initial values. Please see the comments in the code as well since it describes what is being queried/set, etc.
This example is an application. You can run it by running ./bin64/exampleSequenceControl.exe (on Windows) or ./bin64/exampleSequenceControl (on Linux). For more information about running examples, please see Running Applications and Examples.
#include <vrvOsg/DtOsgArticulatedModelAgent.hpp>
#include <vrvCore/DtArticulatedModelAgent.hpp>
#include <vrvCore/DtSceneObjectAgent.hpp>
#include <matrix/vlTaitBryan.h>
#include <matrix/vlVector.h>
#include <string>
#include <cmath>
#include <array>
using namespace makVrv;
class MyArticulationDriver :
public DtDriver
{
public:
MyArticulationDriver(
DtDe& de ) :
DtDriver( de.agentManager(),
"MyArticulationDriver" )
, myModelDefinition( "testModel" )
, mySceneObjectsAndModels()
{
const std::string modelFilePath( "../examples/exampleSequenceControl/media/FlipBookTest.flt" );
myModelDefinition.setParameter( "filename", modelFilePath );
resetCounts();
}
virtual void destroyObjects()
{
for( auto& val : mySceneObjectsAndModels )
{
DtSceneObjectAgent* sceneObject = val.first;
DtArticulatedModelAgent* model = val.second;
if( sceneObject && model )
{
sceneObject->removeModel( model );
}
delete model;
model = 0;
delete sceneObject;
sceneObject = 0;
}
}
virtual ~MyArticulationDriver()
{
destroyObjects();
}
virtual bool onStart()
{
for( int i = 0; i < mySceneObjectsAndModels.size(); ++i )
{
DtArticulatedModelAgent* model = DtArticulatedModelAgent::create( myAgentManager );
model->setModelDefinition( myModelDefinition.name() );
DtSceneObjectAgent* sceneObject = DtSceneObjectAgent::create( myAgentManager );
sceneObject->setModelSet( DtModelSetId::Models3D );
sceneObject->addModel( model, 0 );
mySceneObjectsAndModels[i] = std::make_pair( sceneObject, model );
}
const float xPos = 0.0f;
const float yPos = 25.0f;
const float zDecrement = 3;
float posZ = 5.4;
DtTaitBryan taitBryan;
taitBryan.setPsi( -10.554191782826420 );
static const int partId = 4096;
for( int i = 0; i < mySceneObjectsAndModels.size(); ++i )
{
mySceneObjectsAndModels[i].first->setPosition( 0, DtVector( xPos, yPos, posZ ) );
mySceneObjectsAndModels[i].second->setPartOrientation( partId, taitBryan, DtVector() );
posZ -= zDecrement;
}
return true;
}
virtual bool onStop()
{
destroyObjects();
return true;
}
{
{
return "START";
}
{
return "STOP";
}
{
return "PAUSE";
}
{
return "RESUME";
}
return "UNKNOWN!!";
}
virtual bool onTick()
{
static bool once = false;
if( once )
{
return true;
}
DtArticulatedModelAgent* model = mySceneObjectsAndModels[0].second;
if( once == false && model->findObject() )
{
for( auto it = animations.begin(); it != animations.end(); ++it )
{
std::cout << "animation: " << it->first << ":" << std::endl;
const std::string& animationName = it->first;
std::cout <<
" animationFrameCount: " << articulatedModel->
animationFrameCount( animationName ) << std::endl;
std::cout <<
" animationFirstFrame: " << articulatedModel->
animationFirstFrame( animationName ) << std::endl;
std::cout <<
" animationLastFrame: " << articulatedModel->
animationLastFrame( animationName ) << std::endl;
std::cout <<
" animationSpeed: " << articulatedModel->
animationSpeed( animationName ) << std::endl;
std::cout <<
" animationState: " << toString( articulatedModel->
animationState( animationName ) ) << std::endl;
std::cout <<
" animationLastFrameTime: " << articulatedModel->
animationLastFrameTime( animationName ) << std::endl;
std::cout <<
" animationLoopCount: " << articulatedModel->
animationLoopCount( animationName ) << std::endl;
std::cout << " ------------------" << std::endl;
std::cout << std::endl;
myAnimationNames.insert( animationName );
once = true;
}
}
return true;
}
void setAnimationSpeedAndAnimationFrameTest( void )
{
if( myAnimationNames.empty() )
{
return;
}
if( articulatedModel == nullptr )
{
return;
}
std::cout << "row 1: " << std::endl;
if( mySpeedAndAnimationFrameTestCount == 0 )
{
std::cout << "reset to 0th frame (entity 0)" << std::endl;
std::cout << "reset to 0.5 fps (entity 0)" << std::endl;
std::cout << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
else if( mySpeedAndAnimationFrameTestCount == 1 )
{
std::cout << "reset to 5th frame (entity 0)" << std::endl;
std::cout << "reset to 1 fps (entity 0)" << std::endl;
std::cout << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
else
{
std::cout << "reset to 10th frame (entity 0)" << std::endl;
std::cout << "reset to 5 fps (entity 0)" << std::endl;
std::cout << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
++mySpeedAndAnimationFrameTestCount;
}
void setAnimationRangeTest( void )
{
if( myAnimationNames.empty() )
{
return;
}
if( articulatedModel == nullptr )
{
return;
}
std::cout << "row 2: " << std::endl;
if( myAnimationRangeTestCount == 0 )
{
std::cout << "reset animation range [1-10] (entity 1)" << std::endl;
std::cout << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
else if( myAnimationRangeTestCount == 1 )
{
std::cout << "reset animation range [5-8] (entity 1)" << std::endl;
std::cout << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
else if( myAnimationRangeTestCount == 2 )
{
std::cout << "reset animation range [2-4] (entity 1)" << std::endl;
std::cout << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
else if( myAnimationRangeTestCount == 3 )
{
std::cout << "reset animation range [5-5] (entity 1)" << std::endl;
std::cout << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
++myAnimationRangeTestCount;
}
void setAnimationStateTest( void )
{
if( myAnimationNames.empty() )
{
return;
}
if( articulatedModel == nullptr )
{
return;
}
std::cout << "row 3: " << std::endl;
if( myAnimationStateTestCount % 2 == 0 )
{
std::cout << "Pause first 4" << std::endl;
std::cout << "Stop last 3" << std::endl;
std::cout << std::endl;
}
if( myAnimationStateTestCount % 2 == 1 )
{
std::cout << "Resume first 4" << std::endl;
std::cout << "Start last 3 again" << std::endl;
std::cout << std::endl;
}
++myAnimationStateTestCount;
}
void queryAnimationCurrentFrameTest( void )
{
if( myAnimationNames.empty() )
{
return;
}
if( articulatedModel == nullptr )
{
return;
}
int i = 0;
std::cout << "row 4: " << std::endl;
std::cout << "current frame (left to right): " << std::endl;
std::cout <<
"\t box[" << i++ <<
"]: " << articulatedModel->
animationCurrentFrame(
"Slow" ) << std::endl;
std::cout <<
"\t box[" << i++ <<
"]: " << articulatedModel->
animationCurrentFrame(
"Fast" ) << std::endl;
std::cout <<
"\t box[" << i++ <<
"]: " << articulatedModel->
animationCurrentFrame(
"VeryFast" ) << std::endl;
std::cout <<
"\t box[" << i++ <<
"]: " << articulatedModel->
animationCurrentFrame(
"OneLoop" ) << std::endl;
std::cout <<
"\t box[" << i++ <<
"]: " << articulatedModel->
animationCurrentFrame(
"Swing" ) << std::endl;
std::cout <<
"\t box[" << i++ <<
"]: " << articulatedModel->
animationCurrentFrame(
"Backwards" ) << std::endl;
std::cout <<
"\t box[" << i++ <<
"]: " << articulatedModel->
animationCurrentFrame(
"LastFrame" ) << std::endl;
}
void testOtherThings( void )
{
if( myAnimationNames.empty() )
{
return;
}
std::cout << "row 5: " << std::endl;
if( articulatedModel == nullptr )
{
return;
}
if( myTestOtherStuffCount == 0 )
{
std::cout << "setting animation loop count to 5" << std::endl;
for( auto it = myAnimationNames.begin(); it != myAnimationNames.end(); ++it )
{
}
}
else if( myTestOtherStuffCount == 1 )
{
}
++myTestOtherStuffCount;
}
void resetCounts()
{
mySpeedAndAnimationFrameTestCount = 0;
myAnimationRangeTestCount = 0;
myAnimationStateTestCount = 0;
myTestOtherStuffCount = 0;
}
virtual const std::string& className() const
{
static const std::string name( "MyArticulationDriver" );
return name;
}
{
if( index >= mySceneObjectsAndModels.size() )
{
return nullptr;
}
DtArticulatedModelAgent* articulatedModelAgent = mySceneObjectsAndModels[index].second;
if( articulatedModelAgent == nullptr )
{
return nullptr;
}
return articulatedModel;
}
protected:
std::set<std::string> myAnimationNames;
static const int numEntities = 5;
std::array< std::pair<DtSceneObjectAgent*, DtArticulatedModelAgent*>, numEntities> mySceneObjectsAndModels;
int mySpeedAndAnimationFrameTestCount;
int myAnimationRangeTestCount;
int myAnimationStateTestCount;
int myTestOtherStuffCount;
};
int main( int argc, char** argv )
{
MyArticulationDriver* driver =
new MyArticulationDriver( igApp.
de() );
if( obsFrame )
{
inputDriver.addKeyBinding( obsFrame,
DtKeyState::KEY_4,
"queryAnimationCurrentFrameTest" );
}
return 0;
}