2020-12-31 16:37:28 +01:00
|
|
|
/* Copyright (C) 2020 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"
|
2020-12-31 16:37:28 +01:00
|
|
|
#include "lib/sse.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
|
|
|
|
2020-12-31 16:37:28 +01:00
|
|
|
#if COMPILER_HAS_SSE
|
2011-11-10 00:11:28 +01:00
|
|
|
# include <xmmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
CVector3D CModelDef::SkinPoint(const SModelVertex& vtx,
|
2011-07-13 01:48:05 +02:00
|
|
|
const CMatrix3D newPoseMatrices[])
|
2006-12-15 17:09:30 +01:00
|
|
|
{
|
|
|
|
CVector3D result (0, 0, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < SVertexBlend::SIZE && vtx.m_Blend.m_Bone[i] != 0xff; ++i)
|
|
|
|
{
|
2011-07-13 01:48:05 +02:00
|
|
|
result += newPoseMatrices[vtx.m_Blend.m_Bone[i]].Transform(vtx.m_Coords) * vtx.m_Blend.m_Weight[i];
|
2006-12-15 17:09:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVector3D CModelDef::SkinNormal(const SModelVertex& vtx,
|
2011-07-13 01:48:05 +02:00
|
|
|
const CMatrix3D newPoseMatrices[])
|
2006-12-15 17:09:30 +01:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2011-07-13 01:48:05 +02:00
|
|
|
result += newPoseMatrices[vtx.m_Blend.m_Bone[i]].Rotate(vtx.m_Norm) * vtx.m_Blend.m_Weight[i];
|
2006-12-15 17:09:30 +01:00
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
// 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
|
|
|
|
2020-12-31 16:37:28 +01:00
|
|
|
void(*CModelDef::SkinPointsAndNormals)(
|
|
|
|
size_t numVertices,
|
|
|
|
const VertexArrayIterator<CVector3D>& Position,
|
|
|
|
const VertexArrayIterator<CVector3D>& Normal,
|
|
|
|
const SModelVertex* vertices,
|
|
|
|
const size_t* blendIndices,
|
|
|
|
const CMatrix3D newPoseMatrices[]) {};
|
|
|
|
|
|
|
|
|
|
|
|
static void SkinPointsAndNormalsFallback(
|
2010-09-24 18:54:20 +02:00
|
|
|
size_t numVertices,
|
|
|
|
const VertexArrayIterator<CVector3D>& Position,
|
|
|
|
const VertexArrayIterator<CVector3D>& Normal,
|
|
|
|
const SModelVertex* vertices,
|
2011-07-13 01:48:05 +02:00
|
|
|
const size_t* blendIndices,
|
|
|
|
const CMatrix3D newPoseMatrices[])
|
2010-09-24 18:54:20 +02:00
|
|
|
{
|
2011-11-10 00:11:28 +01:00
|
|
|
// To avoid some performance overhead, get the raw vertex array pointers
|
|
|
|
char* PositionData = Position.GetData();
|
|
|
|
size_t PositionStride = Position.GetStride();
|
|
|
|
char* NormalData = Normal.GetData();
|
|
|
|
size_t NormalStride = Normal.GetStride();
|
|
|
|
|
2010-09-24 18:54:20 +02:00
|
|
|
for (size_t j = 0; j < numVertices; ++j)
|
|
|
|
{
|
2011-07-13 01:48:05 +02:00
|
|
|
const SModelVertex& vtx = vertices[j];
|
2010-09-24 18:54:20 +02:00
|
|
|
|
2011-11-10 00:11:28 +01:00
|
|
|
CVector3D pos = newPoseMatrices[blendIndices[j]].Transform(vtx.m_Coords);
|
|
|
|
CVector3D norm = newPoseMatrices[blendIndices[j]].Rotate(vtx.m_Norm);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
norm.Normalize();
|
|
|
|
|
|
|
|
memcpy(PositionData + PositionStride*j, &pos.X, 3*sizeof(float));
|
|
|
|
memcpy(NormalData + NormalStride*j, &norm.X, 3*sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 16:37:28 +01:00
|
|
|
#if COMPILER_HAS_SSE
|
|
|
|
static void SkinPointsAndNormalsSSE(
|
2011-11-10 00:11:28 +01:00
|
|
|
size_t numVertices,
|
|
|
|
const VertexArrayIterator<CVector3D>& Position,
|
|
|
|
const VertexArrayIterator<CVector3D>& Normal,
|
|
|
|
const SModelVertex* vertices,
|
|
|
|
const size_t* blendIndices,
|
|
|
|
const CMatrix3D newPoseMatrices[])
|
|
|
|
{
|
|
|
|
// To avoid some performance overhead, get the raw vertex array pointers
|
|
|
|
char* PositionData = Position.GetData();
|
|
|
|
size_t PositionStride = Position.GetStride();
|
|
|
|
char* NormalData = Normal.GetData();
|
|
|
|
size_t NormalStride = Normal.GetStride();
|
|
|
|
|
|
|
|
// Must be aligned correctly for SSE
|
|
|
|
ASSERT((intptr_t)newPoseMatrices % 16 == 0);
|
|
|
|
ASSERT((intptr_t)PositionData % 16 == 0);
|
|
|
|
ASSERT((intptr_t)PositionStride % 16 == 0);
|
2012-04-12 17:43:59 +02:00
|
|
|
ASSERT((intptr_t)NormalData % 16 == 0);
|
2011-11-10 00:11:28 +01:00
|
|
|
ASSERT((intptr_t)NormalStride % 16 == 0);
|
|
|
|
|
|
|
|
__m128 col0, col1, col2, col3, vec0, vec1, vec2;
|
|
|
|
|
|
|
|
for (size_t j = 0; j < numVertices; ++j)
|
|
|
|
{
|
|
|
|
const SModelVertex& vtx = vertices[j];
|
|
|
|
const CMatrix3D& mtx = newPoseMatrices[blendIndices[j]];
|
|
|
|
|
|
|
|
// Loads matrix to xmm registers.
|
|
|
|
col0 = _mm_load_ps(mtx._data);
|
|
|
|
col1 = _mm_load_ps(mtx._data + 4);
|
|
|
|
col2 = _mm_load_ps(mtx._data + 8);
|
|
|
|
col3 = _mm_load_ps(mtx._data + 12);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2011-11-10 00:11:28 +01:00
|
|
|
// Loads and computes vertex coordinates.
|
2012-01-28 13:17:44 +01:00
|
|
|
vec0 = _mm_load1_ps(&vtx.m_Coords.X); // v0 = [x, x, x, x]
|
|
|
|
vec0 = _mm_mul_ps(col0, vec0); // v0 = [_11*x, _21*x, _31*x, _41*x]
|
|
|
|
vec1 = _mm_load1_ps(&vtx.m_Coords.Y); // v1 = [y, y, y, y]
|
|
|
|
vec1 = _mm_mul_ps(col1, vec1); // v1 = [_12*y, _22*y, _32*y, _42*y]
|
|
|
|
vec0 = _mm_add_ps(vec0, vec1); // v0 = [_11*x + _12*y, ...]
|
|
|
|
vec1 = _mm_load1_ps(&vtx.m_Coords.Z); // v1 = [z, z, z, z]
|
|
|
|
vec1 = _mm_mul_ps(col2, vec1); // v1 = [_13*z, _23*z, _33*z, _43*z]
|
|
|
|
vec1 = _mm_add_ps(vec1, col3); // v1 = [_13*z + _14, ...]
|
|
|
|
vec0 = _mm_add_ps(vec0, vec1); // v0 = [_11*x + _12*y + _13*z + _14, ...]
|
2011-11-10 00:11:28 +01:00
|
|
|
_mm_store_ps((float*)(PositionData + PositionStride*j), vec0);
|
|
|
|
|
|
|
|
// Loads and computes normal vectors.
|
2012-01-28 13:17:44 +01:00
|
|
|
vec0 = _mm_load1_ps(&vtx.m_Norm.X); // v0 = [x, x, x, x]
|
|
|
|
vec0 = _mm_mul_ps(col0, vec0); // v0 = [_11*x, _21*x, _31*x, _41*x]
|
|
|
|
vec1 = _mm_load1_ps(&vtx.m_Norm.Y); // v1 = [y, y, y, y]
|
|
|
|
vec1 = _mm_mul_ps(col1, vec1); // v1 = [_12*y, _22*y, _32*y, _42*y]
|
|
|
|
vec0 = _mm_add_ps(vec0, vec1); // v0 = [_11*x + _12*y, ...]
|
|
|
|
vec1 = _mm_load1_ps(&vtx.m_Norm.Z); // v1 = [z, z, z, z]
|
|
|
|
vec1 = _mm_mul_ps(col2, vec1); // v1 = [_13*z, _23*z, _33*z, _43*z]
|
|
|
|
vec0 = _mm_add_ps(vec0, vec1); // v0 = [_11*x + _12*y + _13*z, ...]
|
2010-09-24 18:54:20 +02:00
|
|
|
|
|
|
|
// 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
|
2011-11-10 00:11:28 +01:00
|
|
|
{
|
|
|
|
// Normalization.
|
|
|
|
// vec1 = [x*x, y*y, z*z, ?*?]
|
|
|
|
vec1 = _mm_mul_ps(vec0, vec0);
|
2012-01-28 13:17:44 +01:00
|
|
|
// vec2 = [y*y, z*z, x*x, ?*?]
|
|
|
|
vec2 = _mm_shuffle_ps(vec1, vec1, _MM_SHUFFLE(3, 0, 2, 1));
|
2011-11-10 00:11:28 +01:00
|
|
|
vec1 = _mm_add_ps(vec1, vec2);
|
2012-01-28 13:17:44 +01:00
|
|
|
// vec2 = [z*z, x*x, y*y, ?*?]
|
|
|
|
vec2 = _mm_shuffle_ps(vec2, vec2, _MM_SHUFFLE(3, 0, 2, 1));
|
2011-11-10 00:11:28 +01:00
|
|
|
vec1 = _mm_add_ps(vec1, vec2);
|
|
|
|
// rsqrt(a) = 1 / sqrt(a)
|
|
|
|
vec1 = _mm_rsqrt_ps(vec1);
|
|
|
|
vec0 = _mm_mul_ps(vec0, vec1);
|
|
|
|
}
|
|
|
|
_mm_store_ps((float*)(NormalData + NormalStride*j), vec0);
|
2011-07-13 01:48:05 +02:00
|
|
|
}
|
|
|
|
}
|
2011-11-10 00:11:28 +01:00
|
|
|
#endif
|
2010-09-24 18:54:20 +02:00
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
void CModelDef::BlendBoneMatrices(
|
|
|
|
CMatrix3D boneMatrices[])
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_NumBlends; ++i)
|
|
|
|
{
|
|
|
|
const SVertexBlend& blend = m_pBlends[i];
|
2012-03-05 01:42:34 +01:00
|
|
|
CMatrix3D& boneMatrix = boneMatrices[m_NumBones + 1 + i];
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2012-03-05 01:42:34 +01:00
|
|
|
// Note: there is a special case of joint influence, in which the vertex
|
|
|
|
// is influenced by the bind-shape matrix instead of a particular bone,
|
|
|
|
// which we indicate by setting the bone ID to the total number of bones.
|
|
|
|
// It should be blended with the world space transform and we have already
|
|
|
|
// set up this matrix in boneMatrices.
|
|
|
|
// (see http://trac.wildfiregames.com/ticket/1012)
|
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
boneMatrix.Blend(boneMatrices[blend.m_Bone[0]], blend.m_Weight[0]);
|
2012-04-12 17:43:59 +02:00
|
|
|
for (size_t j = 1; j < SVertexBlend::SIZE && blend.m_Bone[j] != 0xFF; ++j)
|
2011-07-13 01:48:05 +02:00
|
|
|
boneMatrix.AddBlend(boneMatrices[blend.m_Bone[j]], blend.m_Weight[j]);
|
2010-09-24 18:54:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
// CModelDef Constructor
|
2011-07-13 01:48:05 +02:00
|
|
|
CModelDef::CModelDef() :
|
2012-07-24 00:49:46 +02:00
|
|
|
m_NumVertices(0), m_NumUVsPerVertex(0), m_pVertices(0), m_NumFaces(0), m_pFaces(0),
|
2012-04-12 17:43:59 +02:00
|
|
|
m_NumBones(0), m_Bones(0), m_InverseBindBoneMatrices(NULL),
|
2011-07-13 01:48:05 +02:00
|
|
|
m_NumBlends(0), m_pBlends(0), m_pBlendIndices(0),
|
2010-11-20 21:16:06 +01:00
|
|
|
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;
|
2012-04-12 17:43:59 +02:00
|
|
|
delete[] m_InverseBindBoneMatrices;
|
2011-07-13 01:48:05 +02:00
|
|
|
delete[] m_pBlends;
|
|
|
|
delete[] m_pBlendIndices;
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// FindPropPoint: find and return pointer to prop point matching given name;
|
2004-05-30 02:46:58 +02:00
|
|
|
// return null if no match (case insensitive search)
|
2010-11-20 21:16:06 +01:00
|
|
|
const SPropPoint* CModelDef::FindPropPoint(const char* name) const
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2010-11-20 21:16:06 +01:00
|
|
|
for (size_t i = 0; i < m_PropPoints.size(); ++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");
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-05-25 03:23:27 +02:00
|
|
|
std::unique_ptr<CModelDef> mdef (new CModelDef());
|
2006-12-20 04:22:24 +01:00
|
|
|
mdef->m_Name = name;
|
2006-04-14 06:35:36 +02:00
|
|
|
|
2016-11-23 14:02:58 +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();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2012-07-24 00:49:46 +02:00
|
|
|
// versions prior to 4 only support 1 UV set, 4 and later store it here
|
2016-11-23 14:02:58 +01:00
|
|
|
if (unpacker.GetVersion() <= 3)
|
2012-07-24 00:49:46 +02:00
|
|
|
{
|
|
|
|
mdef->m_NumUVsPerVertex = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mdef->m_NumUVsPerVertex = unpacker.UnpackSize();
|
|
|
|
}
|
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
mdef->m_pVertices=new SModelVertex[mdef->m_NumVertices];
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2012-07-24 00:49:46 +02:00
|
|
|
for (size_t i = 0; i < mdef->m_NumVertices; ++i)
|
|
|
|
{
|
|
|
|
unpacker.UnpackRaw(&mdef->m_pVertices[i].m_Coords, 12);
|
|
|
|
unpacker.UnpackRaw(&mdef->m_pVertices[i].m_Norm, 12);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2012-07-24 00:49:46 +02:00
|
|
|
for (size_t s = 0; s < mdef->m_NumUVsPerVertex; ++s)
|
|
|
|
{
|
|
|
|
float uv[2];
|
|
|
|
unpacker.UnpackRaw(&uv[0], 8);
|
|
|
|
mdef->m_pVertices[i].m_UVs.push_back(uv[0]);
|
|
|
|
mdef->m_pVertices[i].m_UVs.push_back(uv[1]);
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2012-07-24 00:49:46 +02:00
|
|
|
unpacker.UnpackRaw(&mdef->m_pVertices[i].m_Blend, sizeof(SVertexBlend));
|
|
|
|
}
|
2016-11-23 15:09:58 +01: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
|
|
|
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);
|
2016-11-23 15:09:58 +01: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
|
|
|
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));
|
2011-07-13 01:48:05 +02:00
|
|
|
|
|
|
|
mdef->m_pBlendIndices = new size_t[mdef->m_NumVertices];
|
|
|
|
std::vector<SVertexBlend> blends;
|
|
|
|
for (size_t i = 0; i < mdef->m_NumVertices; i++)
|
|
|
|
{
|
|
|
|
const SVertexBlend &blend = mdef->m_pVertices[i].m_Blend;
|
|
|
|
if (blend.m_Bone[1] == 0xFF)
|
|
|
|
{
|
|
|
|
mdef->m_pBlendIndices[i] = blend.m_Bone[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If there's already a vertex using the same blend as this, then
|
|
|
|
// reuse its entry from blends; otherwise add the new one to blends
|
|
|
|
size_t j;
|
|
|
|
for (j = 0; j < blends.size(); j++)
|
|
|
|
{
|
|
|
|
if (blend == blends[j]) break;
|
|
|
|
}
|
|
|
|
if (j >= blends.size())
|
|
|
|
blends.push_back(blend);
|
2012-03-05 01:42:34 +01:00
|
|
|
// This index is offset by one to allow the special case of a
|
|
|
|
// weighted influence relative to the bind-shape rather than
|
|
|
|
// a particular bone. See comment in BlendBoneMatrices.
|
|
|
|
mdef->m_pBlendIndices[i] = mdef->m_NumBones + 1 + j;
|
2011-07-13 01:48:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mdef->m_NumBlends = blends.size();
|
|
|
|
mdef->m_pBlends = new SVertexBlend[mdef->m_NumBlends];
|
|
|
|
std::copy(blends.begin(), blends.end(), mdef->m_pBlends);
|
2006-12-15 17:09:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unpacker.GetVersion() >= 2)
|
|
|
|
{
|
|
|
|
// versions >=2 also have prop point data
|
2010-11-20 21:16:06 +01:00
|
|
|
size_t numPropPoints = unpacker.UnpackSize();
|
|
|
|
mdef->m_PropPoints.resize(numPropPoints);
|
|
|
|
if (numPropPoints)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < numPropPoints; i++)
|
|
|
|
{
|
2006-12-15 17:09:30 +01:00
|
|
|
unpacker.UnpackString(mdef->m_PropPoints[i].m_Name);
|
2010-11-20 21:16:06 +01:00
|
|
|
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));
|
2006-12-15 17:09:30 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2010-11-20 21:16:06 +01:00
|
|
|
if (unpacker.GetVersion() <= 2)
|
|
|
|
{
|
|
|
|
// Versions <=2 don't include the default 'root' prop point, so add it here
|
|
|
|
|
|
|
|
SPropPoint prop;
|
|
|
|
prop.m_Name = "root";
|
|
|
|
prop.m_Transform.SetIdentity();
|
|
|
|
prop.m_BoneIndex = 0xFF;
|
|
|
|
|
|
|
|
mdef->m_PropPoints.push_back(prop);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2011-07-13 01:48:05 +02:00
|
|
|
mdef->m_pVertices[i].m_Coords = SkinPoint(mdef->m_pVertices[i], &bindPose[0]);
|
|
|
|
mdef->m_pVertices[i].m_Norm = SkinNormal(mdef->m_pVertices[i], &bindPose[0]);
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2012-04-12 17:43:59 +02:00
|
|
|
// Compute the inverse bind-pose matrices, needed by the skinning code
|
|
|
|
mdef->m_InverseBindBoneMatrices = new CMatrix3D[mdef->m_NumBones];
|
|
|
|
CBoneState* defpose = mdef->GetBones();
|
|
|
|
for (size_t i = 0; i < mdef->m_NumBones; ++i)
|
|
|
|
{
|
|
|
|
mdef->m_InverseBindBoneMatrices[i].SetIdentity();
|
|
|
|
mdef->m_InverseBindBoneMatrices[i].Translate(-defpose[i].m_Translation);
|
|
|
|
mdef->m_InverseBindBoneMatrices[i].Rotate(defpose[i].m_Rotation.GetInverse());
|
|
|
|
}
|
|
|
|
|
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
|
2010-11-20 21:16:06 +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);
|
2010-11-20 21:16:06 +01: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);
|
2010-11-20 21:16:06 +01:00
|
|
|
packer.PackRaw(mdef->GetFaces(), sizeof(SModelFace) * numFaces);
|
2016-11-23 15:09:58 +01: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
|
|
|
const size_t numBones = mdef->m_NumBones;
|
|
|
|
packer.PackSize(numBones);
|
|
|
|
if (numBones)
|
2010-11-20 21:16:06 +01:00
|
|
|
packer.PackRaw(mdef->m_Bones, sizeof(CBoneState) * 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
|
|
|
|
2010-11-20 21:16:06 +01:00
|
|
|
const size_t numPropPoints = mdef->m_PropPoints.size();
|
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
|
|
|
packer.PackSize(numPropPoints);
|
2010-11-20 21:16:06 +01:00
|
|
|
for (size_t i = 0; i < numPropPoints; i++)
|
|
|
|
{
|
2004-05-30 02:46:58 +02:00
|
|
|
packer.PackString(mdef->m_PropPoints[i].m_Name);
|
2010-11-20 21:16:06 +01:00
|
|
|
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-30 02:46:58 +02:00
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
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);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2005-10-25 04:00:09 +02:00
|
|
|
if (it != m_RenderData.end())
|
|
|
|
return it->second;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2005-10-25 04:00:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-12-31 16:37:28 +01:00
|
|
|
|
|
|
|
void ModelDefActivateFastImpl()
|
|
|
|
{
|
|
|
|
#if COMPILER_HAS_SSE
|
|
|
|
if (HostHasSSE())
|
|
|
|
{
|
|
|
|
CModelDef::SkinPointsAndNormals = SkinPointsAndNormalsSSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
CModelDef::SkinPointsAndNormals = SkinPointsAndNormalsFallback;
|
|
|
|
}
|