2002-02-23 21:20:00 +00:00
|
|
|
// model.cxx - manage a 3D aircraft model.
|
|
|
|
// Written by David Megginson, started 2002.
|
|
|
|
//
|
|
|
|
// This file is in the Public Domain, and comes with no warranty.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <plib/sg.h>
|
|
|
|
#include <plib/ssg.h>
|
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/misc/exception.hxx>
|
2002-02-25 20:07:34 +00:00
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2002-02-23 21:20:00 +00:00
|
|
|
|
|
|
|
#include "globals.hxx"
|
|
|
|
#include "fg_props.hxx"
|
|
|
|
#include "viewmgr.hxx"
|
|
|
|
#include "model.hxx"
|
|
|
|
|
|
|
|
extern unsigned long int fgSimTime; // FIXME: this is ugly
|
|
|
|
|
|
|
|
extern ssgRoot * scene; // FIXME: from main.cxx
|
|
|
|
|
|
|
|
FGAircraftModel current_model; // FIXME: add to globals
|
|
|
|
|
|
|
|
|
|
|
|
static ssgEntity *
|
|
|
|
find_named_node (ssgEntity * node, const string &name)
|
|
|
|
{
|
|
|
|
char * node_name = node->getName();
|
|
|
|
if (node_name != 0 && name == node_name)
|
|
|
|
return node;
|
|
|
|
else if (node->isAKindOf(ssgTypeBranch())) {
|
|
|
|
int nKids = node->getNumKids();
|
|
|
|
for (int i = 0; i < nKids; i++) {
|
|
|
|
ssgEntity * result =
|
|
|
|
find_named_node(((ssgBranch*)node)->getKid(i), name);
|
|
|
|
if (result != 0)
|
|
|
|
return result;
|
|
|
|
}
|
2002-02-24 21:14:38 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2002-02-23 21:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FGAircraftModel::FGAircraftModel ()
|
2002-02-25 20:07:34 +00:00
|
|
|
: _props(new SGPropertyNode),
|
2002-02-26 00:10:06 +00:00
|
|
|
_model(0),
|
2002-02-23 21:20:00 +00:00
|
|
|
_selector(new ssgSelector),
|
2002-02-26 00:10:06 +00:00
|
|
|
_position(new ssgTransform)
|
2002-02-23 21:20:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FGAircraftModel::~FGAircraftModel ()
|
|
|
|
{
|
2002-02-25 20:07:34 +00:00
|
|
|
delete _props;
|
2002-02-23 21:20:00 +00:00
|
|
|
// since the nodes are attached to the scene graph, they'll be
|
|
|
|
// deleted automatically
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FGAircraftModel::init ()
|
|
|
|
{
|
|
|
|
// TODO: optionally load an XML file with a pointer to the 3D object
|
|
|
|
// and placement and animation info
|
|
|
|
|
2002-02-26 00:10:06 +00:00
|
|
|
SG_LOG(SG_INPUT, SG_INFO, "Initializing aircraft 3D model");
|
|
|
|
|
2002-02-23 21:20:00 +00:00
|
|
|
// Load the 3D aircraft object itself
|
|
|
|
SGPath path = globals->get_fg_root();
|
|
|
|
path.append(fgGetString("/sim/model/path", "Models/Geometry/glider.ac"));
|
2002-02-25 20:07:34 +00:00
|
|
|
|
|
|
|
if (path.str().substr(path.str().size() - 4, 4) == ".xml") {
|
|
|
|
readProperties(path.str(), _props);
|
|
|
|
if (_props->hasValue("/path")) {
|
|
|
|
path = path.dir();;
|
|
|
|
path.append(_props->getStringValue("/path"));
|
|
|
|
} else {
|
|
|
|
path = globals->get_fg_root();
|
|
|
|
path.append("Models/Geometry/glider.ac");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-23 21:20:00 +00:00
|
|
|
ssgTexturePath((char *)path.dir().c_str());
|
2002-02-26 00:10:06 +00:00
|
|
|
_model = ssgLoad((char *)path.c_str());
|
|
|
|
if (_model == 0) {
|
|
|
|
_model = ssgLoad((char *)"Models/Geometry/glider.ac");
|
|
|
|
if (_model == 0)
|
2002-02-23 21:20:00 +00:00
|
|
|
throw sg_exception("Failed to load an aircraft model");
|
|
|
|
}
|
|
|
|
|
2002-02-26 00:10:06 +00:00
|
|
|
// Load animations
|
|
|
|
vector<SGPropertyNode *> animation_nodes =
|
|
|
|
_props->getChildren("animation");
|
|
|
|
for (int i = 0; i < animation_nodes.size(); i++) {
|
|
|
|
_animations.push_back(read_animation(animation_nodes[i]));
|
2002-02-23 21:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the alignment node
|
|
|
|
ssgTransform * align = new ssgTransform;
|
2002-02-26 00:10:06 +00:00
|
|
|
align->addKid(_model);
|
2002-02-23 21:20:00 +00:00
|
|
|
sgMat4 rot_matrix;
|
|
|
|
sgMat4 off_matrix;
|
|
|
|
sgMat4 res_matrix;
|
2002-02-25 20:07:34 +00:00
|
|
|
float h_rot = _props->getFloatValue("/offsets/heading-deg", 0.0);
|
|
|
|
float p_rot = _props->getFloatValue("/offsets/roll-deg", 0.0);
|
|
|
|
float r_rot = _props->getFloatValue("/offsets/pitch-deg", 0.0);
|
|
|
|
float x_off = _props->getFloatValue("/offsets/x-m", 0.0);
|
|
|
|
float y_off = _props->getFloatValue("/offsets/y-m", 0.0);
|
|
|
|
float z_off = _props->getFloatValue("/offsets/z-m", 0.0);
|
2002-02-23 21:20:00 +00:00
|
|
|
sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
|
|
|
|
sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
|
|
|
|
sgMultMat4(res_matrix, off_matrix, rot_matrix);
|
|
|
|
align->setTransform(res_matrix);
|
|
|
|
|
|
|
|
// Set up the position node
|
|
|
|
_position->addKid(align);
|
|
|
|
|
|
|
|
// Set up the selector node
|
|
|
|
_selector->addKid(_position);
|
|
|
|
_selector->clrTraversalMaskBits(SSGTRAV_HOT);
|
|
|
|
scene->addKid(_selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FGAircraftModel::bind ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FGAircraftModel::unbind ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FGAircraftModel::update (int dt)
|
|
|
|
{
|
|
|
|
_current_timestamp.stamp();
|
2002-02-26 00:10:06 +00:00
|
|
|
long elapsed_ms = (_current_timestamp - _last_timestamp) / 1000;
|
2002-02-23 21:20:00 +00:00
|
|
|
_last_timestamp.stamp();
|
|
|
|
|
2002-02-25 20:07:34 +00:00
|
|
|
if (globals->get_viewmgr()->get_current() == 0) {
|
2002-02-23 21:20:00 +00:00
|
|
|
_selector->select(false);
|
|
|
|
} else {
|
2002-02-26 00:10:06 +00:00
|
|
|
for (int i = 0; i < _animations.size(); i++)
|
|
|
|
do_animation(_animations[i], elapsed_ms);
|
|
|
|
|
2002-02-23 21:20:00 +00:00
|
|
|
_selector->select(true);
|
|
|
|
FGViewerRPH *pilot_view =
|
|
|
|
(FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
|
|
|
|
|
|
|
|
sgMat4 sgTRANS;
|
|
|
|
sgMakeTransMat4( sgTRANS, pilot_view->get_view_pos() );
|
|
|
|
|
|
|
|
sgVec3 ownship_up;
|
|
|
|
sgSetVec3( ownship_up, 0.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
sgMat4 sgROT;
|
|
|
|
sgMakeRotMat4( sgROT, -90.0, ownship_up );
|
|
|
|
|
|
|
|
sgMat4 sgTUX;
|
|
|
|
sgCopyMat4( sgTUX, sgROT );
|
|
|
|
sgPostMultMat4( sgTUX, pilot_view->get_VIEW_ROT() );
|
|
|
|
sgPostMultMat4( sgTUX, sgTRANS );
|
|
|
|
|
|
|
|
sgCoord tuxpos;
|
|
|
|
sgSetCoord( &tuxpos, sgTUX );
|
|
|
|
_position->setTransform( &tuxpos );
|
2002-02-26 00:10:06 +00:00
|
|
|
}
|
2002-02-23 21:20:00 +00:00
|
|
|
|
2002-02-26 00:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FGAircraftModel::Animation
|
|
|
|
FGAircraftModel::read_animation (const SGPropertyNode * node)
|
|
|
|
{
|
|
|
|
Animation animation;
|
|
|
|
|
|
|
|
// Figure out the animation type
|
|
|
|
string type_name = node->getStringValue("type");
|
|
|
|
if (type_name == "spin") {
|
|
|
|
SG_LOG(SG_INPUT, SG_INFO, "Reading spin animation");
|
|
|
|
animation.type = Animation::Spin;
|
2002-02-26 14:30:04 +00:00
|
|
|
} 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 animation;
|
2002-02-26 00:10:06 +00:00
|
|
|
} else {
|
|
|
|
animation.type = Animation::None;
|
|
|
|
SG_LOG(SG_INPUT, SG_ALERT, "Unknown animation type " << type_name);
|
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the object to be animated
|
|
|
|
string object_name = node->getStringValue("object-name");
|
|
|
|
ssgEntity * target = find_named_node(_model, object_name);
|
|
|
|
if (target != 0) {
|
|
|
|
SG_LOG(SG_INPUT, SG_INFO, " Target object is " << object_name);
|
|
|
|
} else {
|
|
|
|
animation.type = Animation::None;
|
|
|
|
SG_LOG(SG_INPUT, SG_ALERT, "Object " << object_name
|
|
|
|
<< " not found in model");
|
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splice a transform node into the tree
|
|
|
|
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);
|
|
|
|
|
2002-02-26 14:30:04 +00:00
|
|
|
animation.position = node->getFloatValue("initial-position", 0);
|
|
|
|
animation.factor = node->getFloatValue("factor", 1);
|
|
|
|
|
2002-02-26 00:10:06 +00:00
|
|
|
// Get the center and axis
|
|
|
|
animation.center_x = node->getFloatValue("center/x-m", 0);
|
|
|
|
animation.center_y = node->getFloatValue("center/y-m", 0);
|
|
|
|
animation.center_z = node->getFloatValue("center/z-m", 0);
|
|
|
|
animation.axis_x = node->getFloatValue("axis/x", 0);
|
|
|
|
animation.axis_y = node->getFloatValue("axis/y", 1);
|
|
|
|
animation.axis_z = node->getFloatValue("axis/z", 0);
|
|
|
|
|
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FGAircraftModel::do_animation (Animation &animation, long elapsed_ms)
|
|
|
|
{
|
|
|
|
switch (animation.type) {
|
|
|
|
case Animation::None:
|
|
|
|
return;
|
|
|
|
case Animation::Spin: {
|
2002-02-26 14:30:04 +00:00
|
|
|
float velocity_rpms = animation.prop->getDoubleValue()
|
|
|
|
* animation.factor / 60000.0;
|
2002-02-26 00:10:06 +00:00
|
|
|
animation.position += (elapsed_ms * velocity_rpms * 360);
|
|
|
|
while (animation.position >= 360)
|
|
|
|
animation.position -= 360;
|
|
|
|
sgMakeTransMat4(animation.matrix, -animation.center_x,
|
|
|
|
-animation.center_y, -animation.center_z);
|
|
|
|
sgVec3 axis;
|
|
|
|
sgSetVec3(axis, animation.axis_x, animation.axis_y, animation.axis_z);
|
|
|
|
sgMat4 tmp;
|
|
|
|
sgMakeRotMat4(tmp, animation.position, axis);
|
|
|
|
sgPostMultMat4(animation.matrix, tmp);
|
|
|
|
sgMakeTransMat4(tmp, animation.center_x,
|
|
|
|
animation.center_y, animation.center_z);
|
|
|
|
sgPostMultMat4(animation.matrix, tmp);
|
|
|
|
animation.transform->setTransform(animation.matrix);
|
|
|
|
return;
|
|
|
|
}
|
2002-02-26 14:30:04 +00:00
|
|
|
case Animation::Rotate: {
|
|
|
|
animation.position = animation.prop->getFloatValue() * animation.factor;
|
|
|
|
sgMakeTransMat4(animation.matrix, -animation.center_x,
|
|
|
|
-animation.center_y, -animation.center_z);
|
|
|
|
sgVec3 axis;
|
|
|
|
sgSetVec3(axis, animation.axis_x, animation.axis_y, animation.axis_z);
|
|
|
|
sgMat4 tmp;
|
|
|
|
sgMakeRotMat4(tmp, animation.position, axis);
|
|
|
|
sgPostMultMat4(animation.matrix, tmp);
|
|
|
|
sgMakeTransMat4(tmp, animation.center_x,
|
|
|
|
animation.center_y, animation.center_z);
|
|
|
|
sgPostMultMat4(animation.matrix, tmp);
|
|
|
|
animation.transform->setTransform(animation.matrix);
|
|
|
|
return;
|
|
|
|
}
|
2002-02-26 00:10:06 +00:00
|
|
|
default:
|
|
|
|
return;
|
2002-02-23 21:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of model.cxx
|