1
0
Fork 0

Major restructuring: the Animation class is now an abstract interface,

and specific animation types are derived from it.  This change makes
the code much easier to read, maintain, and extend.

Added a 'translate' animation type for a scaled, 3D linear translation
(such as a sliding throttle knob).

Renamed the 'offset' property to 'offset-m' or 'offset-deg' as
appropriate; ditto for 'min' and 'max' properties.
This commit is contained in:
david 2002-03-30 21:24:19 +00:00
parent 6a443bb173
commit d372548782
2 changed files with 440 additions and 197 deletions

View file

@ -7,6 +7,8 @@
# include <config.h> # include <config.h>
#endif #endif
#include <string.h> // for strcmp()
#include <plib/sg.h> #include <plib/sg.h>
#include <plib/ssg.h> #include <plib/ssg.h>
@ -25,13 +27,16 @@ extern ssgRoot * cockpit; // FIXME: from main.cxx
FGAircraftModel current_model; // FIXME: add to globals FGAircraftModel current_model; // FIXME: add to globals
////////////////////////////////////////////////////////////////////////
// Static utility functions.
////////////////////////////////////////////////////////////////////////
static ssgEntity * static ssgEntity *
find_named_node (ssgEntity * node, const string &name) find_named_node (ssgEntity * node, const char * name)
{ {
char * node_name = node->getName(); char * node_name = node->getName();
if (node_name != 0 && name == node_name) if (node_name != 0 && !strcmp(name, node_name))
return node; return node;
else if (node->isAKindOf(ssgTypeBranch())) { else if (node->isAKindOf(ssgTypeBranch())) {
int nKids = node->getNumKids(); int nKids = node->getNumKids();
@ -45,6 +50,81 @@ find_named_node (ssgEntity * node, const string &name)
return 0; return 0;
} }
/**
* Splice a branch in between all child nodes and their parents.
*/
static void
splice_branch (ssgBranch * branch, ssgEntity * child)
{
int nParents = child->getNumParents();
branch->addKid(child);
for (int i = 0; i < nParents; i++) {
ssgBranch * parent = child->getParent(i);
parent->replaceKid(child, branch);
}
}
/**
* Set up the transform matrix for a spin or rotation.
*/
static void
set_rotation (sgMat4 &matrix, double position_deg,
sgVec3 &center, sgVec3 &axis)
{
float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
float s = (float) sin ( temp_angle ) ;
float c = (float) cos ( temp_angle ) ;
float t = SG_ONE - c ;
// axis was normalized at load time
// hint to the compiler to put these into FP registers
float x = axis[0];
float y = axis[1];
float z = axis[2];
matrix[0][0] = t * x * x + c ;
matrix[0][1] = t * y * x - s * z ;
matrix[0][2] = t * z * x + s * y ;
matrix[0][3] = SG_ZERO;
matrix[1][0] = t * x * y + s * z ;
matrix[1][1] = t * y * y + c ;
matrix[1][2] = t * z * y - s * x ;
matrix[1][3] = SG_ZERO;
matrix[2][0] = t * x * z - s * y ;
matrix[2][1] = t * y * z + s * x ;
matrix[2][2] = t * z * z + c ;
matrix[2][3] = SG_ZERO;
// hint to the compiler to put these into FP registers
x = center[0];
y = center[1];
z = center[2];
matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
matrix[3][3] = SG_ONE;
}
/**
* Set up the transform matrix for a translation.
*/
static void
set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
{
sgVec3 xyz;
sgScaleVec3(xyz, axis, position_m);
sgMakeTransMat4(matrix, xyz);
}
////////////////////////////////////////////////////////////////////////
// Implementation of FGAircraftModel
////////////////////////////////////////////////////////////////////////
FGAircraftModel::FGAircraftModel () FGAircraftModel::FGAircraftModel ()
: _model(0), : _model(0),
@ -57,6 +137,13 @@ FGAircraftModel::~FGAircraftModel ()
{ {
// since the nodes are attached to the scene graph, they'll be // since the nodes are attached to the scene graph, they'll be
// deleted automatically // deleted automatically
for (int i = 0; i < _animations.size(); i++) {
Animation * tmp = _animations[i];
_animations[i] = 0;
delete tmp;
}
} }
void void
@ -95,8 +182,7 @@ FGAircraftModel::init ()
} }
// Load animations // Load animations
vector<SGPropertyNode *> animation_nodes = vector<SGPropertyNode *> animation_nodes = props.getChildren("animation");
props.getChildren("animation");
for (unsigned int i = 0; i < animation_nodes.size(); i++) { for (unsigned int i = 0; i < animation_nodes.size(); i++) {
vector<SGPropertyNode *> name_nodes = vector<SGPropertyNode *> name_nodes =
animation_nodes[i]->getChildren("object-name"); animation_nodes[i]->getChildren("object-name");
@ -104,9 +190,9 @@ FGAircraftModel::init ()
SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation"); SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation");
} else { } else {
for (unsigned int j = 0; j < name_nodes.size(); j++) { for (unsigned int j = 0; j < name_nodes.size(); j++) {
Animation animation; Animation * animation =
read_animation(animation, name_nodes[j]->getStringValue(), make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
animation_nodes[i]); if (animation != 0)
_animations.push_back(animation); _animations.push_back(animation);
} }
} }
@ -154,17 +240,13 @@ FGAircraftModel::update (int dt)
sgMat4 MODEL_ROT, LOCAL; sgMat4 MODEL_ROT, LOCAL;
sgMat4 sgTRANS; sgMat4 sgTRANS;
_current_timestamp.stamp();
long elapsed_ms = (_current_timestamp - _last_timestamp) / 1000;
_last_timestamp.stamp();
int view_number = globals->get_viewmgr()->get_current(); int view_number = globals->get_viewmgr()->get_current();
if (view_number == 0 && !fgGetBool("/sim/view/internal")) { if (view_number == 0 && !fgGetBool("/sim/view/internal")) {
_selector->select(false); _selector->select(false);
} else { } else {
for (unsigned int i = 0; i < _animations.size(); i++) for (unsigned int i = 0; i < _animations.size(); i++)
do_animation(_animations[i], elapsed_ms); _animations[i]->update(dt);
_selector->select(true); _selector->select(true);
FGViewer *current_view = FGViewer *current_view =
@ -206,107 +288,35 @@ FGAircraftModel::update (int dt)
} }
} }
void FGAircraftModel::Animation *
FGAircraftModel::read_animation (Animation &animation, FGAircraftModel::make_animation (const char * object_name,
const string &object_name, SGPropertyNode * node)
const SGPropertyNode * node)
{ {
// Find the object to be animated Animation * animation = 0;
ssgEntity * target = find_named_node(_model, object_name); const char * type = node->getStringValue("type");
if (target != 0) { if (!strcmp("none", type)) {
SG_LOG(SG_INPUT, SG_INFO, " Target object is " << object_name); animation = new NullAnimation();
} else if (!strcmp("spin", type)) {
animation = new SpinAnimation();
} else if (!strcmp("rotate", type)) {
animation = new RotateAnimation();
} else if (!strcmp("translate", type)) {
animation = new TranslateAnimation();
} else { } else {
SG_LOG(SG_INPUT, SG_ALERT, "Object " << object_name animation = new NullAnimation();
<< " not found in model"); SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
return;
} }
// Figure out the animation type ssgEntity * object = find_named_node(_model, object_name);
string type_name = node->getStringValue("type"); if (object == 0) {
if (type_name == "spin") { SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
SG_LOG(SG_INPUT, SG_INFO, "Reading spin animation"); delete animation;
animation.type = Animation::Spin; animation = 0;
} else if (type_name == "rotate") {
SG_LOG(SG_INPUT, SG_INFO, "Reading rotate animation");
animation.type = Animation::Rotate;
} else if (type_name == "none") {
SG_LOG(SG_INPUT, SG_INFO, "Reading disabled animation");
animation.type = Animation::None;
return;
} else { } else {
animation.type = Animation::None; animation->init(object, node);
SG_LOG(SG_INPUT, SG_ALERT, "Unknown animation type " << type_name);
return;
} }
// Splice a transform node into the tree return animation;
animation.transform = new ssgTransform;
int nParents = target->getNumParents();
animation.transform->addKid(target);
for (int i = 0; i < nParents; i++) {
ssgBranch * parent = target->getParent(i);
parent->replaceKid(target, animation.transform);
}
// Get the node
animation.prop =
fgGetNode(node->getStringValue("property", "/null"), true);
animation.position = node->getFloatValue("initial-position", 0);
animation.offset = node->getFloatValue("offset", 0);
if (node->hasValue("min")) {
animation.has_min = true;
animation.min = node->getFloatValue("min");
} else {
animation.has_min = false;
}
if (node->hasValue("max")) {
animation.has_max = true;
animation.max = node->getFloatValue("max");
} else {
animation.has_max = false;
}
animation.factor = node->getFloatValue("factor", 1);
// Get the center and axis
animation.center[0] = node->getFloatValue("center/x-m", 0);
animation.center[1] = node->getFloatValue("center/y-m", 0);
animation.center[2] = node->getFloatValue("center/z-m", 0);
animation.axis[0] = node->getFloatValue("axis/x", 0);
animation.axis[1] = node->getFloatValue("axis/y", 0);
animation.axis[2] = node->getFloatValue("axis/z", 0);
sgNormalizeVec3(animation.axis);
}
void
FGAircraftModel::do_animation (Animation &animation, long elapsed_ms)
{
switch (animation.type) {
case Animation::None:
return;
case Animation::Spin:
{
float velocity_rpms = (animation.prop->getDoubleValue()
* animation.factor / 60000.0);
animation.position += (elapsed_ms * velocity_rpms * 360);
animation.setRotation();
return;
}
case Animation::Rotate: {
animation.position = ((animation.prop->getFloatValue()
+ animation.offset)
* animation.factor);
if (animation.has_min && animation.position < animation.min)
animation.position = animation.min;
if (animation.has_max && animation.position > animation.max)
animation.position = animation.max;
animation.setRotation();
return;
}
default:
return;
}
} }
@ -316,73 +326,212 @@ FGAircraftModel::do_animation (Animation &animation, long elapsed_ms)
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
FGAircraftModel::Animation::Animation () FGAircraftModel::Animation::Animation ()
: name(""),
type(None),
transform(0),
prop(0),
factor(0),
offset(0),
position(0),
has_min(false),
min(0),
has_max(false),
max(0)
{ {
} }
FGAircraftModel::Animation::~Animation () FGAircraftModel::Animation::~Animation ()
{ {
// pointers are managed elsewhere; these are just references
} }
/*
* Transform to rotate an object around its local axis
* from a relative frame of reference at center -- NHV ////////////////////////////////////////////////////////////////////////
*/ // Implementation of FGAircraftModel::NullAnimation
void ////////////////////////////////////////////////////////////////////////
FGAircraftModel::Animation::setRotation()
FGAircraftModel::NullAnimation::NullAnimation ()
{ {
float temp_angle = -position * SG_DEGREES_TO_RADIANS ;
float s = (float) sin ( temp_angle ) ;
float c = (float) cos ( temp_angle ) ;
float t = SG_ONE - c ;
// axis was normalized at load time
// hint to the compiler to put these into FP registers
float x = axis[0];
float y = axis[1];
float z = axis[2];
sgMat4 matrix;
matrix[0][0] = t * x * x + c ;
matrix[0][1] = t * y * x - s * z ;
matrix[0][2] = t * z * x + s * y ;
matrix[0][3] = SG_ZERO;
matrix[1][0] = t * x * y + s * z ;
matrix[1][1] = t * y * y + c ;
matrix[1][2] = t * z * y - s * x ;
matrix[1][3] = SG_ZERO;
matrix[2][0] = t * x * z - s * y ;
matrix[2][1] = t * y * z + s * x ;
matrix[2][2] = t * z * z + c ;
matrix[2][3] = SG_ZERO;
// hint to the compiler to put these into FP registers
x = center[0];
y = center[1];
z = center[2];
matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
matrix[3][3] = SG_ONE;
transform->setTransform(matrix);
} }
FGAircraftModel::NullAnimation::~NullAnimation ()
{
}
void
FGAircraftModel::NullAnimation::init (ssgEntity * object,
SGPropertyNode * node)
{
}
void
FGAircraftModel::NullAnimation::update (int dt)
{
}
////////////////////////////////////////////////////////////////////////
// Implementation of FGAircraftModel::SpinAnimation
////////////////////////////////////////////////////////////////////////
FGAircraftModel::SpinAnimation::SpinAnimation ()
: _prop(0),
_factor(0),
_position_deg(0),
_transform(new ssgTransform)
{
}
FGAircraftModel::SpinAnimation::~SpinAnimation ()
{
_transform = 0;
}
void
FGAircraftModel::SpinAnimation::init (ssgEntity * object,
SGPropertyNode * props)
{
// Splice in the new transform node
splice_branch(_transform, object);
_prop = fgGetNode(props->getStringValue("property", "/null"), true);
_factor = props->getDoubleValue("factor", 1.0);
_position_deg = props->getDoubleValue("starting-position-deg", 0);
_center[0] = props->getFloatValue("center/x-m", 0);
_center[1] = props->getFloatValue("center/y-m", 0);
_center[2] = props->getFloatValue("center/z-m", 0);
_axis[0] = props->getFloatValue("axis/x", 0);
_axis[1] = props->getFloatValue("axis/y", 0);
_axis[2] = props->getFloatValue("axis/z", 0);
sgNormalizeVec3(_axis);
}
void
FGAircraftModel::SpinAnimation::update (int dt)
{
float velocity_rpms = (_prop->getDoubleValue() * _factor / 60000.0);
_position_deg += (dt * velocity_rpms * 360);
while (_position_deg < 0)
_position_deg += 360.0;
while (_position_deg >= 360.0)
_position_deg -= 360.0;
set_rotation(_matrix, _position_deg, _center, _axis);
_transform->setTransform(_matrix);
}
////////////////////////////////////////////////////////////////////////
// Implementation of FGAircraftModel::RotateAnimation
////////////////////////////////////////////////////////////////////////
FGAircraftModel::RotateAnimation::RotateAnimation ()
: _prop(0),
_offset_deg(0.0),
_factor(1.0),
_has_min(false),
_min_deg(0.0),
_has_max(false),
_max_deg(1.0),
_position_deg(0.0),
_transform(new ssgTransform)
{
}
FGAircraftModel::RotateAnimation::~RotateAnimation ()
{
_transform = 0;
}
void
FGAircraftModel::RotateAnimation::init (ssgEntity * object,
SGPropertyNode * props)
{
// Splice in the new transform node
splice_branch(_transform, object);
_prop = fgGetNode(props->getStringValue("property", "/null"), true);
_offset_deg = props->getDoubleValue("offset-deg", 0.0);
_factor = props->getDoubleValue("factor", 1.0);
if (props->hasValue("min-deg")) {
_has_min = true;
_min_deg = props->getDoubleValue("min-deg");
}
if (props->hasValue("max-deg")) {
_has_max = true;
_max_deg = props->getDoubleValue("max-deg");
}
_position_deg = props->getDoubleValue("starting-position-deg", 0);
_center[0] = props->getFloatValue("center/x-m", 0);
_center[1] = props->getFloatValue("center/y-m", 0);
_center[2] = props->getFloatValue("center/z-m", 0);
_axis[0] = props->getFloatValue("axis/x", 0);
_axis[1] = props->getFloatValue("axis/y", 0);
_axis[2] = props->getFloatValue("axis/z", 0);
sgNormalizeVec3(_axis);
}
void
FGAircraftModel::RotateAnimation::update (int dt)
{
_position_deg = ((_prop->getDoubleValue() + _offset_deg) * _factor);
if (_has_min && _position_deg < _min_deg)
_position_deg = _min_deg;
if (_has_max && _position_deg > _max_deg)
_position_deg = _max_deg;
set_rotation(_matrix, _position_deg, _center, _axis);
_transform->setTransform(_matrix);
}
////////////////////////////////////////////////////////////////////////
// Implementation of FGAircraftModel::TranslateAnimation
////////////////////////////////////////////////////////////////////////
FGAircraftModel::TranslateAnimation::TranslateAnimation ()
: _prop(0),
_offset_m(0.0),
_factor(1.0),
_has_min(false),
_min_m(0.0),
_has_max(false),
_max_m(1.0),
_position_m(0.0),
_transform(new ssgTransform)
{
}
FGAircraftModel::TranslateAnimation::~TranslateAnimation ()
{
_transform = 0;
}
void
FGAircraftModel::TranslateAnimation::init (ssgEntity * object,
SGPropertyNode * props)
{
// Splice in the new transform node
splice_branch(_transform, object);
_prop = fgGetNode(props->getStringValue("property", "/null"), true);
_offset_m = props->getDoubleValue("offset-m", 0.0);
_factor = props->getDoubleValue("factor", 1.0);
if (props->hasValue("min-m")) {
_has_min = true;
_min_m = props->getDoubleValue("min-m");
}
if (props->hasValue("max-m")) {
_has_max = true;
_max_m = props->getDoubleValue("max-m");
}
_position_m = props->getDoubleValue("starting-position-m", 0);
_axis[0] = props->getFloatValue("axis/x", 0);
_axis[1] = props->getFloatValue("axis/y", 0);
_axis[2] = props->getFloatValue("axis/z", 0);
sgNormalizeVec3(_axis);
}
void
FGAircraftModel::TranslateAnimation::update (int dt)
{
_position_m = ((_prop->getDoubleValue() + _offset_m) * _factor);
if (_has_min && _position_m < _min_m)
_position_m = _min_m;
if (_has_max && _position_m > _max_m)
_position_m = _max_m;
set_translation(_matrix, _position_m, _axis);
_transform->setTransform(_matrix);
}
// end of model.cxx // end of model.cxx

