Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogre/Tools/3dsmaxExport/LEXIExporter/LexiExport/Sources/LexiOgreSkeletonCompiler.cpp @ 45

Last change on this file since 45 was 6, checked in by anonymous, 18 years ago

=…

File size: 6.2 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of LEXIExporter
4
5Copyright 2006 NDS Limited
6
7Author(s):
8Mark Folkenberg,
9Bo Krohn
10
11This program is free software; you can redistribute it and/or modify it under
12the terms of the GNU Lesser General Public License as published by the Free Software
13Foundation; either version 2 of the License, or (at your option) any later
14version.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19
20You should have received a copy of the GNU Lesser General Public License along with
21this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22Place - Suite 330, Boston, MA 02111-1307, USA, or go to
23http://www.gnu.org/copyleft/lesser.txt.
24-----------------------------------------------------------------------------
25*/
26
27#include "LexiStdAfx.h"
28#include "LexiOgreSkeletonCompiler.h"
29
30COgreSkeletonCompiler::COgreSkeletonCompiler( Ogre::String name, Ogre::MeshPtr ogreMesh )
31{
32        m_pOgreMesh = ogreMesh;
33        m_pISkel = CIntermediateBuilder::Get()->GetSkeletonBuilder()->GetSkeleton();
34
35        if( m_pISkel != NULL)
36        {
37                // extract filename(no path)
38                Ogre::String baseName;
39                Ogre::String tmpPath;
40                Ogre::StringUtil::splitFilename(name, baseName, tmpPath);
41                m_pSkel = Ogre::SkeletonManager::getSingletonPtr()->create(baseName + ".skeleton", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME /*"exporterSkelGroup"*/, true);
42                m_pOgreMesh->setSkeletonName(baseName+".skeleton");
43
44                CreateSkeleton( NULL );
45                m_pSkel->setBindingPose();
46                LOGDEBUG "COgreSkeletonCompiler: Creating Ogre Skeleton Animations");
47                CreateAnimations();
48        }
49        else
50        {
51                LOGWARNING "No skin modifier found, so no animations exported.");
52        }
53}
54
55COgreSkeletonCompiler::~COgreSkeletonCompiler()
56{
57        Ogre::SkeletonManager::getSingletonPtr()->unload(m_pSkel->getHandle());
58        Ogre::SkeletonManager::getSingletonPtr()->remove(m_pSkel->getHandle());
59}
60
61void COgreSkeletonCompiler::CreateSkeleton( CIntermediateBone* pIBone )
62{
63        // The bones have been indexed according to a depthfirst search, so we know
64        // that following this list we will always know the parent, and evenly more important
65        // the bones will be added to the Ogre skeleton in the correct order so the index will
66        // continue in the Ogre Skeletons bone list.
67
68        Ogre::Bone* oBone = NULL;
69
70        for(int i=0; i < m_pISkel->GetBoneCount(); i++)
71        {
72                CIntermediateBone* pIBone = m_pISkel->GetBoneByIndex(i);
73
74                CIntermediateBone* pParent = pIBone->GetParent();
75                if(pParent != NULL)
76                {
77                        Ogre::Bone* oParent = NULL;
78                        try
79                        {
80                                oParent = m_pSkel->getBone(pParent->GetName());
81                        }
82                        catch (...)
83                        {
84                                LOGERROR "Error while creating skeleton..");
85                                return;
86                        }
87                        oBone = m_pSkel->createBone(pIBone->GetName());
88
89                        Ogre::Vector3 pos;
90                        Ogre::Vector3 scale;
91                        Ogre::Quaternion orient;
92                        pIBone->GetBindingPose(pos, orient, scale);
93
94                        oBone->setPosition( pos );
95                        oBone->setOrientation( orient );
96                        oBone->setScale( scale );
97
98                        oParent->addChild(oBone);
99                }
100                else
101                {
102                        oBone = m_pSkel->createBone(pIBone->GetName());
103                        Ogre::Vector3 pos;
104                        Ogre::Vector3 scale;
105                        Ogre::Quaternion orient;
106                        pIBone->GetBindingPose(pos, orient, scale);
107
108                        oBone->setPosition( pos );
109                        oBone->setOrientation( orient );
110                        oBone->setScale( scale );
111                }
112
113
114        }
115}
116
117void COgreSkeletonCompiler::CreateAnimations( void )
118{
119        CIntermediateBone* tmpBone = m_pISkel->GetBoneByIndex(0);
120        std::map< Ogre::String, CAnimationData* > lAnimMap;
121        if(tmpBone != NULL)
122                lAnimMap = tmpBone->GetAnimations();
123        else
124                return;
125
126        std::map< Ogre::String, CAnimationData* >::const_iterator animIter = lAnimMap.begin();
127
128        while(animIter != lAnimMap.end() )
129        {
130                CAnimationData* pCurAnimData = (*animIter).second;
131                Ogre::String curAnimName = (*animIter).first;
132                animIter++;
133
134                if(m_pSkel.get())
135                {
136                        unsigned int iLastFrame = m_pISkel->GetBoneByIndex(0)->GetFrameCount( curAnimName );
137                        float animLength;
138                        Ogre::Vector3 endPos;
139                        Ogre::Vector3 endScale;
140                        Ogre::Quaternion endOrient;
141                        m_pISkel->GetBoneByIndex(0)->GetFrame( curAnimName, iLastFrame-1, animLength, endPos, endOrient, endScale);
142                        Ogre::Animation* anim = m_pSkel->createAnimation(curAnimName, animLength);
143                        if(anim) {
144                                Ogre::AnimationStateSet* animSet = new Ogre::AnimationStateSet(); 
145
146                                for ( int i=0; i < m_pSkel->getNumBones(); i++)
147                                {
148                                        Ogre::Bone* pBone = m_pSkel->getBone(i);
149                                        Ogre::NodeAnimationTrack* pAnimTrack = anim->createNodeTrack(pBone->getHandle(),pBone);
150
151                                        CIntermediateBone* pIBone = m_pISkel->GetBoneByName( pBone->getName().c_str() );
152                                       
153                                        for ( int j=0; j < pIBone->GetFrameCount(curAnimName); j++)
154                                        {
155                                                float fTimeInSecs;
156                                                Ogre::Vector3 pos;
157                                                Ogre::Vector3 scale;
158                                                Ogre::Quaternion orient;
159                                                pIBone->GetFrame(curAnimName,j,fTimeInSecs,pos,orient,scale);
160
161                                                Ogre::TransformKeyFrame* pKeyFrame = pAnimTrack->createNodeKeyFrame(fTimeInSecs);
162                                                pKeyFrame->setRotation( orient );
163                                                pKeyFrame->setScale( scale );
164                                                pKeyFrame->setTranslate( pos );
165                                        }
166
167                                        //// Add end animation Spline keyframe for tangent generation.
168                                        //int iEndFrame = pIBone->GetFrameCount();
169                                        //Ogre::TransformKeyFrame* pKeyFrame = pAnimTrack->createNodeKeyFrame(anim->getLength());
170                                        //Ogre::Vector3 pos(0,0,0);
171                                        //Ogre::Vector3 scale(1,1,1);
172                                        //Ogre::Quaternion orient(Ogre::Radian(90), Ogre::Vector3::NEGATIVE_UNIT_X);
173                                        ////pIBone->GetFrame( iEndFrame-1,pos,orient,scale );
174                                        //pKeyFrame->setRotation( orient );
175                                        //pKeyFrame->setScale( scale );
176                                        //pKeyFrame->setTranslate( pos );
177
178                                        if(pCurAnimData->GetOptimize())
179                                                pAnimTrack->optimise();
180                                }
181                                m_pSkel->_initAnimationState(animSet);
182                                m_pOgreMesh->_initAnimationState(animSet);
183                                m_pSkel->setAnimationState(*animSet);
184                        }
185                }
186        }
187}
188
189bool COgreSkeletonCompiler::WriteOgreSkeleton( const Ogre::String& sFilename )
190{
191        Ogre::SkeletonSerializer* pSkeletonWriter = new Ogre::SkeletonSerializer();
192        try
193        {
194                pSkeletonWriter->exportSkeleton( m_pSkel.get(), sFilename );
195        }
196        catch (Ogre::Exception& e)
197        {
198                LOGERROR "OgreExeception caught: %s", e.getDescription().c_str()); 
199                return false;
200        }
201        delete pSkeletonWriter;
202        return true;
203}
Note: See TracBrowser for help on using the repository browser.