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
|
|
|
|
|
|
2003-05-08 20:28:46 +00:00
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
|
2002-04-13 13:30:38 +00:00
|
|
|
|
#include <string.h> // for strcmp()
|
2002-03-30 21:24:19 +00:00
|
|
|
|
|
2003-05-08 20:28:46 +00:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
2002-02-23 21:20:00 +00:00
|
|
|
|
#include <plib/sg.h>
|
|
|
|
|
#include <plib/ssg.h>
|
2002-11-16 22:01:15 +00:00
|
|
|
|
#include <plib/ul.h>
|
2002-02-23 21:20:00 +00:00
|
|
|
|
|
|
|
|
|
#include <simgear/misc/exception.hxx>
|
2002-02-25 20:07:34 +00:00
|
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2003-05-08 20:28:46 +00:00
|
|
|
|
#include <simgear/props/props.hxx>
|
2003-05-06 23:46:24 +00:00
|
|
|
|
#include <simgear/props/props_io.hxx>
|
2003-05-08 16:33:48 +00:00
|
|
|
|
#include <simgear/scene/model/animation.hxx>
|
2002-02-23 21:20:00 +00:00
|
|
|
|
|
2003-05-08 16:33:48 +00:00
|
|
|
|
#include "model.hxx"
|
|
|
|
|
|
2003-05-08 20:28:46 +00:00
|
|
|
|
SG_USING_STD(vector);
|
|
|
|
|
|
2002-02-23 21:20:00 +00:00
|
|
|
|
|
2002-03-30 21:24:19 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Static utility functions.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2002-03-27 14:52:19 +00:00
|
|
|
|
|
2002-08-07 01:34:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* Callback to update an animation.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
animation_callback (ssgEntity * entity, int mask)
|
|
|
|
|
{
|
|
|
|
|
((Animation *)entity->getUserData())->update();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-04-01 14:00:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Locate a named SSG node in a branch.
|
|
|
|
|
*/
|
2002-02-23 21:20:00 +00:00
|
|
|
|
static ssgEntity *
|
2002-03-30 21:24:19 +00:00
|
|
|
|
find_named_node (ssgEntity * node, const char * name)
|
2002-02-23 21:20:00 +00:00
|
|
|
|
{
|
|
|
|
|
char * node_name = node->getName();
|
2002-03-30 21:24:19 +00:00
|
|
|
|
if (node_name != 0 && !strcmp(name, node_name))
|
2002-02-23 21:20:00 +00:00
|
|
|
|
return node;
|
|
|
|
|
else if (node->isAKindOf(ssgTypeBranch())) {
|
|
|
|
|
int nKids = node->getNumKids();
|
|
|
|
|
for (int i = 0; i < nKids; i++) {
|
|
|
|
|
ssgEntity * result =
|
2002-04-13 13:30:38 +00:00
|
|
|
|
find_named_node(((ssgBranch*)node)->getKid(i), name);
|
2002-02-23 21:20:00 +00:00
|
|
|
|
if (result != 0)
|
2002-04-13 13:30:38 +00:00
|
|
|
|
return result;
|
2002-02-23 21:20:00 +00:00
|
|
|
|
}
|
2002-02-24 21:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
2002-02-23 21:20:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-30 21:24:19 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-10 13:20:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* Make an offset matrix from rotations and position offset.
|
|
|
|
|
*/
|
2003-05-08 20:28:46 +00:00
|
|
|
|
void
|
|
|
|
|
fgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
|
|
|
|
|
double x_off, double y_off, double z_off )
|
2002-06-10 13:20:26 +00:00
|
|
|
|
{
|
|
|
|
|
sgMat4 rot_matrix;
|
|
|
|
|
sgMat4 pos_matrix;
|
|
|
|
|
sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
|
|
|
|
|
sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
|
|
|
|
|
sgMultMat4(*result, pos_matrix, rot_matrix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-08 20:28:46 +00:00
|
|
|
|
void
|
|
|
|
|
fgMakeAnimation( ssgBranch * model,
|
|
|
|
|
const char * name,
|
|
|
|
|
vector<SGPropertyNode_ptr> &name_nodes,
|
|
|
|
|
SGPropertyNode *prop_root,
|
|
|
|
|
SGPropertyNode_ptr node,
|
|
|
|
|
double sim_time_sec )
|
2002-02-23 21:20:00 +00:00
|
|
|
|
{
|
2002-08-07 01:34:49 +00:00
|
|
|
|
Animation * animation = 0;
|
2002-11-13 16:45:21 +00:00
|
|
|
|
const char * type = node->getStringValue("type", "none");
|
2002-08-07 01:34:49 +00:00
|
|
|
|
if (!strcmp("none", type)) {
|
|
|
|
|
animation = new NullAnimation(node);
|
|
|
|
|
} else if (!strcmp("range", type)) {
|
|
|
|
|
animation = new RangeAnimation(node);
|
|
|
|
|
} else if (!strcmp("billboard", type)) {
|
|
|
|
|
animation = new BillboardAnimation(node);
|
|
|
|
|
} else if (!strcmp("select", type)) {
|
2003-05-06 23:46:24 +00:00
|
|
|
|
animation = new SelectAnimation(prop_root, node);
|
2002-08-07 01:34:49 +00:00
|
|
|
|
} else if (!strcmp("spin", type)) {
|
2003-05-06 23:46:24 +00:00
|
|
|
|
animation = new SpinAnimation(prop_root, node, sim_time_sec );
|
2002-11-06 15:47:40 +00:00
|
|
|
|
} else if (!strcmp("timed", type)) {
|
|
|
|
|
animation = new TimedAnimation(node);
|
2002-08-07 01:34:49 +00:00
|
|
|
|
} else if (!strcmp("rotate", type)) {
|
2003-05-06 23:46:24 +00:00
|
|
|
|
animation = new RotateAnimation(prop_root, node);
|
2002-08-07 01:34:49 +00:00
|
|
|
|
} else if (!strcmp("translate", type)) {
|
2003-05-06 23:46:24 +00:00
|
|
|
|
animation = new TranslateAnimation(prop_root, node);
|
2002-08-07 01:34:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
animation = new NullAnimation(node);
|
|
|
|
|
SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-03 15:48:08 +00:00
|
|
|
|
if (name != 0)
|
|
|
|
|
animation->setName((char *)name);
|
|
|
|
|
|
2002-08-07 01:34:49 +00:00
|
|
|
|
ssgEntity * object;
|
2002-11-03 15:42:11 +00:00
|
|
|
|
if (name_nodes.size() > 0) {
|
|
|
|
|
object = find_named_node(model, name_nodes[0]->getStringValue());
|
2002-08-07 01:34:49 +00:00
|
|
|
|
if (object == 0) {
|
2002-11-03 15:42:11 +00:00
|
|
|
|
SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
|
|
|
|
|
<< " not found");
|
2002-08-07 01:34:49 +00:00
|
|
|
|
delete animation;
|
|
|
|
|
animation = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
object = model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssgBranch * branch = animation->getBranch();
|
|
|
|
|
splice_branch(branch, object);
|
2002-11-03 15:42:11 +00:00
|
|
|
|
|
2003-05-06 23:46:24 +00:00
|
|
|
|
for (unsigned int i = 1; i < name_nodes.size(); i++) {
|
2002-11-03 15:42:11 +00:00
|
|
|
|
const char * name = name_nodes[i]->getStringValue();
|
|
|
|
|
object = find_named_node(model, name);
|
|
|
|
|
if (object == 0) {
|
|
|
|
|
SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
|
|
|
|
|
delete animation;
|
|
|
|
|
animation = 0;
|
|
|
|
|
}
|
|
|
|
|
ssgBranch * oldParent = object->getParent(0);
|
|
|
|
|
branch->addKid(object);
|
|
|
|
|
oldParent->removeKid(object);
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-06 15:47:40 +00:00
|
|
|
|
animation->init();
|
2002-08-07 01:34:49 +00:00
|
|
|
|
branch->setUserData(animation);
|
|
|
|
|
branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
|
2002-02-23 21:20:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-30 21:24:19 +00:00
|
|
|
|
|
2003-05-08 20:28:46 +00:00
|
|
|
|
|
2002-08-07 01:34:49 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Global functions.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2002-02-23 21:20:00 +00:00
|
|
|
|
|
2002-08-07 01:34:49 +00:00
|
|
|
|
ssgBranch *
|
2003-05-06 23:46:24 +00:00
|
|
|
|
fgLoad3DModel( const string &fg_root, const string &path,
|
|
|
|
|
SGPropertyNode *prop_root,
|
|
|
|
|
double sim_time_sec )
|
2002-02-23 21:20:00 +00:00
|
|
|
|
{
|
2002-08-07 01:34:49 +00:00
|
|
|
|
ssgBranch * model = 0;
|
2002-02-27 12:46:38 +00:00
|
|
|
|
SGPropertyNode props;
|
|
|
|
|
|
2002-04-13 13:30:38 +00:00
|
|
|
|
// Load the 3D aircraft object itself
|
2002-10-13 10:43:57 +00:00
|
|
|
|
SGPath xmlpath;
|
2002-04-05 03:19:34 +00:00
|
|
|
|
SGPath modelpath = path;
|
2002-11-16 22:01:15 +00:00
|
|
|
|
if ( ulIsAbsolutePathName( path.c_str() ) ) {
|
2002-10-13 10:43:57 +00:00
|
|
|
|
xmlpath = modelpath;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2003-05-06 23:46:24 +00:00
|
|
|
|
xmlpath = fg_root;
|
2002-10-13 10:43:57 +00:00
|
|
|
|
xmlpath.append(modelpath.str());
|
|
|
|
|
}
|
2002-03-20 22:40:11 +00:00
|
|
|
|
|
2002-04-13 13:30:38 +00:00
|
|
|
|
// Check for an XML wrapper
|
2002-03-20 22:40:11 +00:00
|
|
|
|
if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
|
|
|
|
|
readProperties(xmlpath.str(), &props);
|
2002-02-27 12:46:38 +00:00
|
|
|
|
if (props.hasValue("/path")) {
|
2002-03-20 22:40:11 +00:00
|
|
|
|
modelpath = modelpath.dir();
|
|
|
|
|
modelpath.append(props.getStringValue("/path"));
|
2002-02-25 20:07:34 +00:00
|
|
|
|
} else {
|
2002-08-07 01:34:49 +00:00
|
|
|
|
if (model == 0)
|
2002-11-06 15:47:40 +00:00
|
|
|
|
model = new ssgBranch;
|
2002-02-25 20:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-13 13:30:38 +00:00
|
|
|
|
// Assume that textures are in
|
|
|
|
|
// the same location as the XML file.
|
2002-08-07 01:34:49 +00:00
|
|
|
|
if (model == 0) {
|
2002-06-19 03:25:40 +00:00
|
|
|
|
ssgTexturePath((char *)xmlpath.dir().c_str());
|
2002-08-07 01:34:49 +00:00
|
|
|
|
model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
|
|
|
|
|
if (model == 0)
|
2002-06-19 03:25:40 +00:00
|
|
|
|
throw sg_exception("Failed to load 3D model");
|
|
|
|
|
}
|
2002-02-23 21:20:00 +00:00
|
|
|
|
|
2002-04-13 13:30:38 +00:00
|
|
|
|
// Set up the alignment node
|
2002-11-17 00:30:40 +00:00
|
|
|
|
ssgTransform * alignmainmodel = new ssgTransform;
|
|
|
|
|
alignmainmodel->addKid(model);
|
2002-02-23 21:20:00 +00:00
|
|
|
|
sgMat4 res_matrix;
|
2003-05-08 20:28:46 +00:00
|
|
|
|
fgMakeOffsetsMatrix(&res_matrix,
|
2002-11-06 15:47:40 +00:00
|
|
|
|
props.getFloatValue("/offsets/heading-deg", 0.0),
|
|
|
|
|
props.getFloatValue("/offsets/roll-deg", 0.0),
|
|
|
|
|
props.getFloatValue("/offsets/pitch-deg", 0.0),
|
|
|
|
|
props.getFloatValue("/offsets/x-m", 0.0),
|
|
|
|
|
props.getFloatValue("/offsets/y-m", 0.0),
|
|
|
|
|
props.getFloatValue("/offsets/z-m", 0.0));
|
2002-11-17 00:30:40 +00:00
|
|
|
|
alignmainmodel->setTransform(res_matrix);
|
2002-02-23 21:20:00 +00:00
|
|
|
|
|
2002-11-13 16:45:21 +00:00
|
|
|
|
unsigned int i;
|
|
|
|
|
|
2002-04-20 17:07:47 +00:00
|
|
|
|
// Load animations
|
2002-05-11 00:59:26 +00:00
|
|
|
|
vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
|
2002-04-20 17:07:47 +00:00
|
|
|
|
for (i = 0; i < animation_nodes.size(); i++) {
|
2002-11-03 15:48:08 +00:00
|
|
|
|
const char * name = animation_nodes[i]->getStringValue("name", 0);
|
2002-05-11 00:59:26 +00:00
|
|
|
|
vector<SGPropertyNode_ptr> name_nodes =
|
2002-04-20 17:07:47 +00:00
|
|
|
|
animation_nodes[i]->getChildren("object-name");
|
2003-05-08 20:28:46 +00:00
|
|
|
|
fgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
|
|
|
|
|
sim_time_sec);
|
2002-06-10 13:20:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-06 15:47:40 +00:00
|
|
|
|
// Load sub-models
|
2002-06-10 13:20:26 +00:00
|
|
|
|
vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
|
|
|
|
|
for (i = 0; i < model_nodes.size(); i++) {
|
|
|
|
|
SGPropertyNode_ptr node = model_nodes[i];
|
|
|
|
|
ssgTransform * align = new ssgTransform;
|
|
|
|
|
sgMat4 res_matrix;
|
2003-05-08 20:28:46 +00:00
|
|
|
|
fgMakeOffsetsMatrix(&res_matrix,
|
2002-11-06 15:47:40 +00:00
|
|
|
|
node->getFloatValue("offsets/heading-deg", 0.0),
|
|
|
|
|
node->getFloatValue("offsets/roll-deg", 0.0),
|
|
|
|
|
node->getFloatValue("offsets/pitch-deg", 0.0),
|
|
|
|
|
node->getFloatValue("offsets/x-m", 0.0),
|
|
|
|
|
node->getFloatValue("offsets/y-m", 0.0),
|
|
|
|
|
node->getFloatValue("offsets/z-m", 0.0));
|
2002-06-10 13:20:26 +00:00
|
|
|
|
align->setTransform(res_matrix);
|
2002-02-26 00:10:06 +00:00
|
|
|
|
|
2003-05-06 23:46:24 +00:00
|
|
|
|
ssgBranch * kid = fgLoad3DModel( fg_root, node->getStringValue("path"),
|
|
|
|
|
prop_root, sim_time_sec );
|
2002-08-07 01:34:49 +00:00
|
|
|
|
align->addKid(kid);
|
|
|
|
|
model->addKid(align);
|
2002-02-27 20:32:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-17 00:30:40 +00:00
|
|
|
|
return alignmainmodel;
|
2002-03-30 21:24:19 +00:00
|
|
|
|
}
|
2002-02-26 00:10:06 +00:00
|
|
|
|
|
|
|
|
|
|
2002-02-23 21:20:00 +00:00
|
|
|
|
// end of model.cxx
|