[Gcblue-commits] gcb_wx/src/graphics tc3DModel.cpp,NONE,1.1 ObjectUpdater.cpp,1.2,1.3
Status: Alpha
Brought to you by:
ddcforge
From: <ddc...@us...> - 2004-01-12 03:01:39
|
Update of /cvsroot/gcblue/gcb_wx/src/graphics In directory sc8-pr-cvs1:/tmp/cvs-serv7013/src/graphics Modified Files: ObjectUpdater.cpp Added Files: tc3DModel.cpp Log Message: animation test code --- NEW FILE: tc3DModel.cpp --- /** @file tc3DModel.cpp */ /* ** Copyright (C) 2003 Dewitt "Cole" Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "stdwx.h" // precompiled header file #ifndef WX_PRECOMP #include "wx/wx.h" #ifdef WIN32 #include "wx/msw/private.h" // for MS Windows specific definitions #endif #endif #include "tc3DModel.h" #include "tcGameObject.h" #include "ObjectUpdater.h" #include <osg/MatrixTransform> #include <osgDB/ReadFile> #include <osgUtil/SmoothingVisitor> #ifdef _DEBUG #define new DEBUG_NEW #endif /** * A visitor for model prototypes to attach animation * bindings to the appropriate nodes. Each prototype * only needs to be processed once, immediately after * loading the model. The bindings are used when the * model is later cloned by SceneModel to attach * 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 if (node.getNumParents() == 1) { osg::BoundingSphere bs = node.getBound(); osg::Matrix untransMatrix; osg::Matrix transMatrix; untransMatrix.setTrans(-bs.center()); transMatrix.setTrans(bs.center()); osg::MatrixTransform *untranslateTransform = new osg::MatrixTransform(untransMatrix); osg::MatrixTransform *translateTransform = new osg::MatrixTransform(transMatrix); osg::MatrixTransform *rotateTransform = new osg::MatrixTransform; // temporarily hold node to avoid delete when removing from parent 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 { std::cerr << "animation node does not have 1 parent\n"; } } traverse(node); } }; /** * Adds child to modelTransform of this model. */ void tc3DModel::AddChild(tc3DModel *child) { modelTransform->addChild(child->GetNode().get()); } tc3DModel* tc3DModel::Clone() { tc3DModel *clonedModel = new tc3DModel(this); return clonedModel; } void tc3DModel::DetachFromParent() { modelGroup->getParent(0)->removeChild(modelGroup.get()); } osg::ref_ptr<osg::Node> tc3DModel::GetNode() { osg::ref_ptr<osg::Node> node = modelGroup.get(); return node; } unsigned int tc3DModel::GetNumParents() { wxASSERT(modelGroup.valid()); return modelGroup->getNumParents(); } void tc3DModel::Load(std::string model_name) { if (modelNode.valid()) return; // model already loaded std::cout << "Loading 3D model: " << model_name << " \n"; std::cerr << "Loading 3D model: " << model_name << " \n"; model_name = model_name + ".3ds"; modelNode = osgDB::readNodeFile(model_name.c_str()); if (!modelNode.valid()) { std::cout << "Load of 3D model: " << model_name << " failed.\n"; std::cerr << "Load of 3D model: " << model_name << " failed.\n"; return; } if (useSmoothing) { osgUtil::SmoothingVisitor smoothingVisitor; 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. */ void tc3DModel::SetupUpdate(tcGameObject *obj) { wxASSERT(modelTransform->getUpdateCallback() == NULL); gameObj = obj; modelTransform->setUpdateCallback(new ObjectUpdater(this)); } /** * This constructor is used for the source/factory, notionally * stored as part of the database object. * Skips initialization of members used in scene graph. */ tc3DModel::tc3DModel() { useSmoothing = true; } /** * SetupUpdate should be called after this to set update callback * for model. */ tc3DModel::tc3DModel(const tc3DModel* source) { 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()); animationInfo.clear(); size_t nAnimations = source->animationInfo.size(); for(size_t n=0;n<nAnimations;n++) { animationInfo.push_back(source->animationInfo[n]); } } tc3DModel::~tc3DModel() { } Index: ObjectUpdater.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/graphics/ObjectUpdater.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ObjectUpdater.cpp 29 Oct 2003 01:56:40 -0000 1.2 --- ObjectUpdater.cpp 12 Jan 2004 03:01:36 -0000 1.3 *************** *** 1,4 **** --- 1,5 ---- #include "ObjectUpdater.h" #include "tcGameObject.h" + #include "tc3DModel.h" #include "tc3DViewer.h" #include <osg/MatrixTransform> *************** *** 27,30 **** --- 28,41 ---- }; + void ObjectUpdater::UpdateAnimations() + { + 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)); + } + } void ObjectUpdater::operator()(Node* node, NodeVisitor* nv) *************** *** 35,38 **** --- 46,51 ---- float yawCorrection = 0; + UpdateAnimations(); + tcGameObject *gameObject = model->GetGameObj(); if (gameObject) { *************** *** 94,103 **** ObjectUpdater::ObjectUpdater() { ! gameObject = NULL; useRelativeChildPos = true; } ! ObjectUpdater::ObjectUpdater(tcGameObject *obj) : ! gameObject(obj) { useRelativeChildPos = true; --- 107,116 ---- ObjectUpdater::ObjectUpdater() { ! model = NULL; useRelativeChildPos = true; } ! ObjectUpdater::ObjectUpdater(tc3DModel *mod) : ! model(mod) { useRelativeChildPos = true; |