From 604580d75dbb225005b91841dc09c66785c0692c Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 7 May 2015 21:17:15 +0200 Subject: [PATCH] Move toMatrix to Nif::Node --- components/nif/niftypes.hpp | 13 ++++ components/nifbullet/bulletnifloader.cpp | 93 +++++++----------------- components/nifbullet/bulletnifloader.hpp | 4 +- components/nifosg/nifloader.cpp | 22 +----- 4 files changed, 44 insertions(+), 88 deletions(-) diff --git a/components/nif/niftypes.hpp b/components/nif/niftypes.hpp index 7fefb92a2..d95180145 100644 --- a/components/nif/niftypes.hpp +++ b/components/nif/niftypes.hpp @@ -25,6 +25,7 @@ #define OPENMW_COMPONENTS_NIF_NIFTYPES_HPP #include +#include // Common types used in NIF files @@ -49,6 +50,18 @@ struct Transformation Matrix3 rotation; // this can contain scale components too, including negative and nonuniform scales float scale; + osg::Matrixf toMatrix() const + { + osg::Matrixf transform; + transform.setTrans(pos); + + for (int i=0;i<3;++i) + for (int j=0;j<3;++j) + transform(j,i) = rotation.mValues[i][j] * scale; // NB column/row major difference + + return transform; + } + static const Transformation& getIdentity() { static const Transformation identity = { diff --git a/components/nifbullet/bulletnifloader.cpp b/components/nifbullet/bulletnifloader.cpp index 1d07bea26..0ddb6291c 100644 --- a/components/nifbullet/bulletnifloader.cpp +++ b/components/nifbullet/bulletnifloader.cpp @@ -41,55 +41,16 @@ http://www.gnu.org/licenses/ . // For warning messages #include -// Extract a list of keyframe-controlled nodes from a .kf file -// FIXME: this is a similar copy of OgreNifLoader::loadKf -void extractControlledNodes(Nif::NIFFilePtr kfFile, std::set& controlled) +namespace { - if(kfFile->numRoots() < 1) - { - kfFile->warn("Found no root nodes in "+kfFile->getFilename()+"."); - return; - } - const Nif::Record *r = kfFile->getRoot(0); - assert(r != NULL); - - if(r->recType != Nif::RC_NiSequenceStreamHelper) - { - kfFile->warn("First root was not a NiSequenceStreamHelper, but a "+ - r->recName+"."); - return; - } - const Nif::NiSequenceStreamHelper *seq = static_cast(r); - - Nif::ExtraPtr extra = seq->extra; - if(extra.empty() || extra->recType != Nif::RC_NiTextKeyExtraData) - { - kfFile->warn("First extra data was not a NiTextKeyExtraData, but a "+ - (extra.empty() ? std::string("nil") : extra->recName)+"."); - return; - } - - extra = extra->extra; - Nif::ControllerPtr ctrl = seq->controller; - for(;!extra.empty() && !ctrl.empty();(extra=extra->extra),(ctrl=ctrl->next)) - { - if(extra->recType != Nif::RC_NiStringExtraData || ctrl->recType != Nif::RC_NiKeyframeController) - { - kfFile->warn("Unexpected extra data "+extra->recName+" with controller "+ctrl->recName); - continue; - } - - if (!(ctrl->flags & Nif::NiNode::ControllerFlag_Active)) - continue; - - const Nif::NiStringExtraData *strdata = static_cast(extra.getPtr()); - const Nif::NiKeyframeController *key = static_cast(ctrl.getPtr()); +osg::Matrixf getWorldTransform(const Nif::Node *node) +{ + if(node->parent != NULL) + return node->trafo.toMatrix() * getWorldTransform(node->parent); + return node->trafo.toMatrix(); +} - if(key->data.empty()) - continue; - controlled.insert(strdata->string); - } } namespace NifBullet @@ -133,7 +94,7 @@ void ManualBulletShapeLoader::loadResource(Ogre::Resource *resource) if (Ogre::ResourceGroupManager::getSingleton().resourceExistsInAnyGroup(kfname)) { Nif::NIFFilePtr kf;// (Nif::Cache::getInstance().load(kfname)); - extractControlledNodes(kf, mControlledNodes); + //extractControlledNodes(kf, mControlledNodes); } Nif::Record *r = nif.getRoot(0); @@ -259,7 +220,7 @@ void ManualBulletShapeLoader::handleNode(const Nif::Node *node, int flags, } else if(node->recType == Nif::RC_NiTriShape) { - handleNiTriShape(static_cast(node), flags, Ogre::Matrix4()/*node->getWorldTransform()*/, isAnimated); + handleNiTriShape(static_cast(node), flags, getWorldTransform(node), isAnimated); } } @@ -276,7 +237,7 @@ void ManualBulletShapeLoader::handleNode(const Nif::Node *node, int flags, } } -void ManualBulletShapeLoader::handleNiTriShape(const Nif::NiTriShape *shape, int flags, const Ogre::Matrix4 &transform, bool isAnimated) +void ManualBulletShapeLoader::handleNiTriShape(const Nif::NiTriShape *shape, int flags, const osg::Matrixf &transform, bool isAnimated) { assert(shape != NULL); @@ -312,18 +273,18 @@ void ManualBulletShapeLoader::handleNiTriShape(const Nif::NiTriShape *shape, int childMesh->preallocateVertices(data->vertices.size()); childMesh->preallocateIndices(data->triangles.size()); - //const std::vector &vertices = data->vertices; - //const std::vector &triangles = data->triangles; + const std::vector &vertices = data->vertices; + const std::vector &triangles = data->triangles; for(size_t i = 0;i < data->triangles.size();i+=3) { - //Ogre::Vector3 b1 = vertices[triangles[i+0]]; - //Ogre::Vector3 b2 = vertices[triangles[i+1]]; - //Ogre::Vector3 b3 = vertices[triangles[i+2]]; - //childMesh->addTriangle(btVector3(b1.x,b1.y,b1.z),btVector3(b2.x,b2.y,b2.z),btVector3(b3.x,b3.y,b3.z)); + osg::Vec3f b1 = vertices[triangles[i+0]]; + osg::Vec3f b2 = vertices[triangles[i+1]]; + osg::Vec3f b3 = vertices[triangles[i+2]]; + childMesh->addTriangle(btVector3(b1.x(),b1.y(),b1.z()),btVector3(b2.x(),b2.y(),b2.z()),btVector3(b3.x(),b3.y(),b3.z())); } - //TriangleMeshShape* childShape = new TriangleMeshShape(childMesh,true); + TriangleMeshShape* childShape = new TriangleMeshShape(childMesh,true); float scale = shape->trafo.scale; const Nif::Node* parent = shape; @@ -332,15 +293,15 @@ void ManualBulletShapeLoader::handleNiTriShape(const Nif::NiTriShape *shape, int parent = parent->parent; scale *= parent->trafo.scale; } - //Ogre::Quaternion q = transform.extractQuaternion(); - //Ogre::Vector3 v = transform.getTrans(); - //childShape->setLocalScaling(btVector3(scale, scale, scale)); + osg::Quat q = transform.getRotate(); + osg::Vec3f v = transform.getTrans(); + childShape->setLocalScaling(btVector3(scale, scale, scale)); - //btTransform trans(btQuaternion(q.x, q.y, q.z, q.w), btVector3(v.x, v.y, v.z)); + btTransform trans(btQuaternion(q.x(), q.y(), q.z(), q.w()), btVector3(v.x(), v.y(), v.z())); - //mShape->mAnimatedShapes.insert(std::make_pair(shape->recIndex, mCompoundShape->getNumChildShapes())); + mShape->mAnimatedShapes.insert(std::make_pair(shape->recIndex, mCompoundShape->getNumChildShapes())); - //mCompoundShape->addChildShape(trans, childShape); + mCompoundShape->addChildShape(trans, childShape); } else { @@ -349,17 +310,15 @@ void ManualBulletShapeLoader::handleNiTriShape(const Nif::NiTriShape *shape, int // Static shape, just transform all vertices into position const Nif::NiTriShapeData *data = shape->data.getPtr(); - //const std::vector &vertices = data->vertices; - //const std::vector &triangles = data->triangles; + const std::vector &vertices = data->vertices; + const std::vector &triangles = data->triangles; for(size_t i = 0;i < data->triangles.size();i+=3) { - /* osg::Vec3f b1 = transform*vertices[triangles[i+0]]; osg::Vec3f b2 = transform*vertices[triangles[i+1]]; osg::Vec3f b3 = transform*vertices[triangles[i+2]]; - mStaticMesh->addTriangle(btVector3(b1.x,b1.y,b1.z),btVector3(b2.x,b2.y,b2.z),btVector3(b3.x,b3.y,b3.z)); - */ + mStaticMesh->addTriangle(btVector3(b1.x(),b1.y(),b1.z()),btVector3(b2.x(),b2.y(),b2.z()),btVector3(b3.x(),b3.y(),b3.z())); } } } diff --git a/components/nifbullet/bulletnifloader.hpp b/components/nifbullet/bulletnifloader.hpp index 3ffb6ac8f..733e9264b 100644 --- a/components/nifbullet/bulletnifloader.hpp +++ b/components/nifbullet/bulletnifloader.hpp @@ -32,6 +32,8 @@ #include #include +#include + // For warning messages #include @@ -117,7 +119,7 @@ private: /** *convert a NiTriShape to a bullet trishape. */ - void handleNiTriShape(const Nif::NiTriShape *shape, int flags, const Ogre::Matrix4 &transform, bool isAnimated); + void handleNiTriShape(const Nif::NiTriShape *shape, int flags, const osg::Matrixf& transform, bool isAnimated); std::string mResourceName; diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 936f266dd..adf7e94b4 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -49,24 +49,6 @@ namespace { - osg::Matrixf toMatrix(const Nif::Transformation& nifTrafo) - { - osg::Matrixf transform; - transform.setTrans(nifTrafo.pos); - - for (int i=0;i<3;++i) - for (int j=0;j<3;++j) - transform(j,i) = nifTrafo.rotation.mValues[i][j] * nifTrafo.scale; // NB column/row major difference - - return transform; - } - - osg::Matrixf getWorldTransform(const Nif::Node* node) - { - if(node->parent != NULL) - return toMatrix(node->trafo) * getWorldTransform(node->parent); - return toMatrix(node->trafo); - } void getAllNiNodes(const Nif::Node* node, std::vector& outIndices) { @@ -467,7 +449,7 @@ namespace NifOsg static osg::ref_ptr handleNode(const Nif::Node* nifNode, osg::Group* parentNode, Resource::TextureManager* textureManager, std::map boundTextures, int animflags, int particleflags, bool skipMeshes, TextKeyMap* textKeys, osg::Node* rootNode=NULL) { - osg::ref_ptr transformNode = new osg::MatrixTransform(toMatrix(nifNode->trafo)); + osg::ref_ptr transformNode = new osg::MatrixTransform(nifNode->trafo.toMatrix()); if (nifNode->recType == Nif::RC_NiBillboardNode) { @@ -1072,7 +1054,7 @@ namespace NifOsg std::pair indexWeight = std::make_pair(weights[j].vertex, weights[j].weight); influence.mWeights.insert(indexWeight); } - influence.mInvBindMatrix = toMatrix(data->bones[i].trafo); + influence.mInvBindMatrix = data->bones[i].trafo.toMatrix(); influence.mBoundSphere = osg::BoundingSpheref(data->bones[i].boundSphereCenter, data->bones[i].boundSphereRadius); map->mMap.insert(std::make_pair(boneName, influence));