2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Raw description of a skeleton animation
|
2007-05-07 18:33:24 +02:00
|
|
|
*/
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
#include "SkeletonAnimDef.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
#include "maths/MathUtil.h"
|
2008-02-04 12:40:42 +01:00
|
|
|
#include "ps/FileIo.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CSkeletonAnimDef constructor
|
2005-10-07 17:24:29 +02:00
|
|
|
CSkeletonAnimDef::CSkeletonAnimDef() : m_FrameTime(0), m_NumKeys(0), m_NumFrames(0), m_Keys(0)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CSkeletonAnimDef destructor
|
|
|
|
CSkeletonAnimDef::~CSkeletonAnimDef()
|
|
|
|
{
|
|
|
|
delete[] m_Keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BuildBoneMatrices: build matrices for all bones at the given time (in MS) in this
|
|
|
|
// animation
|
2007-03-01 01:14:35 +01:00
|
|
|
void CSkeletonAnimDef::BuildBoneMatrices(float time, CMatrix3D* matrices, bool loop) const
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2007-02-10 04:09:52 +01:00
|
|
|
float fstartframe = time/m_FrameTime;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t startframe = (size_t)(int)(time/m_FrameTime);
|
2007-02-10 04:09:52 +01:00
|
|
|
float deltatime = fstartframe-startframe;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
startframe %= m_NumFrames;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t endframe = startframe + 1;
|
2007-02-10 04:09:52 +01:00
|
|
|
endframe %= m_NumFrames;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2007-03-01 01:14:35 +01:00
|
|
|
if (!loop && endframe == 0)
|
2007-02-10 04:09:52 +01:00
|
|
|
{
|
|
|
|
// This might be something like a death animation, and interpolating
|
|
|
|
// between the final frame and the initial frame is wrong, because they're
|
|
|
|
// totally different. So if we've looped around to endframe==0, just display
|
|
|
|
// the animation's final frame with no interpolation.
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for (size_t i = 0; i < m_NumKeys; i++)
|
2007-02-10 04:09:52 +01:00
|
|
|
{
|
|
|
|
const Key& key = GetKey(startframe, i);
|
|
|
|
matrices[i].SetIdentity();
|
|
|
|
matrices[i].Rotate(key.m_Rotation);
|
|
|
|
matrices[i].Translate(key.m_Translation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for (size_t i = 0; i < m_NumKeys; i++)
|
2007-02-10 04:09:52 +01:00
|
|
|
{
|
2007-03-01 01:14:35 +01:00
|
|
|
const Key& startkey = GetKey(startframe, i);
|
2007-02-10 04:09:52 +01:00
|
|
|
const Key& endkey = GetKey(endframe, i);
|
|
|
|
|
|
|
|
CVector3D trans = Interpolate(startkey.m_Translation, endkey.m_Translation, deltatime);
|
|
|
|
// TODO: is slerp the best thing to use here?
|
|
|
|
CQuaternion rot;
|
|
|
|
rot.Slerp(startkey.m_Rotation, endkey.m_Rotation, deltatime);
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2007-03-01 01:14:35 +01:00
|
|
|
rot.ToMatrix(matrices[i]);
|
2007-02-10 04:09:52 +01:00
|
|
|
matrices[i].Translate(trans);
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Load: try to load the anim from given file; return a new anim if successful
|
2008-01-07 21:03:19 +01:00
|
|
|
CSkeletonAnimDef* CSkeletonAnimDef::Load(const VfsPath& filename)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
CFileUnpacker unpacker;
|
|
|
|
unpacker.Read(filename,"PSSA");
|
|
|
|
|
|
|
|
// check version
|
|
|
|
if (unpacker.GetVersion()<FILE_READ_VERSION) {
|
2006-04-04 06:14:10 +02:00
|
|
|
throw PSERROR_File_InvalidVersion();
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// unpack the data
|
|
|
|
CSkeletonAnimDef* anim=new CSkeletonAnimDef;
|
|
|
|
try {
|
2006-03-17 04:59:49 +01:00
|
|
|
CStr name; // unused - just here to maintain compatibility with the animation files
|
|
|
|
unpacker.UnpackString(name);
|
2004-05-29 22:56:24 +02:00
|
|
|
unpacker.UnpackRaw(&anim->m_FrameTime,sizeof(anim->m_FrameTime));
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
anim->m_NumKeys = unpacker.UnpackSize();
|
|
|
|
anim->m_NumFrames = unpacker.UnpackSize();
|
2004-05-29 22:56:24 +02:00
|
|
|
anim->m_Keys=new Key[anim->m_NumKeys*anim->m_NumFrames];
|
|
|
|
unpacker.UnpackRaw(anim->m_Keys,anim->m_NumKeys*anim->m_NumFrames*sizeof(Key));
|
2006-04-04 06:14:10 +02:00
|
|
|
} catch (PSERROR_File&) {
|
2004-05-29 22:56:24 +02:00
|
|
|
delete anim;
|
2005-10-30 22:30:52 +01:00
|
|
|
throw;
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return anim;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Save: try to save anim to file
|
2009-11-03 22:46:35 +01:00
|
|
|
void CSkeletonAnimDef::Save(const VfsPath& pathname,const CSkeletonAnimDef* anim)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2004-12-15 15:26:21 +01:00
|
|
|
CFilePacker packer(FILE_VERSION, "PSSA");
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
// pack up all the data
|
2006-03-17 04:59:49 +01:00
|
|
|
packer.PackString("");
|
2004-05-29 22:56:24 +02:00
|
|
|
packer.PackRaw(&anim->m_FrameTime,sizeof(anim->m_FrameTime));
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t numKeys = anim->m_NumKeys;
|
|
|
|
packer.PackSize(numKeys);
|
|
|
|
const size_t numFrames = anim->m_NumFrames;
|
|
|
|
packer.PackSize(numFrames);
|
|
|
|
packer.PackRaw(anim->m_Keys,numKeys*numFrames*sizeof(Key));
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
// now write it
|
2009-11-03 22:46:35 +01:00
|
|
|
packer.Write(pathname);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|