⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogremayaskeleton.cpp

📁 赫赫大名的 OGRE 游戏引擎
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
============================================================================
This source file is part of the Ogre-Maya Tools.
Distributed as part of Ogre (Object-oriented Graphics Rendering Engine).
Copyright (C) 2003 Fifty1 Software Inc., Bytelords

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
or go to http://www.gnu.org/licenses/gpl.txt
============================================================================
*/
#include "OgreMayaSkeleton.h"
#include "OgreMayaOptions.h"

#include <maya/MString.h>
#include <maya/MArgList.h>
#include <maya/MAnimControl.h>

#include <maya/MFnMesh.h>
#include <maya/MFnIkJoint.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnSkinCluster.h>
#include <maya/MFnMatrixData.h>
#include <maya/MFnSet.h>
#include <maya/MFnLambertShader.h>
#include <maya/MFnBlinnShader.h>
#include <maya/MFnPhongShader.h>

#include <maya/MItGeometry.h>
#include <maya/MItDag.h>
#include <maya/MItDependencyGraph.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MItMeshVertex.h>
#include <maya/MItMeshPolygon.h>

#include <maya/MPlug.h>
#include <maya/MDagPathArray.h>
#include <maya/MFloatPointArray.h>
#include <maya/MFloatVectorArray.h>
#include <maya/MFloatArray.h>
#include <maya/MPointArray.h>
#include <maya/MMatrix.h>
#include <maya/MGlobal.h>
#include <maya/MStatus.h>

#include <iostream>

namespace OgreMaya {
	
	using namespace std;

    void printMMatrix(MMatrix const& m) {
        cout.setf(ios::showpos | ios::fixed);
        cout.precision(5);
        cout << "("<<m(0,0)<<", "<<m(0,1)<<", "<<m(0,2)<<", "<<m(0,3)<<")" << '\n';
        cout << "("<<m(1,0)<<", "<<m(1,1)<<", "<<m(1,2)<<", "<<m(1,3)<<")" << '\n';
        cout << "("<<m(2,0)<<", "<<m(2,1)<<", "<<m(2,2)<<", "<<m(2,3)<<")" << '\n';
        cout << "("<<m(3,0)<<", "<<m(3,1)<<", "<<m(3,2)<<", "<<m(3,3)<<")" << '\n';
    }

    void printMQuaternion(MQuaternion const& q) {
        cout.setf(ios::showpos | ios::fixed);
        cout.precision(5);
        cout << "("<<q[0]<<", "<<q[1]<<", "<<q[2]<<", "<<q[3]<<")" << '\n';
    }

    void printMVector(MVector const& v) {
        cout.setf(ios::showpos | ios::fixed);
        cout.precision(5);
        cout << "("<<v[0]<<", "<<v[1]<<", "<<v[2]<<")" << '\n';
    }

	//	--------------------------------------------------------------------------
	/** Standard constructor. Creates Ogre Mesh and defines known options.
	*/	
	//	--------------------------------------------------------------------------
	SkeletonGenerator::SkeletonGenerator() {
	}


	//	--------------------------------------------------------------------------
	/** Destructor.
	*/	
	//	--------------------------------------------------------------------------
	SkeletonGenerator::~SkeletonGenerator()
	{
	}


