fix the example

This commit is contained in:
Julien Valentin 2017-08-28 17:13:23 +02:00
parent da1d2b67f7
commit 8707086914

View File

@ -45,84 +45,121 @@ osg::ref_ptr<osg::Program> program;
struct MyRigTransformHardware : public osgAnimation::RigTransformHardware struct MyRigTransformHardware : public osgAnimation::RigTransformHardware
{ {
void operator()(osgAnimation::RigGeometry& geom) virtual bool prepareData(osgAnimation::RigGeometry& rig)
{ {
if (_needInit) if(!rig.getSkeleton() && !rig.getParents().empty())
if (!init(geom)) {
return; osgAnimation::RigGeometry::FindNearestParentSkeleton finder;
computeMatrixPaletteUniform(geom.getMatrixFromSkeletonToGeometry(), geom.getInvMatrixFromSkeletonToGeometry()); if(rig.getParents().size() > 1)
} osg::notify(osg::WARN) << "A RigGeometry should not have multi parent ( " << rig.getName() << " )" << std::endl;
rig.getParents()[0]->accept(finder);
bool init(osgAnimation::RigGeometry& geom) if(!finder._root.valid())
{ {
osg::Vec3Array* pos = dynamic_cast<osg::Vec3Array*>(geom.getVertexArray()); osg::notify(osg::WARN) << "A RigGeometry did not find a parent skeleton for RigGeometry ( " << rig.getName() << " )" << std::endl;
if (!pos) {
osg::notify(osg::WARN) << "RigTransformHardware no vertex array in the geometry " << geom.getName() << std::endl;
return false; return false;
} }
rig.setSkeleton(finder._root.get());
if (!geom.getSkeleton()) {
osg::notify(osg::WARN) << "RigTransformHardware no skeleting set in geometry " << geom.getName() << std::endl;
return false;
} }
osgAnimation::BoneMapVisitor mapVisitor; osgAnimation::BoneMapVisitor mapVisitor;
geom.getSkeleton()->accept(mapVisitor); rig.getSkeleton()->accept(mapVisitor);
osgAnimation::BoneMap bm = mapVisitor.getBoneMap(); osgAnimation::BoneMap boneMap = mapVisitor.getBoneMap();
if (!createPalette(pos->size(),bm, geom.getVertexInfluenceSet().getVertexToBoneList())) if (!buildPalette(boneMap,rig) )
return false; return false;
int attribIndex = 11; osg::Geometry& source = *rig.getSourceGeometry();
int nbAttribs = getNumVertexAttrib(); osg::Vec3Array* positionSrc = dynamic_cast<osg::Vec3Array*>(source.getVertexArray());
if (!positionSrc)
{
OSG_WARN << "RigTransformHardware no vertex array in the geometry " << rig.getName() << std::endl;
return false;
}
// use a global program for all avatar // copy shallow from source geometry to rig
if (!program.valid()) { rig.copyFrom(source);
// osg::ref_ptr<osg::Program> program ;
osg::ref_ptr<osg::Shader> vertexshader;
osg::ref_ptr<osg::StateSet> stateset = rig.getOrCreateStateSet();
//grab geom source program and vertex shader if _shader is not setted
if(!_shader.valid() && (program = (osg::Program*)stateset->getAttribute(osg::StateAttribute::PROGRAM)))
{
for(unsigned int i=0; i<program->getNumShaders(); ++i)
if(program->getShader(i)->getType()==osg::Shader::VERTEX)
{
vertexshader=program->getShader(i);
program->removeShader(vertexshader);
}
}
else
{
program = new osg::Program; program = new osg::Program;
program->setName("HardwareSkinning"); program->setName("HardwareSkinning");
}
//set default source if _shader is not user setted
if (!vertexshader.valid())
{
if (!_shader.valid()) if (!_shader.valid())
_shader = osg::Shader::readShaderFile(osg::Shader::VERTEX,"shaders/skinning.vert"); vertexshader = osg::Shader::readShaderFile(osg::Shader::VERTEX,"shaders/skinning.vert");
else vertexshader=_shader;
}
if (!_shader.valid()) {
osg::notify(osg::WARN) << "RigTransformHardware can't load VertexShader" << std::endl; if (!vertexshader.valid())
{
OSG_WARN << "RigTransformHardware can't load VertexShader" << std::endl;
return false; return false;
} }
// replace max matrix by the value from uniform // replace max matrix by the value from uniform
{ {
std::string str = _shader->getShaderSource(); std::string str = vertexshader->getShaderSource();
std::string toreplace = std::string("MAX_MATRIX"); std::string toreplace = std::string("MAX_MATRIX");
std::size_t start = str.find(toreplace); std::size_t start = str.find(toreplace);
if (std::string::npos != start)
{
std::stringstream ss; std::stringstream ss;
ss << getMatrixPaletteUniform()->getNumElements(); ss << getMatrixPaletteUniform()->getNumElements();
str.replace(start, toreplace.size(), ss.str()); str.replace(start, toreplace.size(), ss.str());
_shader->setShaderSource(str); vertexshader->setShaderSource(str);
osg::notify(osg::INFO) << "Shader " << str << std::endl; }
else
{
OSG_INFO<< "MAX_MATRIX not found in Shader! " << str << std::endl;
}
OSG_INFO << "Shader " << str << std::endl;
} }
program->addShader(_shader.get()); unsigned int attribIndex = 11;
unsigned int nbAttribs = getNumVertexAttrib();
for (int i = 0; i < nbAttribs; i++) if(nbAttribs==0)
OSG_WARN << "nbAttribs== " << nbAttribs << std::endl;
for (unsigned int i = 0; i < nbAttribs; i++)
{ {
std::stringstream ss; std::stringstream ss;
ss << "boneWeight" << i; ss << "boneWeight" << i;
program->addBindAttribLocation(ss.str(), attribIndex + i); program->addBindAttribLocation(ss.str(), attribIndex + i);
osg::notify(osg::INFO) << "set vertex attrib " << ss.str() << std::endl; if(getVertexAttrib(i)->getNumElements()!=_nbVertexes)
} OSG_WARN << "getVertexAttrib== " << getVertexAttrib(i)->getNumElements() << std::endl;
} rig.setVertexAttribArray(attribIndex + i, getVertexAttrib(i));
for (int i = 0; i < nbAttribs; i++) OSG_INFO << "set vertex attrib " << ss.str() << std::endl;
{
std::stringstream ss;
ss << "boneWeight" << i;
geom.setVertexAttribArray(attribIndex + i, getVertexAttrib(i));
} }
osg::ref_ptr<osg::StateSet> ss = new osg::StateSet;
ss->addUniform(getMatrixPaletteUniform()); program->addShader(vertexshader.get());
ss->addUniform(new osg::Uniform("nbBonesPerVertex", getNumBonesPerVertex())); stateset->removeUniform("nbBonesPerVertex");
ss->setAttributeAndModes(program.get()); stateset->addUniform(new osg::Uniform("nbBonesPerVertex",_bonesPerVertex));
geom.setStateSet(ss.get()); stateset->removeUniform("matrixPalette");
stateset->addUniform(getMatrixPaletteUniform());
stateset->removeAttribute(osg::StateAttribute::PROGRAM);
if(!stateset->getAttribute(osg::StateAttribute::PROGRAM))
stateset->setAttributeAndModes(program.get());
_needInit = false; _needInit = false;
return true; return true;
} }
@ -142,10 +179,11 @@ struct SetupRigGeometry : public osg::NodeVisitor
} }
void apply(osg::Drawable& geom) void apply(osg::Drawable& geom)
{ {
if (_hardware) { if (_hardware)
{
osgAnimation::RigGeometry* rig = dynamic_cast<osgAnimation::RigGeometry*>(&geom); osgAnimation::RigGeometry* rig = dynamic_cast<osgAnimation::RigGeometry*>(&geom);
if (rig) if (rig)
rig->setRigTransformImplementation(new osgAnimation::RigTransformHardware); rig->setRigTransformImplementation(new MyRigTransformHardware);
} }
#if 0 #if 0
@ -169,7 +207,8 @@ osg::Group* createCharacterInstance(osg::Group* character, bool hardware)
osgAnimation::BasicAnimationManager* anim = dynamic_cast<osgAnimation::BasicAnimationManager*>(animationManager); osgAnimation::BasicAnimationManager* anim = dynamic_cast<osgAnimation::BasicAnimationManager*>(animationManager);
const osgAnimation::AnimationList& list = animationManager->getAnimationList(); const osgAnimation::AnimationList& list = animationManager->getAnimationList();
int v = getRandomValueinRange(list.size()); int v = getRandomValueinRange(list.size());
if (list[v]->getName() == std::string("MatIpo_ipo")) { if (list[v]->getName() == std::string("MatIpo_ipo"))
{
anim->playAnimation(list[v].get()); anim->playAnimation(list[v].get());
v = (v + 1)%list.size(); v = (v + 1)%list.size();
} }
@ -193,7 +232,10 @@ int main (int argc, char* argv[])
bool hardware = true; bool hardware = true;
int maxChar = 10; int maxChar = 10;
while (psr.read("--software")) { hardware = false; } while (psr.read("--software"))
{
hardware = false;
}
while (psr.read("--number", maxChar)) {} while (psr.read("--number", maxChar)) {}
osg::ref_ptr<osg::Node> node = osgDB::readRefNodeFiles(psr); osg::ref_ptr<osg::Node> node = osgDB::readRefNodeFiles(psr);
@ -243,8 +285,10 @@ int main (int argc, char* argv[])
double xChar = maxChar; double xChar = maxChar;
double yChar = xChar * 9.0/16; double yChar = xChar * 9.0/16;
for (double i = 0.0; i < xChar; i++) { for (double i = 0.0; i < xChar; i++)
for (double j = 0.0; j < yChar; j++) { {
for (double j = 0.0; j < yChar; j++)
{
osg::ref_ptr<osg::Group> c = createCharacterInstance(root.get(), hardware); osg::ref_ptr<osg::Group> c = createCharacterInstance(root.get(), hardware);
osg::MatrixTransform* tr = new osg::MatrixTransform; osg::MatrixTransform* tr = new osg::MatrixTransform;