View file

@ -42,45 +42,139 @@ public:
private: private:
struct Animation class Animation;
{
Animation ();
virtual ~Animation ();
enum Type {
None,
Spin,
Rotate
};
string name;
Type type;
ssgTransform * transform;
sgMat4 matrix;
SGPropertyNode * prop;
float factor;
float offset;
float position;
bool has_min;
float min;
bool has_max;
float max;
sgVec3 center;
sgVec3 axis;
void setRotation ();
};
void read_animation (Animation &animation, Animation * make_animation (const char * object_name, SGPropertyNode * node);
const string &object_name,
const SGPropertyNode * node);
void do_animation (Animation &animation, long elapsed_ms);
ssgEntity * _model; ssgEntity * _model;
ssgSelector * _selector; ssgSelector * _selector;
ssgTransform * _position; ssgTransform * _position;
SGTimeStamp _last_timestamp; vector <Animation *> _animations;
SGTimeStamp _current_timestamp;
vector<Animation> _animations;
//////////////////////////////////////////////////////////////////////
// Internal classes for individual animations.
//////////////////////////////////////////////////////////////////////
/**
* Abstract base class for all animations.
*/
class Animation
{
public:
Animation ();
virtual ~Animation ();
/**
* Initialize the animation.
*
* @param object The object to animate.
* @param props The property node with configuration information.
*/
virtual void init (ssgEntity * object, SGPropertyNode * props) = 0;
/**
* Update the animation.
*
* @param dt The elapsed time in milliseconds since the last call.
*/
virtual void update (int dt) = 0;
};
/**
* A no-op animation.
*/
class NullAnimation : public Animation
{
public:
NullAnimation ();
virtual ~NullAnimation ();
virtual void init (ssgEntity * object, SGPropertyNode * props);
virtual void update (int dt);
};
/**
* Animation to spin an object around a center point.
*
* This animation rotates at a specific velocity.
*/
class SpinAnimation : public Animation
{
public:
SpinAnimation ();
virtual ~SpinAnimation ();
virtual void init (ssgEntity * object, SGPropertyNode * props);
virtual void update (int dt);
private:
SGPropertyNode * _prop;
double _factor;
double _position_deg;
sgMat4 _matrix;
sgVec3 _center;
sgVec3 _axis;
ssgTransform * _transform;
};
/**
* Animation to rotate an object around a center point.
*
* This animation rotates to a specific position.
*/
class RotateAnimation : public Animation
{
public:
RotateAnimation ();
virtual ~RotateAnimation ();
virtual void init (ssgEntity * object, SGPropertyNode * props);
virtual void update (int dt);
private:
SGPropertyNode * _prop;
double _offset_deg;
double _factor;
bool _has_min;
double _min_deg;
bool _has_max;
double _max_deg;
double _position_deg;
sgMat4 _matrix;
sgVec3 _center;
sgVec3 _axis;
ssgTransform * _transform;
};
/**
* Animation to slide along an axis.
*/
class TranslateAnimation : public Animation
{
public:
TranslateAnimation ();
virtual ~TranslateAnimation ();
virtual void init (ssgEntity * object, SGPropertyNode * props);
virtual void update (int dt);
private:
SGPropertyNode * _prop;
double _offset_m;
double _factor;
bool _has_min;
double _min_m;
bool _has_max;
double _max_m;
double _position_m;
sgMat4 _matrix;
sgVec3 _axis;
ssgTransform * _transform;
};
}; };