Hi Robert,
I used the osganimation branch and made 1 code change and a few change
to the cmake find module. Please find them attached. I tried out fbx sdk
versions 2016.1.2 and 2018.1.1 and the osg builds fine with both.
I will try out our application later.
Cheers
Raymond
/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <[email protected]>
* Copyright (C) 2017 Julien Valentin <[email protected]>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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
* OpenSceneGraph Public License for more details.
*/
#include <osgAnimation/VertexInfluence>
#include <osgAnimation/RigGeometry>
#include <osgAnimation/BoneMapVisitor>
#include <osg/Notify>
#include <iostream>
#include <algorithm>
#include <set>
using namespace osgAnimation;
struct invweight_ordered
{
inline bool operator() (const BoneWeight& bw1, const BoneWeight& bw2) const
{
if (bw1.second > bw2.second)return true;
if (bw1.second < bw2.second)return false;
return(bw1.first < bw2.first);
}
};
void VertexInfluenceMap::normalize(unsigned int numvert)
{
typedef std::pair<float, std::vector<float*> > PerVertWeights;
std::vector<PerVertWeights > localstore;
localstore.resize(numvert);
for(VertexInfluenceMap::iterator mapit = this->begin(); mapit !=
this->end(); ++mapit)
{
IndexWeightList &curvecinf = mapit->second;
for(IndexWeightList::iterator curinf = curvecinf.begin(); curinf !=
curvecinf.end(); ++curinf)
{
VertexIndexWeight& inf = *curinf;
localstore[inf.first].first += inf.second;
localstore[inf.first].second.push_back(&inf.second);
}
}
unsigned int vertid = 0;
for(std::vector<PerVertWeights >::iterator itvert = localstore.begin();
itvert != localstore.end();
++itvert, ++vertid)
{
PerVertWeights & weights = *itvert;
if(weights.first< 1e-4)
{
OSG_WARN << "VertexInfluenceMap::normalize warning the vertex "
<<vertid << " seems to have 0 weight, skip normalize for this vertex" <<
std::endl;
}
else
{
float mult = 1.0/weights.first;
for (std::vector<float*>::iterator itf = weights.second.begin();
itf != weights.second.end(); ++itf)
**itf *= mult;
}
}
}
///remove weakest influences in order to fit targetted numbonepervertex
void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int
numbonepervertex,float minweight, bool renormalize)
{
typedef std::set<BoneWeight, invweight_ordered > BoneWeightOrdered;
std::map<int, BoneWeightOrdered > tempVec2Bones;
for(VertexInfluenceMap::iterator mapit = this->begin(); mapit !=
this->end(); ++mapit)
{
const std::string& bonename = mapit->first;
IndexWeightList &curvecinf = mapit->second;
for(IndexWeightList::iterator curinf = curvecinf.begin(); curinf !=
curvecinf.end(); ++curinf)
{
VertexIndexWeight& inf = *curinf;
if( bonename.empty())
{
OSG_WARN << "VertexInfluenceSet::cullInfluenceCountPerVertex
warning vertex " << inf.first << " is not assigned to a bone" << std::endl;
}
else
if(inf.second>minweight)tempVec2Bones[inf.first].insert(BoneWeight(bonename,
inf.second));
}
}
this->clear();
for( std::map<int,BoneWeightOrdered >::iterator mapit =
tempVec2Bones.begin(); mapit != tempVec2Bones.end(); ++mapit)
{
BoneWeightOrdered& bwset = mapit->second;
unsigned int newsize =
numbonepervertex<bwset.size()?numbonepervertex:bwset.size();
float sum = 0.0f;
while(bwset.size()>newsize)bwset.erase(*bwset.rbegin());
if(renormalize)
{
for(BoneWeightOrdered::iterator bwit = bwset.begin(); bwit !=
bwset.end(); ++bwit)
sum += bwit->second;
if(sum > 1e-4)
{
sum = 1.0f/sum;
for(BoneWeightOrdered::iterator bwit = bwset.begin(); bwit !=
bwset.end(); ++bwit)
{
VertexInfluence & inf = (*this)[bwit->first];
inf.push_back(VertexIndexWeight(mapit->first,
bwit->second*sum));
inf.setName(bwit->first);
}
}
}
else
{
for(BoneWeightOrdered::iterator bwit = bwset.begin(); bwit !=
bwset.end(); ++bwit)
{
VertexInfluence & inf = (*this)[bwit->first];
inf.push_back(VertexIndexWeight(mapit->first,bwit->second));
inf.setName(bwit->first);
}
}
}
}
void
VertexInfluenceMap::computePerVertexInfluenceList(std::vector<BoneWeightList>&
vertex2Bones,unsigned int numvert)const
{
vertex2Bones.resize(numvert);
for (osgAnimation::VertexInfluenceMap::const_iterator it = begin(); it !=
end(); ++it)
{
const IndexWeightList& inflist = it->second;
if (it->first.empty())
{
OSG_WARN << "VertexInfluenceMap::computePerVertexInfluenceList
contains unamed bone IndexWeightList" << std::endl;
}
for(IndexWeightList::const_iterator infit = inflist.begin(); infit !=
inflist.end(); ++infit)
{
const VertexIndexWeight &iw = *infit;
const unsigned int &index = iw.first;
const float &weight = iw.second;
vertex2Bones[index].push_back(BoneWeight(it->first, weight));;
}
}
}
// sort by name and weight
struct SortByNameAndWeight : public std::less<BoneWeight>
{
bool operator()(const BoneWeight& b0,
const BoneWeight& b1) const
{
if (b0.first < b1.first)
return true;
else if (b0.first > b1.first)
return false;
return (b0.second < b1.second);
}
};
struct SortByBoneWeightList : public std::less<BoneWeightList>
{
bool operator()(const BoneWeightList& b0,
const BoneWeightList& b1) const
{
if (b0.size() < b1.size())
return true;
else if (b0.size() > b1.size())
return false;
int size = b0.size();
for (int i = 0; i < size; i++)
{
if (SortByNameAndWeight()(b0[i], b1[i]))
return true;
else if (SortByNameAndWeight()(b1[i], b0[i]))
return false;
}
return false;
}
};
void
VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>&
uniqVertexGroupList, unsigned int numvert) const
{
uniqVertexGroupList.clear();
std::vector<BoneWeightList> vertex2Bones;
computePerVertexInfluenceList(vertex2Bones,numvert);
typedef std::map<BoneWeightList,VertexGroup, SortByBoneWeightList>
UnifyBoneGroup;
UnifyBoneGroup unifyBuffer;
unsigned int vertexID = 0;
for (std::vector<BoneWeightList>::iterator it = vertex2Bones.begin(); it !=
vertex2Bones.end(); ++it,++vertexID)
{
BoneWeightList &boneweightlist = *it;
// sort the vector to have a consistent key
std::sort(boneweightlist.begin(), boneweightlist.end(),
SortByNameAndWeight());
// we use the vector<BoneWeight> as key to differentiate group
UnifyBoneGroup::iterator result = unifyBuffer.find(boneweightlist);
if (result == unifyBuffer.end())
unifyBuffer[boneweightlist].setBoneWeights(boneweightlist);
unifyBuffer[boneweightlist].vertIDs().push_back(vertexID);
}
if(vertex2Bones.size() == unifyBuffer.size())
{
OSG_WARN << "VertexInfluenceMap::computeMinimalVertexGroupList is
useless no duplicate VertexGroup" << std::endl;
}
uniqVertexGroupList.reserve(unifyBuffer.size());
for (UnifyBoneGroup::iterator it = unifyBuffer.begin(); it !=
unifyBuffer.end(); ++it)
{
uniqVertexGroupList.push_back(it->second);
}
}
//Experimental Bone removal stuff
typedef std::vector<osgAnimation::RigGeometry*> RigList;
class CollectRigVisitor : public osg::NodeVisitor
{
public:
META_NodeVisitor(osgAnimation, CollectRigVisitor)
CollectRigVisitor();
void apply(osg::Geometry& node);
inline const RigList& getRigList() const{return _map;}
protected:
RigList _map;
};
CollectRigVisitor::CollectRigVisitor() :
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
void CollectRigVisitor::apply(osg::Geometry& node)
{
RigGeometry* rig = dynamic_cast<RigGeometry*>(&node);
if ( rig )
_map.push_back(rig);
}
bool recursiveisUsefull( Bone* bone, std::set<std::string> foundnames) {
for(unsigned int i=0; i<bone->getNumChildren(); ++i) {
Bone* child = dynamic_cast< Bone* >(bone->getChild(i));
if(child){
if( foundnames.find(child->getName()) != foundnames.end() )
return true;
if( recursiveisUsefull(child,foundnames) )
return true;
}
}
return false;
}
void VertexInfluenceMap::removeUnexpressedBones(Skeleton &skel) const
{
BoneMapVisitor mapVisitor;
skel.accept(mapVisitor);
CollectRigVisitor rigvis;
skel.accept(rigvis);
RigList rigs = rigvis.getRigList();
BoneMap boneMap = mapVisitor.getBoneMap();
unsigned int removed=0;
Bone* child, *par;
std::set<std::string> usebones;
for(RigList::iterator rigit = rigs.begin(); rigit != rigs.end(); ++rigit) {
for(VertexInfluenceMap::iterator mapit =
(*rigit)->getInfluenceMap()->begin();
mapit != (*rigit)->getInfluenceMap()->end();
++mapit) {
usebones.insert((*mapit).first);
}
}
for(BoneMap::iterator bmit = boneMap.begin(); bmit != boneMap.end();) {
if(usebones.find(bmit->second->getName()) == usebones.end()) {
if( !(par = bmit->second->getBoneParent()) )
{
++bmit;
continue;
}
Bone * bone2rm = bmit->second.get();
if( recursiveisUsefull(bone2rm,usebones)) {
++bmit;
continue;
}
///Bone can be removed
++ removed;
OSG_INFO<<"removing useless bone: "<< bone2rm->getName()
<<std::endl;
osg::NodeList nodes;
for(unsigned int numchild = 0; numchild <
bone2rm->getNumChildren(); numchild++)
{
if( (child = dynamic_cast<Bone*>(bone2rm->getChild(numchild))) )
{
if(par!=child &&child!=bone2rm) {
par->addChild(child);
nodes.push_back(child);
}
}
}
for(unsigned int i=0; i<nodes.size(); ++i)
bone2rm->removeChild(nodes[i]);
par->removeChild(bone2rm);
///rebuild bonemap after bone removal
BoneMapVisitor mapVis ;
skel.accept(mapVis);
boneMap = mapVis.getBoneMap();
bmit = boneMap.begin();
}
else ++bmit;
}
OSG_WARN<<"Number of bone removed "<<removed<<std::endl;
}
# Locate FBX
# This module defines:
# FBX_INCLUDE_DIR, where to find the headers
#
# FBX_LIBRARY, FBX_LIBRARY_DEBUG
# FBX_FOUND
#
# $FBX_DIR is an environment variable that would
# correspond to the ./configure --prefix=$FBX_DIR
IF(APPLE)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# using regular Clang or AppleClang
SET(FBX_LIBDIR "clang")
else()
SET(FBX_LIBDIR "gcc4/ub")
endif()
ELSEIF(CMAKE_COMPILER_IS_GNUCXX)
SET(FBX_LIBDIR "gcc4")
ELSEIF(MSVC80)
SET(FBX_LIBDIR "vs2005")
ELSEIF(MSVC90)
SET(FBX_LIBDIR "vs2008")
ELSEIF(MSVC10)
SET(FBX_LIBDIR "vs2010")
ELSEIF(MSVC11)
SET(FBX_LIBDIR "vs2012")
ELSEIF(MSVC12 OR MSVC_VERSION==1800)
SET(FBX_LIBDIR "vs2013")
ELSEIF(MSVC14 OR MSVC_VERSION>1900)
SET(FBX_LIBDIR "vs2015")
ENDIF()
IF(APPLE)
# do nothing
ELSEIF(CMAKE_CL_64)
SET(FBX_LIBDIR ${FBX_LIBDIR}/x64)
ELSEIF(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_SIZEOF_VOID_P EQUAL 8)
SET(FBX_LIBDIR ${FBX_LIBDIR}/x64)
ELSE()
SET(FBX_LIBDIR ${FBX_LIBDIR}/x86)
ENDIF()
#try to use 2015.1 or 2014.2 version
IF(APPLE)
SET(FBX_LIBNAME "libfbxsdk")
ELSEIF(CMAKE_COMPILER_IS_GNUCXX)
SET(FBX_LIBNAME "fbxsdk")
ELSE()
OPTION(FBX_SHARED OFF)
IF(FBX_SHARED)
SET(FBX_LIBNAME "libfbxsdk")
ELSE()
SET(FBX_LIBNAME "libfbxsdk-md")
ENDIF()
ENDIF()
SET(FBX_LIBNAME_DEBUG ${FBX_LIBNAME}d)
SET( FBX_SEARCH_PATHS
$ENV{FBX_DIR}
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2018.1.1"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2018.1.1"
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2017.1"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2017.1"
"/Applications/Autodesk/FBX SDK/2017.1"
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2016.1.2"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2016.1.2"
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2016.1.1"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2016.1.1"
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2015.1"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2015.1"
"/Applications/Autodesk/FBX/FBX SDK/2015.1"
/Applications/Autodesk/FBXSDK20151
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2014.2"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2014.2"
"/Applications/Autodesk/FBX/FBX SDK/2014.2"
/Applications/Autodesk/FBXSDK20142
)
# search for headers & debug/release libraries
FIND_PATH(FBX_INCLUDE_DIR "fbxsdk.h"
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "include")
FIND_LIBRARY( FBX_LIBRARY ${FBX_LIBNAME}
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "lib/${FBX_LIBDIR}/release" "lib/${FBX_LIBDIR}")
#Once one of the calls succeeds the result variable will be set and stored in
the cache so that no call will search again.
#no debug d suffix, search in debug folder only
FIND_LIBRARY( FBX_LIBRARY_DEBUG ${FBX_LIBNAME}
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "lib/${FBX_LIBDIR}/debug")
FIND_LIBRARY( FBX_LIBRARY_DEBUG ${FBX_LIBNAME_DEBUG}
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "lib/${FBX_LIBDIR}")
IF(FBX_LIBRARY AND FBX_LIBRARY_DEBUG AND FBX_INCLUDE_DIR)
SET(FBX_FOUND "YES")
ELSE()
SET(FBX_FOUND "NO")
ENDIF()
IF(NOT FBX_FOUND)
#try to use 2014.1 version
IF(APPLE)
SET(FBX_LIBNAME "fbxsdk-2014.1")
ELSEIF(CMAKE_COMPILER_IS_GNUCXX)
SET(FBX_LIBNAME "fbxsdk-2014.1")
ELSE()
SET(FBX_LIBNAME "fbxsdk-2014.1")
ENDIF()
SET(FBX_LIBNAME_DEBUG ${FBX_LIBNAME}d)
SET( FBX_SEARCH_PATHS
$ENV{FBX_DIR}
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2014.1"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2014.1"
"/Applications/Autodesk/FBX/FBX SDK/2014.1"
/Applications/Autodesk/FBXSDK20141
)
# search for headers & debug/release libraries
FIND_PATH(FBX_INCLUDE_DIR "fbxsdk.h"
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "include")
FIND_LIBRARY( FBX_LIBRARY ${FBX_LIBNAME}
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "lib/${FBX_LIBDIR}")
FIND_LIBRARY( FBX_LIBRARY_DEBUG ${FBX_LIBNAME_DEBUG}
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "lib/${FBX_LIBDIR}")
IF(FBX_LIBRARY AND FBX_LIBRARY_DEBUG AND FBX_INCLUDE_DIR)
SET(FBX_FOUND "YES")
ELSE()
SET(FBX_FOUND "NO")
ENDIF()
ENDIF()
IF(NOT FBX_FOUND)
#try to use 2013.3 version
IF(APPLE)
SET(FBX_LIBNAME "fbxsdk-2013.3-static")
ELSEIF(CMAKE_COMPILER_IS_GNUCXX)
SET(FBX_LIBNAME "fbxsdk-2013.3-static")
ELSE()
SET(FBX_LIBNAME "fbxsdk-2013.3-md")
ENDIF()
SET(FBX_LIBNAME_DEBUG ${FBX_LIBNAME}d)
SET( FBX_SEARCH_PATHS
$ENV{FBX_DIR}
"$ENV{ProgramW6432}/Autodesk/FBX/FBX SDK/2013.3"
"$ENV{PROGRAMFILES}/Autodesk/FBX/FBX SDK/2013.3"
"/Applications/Autodesk/FBX/FBX SDK/2013.3"
/Applications/Autodesk/FBXSDK20133
)
# search for headers & debug/release libraries
FIND_PATH(FBX_INCLUDE_DIR "fbxsdk.h"
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "include")
FIND_LIBRARY( FBX_LIBRARY ${FBX_LIBNAME}
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "lib/${FBX_LIBDIR}")
FIND_LIBRARY( FBX_LIBRARY_DEBUG ${FBX_LIBNAME_DEBUG}
PATHS ${FBX_SEARCH_PATHS}
PATH_SUFFIXES "lib/${FBX_LIBDIR}")
IF(FBX_LIBRARY AND FBX_LIBRARY_DEBUG AND FBX_INCLUDE_DIR)
SET(FBX_FOUND "YES")
ELSE()
SET(FBX_FOUND "NO")
ENDIF()
ENDIF()
_______________________________________________
osg-submissions mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org