void Framework::loadRigid( MyGUI::xml::ElementPtr node,JointPtr parent ) { ObjectFactory& factory = ObjectFactory::getSingleton(); RigidPtr rgp = boost::dynamic_pointer_cast<Rigid>(factory.createObject("Rigid")); if(rgp) { rgp->load(node); if( parent ) { parent->mRigid[1] = rgp; parent->linkRigid(parent->mRigid[0], parent->mRigid[1]); } MyGUI::xml::ElementEnumerator ce = node->getElementEnumerator(); while(ce.next()) { MyGUI::xml::ElementPtr child = ce.current(); if( child->getName() == "joint" ) { loadJoint( child,rgp ); } } } else { WARNING_LOG("Factory can't make Rigid object!"); } }
MStatus skeleton::load(MFnSkinCluster* pSkinCluster) { MStatus stat; //update skin cluster pointer if (!pSkinCluster) { std::cout << "Could not load skeleton data, no skin cluster specified\n"; return MS::kFailure; } //retrieve and load joints from the skin cluster MDagPath jointDag,rootDag; MDagPathArray influenceDags; int numInfluenceObjs = pSkinCluster->influenceObjects(influenceDags,&stat); for (int i=0; i<numInfluenceObjs; i++) { jointDag = influenceDags[i]; if (influenceDags[i].hasFn(MFn::kJoint)) { //retrieve root joint rootDag = jointDag; while (jointDag.length()>0) { jointDag.pop(); if (jointDag.hasFn(MFn::kJoint) && jointDag.length()>0) rootDag = jointDag; } //check if skeleton has already been loaded bool skip = false; for (int j=0; j<m_joints.size() && !skip; j++) { //skip skeleton if already loaded if (rootDag.partialPathName() == m_joints[j].name) { skip = true; } } //load joints data from root if (!skip) { //load joints data loadJoint(rootDag,NULL); } } } return MS::kSuccess; }
Joint* WFObjLoader::loadJoint(BinaryStream& stream) { Joint *jt = new Joint(); bool hasSid = stream.readByte() == 1 ? true : false; if (hasSid) { short sidSize = stream.readShort(); for (int i = 0; i < sidSize; i++) { jt->sid.append(1, stream.readByte()); } } bool hasMatrix = stream.readByte() == 1 ? true : false; if (hasMatrix) { float m[16]; for (int i = 0; i < 16; i++) { m[i] = stream.readFloat(); } jt->baseMatrix->setElements(m); jt->transForm->setElements(m); } bool hasBindPoses = stream.readByte() == 1 ? true : false; if (hasBindPoses) { float m[16]; for (int i = 0; i < 16; i++) { m[i] = stream.readFloat(); } jt->invBindMatrix->setElements(m); } else { jt->hasMesh = false; } int childrenSize = stream.readShort(); for (int i = 0; i < childrenSize; i++) { short type = stream.readShort(); if (type != TYPE_NODE) { LOGE("Illegal type. [type=%d]", type); } jt->addChild(loadJoint(stream)); } return jt; }
Figure* WFObjLoader::loadGCBData(std::vector<char>* data) { BinaryStream stream(data); // マジックナンバーチェック if (stream.readByte() != 'g' || stream.readByte() != 'c' || stream.readByte() != 'u' || stream.readByte() != 'b') { return NULL; } Figure *fig = new Figure(); while (!stream.isEnd()) { short type = stream.readShort(); switch (type) { case TYPE_VERTEX: { int size = stream.readInt(); for (int i = 0; i < size; i++) { float v = stream.readFloat(); fig->addVertices(&v, 1); } } break; case TYPE_NORMAL: { int size = stream.readInt(); for (int i = 0; i < size; i++) { float v = stream.readFloat(); fig->addNormal(&v, 1); } } break; case TYPE_TEXCOOD: { int size = stream.readInt(); for (int i = 0; i < size; i++) { float v = stream.readFloat(); fig->addTextureCoords(&v, 1); } } break; case TYPE_INDEX: { int size = stream.readInt(); for (int i = 0; i < size; i++) { unsigned short v = stream.readInt(); fig->addVertexIndexes(&v, 1); } } break; case TYPE_COLOR: { int size = stream.readInt(); for (int i = 0; i < size; i++) { float v = stream.readFloat(); fig->addColors(&v, 1); } } break; case TYPE_WEIGHT: { loadWeight(stream, fig); } break; case TYPE_NODE: { fig->joint = loadJoint(stream); } break; case TYPE_TEXCOOD_MLT: { int size = stream.readInt(); for (int i = 0; i < size; i++) { float v = stream.readFloat(); fig->addTextureCoordsMlt(&v, 1); } } break; default: { LOGE("Unknown Type. [type=%d]", type); return NULL; } break; } } return fig; }
// Load a joint void skeleton::loadJoint(MDagPath& jointDag,joint* parent) { int i; joint newJoint; joint* parentJoint = parent; if (jointDag.hasFn(MFn::kJoint)) { MFnIkJoint jointFn(jointDag); // Get parent index int idx=-1; if (parent) { idx = parent->id; } // Get joint matrix MMatrix worldMatrix = jointDag.inclusiveMatrix(); /*float translation1[3]; float rotation1[4]; float scale1[3]; extractTranMatrix(worldMatrix,translation1,rotation1,scale1); Quaternion q(rotation1[0],rotation1[1],rotation1[2],rotation1[3]); float angle; Vector3 axis; q.ToAngleAxis(angle,axis); Vector3 x,y,z; q.ToAxes(x,y,z);*/ //printMatrix(worldMatrix); // Calculate scaling factor inherited by parent // Calculate Local Matrix MMatrix localMatrix = worldMatrix; if (parent) localMatrix = worldMatrix * parent->worldMatrix.inverse(); float translation2[3]; float rotation2[4]; float scale2[3]; extractTranMatrix(worldMatrix,translation2,rotation2,scale2); //printMatrix(localMatrix); // Set joint info newJoint.name = jointFn.partialPathName(); newJoint.id = m_joints.size(); newJoint.parentIndex = idx; newJoint.jointDag = jointDag; newJoint.worldMatrix = worldMatrix; newJoint.localMatrix = localMatrix; for (int iRow = 0; iRow < 4; iRow++) for (int iCol = 0; iCol < 3; iCol++) newJoint.tran.m_mat[iRow][iCol] = (FLOAT)worldMatrix[iRow][iCol]; //printMatrix(worldMatrix); /*MQuaternion q; q = worldMatrix; newJoint.tran.q[0] = (float)q.x; newJoint.tran.q[1] = (float)q.y; newJoint.tran.q[2] = (float)q.z; newJoint.tran.q[3] = (float)q.w; newJoint.tran.t[0] = (float)worldMatrix[3][0]; newJoint.tran.t[1] = (float)worldMatrix[3][1]; newJoint.tran.t[2] = (float)worldMatrix[3][2];*/ MPlug plug = jointFn.findPlug("unRibbonEnabled"); if(!plug.isNull()) { bool enabled; plug.getValue(enabled); if(enabled) { plug = jointFn.findPlug("unRibbonVisible"); bool visible; plug.getValue(visible); plug = jointFn.findPlug("unRibbonAbove"); float above; plug.getValue(above); plug = jointFn.findPlug("unRibbonBelow"); float below; plug.getValue(below); plug = jointFn.findPlug("unRibbonEdgesPerSecond"); short edgePerSecond; plug.getValue(edgePerSecond); plug = jointFn.findPlug("unRibbonEdgeLife"); float edgeLife; plug.getValue(edgeLife); plug = jointFn.findPlug("unRibbonGravity"); float gravity; plug.getValue(gravity); plug = jointFn.findPlug("unRibbonTextureRows"); short rows; plug.getValue(rows); plug = jointFn.findPlug("unRibbonTextureCols"); short cols; plug.getValue(cols); plug = jointFn.findPlug("unRibbonTextureSlot"); short slot; plug.getValue(slot); plug = jointFn.findPlug("unRibbonVertexColor"); MObject object; plug.getValue(object); MFnNumericData data(object); float r,g,b; data.getData(r,g,b); plug = jointFn.findPlug("unRibbonVertexAlpha"); float alpha; plug.getValue(alpha); plug = jointFn.findPlug("unRibbonBlendMode"); short blendMode; plug.getValue(blendMode); plug = jointFn.findPlug("unRibbonTextureFilename"); MItDependencyGraph dgIt(plug, MFn::kFileTexture, MItDependencyGraph::kUpstream, MItDependencyGraph::kBreadthFirst, MItDependencyGraph::kNodeLevel); dgIt.disablePruningOnFilter(); MString textureName; if (!dgIt.isDone()) { MObject textureNode = dgIt.thisNode(); MPlug filenamePlug = MFnDependencyNode(textureNode).findPlug("fileTextureName"); filenamePlug.getValue(textureName); } else { char str[256]; sprintf(str,"%s ribbon system must has file-texture",newJoint.name.asChar()); MessageBox(0,str,0,0); } newJoint.hasRibbonSystem = true; newJoint.ribbon.visible = visible; newJoint.ribbon.above = above; newJoint.ribbon.below = below; newJoint.ribbon.gravity = gravity; newJoint.ribbon.edgePerSecond = edgePerSecond; newJoint.ribbon.edgeLife = edgeLife; newJoint.ribbon.rows = rows; newJoint.ribbon.cols = cols; newJoint.ribbon.slot = slot; newJoint.ribbon.color[0] = r; newJoint.ribbon.color[1] = g; newJoint.ribbon.color[2] = b; newJoint.ribbon.alpha = alpha; newJoint.ribbon.blendMode = blendMode; newJoint.ribbon.textureFilename = textureName.asChar(); } } plug = jointFn.findPlug("unParticleEnabled"); if(!plug.isNull()) { bool enabled; plug.getValue(enabled); if(enabled) { newJoint.hasParticleSystem = true; plug = jointFn.findPlug("unParticleVisible"); bool visible; plug.getValue(visible); plug = jointFn.findPlug("unParticleSpeed"); float speed; plug.getValue(speed); plug = jointFn.findPlug("unParticleVariationPercent"); float variation; plug.getValue(variation); plug = jointFn.findPlug("unParticleConeAngle"); float coneAngle; plug.getValue(coneAngle); plug = jointFn.findPlug("unParticleGravity"); float gravity; plug.getValue(gravity); plug = jointFn.findPlug("unParticleExplosiveForce"); float explosiveForce = 0.0f; if(!plug.isNull()) { plug.getValue(explosiveForce); } plug = jointFn.findPlug("unParticleLife"); float life; plug.getValue(life); plug = jointFn.findPlug("unParticleLifeVariation"); float lifeVar; if(plug.isNull()) { lifeVar = 0.0f; } else { plug.getValue(lifeVar); } plug = jointFn.findPlug("unParticleEmissionRate"); float emissionRate; plug.getValue(emissionRate); plug = jointFn.findPlug("unParticleLimitNum"); short limitNum; plug.getValue(limitNum); plug = jointFn.findPlug("unParticleInitialNum"); short initialNum = 0; if(!plug.isNull())plug.getValue(initialNum); plug = jointFn.findPlug("unParticleAttachToEmitter"); bool attachToEmitter; plug.getValue(attachToEmitter); plug = jointFn.findPlug("unParticleMoveWithEmitter"); bool moveWithEmitter = false; if(!plug.isNull())plug.getValue(moveWithEmitter); //23 plug = jointFn.findPlug("unParticleForTheSword"); bool forTheSword = false; if(!plug.isNull())plug.getValue(forTheSword); //24 plug = jointFn.findPlug("unParticleForTheSwordInitialAngle"); float forTheSwordInitialAngle = 0; if(!plug.isNull())plug.getValue(forTheSwordInitialAngle); //25 plug = jointFn.findPlug("unParticleWander"); bool wander = false; if(!plug.isNull())plug.getValue(wander); //25 plug = jointFn.findPlug("unParticleWanderRadius"); float wanderRadius = 0.0f; if(!plug.isNull())plug.getValue(wanderRadius); //25 plug = jointFn.findPlug("unParticleWanderSpeed"); float wanderSpeed = 0.0f; if(!plug.isNull())plug.getValue(wanderSpeed); plug = jointFn.findPlug("unParticleAspectRatio"); float aspectRatio; if(plug.isNull()) { aspectRatio = 1.0f; } else { plug.getValue(aspectRatio); } plug = jointFn.findPlug("unParticleInitialAngleBegin"); float angleBegin; if(plug.isNull()) { angleBegin = 0.0f; } else { plug.getValue(angleBegin); } plug = jointFn.findPlug("unParticleInitialAngleEnd"); float angleEnd; if(plug.isNull()) { angleEnd = 0.0f; } else { plug.getValue(angleEnd); } plug = jointFn.findPlug("unParticleRotationSpeed"); float rotationSpeed; if(plug.isNull()) { rotationSpeed = 0; } else { plug.getValue(rotationSpeed); } plug = jointFn.findPlug("unParticleRotationSpeedVar"); float rotationSpeedVar; if(plug.isNull()) { rotationSpeedVar = 0; } else { plug.getValue(rotationSpeedVar); } plug = jointFn.findPlug("unParticleEmitterWidth"); float width; plug.getValue(width); plug = jointFn.findPlug("unParticleEmitterLength"); float length; plug.getValue(length); plug = jointFn.findPlug("unParticleEmitterHeight"); float height = 0.0f; if(!plug.isNull()) { plug.getValue(height); } plug = jointFn.findPlug("unParticleBlendMode"); short blendMode; plug.getValue(blendMode); plug = jointFn.findPlug("unParticleTextureFilename"); MItDependencyGraph dgIt(plug, MFn::kFileTexture, MItDependencyGraph::kUpstream, MItDependencyGraph::kBreadthFirst, MItDependencyGraph::kNodeLevel); dgIt.disablePruningOnFilter(); MString textureName; if (!dgIt.isDone()) { MObject textureNode = dgIt.thisNode(); MPlug filenamePlug = MFnDependencyNode(textureNode).findPlug("fileTextureName"); filenamePlug.getValue(textureName); } else { char str[256]; sprintf(str,"%s particle system must has file-texture",newJoint.name.asChar()); MessageBox(0,str,0,0); } plug = jointFn.findPlug("unParticleTextureRows"); short rows; plug.getValue(rows); plug = jointFn.findPlug("unParticleTextureCols"); short cols; plug.getValue(cols); plug = jointFn.findPlug("unParticleTextureChangeStyle"); short changeStyle; if(plug.isNull()) { //0 - 顺序 //1 - 随机 changeStyle = 0; } else { plug.getValue(changeStyle); } plug = jointFn.findPlug("unParticleTextureChangeInterval"); short changeInterval; if(plug.isNull()) { //默认30ms换一个 changeInterval = 30; } else { plug.getValue(changeInterval); } plug = jointFn.findPlug("unParticleTailLength"); float tailLength; plug.getValue(tailLength); plug = jointFn.findPlug("unParticleTimeMiddle"); float timeMiddle; plug.getValue(timeMiddle); plug = jointFn.findPlug("unParticleColorStart"); MObject object; plug.getValue(object); MFnNumericData dataS(object); float colorStart[3]; dataS.getData(colorStart[0],colorStart[1],colorStart[2]); plug = jointFn.findPlug("unParticleColorMiddle"); plug.getValue(object); MFnNumericData dataM(object); float colorMiddle[3]; dataM.getData(colorMiddle[0],colorMiddle[1],colorMiddle[2]); plug = jointFn.findPlug("unParticleColorEnd"); plug.getValue(object); MFnNumericData dataE(object); float colorEnd[3]; dataE.getData(colorEnd[0],colorEnd[1],colorEnd[2]); plug = jointFn.findPlug("unParticleAlpha"); plug.getValue(object); MFnNumericData dataAlpha(object); float alpha[3]; dataAlpha.getData(alpha[0],alpha[1],alpha[2]); //Scale plug = jointFn.findPlug("unParticleScale"); plug.getValue(object); MFnNumericData dataScale(object); float scale[3]; dataScale.getData(scale[0],scale[1],scale[2]); //ScaleVar plug = jointFn.findPlug("unParticleScaleVar"); float scaleVar[3] = {0.0f,0.0f,0.0f}; if(!plug.isNull()) { plug.getValue(object); MFnNumericData dataScaleVar(object); dataScaleVar.getData(scaleVar[0],scaleVar[1],scaleVar[2]); } //FixedSize plug = jointFn.findPlug("unParticleFixedSize"); bool fixedSize = false; if(!plug.isNull()) { plug.getValue(fixedSize); } //HeadLifeSpan plug = jointFn.findPlug("unParticleHeadLifeSpan"); plug.getValue(object); MFnNumericData dataHeadLifeSpan(object); short headLifeSpan[3]; dataHeadLifeSpan.getData(headLifeSpan[0],headLifeSpan[1],headLifeSpan[2]); plug = jointFn.findPlug("unParticleHeadDecay"); plug.getValue(object); MFnNumericData dataHeadDecay(object); short headDecay[3]; dataHeadDecay.getData(headDecay[0],headDecay[1],headDecay[2]); plug = jointFn.findPlug("unParticleTailLifeSpan"); plug.getValue(object); MFnNumericData dataTailLifeSpan(object); short tailLifeSpan[3]; dataTailLifeSpan.getData(tailLifeSpan[0],tailLifeSpan[1],tailLifeSpan[2]); plug = jointFn.findPlug("unParticleTailDecay"); plug.getValue(object); MFnNumericData dataTailDecay(object); short tailDecay[3]; dataTailDecay.getData(tailDecay[0],tailDecay[1],tailDecay[2]); plug = jointFn.findPlug("unParticleHead"); bool head; plug.getValue(head); plug = jointFn.findPlug("unParticleTail"); bool tail; plug.getValue(tail); plug = jointFn.findPlug("unParticleUnShaded"); bool unshaded; plug.getValue(unshaded); plug = jointFn.findPlug("unParticleUnFogged"); bool unfogged; plug.getValue(unfogged); plug = jointFn.findPlug("unParticleBlockByY0"); bool blockByY0 = false; if(!plug.isNull()) plug.getValue(blockByY0); newJoint.particle.visible = visible; newJoint.particle.speed = speed; newJoint.particle.variation = variation / 100.0f; newJoint.particle.coneAngle = coneAngle; newJoint.particle.gravity = gravity; newJoint.particle.explosiveForce = explosiveForce; newJoint.particle.life = life; newJoint.particle.lifeVar = lifeVar; newJoint.particle.emissionRate = emissionRate; newJoint.particle.initialNum = initialNum; newJoint.particle.limitNum = limitNum; newJoint.particle.attachToEmitter = attachToEmitter; newJoint.particle.moveWithEmitter = moveWithEmitter; newJoint.particle.forTheSword = forTheSword; newJoint.particle.forTheSwordInitialAngle = forTheSwordInitialAngle; newJoint.particle.wander = wander; newJoint.particle.wanderRadius = wanderRadius; newJoint.particle.wanderSpeed = wanderSpeed; newJoint.particle.aspectRatio = aspectRatio; newJoint.particle.initialAngleBegin = angleBegin; newJoint.particle.initialAngleEnd = angleEnd; newJoint.particle.rotationSpeed = rotationSpeed; newJoint.particle.rotationSpeedVar = rotationSpeedVar; newJoint.particle.width = width; newJoint.particle.length = length; newJoint.particle.height = height; newJoint.particle.blendMode = blendMode; newJoint.particle.textureFilename = textureName.asChar(); newJoint.particle.textureRows = rows; newJoint.particle.textureCols = cols; newJoint.particle.changeStyle = changeStyle; newJoint.particle.changeInterval = changeInterval; newJoint.particle.tailLength = tailLength; newJoint.particle.timeMiddle = timeMiddle; newJoint.particle.colorStart[0] = colorStart[0]; newJoint.particle.colorStart[1] = colorStart[1]; newJoint.particle.colorStart[2] = colorStart[2]; newJoint.particle.colorMiddle[0] = colorMiddle[0]; newJoint.particle.colorMiddle[1] = colorMiddle[1]; newJoint.particle.colorMiddle[2] = colorMiddle[2]; newJoint.particle.colorEnd[0] = colorEnd[0]; newJoint.particle.colorEnd[1] = colorEnd[1]; newJoint.particle.colorEnd[2] = colorEnd[2]; newJoint.particle.alpha[0] = alpha[0]; newJoint.particle.alpha[1] = alpha[1]; newJoint.particle.alpha[2] = alpha[2]; newJoint.particle.scale[0] = scale[0]; newJoint.particle.scale[1] = scale[1]; newJoint.particle.scale[2] = scale[2]; newJoint.particle.scaleVar[0] = scaleVar[0]; newJoint.particle.scaleVar[1] = scaleVar[1]; newJoint.particle.scaleVar[2] = scaleVar[2]; newJoint.particle.fixedSize = fixedSize; newJoint.particle.headLifeSpan[0] = headLifeSpan[0]; newJoint.particle.headLifeSpan[1] = headLifeSpan[1]; newJoint.particle.headLifeSpan[2] = headLifeSpan[2]; newJoint.particle.headDecay[0] = headDecay[0]; newJoint.particle.headDecay[1] = headDecay[1]; newJoint.particle.headDecay[2] = headDecay[2]; newJoint.particle.tailLifeSpan[0] = tailLifeSpan[0]; newJoint.particle.tailLifeSpan[1] = tailLifeSpan[1]; newJoint.particle.tailLifeSpan[2] = tailLifeSpan[2]; newJoint.particle.tailDecay[0] = tailDecay[0]; newJoint.particle.tailDecay[1] = tailDecay[1]; newJoint.particle.tailDecay[2] = tailDecay[2]; newJoint.particle.head = head; newJoint.particle.tail = tail; newJoint.particle.unshaded = unshaded; newJoint.particle.unfogged = unfogged; newJoint.particle.blockByY0 = blockByY0; } } m_joints.push_back(newJoint); // Get pointer to newly created joint parentJoint = &newJoint; } // Load children joints for (i=0; i<jointDag.childCount();i++) { MObject child; child = jointDag.child(i); MDagPath childDag = jointDag; childDag.push(child); loadJoint(childDag,parentJoint); } }