2010-09-24 18:54:20 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* 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
|
|
|
/*
|
|
|
|
* Defines a raw 3d model.
|
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 "ModelDef.h"
|
2006-12-15 17:09:30 +01:00
|
|
|
#include "graphics/SkeletonAnimDef.h"
|
2008-02-04 12:40:42 +01:00
|
|
|
#include "ps/FileIo.h"
|
2006-12-15 17:09:30 +01:00
|
|
|
#include "maths/Vector4D.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
CVector3D CModelDef::SkinPoint(const SModelVertex& vtx,
|
|
|
|
const CMatrix3D newPoseMatrices[],
|
|
|
|
const CMatrix3D inverseBindMatrices[])
|
|
|
|
{
|
|
|
|
CVector3D result (0, 0, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < SVertexBlend::SIZE && vtx.m_Blend.m_Bone[i] != 0xff; ++i)
|
|
|
|
{
|
|
|
|
CVector3D bindSpace = inverseBindMatrices[vtx.m_Blend.m_Bone[i]].Transform(vtx.m_Coords);
|
|
|
|
CVector3D worldSpace = newPoseMatrices[vtx.m_Blend.m_Bone[i]].Transform(bindSpace);
|
|
|
|
result += worldSpace * vtx.m_Blend.m_Weight[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVector3D CModelDef::SkinNormal(const SModelVertex& vtx,
|
|
|
|
const CMatrix3D newPoseMatrices[],
|
|
|
|
const CMatrix3D inverseBindMatrices[])
|
|
|
|
{
|
|
|
|
// To be correct, the normal vectors apparently need to be multiplied by the
|
|
|
|
// inverse of the transpose. Unfortunately inverses are slow.
|
|
|
|
// If a matrix is orthogonal, M * M^T = I and so the inverse of the transpose
|
|
|
|
// is the original matrix. But that's not entirely relevant here, because
|
2007-02-09 18:04:55 +01:00
|
|
|
// the bone matrices include translation components and so they're not
|
2006-12-15 17:09:30 +01:00
|
|
|
// orthogonal.
|
|
|
|
// But that's okay because we have
|
|
|
|
// M = T * R
|
|
|
|
// and want to find
|
|
|
|
// n' = (M^T^-1) * n
|
|
|
|
// = (T * R)^T^-1 * n
|
|
|
|
// = (R^T * T^T)^-1 * n
|
|
|
|
// = (T^T^-1 * R^T^-1) * n
|
|
|
|
// R is indeed orthogonal so R^T^-1 = R. T isn't orthogonal at all.
|
|
|
|
// But n is only a 3-vector, and from the forms of T and R (which have
|
|
|
|
// lots of zeroes) I can convince myself that replacing T with T^T^-1 has no
|
|
|
|
// effect on anything but the fourth component of M^T^-1 - and the fourth
|
|
|
|
// component is discarded since it has no effect on n', and so we can happily
|
|
|
|
// use n' = M*n.
|
|
|
|
//
|
|
|
|
// (This isn't very good as a proof, but it's better than assuming M is
|
|
|
|
// orthogonal when it's clearly not.)
|
|
|
|
|
|
|
|
CVector3D result (0, 0, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < SVertexBlend::SIZE && vtx.m_Blend.m_Bone[i] != 0xff; ++i)
|
|
|
|
{
|
|
|
|
CVector3D bindSpace = inverseBindMatrices[vtx.m_Blend.m_Bone[i]].Rotate(vtx.m_Norm);
|
|
|
|
CVector3D worldSpace = newPoseMatrices[vtx.m_Blend.m_Bone[i]].Rotate(bindSpace);
|
|
|
|
result += worldSpace * vtx.m_Blend.m_Weight[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was more than one influence, the result is probably not going
|
|
|
|
// to be of unit length (since it's a weighted sum of several independent
|
|
|
|
// unit vectors), so we need to normalise it.
|
|
|
|
// (It's fairly common to only have one influence, so it seems sensible to
|
|
|
|
// optimise that case a bit.)
|
|
|
|
if (vtx.m_Blend.m_Bone[1] != 0xff) // if more than one influence
|
|
|
|
result.Normalize();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2005-10-25 04:00:09 +02:00
|
|
|
|
2010-09-24 18:54:20 +02:00
|
|
|
void CModelDef::SkinPointsAndNormals(
|
|
|
|
size_t numVertices,
|
|
|
|
const VertexArrayIterator<CVector3D>& Position,
|
|
|
|
const VertexArrayIterator<CVector3D>& Normal,
|
|
|
|
const SModelVertex* vertices,
|
|
|
|
const CMatrix3D newPoseMatrices[],
|
|
|
|
const CMatrix3D inverseBindMatrices[])
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < numVertices; ++j)
|
|
|
|
{
|
|
|
|
const SModelVertex vtx = vertices[j];
|
|
|
|
|
|
|
|
CVector3D pos(0, 0, 0);
|
|
|
|
CVector3D normal(0, 0, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < SVertexBlend::SIZE && vtx.m_Blend.m_Bone[i] != 0xff; ++i)
|
|
|
|
{
|
|
|
|
CVector3D posBindSpace = inverseBindMatrices[vtx.m_Blend.m_Bone[i]].Transform(vtx.m_Coords);
|
|
|
|
CVector3D normBindSpace = inverseBindMatrices[vtx.m_Blend.m_Bone[i]].Rotate(vtx.m_Norm);
|
|
|
|
|
|
|
|
CVector3D posWorldSpace = newPoseMatrices[vtx.m_Blend.m_Bone[i]].Transform(posBindSpace);
|
|
|
|
CVector3D normWorldSpace = newPoseMatrices[vtx.m_Blend.m_Bone[i]].Rotate(normBindSpace);
|
|
|
|
|
|
|
|
pos += posWorldSpace * vtx.m_Blend.m_Weight[i];
|
|
|
|
normal += normWorldSpace * vtx.m_Blend.m_Weight[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was more than one influence, the result is probably not going
|
|
|
|
// to be of unit length (since it's a weighted sum of several independent
|
|
|
|
// unit vectors), so we need to normalise it.
|
|
|
|
// (It's fairly common to only have one influence, so it seems sensible to
|
|
|
|
// optimise that case a bit.)
|
|
|
|
if (vtx.m_Blend.m_Bone[1] != 0xff) // if more than one influence
|
|
|
|
normal.Normalize();
|
|
|
|
|
|
|
|
Position[j] = pos;
|
|
|
|
Normal[j] = normal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
// CModelDef Constructor
|
|
|
|
CModelDef::CModelDef()
|
2005-10-07 17:24:29 +02:00
|
|
|
: m_NumVertices(0), m_pVertices(0), m_NumFaces(0), m_pFaces(0), m_NumBones(0), m_Bones(0),
|
2009-11-03 22:46:35 +01:00
|
|
|
m_NumPropPoints(0), m_PropPoints(0), m_Name(L"[not loaded]")
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// CModelDef Destructor
|
|
|
|
CModelDef::~CModelDef()
|
|
|
|
{
|
2005-10-25 04:00:09 +02:00
|
|
|
for(RenderDataMap::iterator it = m_RenderData.begin(); it != m_RenderData.end(); ++it)
|
|
|
|
delete it->second;
|
2004-05-29 22:56:24 +02:00
|
|
|
delete[] m_pVertices;
|
|
|
|
delete[] m_pFaces;
|
2004-05-30 02:46:58 +02:00
|
|
|
delete[] m_Bones;
|
2004-05-29 22:56:24 +02:00
|
|
|
delete[] m_PropPoints;
|
|
|
|
}
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
// FindPropPoint: find and return pointer to prop point matching given name;
|
|
|
|
// return null if no match (case insensitive search)
|
|
|
|
SPropPoint* CModelDef::FindPropPoint(const char* name) const
|
|
|
|
{
|
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_NumPropPoints; ++i)
|
2006-12-15 17:09:30 +01:00
|
|
|
if (m_PropPoints[i].m_Name == name)
|
2004-05-30 02:46:58 +02:00
|
|
|
return &m_PropPoints[i];
|
2006-12-15 17:09:30 +01:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
// Load: read and return a new CModelDef initialised with data from given file
|
2009-11-03 22:46:35 +01:00
|
|
|
CModelDef* CModelDef::Load(const VfsPath& filename, const VfsPath& name)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
CFileUnpacker unpacker;
|
2006-04-14 06:35:36 +02:00
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
// read everything in from file
|
|
|
|
unpacker.Read(filename,"PSMD");
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2006-12-20 04:22:24 +01:00
|
|
|
std::auto_ptr<CModelDef> mdef (new CModelDef());
|
|
|
|
mdef->m_Name = name;
|
2006-04-14 06:35:36 +02:00
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
// now unpack everything
|
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
|
|
|
mdef->m_NumVertices = unpacker.UnpackSize();
|
2006-12-15 17:09:30 +01:00
|
|
|
mdef->m_pVertices=new SModelVertex[mdef->m_NumVertices];
|
|
|
|
unpacker.UnpackRaw(mdef->m_pVertices,sizeof(SModelVertex)*mdef->m_NumVertices);
|
|
|
|
|
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
|
|
|
mdef->m_NumFaces = unpacker.UnpackSize();
|
2006-12-15 17:09:30 +01:00
|
|
|
mdef->m_pFaces=new SModelFace[mdef->m_NumFaces];
|
|
|
|
unpacker.UnpackRaw(mdef->m_pFaces,sizeof(SModelFace)*mdef->m_NumFaces);
|
|
|
|
|
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
|
|
|
mdef->m_NumBones = unpacker.UnpackSize();
|
2006-12-15 17:09:30 +01:00
|
|
|
if (mdef->m_NumBones)
|
|
|
|
{
|
|
|
|
mdef->m_Bones=new CBoneState[mdef->m_NumBones];
|
|
|
|
unpacker.UnpackRaw(mdef->m_Bones,mdef->m_NumBones*sizeof(CBoneState));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unpacker.GetVersion() >= 2)
|
|
|
|
{
|
|
|
|
// versions >=2 also have prop point data
|
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
|
|
|
mdef->m_NumPropPoints = unpacker.UnpackSize();
|
2006-12-15 17:09:30 +01:00
|
|
|
if (mdef->m_NumPropPoints) {
|
|
|
|
mdef->m_PropPoints=new SPropPoint[mdef->m_NumPropPoints];
|
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<mdef->m_NumPropPoints;i++) {
|
2006-12-15 17:09:30 +01:00
|
|
|
unpacker.UnpackString(mdef->m_PropPoints[i].m_Name);
|
|
|
|
unpacker.UnpackRaw(&mdef->m_PropPoints[i].m_Position.X,sizeof(mdef->m_PropPoints[i].m_Position));
|
|
|
|
unpacker.UnpackRaw(&mdef->m_PropPoints[i].m_Rotation.m_V.X,sizeof(mdef->m_PropPoints[i].m_Rotation));
|
|
|
|
unpacker.UnpackRaw(&mdef->m_PropPoints[i].m_BoneIndex,sizeof(mdef->m_PropPoints[i].m_BoneIndex));
|
|
|
|
|
|
|
|
// build prop point transform
|
|
|
|
mdef->m_PropPoints[i].m_Transform.SetIdentity();
|
|
|
|
mdef->m_PropPoints[i].m_Transform.Rotate(mdef->m_PropPoints[i].m_Rotation);
|
|
|
|
mdef->m_PropPoints[i].m_Transform.Translate(mdef->m_PropPoints[i].m_Position);
|
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2006-12-15 17:09:30 +01:00
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
if (unpacker.GetVersion() <= 2)
|
|
|
|
{
|
|
|
|
// Versions <=2 store the vertexes relative to the bind pose. That
|
|
|
|
// isn't useful when you want to do correct skinning, so later versions
|
|
|
|
// store them in world space. So, fix the old models by skinning each
|
|
|
|
// vertex:
|
|
|
|
|
|
|
|
if (mdef->m_NumBones) // only do skinned models
|
|
|
|
{
|
|
|
|
CMatrix3D identity;
|
|
|
|
identity.SetIdentity();
|
|
|
|
std::vector<CMatrix3D> identityBones (mdef->m_NumBones, identity);
|
|
|
|
|
|
|
|
std::vector<CMatrix3D> bindPose (mdef->m_NumBones);
|
|
|
|
|
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 < mdef->m_NumBones; ++i)
|
2006-12-15 17:09:30 +01:00
|
|
|
{
|
|
|
|
bindPose[i].SetIdentity();
|
|
|
|
bindPose[i].Rotate(mdef->m_Bones[i].m_Rotation);
|
|
|
|
bindPose[i].Translate(mdef->m_Bones[i].m_Translation);
|
|
|
|
}
|
|
|
|
|
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 < mdef->m_NumVertices; ++i)
|
2006-12-15 17:09:30 +01:00
|
|
|
{
|
|
|
|
mdef->m_pVertices[i].m_Coords = SkinPoint(mdef->m_pVertices[i], &bindPose[0], &identityBones[0]);
|
|
|
|
mdef->m_pVertices[i].m_Norm = SkinNormal(mdef->m_pVertices[i], &bindPose[0], &identityBones[0]);
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
return mdef.release();
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save: write the given CModelDef to the given file
|
2008-01-07 21:03:19 +01:00
|
|
|
void CModelDef::Save(const VfsPath& filename,const CModelDef* mdef)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2004-12-15 15:26:21 +01:00
|
|
|
CFilePacker packer(FILE_VERSION, "PSMD");
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
// pack everything up
|
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 numVertices = mdef->GetNumVertices();
|
|
|
|
packer.PackSize(numVertices);
|
2004-05-29 22:56:24 +02:00
|
|
|
packer.PackRaw(mdef->GetVertices(),sizeof(SModelVertex)*numVertices);
|
|
|
|
|
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 numFaces = mdef->GetNumFaces();
|
|
|
|
packer.PackSize(numFaces);
|
2004-05-29 22:56:24 +02:00
|
|
|
packer.PackRaw(mdef->GetFaces(),sizeof(SModelFace)*numFaces);
|
|
|
|
|
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 numBones = mdef->m_NumBones;
|
|
|
|
packer.PackSize(numBones);
|
|
|
|
if (numBones)
|
|
|
|
packer.PackRaw(mdef->m_Bones,sizeof(CBoneState)*numBones);
|
|
|
|
|
|
|
|
const size_t numPropPoints = mdef->m_NumPropPoints;
|
|
|
|
packer.PackSize(numPropPoints);
|
|
|
|
for (size_t i=0;i<mdef->m_NumPropPoints;i++) {
|
2004-05-30 02:46:58 +02:00
|
|
|
packer.PackString(mdef->m_PropPoints[i].m_Name);
|
|
|
|
packer.PackRaw(&mdef->m_PropPoints[i].m_Position.X,sizeof(mdef->m_PropPoints[i].m_Position));
|
|
|
|
packer.PackRaw(&mdef->m_PropPoints[i].m_Rotation.m_V.X,sizeof(mdef->m_PropPoints[i].m_Rotation));
|
|
|
|
packer.PackRaw(&mdef->m_PropPoints[i].m_BoneIndex,sizeof(mdef->m_PropPoints[i].m_BoneIndex));
|
|
|
|
}
|
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
// flush everything out to file
|
2004-12-15 15:26:21 +01:00
|
|
|
packer.Write(filename);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-10-25 04:00:09 +02:00
|
|
|
// SetRenderData: Set the render data object for the given key,
|
|
|
|
void CModelDef::SetRenderData(const void* key, CModelDefRPrivate* data)
|
2005-10-02 23:11:11 +02:00
|
|
|
{
|
2005-10-25 04:00:09 +02:00
|
|
|
delete m_RenderData[key];
|
|
|
|
m_RenderData[key] = data;
|
2005-10-02 23:11:11 +02:00
|
|
|
}
|
|
|
|
|
2005-10-25 04:00:09 +02:00
|
|
|
// GetRenderData: Get the render data object for the given key,
|
|
|
|
// or 0 if no such object exists.
|
|
|
|
// Reference count of the render data object is automatically increased.
|
|
|
|
CModelDefRPrivate* CModelDef::GetRenderData(const void* key) const
|
|
|
|
{
|
|
|
|
RenderDataMap::const_iterator it = m_RenderData.find(key);
|
|
|
|
|
|
|
|
if (it != m_RenderData.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|