Merge remote-tracking branch 'chris/animation2'

actorid
Marc Zinnschlag 12 years ago
commit 043e6c09fe

@ -43,16 +43,26 @@
#include <extern/shiny/Main/Factory.hpp> #include <extern/shiny/Main/Factory.hpp>
#include <components/nif/node.hpp>
#include <components/settings/settings.hpp> #include <components/settings/settings.hpp>
#include <components/nifoverrides/nifoverrides.hpp> #include <components/nifoverrides/nifoverrides.hpp>
typedef unsigned char ubyte; typedef unsigned char ubyte;
using namespace std; namespace std
using namespace Nif; {
using namespace NifOgre;
// These operators allow extra data types to be stored in an Ogre::Any
// object, which can then be stored in user object bindings on the nodes
// TODO: Do something useful
ostream& operator<<(ostream &o, const NifOgre::TextKeyMap&)
{ return o; }
}
namespace NifOgre
{
// Helper class that computes the bounding box and of a mesh // Helper class that computes the bounding box and of a mesh
class BoundsFinder class BoundsFinder
{ {
@ -62,7 +72,7 @@ class BoundsFinder
MaxMinFinder() MaxMinFinder()
{ {
min = numeric_limits<float>::infinity(); min = std::numeric_limits<float>::infinity();
max = -min; max = -min;
} }
@ -152,8 +162,9 @@ static void fail(const std::string &msg)
} }
static void insertTextKeys(const Nif::NiTextKeyExtraData *tk, TextKeyMap *textkeys) static TextKeyMap extractTextKeys(const Nif::NiTextKeyExtraData *tk)
{ {
TextKeyMap textkeys;
for(size_t i = 0;i < tk->list.size();i++) for(size_t i = 0;i < tk->list.size();i++)
{ {
const std::string &str = tk->list[i].text; const std::string &str = tk->list[i].text;
@ -166,11 +177,12 @@ static void insertTextKeys(const Nif::NiTextKeyExtraData *tk, TextKeyMap *textke
break; break;
std::string::size_type nextpos = std::min(str.find('\r', pos), str.find('\n', pos)); std::string::size_type nextpos = std::min(str.find('\r', pos), str.find('\n', pos));
textkeys->insert(std::make_pair(tk->list[i].time, str.substr(pos, nextpos-pos))); textkeys.insert(std::make_pair(tk->list[i].time, str.substr(pos, nextpos-pos)));
pos = nextpos; pos = nextpos;
} }
} }
return textkeys;
} }
@ -197,6 +209,17 @@ void buildBones(Ogre::Skeleton *skel, const Nif::Node *node, std::vector<Nif::Ni
ctrl = ctrl->next; ctrl = ctrl->next;
} }
Nif::ExtraPtr e = node->extra;
while(!e.empty())
{
if(e->recType == Nif::RC_NiTextKeyExtraData)
{
const Nif::NiTextKeyExtraData *tk = static_cast<const Nif::NiTextKeyExtraData*>(e.getPtr());
bone->getUserObjectBindings().setUserAny("TextKeyExtraData", Ogre::Any(extractTextKeys(tk)));
}
e = e->extra;
}
const Nif::NiNode *ninode = dynamic_cast<const Nif::NiNode*>(node); const Nif::NiNode *ninode = dynamic_cast<const Nif::NiNode*>(node);
if(ninode) if(ninode)
{ {
@ -219,7 +242,7 @@ struct KeyTimeSort
}; };
typedef std::map<std::string,NIFSkeletonLoader,ciLessBoost> LoaderMap; typedef std::map<std::string,NIFSkeletonLoader> LoaderMap;
static LoaderMap sLoaders; static LoaderMap sLoaders;
public: public:
@ -269,16 +292,16 @@ void loadResource(Ogre::Resource *resource)
Nif::NiKeyframeData *kf = kfc->data.getPtr(); Nif::NiKeyframeData *kf = kfc->data.getPtr();
/* Get the keyframes and make sure they're sorted first to last */ /* Get the keyframes and make sure they're sorted first to last */
QuaternionKeyList quatkeys = kf->mRotations; Nif::QuaternionKeyList quatkeys = kf->mRotations;
Vector3KeyList trankeys = kf->mTranslations; Nif::Vector3KeyList trankeys = kf->mTranslations;
FloatKeyList scalekeys = kf->mScales; Nif::FloatKeyList scalekeys = kf->mScales;
std::sort(quatkeys.mKeys.begin(), quatkeys.mKeys.end(), KeyTimeSort<Ogre::Quaternion>()); std::sort(quatkeys.mKeys.begin(), quatkeys.mKeys.end(), KeyTimeSort<Ogre::Quaternion>());
std::sort(trankeys.mKeys.begin(), trankeys.mKeys.end(), KeyTimeSort<Ogre::Vector3>()); std::sort(trankeys.mKeys.begin(), trankeys.mKeys.end(), KeyTimeSort<Ogre::Vector3>());
std::sort(scalekeys.mKeys.begin(), scalekeys.mKeys.end(), KeyTimeSort<float>()); std::sort(scalekeys.mKeys.begin(), scalekeys.mKeys.end(), KeyTimeSort<float>());
QuaternionKeyList::VecType::const_iterator quatiter = quatkeys.mKeys.begin(); Nif::QuaternionKeyList::VecType::const_iterator quatiter = quatkeys.mKeys.begin();
Vector3KeyList::VecType::const_iterator traniter = trankeys.mKeys.begin(); Nif::Vector3KeyList::VecType::const_iterator traniter = trankeys.mKeys.begin();
FloatKeyList::VecType::const_iterator scaleiter = scalekeys.mKeys.begin(); Nif::FloatKeyList::VecType::const_iterator scaleiter = scalekeys.mKeys.begin();
Ogre::Bone *bone = skel->getBone(targets[i]); Ogre::Bone *bone = skel->getBone(targets[i]);
const Ogre::Quaternion startquat = bone->getInitialOrientation(); const Ogre::Quaternion startquat = bone->getInitialOrientation();
@ -346,7 +369,7 @@ void loadResource(Ogre::Resource *resource)
kframe->setRotation(curquat); kframe->setRotation(curquat);
else else
{ {
QuaternionKeyList::VecType::const_iterator last = quatiter-1; Nif::QuaternionKeyList::VecType::const_iterator last = quatiter-1;
float diff = (curtime-last->mTime) / (quatiter->mTime-last->mTime); float diff = (curtime-last->mTime) / (quatiter->mTime-last->mTime);
kframe->setRotation(Ogre::Quaternion::nlerp(diff, lastquat, curquat)); kframe->setRotation(Ogre::Quaternion::nlerp(diff, lastquat, curquat));
} }
@ -354,7 +377,7 @@ void loadResource(Ogre::Resource *resource)
kframe->setTranslate(curtrans); kframe->setTranslate(curtrans);
else else
{ {
Vector3KeyList::VecType::const_iterator last = traniter-1; Nif::Vector3KeyList::VecType::const_iterator last = traniter-1;
float diff = (curtime-last->mTime) / (traniter->mTime-last->mTime); float diff = (curtime-last->mTime) / (traniter->mTime-last->mTime);
kframe->setTranslate(lasttrans + ((curtrans-lasttrans)*diff)); kframe->setTranslate(lasttrans + ((curtrans-lasttrans)*diff));
} }
@ -362,7 +385,7 @@ void loadResource(Ogre::Resource *resource)
kframe->setScale(curscale); kframe->setScale(curscale);
else else
{ {
FloatKeyList::VecType::const_iterator last = scaleiter-1; Nif::FloatKeyList::VecType::const_iterator last = scaleiter-1;
float diff = (curtime-last->mTime) / (scaleiter->mTime-last->mTime); float diff = (curtime-last->mTime) / (scaleiter->mTime-last->mTime);
kframe->setScale(lastscale + ((curscale-lastscale)*diff)); kframe->setScale(lastscale + ((curscale-lastscale)*diff));
} }
@ -371,34 +394,17 @@ void loadResource(Ogre::Resource *resource)
anim->optimise(); anim->optimise();
} }
bool createSkeleton(const std::string &name, const std::string &group, TextKeyMap *textkeys, const Nif::Node *node) bool createSkeleton(const std::string &name, const std::string &group, const Nif::Node *node)
{
if(textkeys)
{
Nif::ExtraPtr e = node->extra;
while(!e.empty())
{ {
if(e->recType == Nif::RC_NiTextKeyExtraData)
{
const Nif::NiTextKeyExtraData *tk = static_cast<const Nif::NiTextKeyExtraData*>(e.getPtr());
insertTextKeys(tk, textkeys);
}
e = e->extra;
}
}
if(node->boneTrafo != NULL) if(node->boneTrafo != NULL)
{ {
Ogre::SkeletonManager &skelMgr = Ogre::SkeletonManager::getSingleton(); Ogre::SkeletonManager &skelMgr = Ogre::SkeletonManager::getSingleton();
Ogre::SkeletonPtr skel = skelMgr.getByName(name); Ogre::SkeletonPtr skel = skelMgr.getByName(name);
if(skel.isNull()) if(skel.isNull())
{ {
NIFSkeletonLoader *loader = &sLoaders[name]; NIFSkeletonLoader *loader = &sLoaders[name];
skel = skelMgr.create(name, group, true, loader); skel = skelMgr.create(name, group, true, loader);
} }
if(!textkeys || textkeys->size() > 0)
return true; return true;
} }
@ -410,7 +416,7 @@ bool createSkeleton(const std::string &name, const std::string &group, TextKeyMa
{ {
if(!children[i].empty()) if(!children[i].empty())
{ {
if(createSkeleton(name, group, textkeys, children[i].getPtr())) if(createSkeleton(name, group, children[i].getPtr()))
return true; return true;
} }
} }
@ -484,7 +490,7 @@ static void fail(const std::string &msg)
public: public:
static Ogre::String getMaterial(const NiTriShape *shape, const Ogre::String &name, const Ogre::String &group) static Ogre::String getMaterial(const Nif::NiTriShape *shape, const Ogre::String &name, const Ogre::String &group)
{ {
Ogre::MaterialManager &matMgr = Ogre::MaterialManager::getSingleton(); Ogre::MaterialManager &matMgr = Ogre::MaterialManager::getSingleton();
Ogre::MaterialPtr material = matMgr.getByName(name); Ogre::MaterialPtr material = matMgr.getByName(name);
@ -504,24 +510,24 @@ static Ogre::String getMaterial(const NiTriShape *shape, const Ogre::String &nam
bool vertexColour = (shape->data->colors.size() != 0); bool vertexColour = (shape->data->colors.size() != 0);
// These are set below if present // These are set below if present
const NiTexturingProperty *t = NULL; const Nif::NiTexturingProperty *t = NULL;
const NiMaterialProperty *m = NULL; const Nif::NiMaterialProperty *m = NULL;
const NiAlphaProperty *a = NULL; const Nif::NiAlphaProperty *a = NULL;
// Scan the property list for material information // Scan the property list for material information
const PropertyList &list = shape->props; const Nif::PropertyList &list = shape->props;
for (size_t i = 0;i < list.length();i++) for (size_t i = 0;i < list.length();i++)
{ {
// Entries may be empty // Entries may be empty
if (list[i].empty()) continue; if (list[i].empty()) continue;
const Property *pr = list[i].getPtr(); const Nif::Property *pr = list[i].getPtr();
if (pr->recType == RC_NiTexturingProperty) if (pr->recType == Nif::RC_NiTexturingProperty)
t = static_cast<const NiTexturingProperty*>(pr); t = static_cast<const Nif::NiTexturingProperty*>(pr);
else if (pr->recType == RC_NiMaterialProperty) else if (pr->recType == Nif::RC_NiMaterialProperty)
m = static_cast<const NiMaterialProperty*>(pr); m = static_cast<const Nif::NiMaterialProperty*>(pr);
else if (pr->recType == RC_NiAlphaProperty) else if (pr->recType == Nif::RC_NiAlphaProperty)
a = static_cast<const NiAlphaProperty*>(pr); a = static_cast<const Nif::NiAlphaProperty*>(pr);
else else
warn("Skipped property type: "+pr->recName); warn("Skipped property type: "+pr->recName);
} }
@ -529,21 +535,18 @@ static Ogre::String getMaterial(const NiTriShape *shape, const Ogre::String &nam
// Texture // Texture
if (t && t->textures[0].inUse) if (t && t->textures[0].inUse)
{ {
NiSourceTexture *st = t->textures[0].texture.getPtr(); Nif::NiSourceTexture *st = t->textures[0].texture.getPtr();
if (st->external) if (st->external)
{ {
/* Bethesda at some at some point converted all their BSA /* Bethesda at some at some point converted all their BSA
* textures from tga to dds for increased load speed, but all * textures from tga to dds for increased load speed, but all
* texture file name references were kept as .tga. * texture file name references were kept as .tga.
*/ */
static const char path[] = "textures\\";
static const char * path = "textures\\";
texName = path + st->filename; texName = path + st->filename;
Ogre::String::size_type pos = texName.rfind('.'); Ogre::String::size_type pos = texName.rfind('.');
if(pos != Ogre::String::npos && texName.compare(pos, texName.length() - pos, ".dds") != 0)
if (texName.compare (pos, texName.size () - pos, ".dds") != 0)
{ {
// since we know all (GOTY edition or less) textures end // since we know all (GOTY edition or less) textures end
// in .dds, we change the extension // in .dds, we change the extension
@ -930,7 +933,7 @@ class NIFMeshLoader : Ogre::ManualResourceLoader
} }
typedef std::map<std::string,NIFMeshLoader,ciLessBoost> LoaderMap; typedef std::map<std::string,NIFMeshLoader> LoaderMap;
static LoaderMap sLoaders; static LoaderMap sLoaders;
public: public:
@ -993,7 +996,7 @@ public:
if(node->recType == Nif::RC_NiTriShape) if(node->recType == Nif::RC_NiTriShape)
{ {
const NiTriShape *shape = dynamic_cast<const NiTriShape*>(node); const Nif::NiTriShape *shape = dynamic_cast<const Nif::NiTriShape*>(node);
Ogre::MeshManager &meshMgr = Ogre::MeshManager::getSingleton(); Ogre::MeshManager &meshMgr = Ogre::MeshManager::getSingleton();
std::string fullname = mName+"@shape="+shape->name; std::string fullname = mName+"@shape="+shape->name;
@ -1016,7 +1019,7 @@ public:
mesh->setAutoBuildEdgeLists(false); mesh->setAutoBuildEdgeLists(false);
} }
meshes.push_back(std::make_pair(mesh, shape->name)); meshes.push_back(std::make_pair(mesh->getName(), shape->name));
} }
else if(node->recType != Nif::RC_NiNode && node->recType != Nif::RC_RootCollisionNode && else if(node->recType != Nif::RC_NiNode && node->recType != Nif::RC_RootCollisionNode &&
node->recType != Nif::RC_NiRotatingParticles) node->recType != Nif::RC_NiRotatingParticles)
@ -1037,13 +1040,19 @@ public:
NIFMeshLoader::LoaderMap NIFMeshLoader::sLoaders; NIFMeshLoader::LoaderMap NIFMeshLoader::sLoaders;
MeshPairList NIFLoader::load(std::string name, std::string skelName, TextKeyMap *textkeys, const std::string &group) typedef std::map<std::string,MeshPairList> MeshPairMap;
{ static MeshPairMap sMeshPairMap;
MeshPairList meshes;
MeshPairList NIFLoader::load(std::string name, std::string skelName, const std::string &group)
{
std::transform(name.begin(), name.end(), name.begin(), ::tolower); std::transform(name.begin(), name.end(), name.begin(), ::tolower);
std::transform(skelName.begin(), skelName.end(), skelName.begin(), ::tolower); std::transform(skelName.begin(), skelName.end(), skelName.begin(), ::tolower);
MeshPairMap::const_iterator meshiter = sMeshPairMap.find(name+"@skel="+skelName);
if(meshiter != sMeshPairMap.end())
return meshiter->second;
MeshPairList &meshes = sMeshPairMap[name+"@skel="+skelName];
Nif::NIFFile nif(name); Nif::NIFFile nif(name);
if (nif.numRecords() < 1) if (nif.numRecords() < 1)
{ {
@ -1064,7 +1073,7 @@ MeshPairList NIFLoader::load(std::string name, std::string skelName, TextKeyMap
} }
NIFSkeletonLoader skelldr; NIFSkeletonLoader skelldr;
bool hasSkel = skelldr.createSkeleton(skelName, group, textkeys, node); bool hasSkel = skelldr.createSkeleton(name, group, node);
NIFMeshLoader meshldr(name, group, (hasSkel ? skelName : std::string())); NIFMeshLoader meshldr(name, group, (hasSkel ? skelName : std::string()));
meshldr.createMeshes(node, meshes); meshldr.createMeshes(node, meshes);
@ -1076,19 +1085,37 @@ EntityList NIFLoader::createEntities(Ogre::SceneNode *parent, TextKeyMap *textke
{ {
EntityList entitylist; EntityList entitylist;
MeshPairList meshes = load(name, name, textkeys, group); MeshPairList meshes = load(name, name, group);
if(meshes.size() == 0) if(meshes.size() == 0)
return entitylist; return entitylist;
Ogre::SceneManager *sceneMgr = parent->getCreator(); Ogre::SceneManager *sceneMgr = parent->getCreator();
for(size_t i = 0;i < meshes.size();i++) for(size_t i = 0;i < meshes.size();i++)
{ {
entitylist.mEntities.push_back(sceneMgr->createEntity(meshes[i].first->getName())); entitylist.mEntities.push_back(sceneMgr->createEntity(meshes[i].first));
Ogre::Entity *entity = entitylist.mEntities.back(); Ogre::Entity *entity = entitylist.mEntities.back();
if(!entitylist.mSkelBase && entity->hasSkeleton()) if(!entitylist.mSkelBase && entity->hasSkeleton())
entitylist.mSkelBase = entity; entitylist.mSkelBase = entity;
} }
if(entitylist.mSkelBase && textkeys)
{
// Would be nice if Ogre::SkeletonInstance allowed access to the 'master' Ogre::SkeletonPtr.
Ogre::SkeletonManager &skelMgr = Ogre::SkeletonManager::getSingleton();
Ogre::SkeletonPtr skel = skelMgr.getByName(entitylist.mSkelBase->getSkeleton()->getName());
Ogre::Skeleton::BoneIterator iter = skel->getBoneIterator();
while(iter.hasMoreElements())
{
Ogre::Bone *bone = iter.getNext();
const Ogre::Any &data = bone->getUserObjectBindings().getUserAny("TextKeyExtraData");
if(!data.isEmpty())
{
*textkeys = Ogre::any_cast<TextKeyMap>(data);
break;
}
}
}
if(entitylist.mSkelBase) if(entitylist.mSkelBase)
{ {
parent->attachObject(entitylist.mSkelBase); parent->attachObject(entitylist.mSkelBase);
@ -1113,13 +1140,6 @@ EntityList NIFLoader::createEntities(Ogre::SceneNode *parent, TextKeyMap *textke
return entitylist; return entitylist;
} }
struct checklow {
bool operator()(const char &a, const char &b) const
{
return ::tolower(a) == ::tolower(b);
}
};
EntityList NIFLoader::createEntities(Ogre::Entity *parent, const std::string &bonename, EntityList NIFLoader::createEntities(Ogre::Entity *parent, const std::string &bonename,
Ogre::SceneNode *parentNode, Ogre::SceneNode *parentNode,
const std::string &name, const std::string &name,
@ -1127,23 +1147,24 @@ EntityList NIFLoader::createEntities(Ogre::Entity *parent, const std::string &bo
{ {
EntityList entitylist; EntityList entitylist;
MeshPairList meshes = load(name, parent->getMesh()->getSkeletonName(), NULL, group); MeshPairList meshes = load(name, parent->getMesh()->getSkeletonName(), group);
if(meshes.size() == 0) if(meshes.size() == 0)
return entitylist; return entitylist;
Ogre::SceneManager *sceneMgr = parentNode->getCreator(); Ogre::SceneManager *sceneMgr = parentNode->getCreator();
std::string filter = "Tri "+bonename; std::string filter = "tri "+bonename;
std::transform(filter.begin()+4, filter.end(), filter.begin()+4, ::tolower);
for(size_t i = 0;i < meshes.size();i++) for(size_t i = 0;i < meshes.size();i++)
{ {
Ogre::Entity *ent = sceneMgr->createEntity(meshes[i].first->getName()); Ogre::Entity *ent = sceneMgr->createEntity(meshes[i].first);
if(ent->hasSkeleton()) if(ent->hasSkeleton())
{ {
std::transform(meshes[i].second.begin(), meshes[i].second.end(), meshes[i].second.begin(), ::tolower);
if(meshes[i].second.length() < filter.length() || if(meshes[i].second.length() < filter.length() ||
std::mismatch(filter.begin(), filter.end(), meshes[i].second.begin(), checklow()).first != filter.end()) meshes[i].second.compare(0, filter.length(), filter) != 0)
{ {
sceneMgr->destroyEntity(ent); sceneMgr->destroyEntity(ent);
meshes.erase(meshes.begin()+i);
i--;
continue; continue;
} }
if(!entitylist.mSkelBase) if(!entitylist.mSkelBase)
@ -1228,3 +1249,5 @@ extern "C" void ogre_insertTexture(char* name, uint32_t width, uint32_t height,
*/ */
} // nsmaepace NifOgre

@ -30,23 +30,6 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <cassert>
#include <boost/algorithm/string.hpp>
#include "../nif/node.hpp"
#include <libs/platform/strings.h>
class BoundsFinder;
struct ciLessBoost : std::binary_function<std::string, std::string, bool>
{
bool operator() (const std::string & s1, const std::string & s2) const
{
//case insensitive version of is_less
return boost::algorithm::lexicographical_compare(s1, s2, boost::algorithm::is_iless());
}
};
namespace Nif namespace Nif
{ {
@ -69,9 +52,8 @@ struct EntityList {
}; };
/** This holds a list of meshes along with the names of their parent nodes /** This holds a list of mesh names along with the names of their parent nodes */
*/ typedef std::vector< std::pair<std::string,std::string> > MeshPairList;
typedef std::vector< std::pair<Ogre::MeshPtr,std::string> > MeshPairList;
/** Manual resource loader for NIF meshes. This is the main class /** Manual resource loader for NIF meshes. This is the main class
responsible for translating the internal NIF mesh structure into responsible for translating the internal NIF mesh structure into
@ -87,7 +69,7 @@ typedef std::vector< std::pair<Ogre::MeshPtr,std::string> > MeshPairList;
*/ */
class NIFLoader class NIFLoader
{ {
static MeshPairList load(std::string name, std::string skelName, TextKeyMap *textkeys, const std::string &group); static MeshPairList load(std::string name, std::string skelName, const std::string &group);
public: public:
static EntityList createEntities(Ogre::Entity *parent, const std::string &bonename, static EntityList createEntities(Ogre::Entity *parent, const std::string &bonename,

Loading…
Cancel
Save