|
From: <cn...@us...> - 2009-09-03 05:57:58
|
Revision: 535
http://hgengine.svn.sourceforge.net/hgengine/?rev=535&view=rev
Author: cnlohr
Date: 2009-09-03 05:57:47 +0000 (Thu, 03 Sep 2009)
Log Message:
-----------
more progress, maybe?
Modified Paths:
--------------
Mercury2/tools/collada2hgmdl/ColladaConvert.cpp
Modified: Mercury2/tools/collada2hgmdl/ColladaConvert.cpp
===================================================================
--- Mercury2/tools/collada2hgmdl/ColladaConvert.cpp 2009-09-03 04:04:45 UTC (rev 534)
+++ Mercury2/tools/collada2hgmdl/ColladaConvert.cpp 2009-09-03 05:57:47 UTC (rev 535)
@@ -5,7 +5,93 @@
Model mout;
+int FindBone( vector< Bone > & bl, const string & sName )
+{
+ for( unsigned i = 0; i < bl.size(); i++ )
+ if( bl[i].sName == sName )
+ return i;
+ fprintf( stderr, "Warning: Could not find bone with name \"%s\".", sName.c_str() );
+ return -1;
+}
+
+//XXX: TODO: Actually use this function, since it's good and can reduce code.
+void IntParse( const string & sData, vector< int > & is )
+{
+ const char * c = sData.c_str();
+ string stmp = "";
+ for( unsigned i = 0; i <= sData.length(); i++ )
+ {
+ if( c[i] == ' ' )
+ {
+ is.push_back( atoi( stmp.c_str() ) );
+ stmp = "";
+ }
+ stmp += c[i];
+ }
+}
+
+void FloatParse( const string & sData, vector< float > & is )
+{
+ const char * c = sData.c_str();
+ string stmp = "";
+ for( unsigned i = 0; i <= sData.length(); i++ )
+ {
+ if( c[i] == ' ' )
+ {
+ is.push_back( atof( stmp.c_str() ) );
+ stmp = "";
+ }
+ stmp += c[i];
+ }
+}
+
+void SetupBone( XMLCog * tp, XMLCog * tc, vector< Bone > & bl )
+{
+ int oParent = -1;
+ int oThis = -1;
+
+ if( tp )
+ {
+ oParent = FindBone( bl, tp->leaves["name"] );
+ }
+
+ oThis = FindBone( bl, tc->leaves["name"] );
+
+ if( oThis == -1 )
+ {
+ fprintf( stderr, "Could not find bone \"%s\".", tc->leaves["name"].c_str() );
+ return;
+ }
+
+ //Setup "this" bone
+ bl[oThis].iParentIndex = oParent;
+ bl[oThis].iAssociatedMesh = -1;
+
+
+ //Setup all children bones.
+ for( unsigned i = 0; i < tc->children.size(); i++ )
+ {
+ if( tc->children[i].data == "node" && tc->children[i].leaves["type"] == "JOINT" )
+ SetupBone( tc, &tc->children[i], bl );
+ else if( tc->children[i].data == "matrix" )
+ {
+ string sMatrix = tc->children[i].payload;
+ vector< float > fd;
+ FloatParse( sMatrix, fd );
+ bl[oThis].pJointPos.x = fd[3];
+ bl[oThis].pJointPos.y = fd[7];
+ bl[oThis].pJointPos.z = fd[11];
+
+ bl[oThis].qJointRot.w = sqrtf( 1.0 + fd[0] + fd[5] + fd[10] );
+ float w4 = ( 4.0 * bl[oThis].qJointRot.w );
+ bl[oThis].qJointRot.x = (fd[6] - fd[9]) / w4;
+ bl[oThis].qJointRot.y = (fd[8] - fd[2]) / w4;
+ bl[oThis].qJointRot.z = (fd[1] - fd[4]) / w4;
+ }
+ }
+}
+
int main( int argc, char ** argv )
{
float maxx = -10000, maxy = -10000, maxz = -10000;
@@ -306,6 +392,155 @@
}
}
+ if( lcontrol != -1 )
+ {
+ XMLCog & skin = c.children[lcontrol].children[0].children[0];
+ XMLCog & lv = c.children[lvscenes].children[0].children[0];
+ //Setup all the bones now...
+ vector< Bone > & bl = mout.vBones;
+
+ //Enumerate All Bones
+ for( unsigned i = 0; i < skin.children.size(); i++ )
+ {
+ if( skin.children[i].data == "source" )
+ {
+ for( unsigned j = 0; j < skin.children[i].children.size(); j++ )
+ {
+ XMLCog & c = skin.children[i].children[j];
+ if( c.data == "Name_array" )
+ {
+ string bones = c.payload;
+
+ string sThisBone = "";
+ for( unsigned i = 0; i <= bones.length(); i++ )
+ {
+ if( bones.c_str()[i] == ' ' || bones.length() == i )
+ {
+ bl.resize( bl.size() + 1 );
+ bl[bl.size()-1].sName = sThisBone;
+ sThisBone = "";
+ }
+ else
+ sThisBone+= bones.c_str()[i];
+ }
+ }
+ }
+ }
+ }
+
+ printf( "Bones found: %d\n", bl.size() );
+
+ for( unsigned i = 0; i < lv.children.size(); i++ )
+ {
+ if( lv.children[i].data == "node" && lv.children[i].leaves["type"] == "JOINT" )
+ {
+ SetupBone( 0, &lv.children[i], bl );
+ }
+ }
+
+ map< string, vector< int > > sources;
+
+ for( unsigned i = 0; i < skin.children.size(); i++ )
+ {
+ if( skin.children[i].data == "source" )
+ {
+ string sName = skin.children[i].leaves["name"];
+ for( unsigned j = 0; j < skin.children[i].children.size(); j++ )
+ {
+ XMLCog & sid = skin.children[i].children[j];
+
+ if( sid.data == "float_array" )
+ {
+ const char * pl = sid.payload.c_str();
+ string stmp = "";
+ for( unsigned i = 0; i <= sid.payload.length(); i++ )
+ {
+ if( pl[i] == ' ' || i == sid.payload.length() )
+ {
+ sources[sName].push_back( atof( stmp.c_str() ) );
+ stmp = "";
+ }
+ else
+ {
+ stmp += pl[i];
+ }
+ }
+ }
+ }
+ }
+ }
+
+ //Sources is now filled out, go find the vertex weight
+ for( unsigned i = 0; i < skin.children.size(); i++ )
+ {
+ if( skin.children[i].data == "vertex_weights" )
+ {
+ string jid = "";
+ string wid = "";
+
+ int jido = 0;
+ int wido = 1;
+
+ vector< int > vcount;
+ vector< int > vs;
+
+ for( unsigned j = 0; j < skin.children[i].children.size(); j++ )
+ {
+ XMLCog & c = skin.children[i].children[j];
+ if( c.data == "input" )
+ {
+ if( c.leaves["semantic"] == "JOINT" )
+ {
+ jid = c.leaves["source"].substr(1);
+ jido = atoi( c.leaves["offset"].c_str() );
+ }
+ if( c.leaves["semantic"] == "WEIGHT" )
+ {
+ wid = c.leaves["source"].substr(1);
+ wido = atoi( c.leaves["offset"].c_str() );
+ }
+ }
+
+ if( c.data == "vcount" )
+ IntParse( c.payload, vcount );
+ if( c.data == "v" )
+ IntParse( c.payload, vs );
+ }
+
+ int iCurVertex = 0;
+ int iTC = 0;
+
+ //Wooh, we have everything oriented by vertex...
+ for( unsigned i = 0; i < vs.size()/2; i++ )
+ {
+ iTC++;
+ if( vcount[iCurVertex] < iTC )
+ {
+ iCurVertex++;
+ iTC = 0;
+ }
+
+ int iJoint = vs[i*2+jido];
+ int iWeight = vs[i*2+wido];
+
+ if( iJoint >= bl.size() )
+ {
+ fprintf( stderr, "Warning. Attempting to access joint outside workable area (joint %d)\n", iJoint );
+ }
+ else
+ {
+ bl[iJoint].vAssignments.resize( bl[iJoint].vAssignments.size() + 1 );
+ bl[iJoint].vAssignments[bl[iJoint].vAssignments.size()-1].iMesh = 0;
+ bl[iJoint].vAssignments[bl[iJoint].vAssignments.size()-1].iVertex = iCurVertex;
+ bl[iJoint].vAssignments[bl[iJoint].vAssignments.size()-1].fWeight = sources[wid][iWeight];
+ }
+ }
+ }
+ }
+ }
+
+
+
printf( "Extents: %f %f %f -> %f %f %f\n", minx, miny, minz, maxx, maxy, maxz );
mout.SaveModel( argv[2] );
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|