[Gcblue-commits] gcb_wx/src/graphics ObjectUpdater.cpp,1.3,1.4 tc3DModel.cpp,1.1,1.2
Status: Alpha
Brought to you by:
ddcforge
From: <ddc...@us...> - 2004-01-14 01:13:25
|
Update of /cvsroot/gcblue/gcb_wx/src/graphics In directory sc8-pr-cvs1:/tmp/cvs-serv10758/src/graphics Modified Files: ObjectUpdater.cpp tc3DModel.cpp Log Message: more animation work Index: ObjectUpdater.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/graphics/ObjectUpdater.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ObjectUpdater.cpp 12 Jan 2004 03:01:36 -0000 1.3 --- ObjectUpdater.cpp 14 Jan 2004 01:13:22 -0000 1.4 *************** *** 31,37 **** { size_t nAnimations = model->animationInfo.size(); for(size_t n=0;n<nAnimations;n++) { ! osg::MatrixTransform *xform = model->animationInfo[n].transform.get(); osg::Matrix matrix = xform->getMatrix(); xform->setMatrix(matrix * matrix.rotate(0.5f, model->animationInfo[n].axis)); --- 31,42 ---- { size_t nAnimations = model->animationInfo.size(); + if (nAnimations == 0) return; + + // do not update animations if camera is beyond animation LOD distance + //if (distanceFromCamera > 10000.0f) return; + for(size_t n=0;n<nAnimations;n++) { ! osg::MatrixTransform *xform = model->animationInfo[n].transform; osg::Matrix matrix = xform->getMatrix(); xform->setMatrix(matrix * matrix.rotate(0.5f, model->animationInfo[n].axis)); *************** *** 46,50 **** float yawCorrection = 0; - UpdateAnimations(); tcGameObject *gameObject = model->GetGameObj(); if (gameObject) --- 51,54 ---- *************** *** 87,91 **** } } ! osg::Matrix m = --- 91,99 ---- } } ! ! // not correct for child objects ! Producer::Vec3 cp = viewer->GetCameraPosition(); ! osg::Vec3 cameraDelta = osg::Vec3(x,y,z) - osg::Vec3(cp.x(),cp.y(),cp.z()); ! distanceFromCamera = cameraDelta.length(); osg::Matrix m = *************** *** 100,103 **** --- 108,114 ---- } } + + UpdateAnimations(); + // must call any nested node callbacks and continue subgraph traversal. NodeCallback::traverse(node,nv); Index: tc3DModel.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/graphics/tc3DModel.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** tc3DModel.cpp 12 Jan 2004 03:01:36 -0000 1.1 --- tc3DModel.cpp 14 Jan 2004 01:13:22 -0000 1.2 *************** *** 31,34 **** --- 31,35 ---- #include "tcGameObject.h" #include "ObjectUpdater.h" + #include "tcGenericDBObject.h" #include <osg/MatrixTransform> #include <osgDB/ReadFile> *************** *** 40,44 **** #endif ! /** * A visitor for model prototypes to attach animation --- 41,45 ---- #endif ! // started with CSP AnimationProcessor class /** * A visitor for model prototypes to attach animation *************** *** 49,94 **** * animation callbacks. */ ! class ModelProcessor: public osg::NodeVisitor { osg::ref_ptr<osg::Node> m_Root; ! std::vector<tcAnimationInfo>& animationInfo; //simdata::Link<Animation>::vector const *m_Animations; public: ! //ModelProcessor(): NodeVisitor(TRAVERSE_ALL_CHILDREN), m_Animations(0) { } ! ModelProcessor(std::vector<tcAnimationInfo>& ai) : NodeVisitor(TRAVERSE_ALL_CHILDREN) ! , animationInfo(ai) { } /* - void setAnimations(simdata::Link<Animation>::vector const *animations) { - m_Animations = animations; - } - */ virtual void apply(osg::Transform &node) { - //if (!m_Animations) return; std::string name = node.getName(); std::cout << "MODEL TRANSFORM: " << name << "\n"; ! /* ! if (name.substr(0,6) == "ANIM: ") { ! simdata::Key id = name.substr(6); ! simdata::Link<Animation>::vector::const_iterator i = m_Animations->begin(); ! std::cout << "SEARCHING FOR " << name.substr(6) << " (" << id.asString() << ")\n"; ! for (; i != m_Animations->end(); ++i) { ! std::cout << "COMPARING TO " << (*i)->getModelID().asString() << "\n"; ! if ((*i)->getModelID() == id) { ! node.setUserData(new AnimationBinding(i->get())); ! std::cout << "FOUND\n"; ! break; ! } ! } ! } ! */ } virtual void apply(osg::Node &node) { std::string name = node.getName(); std::cout << "MODEL NODE: " << name << "\n"; ! if (name == "pr1") { // insert a ModelTransform between this node and its parent --- 50,96 ---- * animation callbacks. */ ! class AnimationProcessor: public osg::NodeVisitor { osg::ref_ptr<osg::Node> m_Root; ! std::vector<tcAnimationInfo>& animationInfo; // vector of animation state info to update ! std::vector<animationDBInfo>& databaseInfo; ///< database info to find and configure animations //simdata::Link<Animation>::vector const *m_Animations; public: ! //AnimationProcessor(): NodeVisitor(TRAVERSE_ALL_CHILDREN), m_Animations(0) { } ! AnimationProcessor(std::vector<tcAnimationInfo>& ai, std::vector<animationDBInfo>& adb) : NodeVisitor(TRAVERSE_ALL_CHILDREN) ! , animationInfo(ai), databaseInfo(adb) { } /* virtual void apply(osg::Transform &node) { std::string name = node.getName(); std::cout << "MODEL TRANSFORM: " << name << "\n"; ! traverse(node); } + */ virtual void apply(osg::Node &node) { std::string name = node.getName(); std::cout << "MODEL NODE: " << name << "\n"; ! /* ! if (node.getNumParents()) ! { ! osg::Node *parent = node.getParent(0); ! std::cout << " parent: " << parent->getName() << "\n"; ! } ! */ ! // search database for entry matching this node ! bool animateNode = false; ! animationDBInfo info; ! size_t nAnimations = databaseInfo.size(); ! for(size_t n=0;(n<nAnimations)&&(!animateNode);n++) ! { ! info = databaseInfo[n]; ! if (info.objectName == name) animateNode = true; ! } ! ! ! if (animateNode) { // insert a ModelTransform between this node and its parent *************** *** 107,124 **** osg::ref_ptr<osg::Node> temp = &node; osg::Group *parent = node.getParent(0); ! parent->removeChild(&node); untranslateTransform->addChild(&node); rotateTransform->addChild(untranslateTransform); translateTransform->addChild(rotateTransform); ! parent->addChild(translateTransform); tcAnimationInfo ai; ! ai.axis = osg::Vec3(0,1.0f,0); ai.omega = 1; ai.switchVariable = NULL; ai.transform = rotateTransform; animationInfo.push_back(ai); ! std::cout << "added animation info\n"; } else --- 109,128 ---- osg::ref_ptr<osg::Node> temp = &node; osg::Group *parent = node.getParent(0); ! unsigned childIdx = parent->getChildIndex(&node); ! parent->setChild(childIdx, translateTransform); // replace child with transform untranslateTransform->addChild(&node); rotateTransform->addChild(untranslateTransform); translateTransform->addChild(rotateTransform); ! tcAnimationInfo ai; ! ai.axis = (info.animationType == "propeller") ? ! osg::Vec3(0,1.0f,0) : osg::Vec3(0,0,1.0f); ai.omega = 1; ai.switchVariable = NULL; ai.transform = rotateTransform; animationInfo.push_back(ai); ! std::cout << "added animation info for: " << name <<"\n"; } else *************** *** 133,136 **** --- 137,212 ---- }; + /** + * Custom copy op for cloning models with animation + */ + class ModelCopyOp : public osg::CopyOp + { + public: + + inline ModelCopyOp(tc3DModel *model): + osg::CopyOp(SHALLOW_COPY), + _model(model) + {} + + + virtual osg::Node* operator() (const osg::Node* node) const + { + if (const osg::Transform *xform = node->asTransform()) + { + size_t nAnimations = _model->animationInfo.size(); + tcAnimationInfo *info = NULL; + bool foundAnimation = false; + for (size_t n=0;(n<nAnimations)&&(!foundAnimation);n++) + { + info = &_model->animationInfo[n]; + if (info->transform == xform) + { + foundAnimation = true; + } + } + + osg::Object *obj = (node->clone(ModelCopyOp(_model))); + osg::Node* ret_node = dynamic_cast<osg::Node*>(obj); + + if (foundAnimation) + { + std::cout << " Found matching animation\n"; + osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(ret_node->asTransform()); + if (mt) + { + info->transform = mt; + } + else + { + std::cerr << "Error binding animation in model \n"; + } + } + return ret_node; + } + else + { + osg::Node* ret_node = CopyOp::operator()(node); + return ret_node; + } + } + + virtual osg::StateAttribute* operator() (const osg::StateAttribute* attr) const + { + osg::StateAttribute* ret_attr; + if (attr) + { + ret_attr = dynamic_cast<osg::StateAttribute*> + (attr->clone(CopyOp(osg::CopyOp::DEEP_COPY_STATEATTRIBUTES))); + } + else + { + ret_attr = NULL; + } + return ret_attr; + } + + protected: + tc3DModel *_model; + }; /** *************** *** 184,193 **** modelNode->accept(smoothingVisitor); } - ModelProcessor mp(animationInfo); - modelNode->accept(mp); fprintf(stdout,"tc3DModel--Loaded 3D model: %s\n", model_name.c_str()); } /** * Called once per instance to set and configure update callback. --- 260,273 ---- modelNode->accept(smoothingVisitor); } fprintf(stdout,"tc3DModel--Loaded 3D model: %s\n", model_name.c_str()); } + void tc3DModel::ProcessAnimations(std::vector<animationDBInfo>& animDBInfo) + { + AnimationProcessor animationProcessor(animationInfo, animDBInfo); + modelNode->accept(animationProcessor); + } + /** * Called once per instance to set and configure update callback. *************** *** 217,227 **** { wxASSERT(source->modelNode.valid()); // error if modelNode not loaded yet ! modelNode = source->modelNode; modelGroup = new osg::Group; modelTransform = new osg::MatrixTransform; modelTransform->setDataVariance(osg::Object::DYNAMIC); - - modelTransform->addChild(modelNode.get()); modelGroup->addChild(modelTransform.get()); --- 297,305 ---- { wxASSERT(source->modelNode.valid()); // error if modelNode not loaded yet ! modelGroup = new osg::Group; modelTransform = new osg::MatrixTransform; modelTransform->setDataVariance(osg::Object::DYNAMIC); modelGroup->addChild(modelTransform.get()); *************** *** 232,235 **** --- 310,320 ---- animationInfo.push_back(source->animationInfo[n]); } + + modelNode = dynamic_cast<osg::Node*>(source->modelNode->clone(ModelCopyOp(this))); + + modelTransform->addChild(modelNode.get()); + + + } |