	//	--------------------------------------------------------------------------
	/** Find and export all joints

		\return		True if exported ok, false otherwise
	*/	
	//	--------------------------------------------------------------------------
	bool SkeletonGenerator::exportAll()	{        

        if(!_querySkeleton())
            return false;

        if(!_querySkeletonAnim())
            return false;

        
        MGlobal::executeCommand("ikSystem -e -sol 0;");
        MGlobal::selectByName(root->name.c_str());
        MGlobal::executeCommand("dagPose -r -g -bp");
        MGlobal::executeCommand("dagPose -r -g -bp");
		//MGlobal::executeCommand("currentTime -edit 0");
        
        
/*        
        MGlobal::executeCommand("ikSystem -e -sol 0;");
	    MGlobal::executeCommand((string("select ")+root->name).c_str());
	    MGlobal::executeCommand("dagPose -r -g -bp");
*/
        
        /////////////////////////////////////////////

        
        {
            ofstream out(OPTIONS.outSkelFile.c_str());

            out.precision(5);
            out.setf(ios::fixed);

            out << "<skeleton>\n";

            
            //
            // BONES
            //
            out << "\t<bones>\n";


            SkeletonJointList::iterator it, end;
            MVector axis;
            double angle;

            for(it=jointList.begin(), end=jointList.end(); it!=end; ++it) {
                
                SkeletonJoint& j = **it;
                                
                /*
                cout << "* worldMatrix:\n";
                printMMatrix(j.worldMatrix);
                cout << "* relPos:\n";
                printMVector(j.relPos);
                cout << "* relRot:\n";
                printMQuaternion(j.relRot);
                cout << "----------------------------\n";
                */

                j.relRot.getAxisAngle(axis, angle);

                out << "\t\t<bone id=\""<<j.index<<"\" name=\""<<j.name<<"\">\n";
                out << "\t\t\t<position x=\""<<j.relPos.x<<"\" y=\""<<j.relPos.y<<"\" z=\""<<j.relPos.z<<"\"/>\n";
                out << "\t\t\t<rotation angle=\""<<((float)angle)<<"\">\n";
                out << "\t\t\t\t<axis x=\""<<axis.x<<"\" y=\""<<axis.y<<"\" z=\""<<axis.z<<"\"/>\n";
                out << "\t\t\t</rotation>\n";
                out << "\t\t</bone>\n";

            }

            out << "\t</bones>\n";

            
            //
            // HIERARCHY
            //
            out << "\t<bonehierarchy>\n";
            for(it=jointList.begin(), end=jointList.end(); it!=end; ++it) {
                SkeletonJoint& j = **it;
                if(j.hasParent) {
                    out << "\t\t<boneparent bone=\""<<j.name<<"\" parent=\""<<j.parentName<<"\"/>\n";
                }
            }

            out << "\t</bonehierarchy>\n";
            

            //
            // ANIMATIONS
            //
            out << "\t<animations>\n";
            AnimationMap::iterator animIt = animations.begin();
            AnimationMap::iterator animEnd = animations.end();
            for(; animIt!=animEnd; ++animIt) {
                string animName = (*animIt).first;
                Animation& anim = (*animIt).second;

                out << "\t\t<animation name=\""<<animName.c_str()<<"\" ";
                out << "length=\""<<anim.time<<"\">\n";
                out << "\t\t\t<tracks>\n";

                KeyframesMap::iterator keyframesIt = anim.keyframes.begin();
                KeyframesMap::iterator keyframesEnd = anim.keyframes.end();
                for(; keyframesIt!=keyframesEnd; ++keyframesIt) {
                    string boneName = (*keyframesIt).first;
                    KeyframeList& l = (*keyframesIt).second;

                    out << "\t\t\t\t<track bone=\""<<boneName.c_str()<<"\">\n";
                    out << "\t\t\t\t\t<keyframes>\n";

                    KeyframeList::iterator it  = l.begin();
                    KeyframeList::iterator end = l.end();
                    for(;it!=end; ++it) {
                        Keyframe& k = *it;

                        MVector axis;
                        double angle;
                        k.rot.getAxisAngle(axis, angle);

                        out << "\t\t\t\t\t\t<keyframe time=\""<<k.time<<"\">\n";                        
                        out << "\t\t\t\t\t\t\t<translate x=\""<<k.pos.x<<"\" y=\""<<k.pos.y<<"\" z=\""<<k.pos.z<<"\"/>\n";
                        out << "\t\t\t\t\t\t\t<rotate angle=\""<<((float)angle)<<"\">\n";
                        out << "\t\t\t\t\t\t\t\t<axis x=\""<<axis.x<<"\" y=\""<<axis.y<<"\" z=\""<<axis.z<<"\"/>\n";
                        out << "\t\t\t\t\t\t\t</rotate>\n";
                        out << "\t\t\t\t\t\t</keyframe>\n";
                    }

                    out << "\t\t\t\t\t</keyframes>\n";
                    out << "\t\t\t\t</track>\n";
                }                    

                out << "\t\t\t</tracks>\n";
                out << "\t\t</animation>\n";
            }
            out << "\t</animations>\n";


            out << "</skeleton>\n";
        }


        deleteAll(jointList.begin(), jointList.end());

        return true;
    }
        
    //	--------------------------------------------------------------------------
	/** Finds and exports all joints

		\return		True if exported ok, false otherwise
	*/	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -