LCOV - code coverage report
Current view: top level - source/graphics - ModelDef.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 75 241 31.1 %
Date: 2026-12-03 01:19:39 Functions: 5 14 35.7 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "ModelDef.h"
      21             : 
      22             : #include "graphics/SkeletonAnimDef.h"
      23             : #include "lib/debug.h"
      24             : #include "lib/sysdep/compiler.h"
      25             : #include "maths/Vector2D.h"
      26             : #include "ps/FileIo.h"
      27             : #include "renderer/VertexArray.h"
      28             : 
      29             : #include <algorithm>
      30             : #include <memory>
      31             : #include <SDL_cpuinfo.h>
      32             : #include <string>
      33             : #include <utility>
      34             : 
      35             : #if COMPILER_HAS_SSE
      36             : # include <xmmintrin.h>
      37             : #endif
      38             : 
      39           1 : void CModelDef::GetMaxBounds(CSkeletonAnimDef* anim, bool loop, CBoundingBoxAligned& result)
      40             : {
      41           1 :     const std::uint32_t animIndex = anim ? anim->m_UID : 0;
      42             : 
      43           1 :     std::unordered_map<std::uint32_t, CBoundingBoxAligned>::const_iterator it = m_MaxBoundsPerAnimDef.find(animIndex);
      44           1 :     if (it != m_MaxBoundsPerAnimDef.end())
      45             :     {
      46           0 :         result = it->second;
      47           1 :         return;
      48             :     }
      49             : 
      50           1 :     size_t numverts = GetNumVertices();
      51           1 :     SModelVertex* verts = GetVertices();
      52             : 
      53           1 :     if (!anim)
      54             :     {
      55           5 :         for (size_t i = 0; i < numverts; ++i)
      56           4 :             result += verts[i].m_Coords;
      57           1 :         m_MaxBoundsPerAnimDef[animIndex] = result;
      58           1 :         return;
      59             :     }
      60           0 :     ENSURE(animIndex != 0);
      61           0 :     CMatrix3D* inverseBindBoneMatrix = GetInverseBindBoneMatrices();
      62           0 :     std::vector<CMatrix3D> boneMatrix(anim->GetNumKeys());
      63             : 
      64           0 :     const size_t numFrames = anim->GetNumFrames();
      65           0 :     const float frameTime = anim->GetFrameTime();
      66           0 :     const size_t numBones = GetNumBones();
      67             : 
      68             :     // NB: by using frames, the bounds are technically pessimistic,
      69             :     // since interpolation between frames can put vertices farther.
      70           0 :     for (size_t j = 0; j < numFrames; ++j)
      71             :     {
      72           0 :         anim->BuildBoneMatrices(j * frameTime, boneMatrix.data(), loop);
      73           0 :         for (size_t i = 0; i < numBones; ++i)
      74           0 :             boneMatrix[i] = boneMatrix[i] * inverseBindBoneMatrix[i];
      75           0 :         for (size_t i = 0; i < numverts; ++i)
      76           0 :             result += SkinPoint(verts[i], boneMatrix.data());
      77             :     }
      78           0 :     m_MaxBoundsPerAnimDef[animIndex] = result;
      79             : }
      80             : 
      81           0 : CVector3D CModelDef::SkinPoint(const SModelVertex& vtx,
      82             :                                const CMatrix3D newPoseMatrices[])
      83             : {
      84           0 :     CVector3D result (0, 0, 0);
      85             : 
      86           0 :     for (int i = 0; i < SVertexBlend::SIZE && vtx.m_Blend.m_Bone[i] != 0xff; ++i)
      87             :     {
      88           0 :         result += newPoseMatrices[vtx.m_Blend.m_Bone[i]].Transform(vtx.m_Coords) * vtx.m_Blend.m_Weight[i];
      89             :     }
      90             : 
      91           0 :     return result;
      92             : }
      93             : 
      94           0 : CVector3D CModelDef::SkinNormal(const SModelVertex& vtx,
      95             :                                 const CMatrix3D newPoseMatrices[])
      96             : {
      97             :     // To be correct, the normal vectors apparently need to be multiplied by the
      98             :     // inverse of the transpose. Unfortunately inverses are slow.
      99             :     // If a matrix is orthogonal, M * M^T = I and so the inverse of the transpose
     100             :     // is the original matrix. But that's not entirely relevant here, because
     101             :     // the bone matrices include translation components and so they're not
     102             :     // orthogonal.
     103             :     // But that's okay because we have
     104             :     //   M = T * R
     105             :     // and want to find
     106             :     //   n' = (M^T^-1) * n
     107             :     //      = (T * R)^T^-1 * n
     108             :     //      = (R^T * T^T)^-1 * n
     109             :     //      = (T^T^-1 * R^T^-1) * n
     110             :     // R is indeed orthogonal so R^T^-1 = R. T isn't orthogonal at all.
     111             :     // But n is only a 3-vector, and from the forms of T and R (which have
     112             :     // lots of zeroes) I can convince myself that replacing T with T^T^-1 has no
     113             :     // effect on anything but the fourth component of M^T^-1 - and the fourth
     114             :     // component is discarded since it has no effect on n', and so we can happily
     115             :     // use n' = M*n.
     116             :     //
     117             :     // (This isn't very good as a proof, but it's better than assuming M is
     118             :     // orthogonal when it's clearly not.)
     119             : 
     120           0 :     CVector3D result (0, 0, 0);
     121             : 
     122           0 :     for (int i = 0; i < SVertexBlend::SIZE && vtx.m_Blend.m_Bone[i] != 0xff; ++i)
     123             :     {
     124           0 :         result += newPoseMatrices[vtx.m_Blend.m_Bone[i]].Rotate(vtx.m_Norm) * vtx.m_Blend.m_Weight[i];
     125             :     }
     126             : 
     127             :     // If there was more than one influence, the result is probably not going
     128             :     // to be of unit length (since it's a weighted sum of several independent
     129             :     // unit vectors), so we need to normalise it.
     130             :     // (It's fairly common to only have one influence, so it seems sensible to
     131             :     // optimise that case a bit.)
     132           0 :     if (vtx.m_Blend.m_Bone[1] != 0xff) // if more than one influence
     133           0 :         result.Normalize();
     134             : 
     135           0 :     return result;
     136             : }
     137             : 
     138             : void(*CModelDef::SkinPointsAndNormals)(
     139             :     size_t numVertices,
     140             :     const VertexArrayIterator<CVector3D>& Position,
     141             :     const VertexArrayIterator<CVector3D>& Normal,
     142             :     const SModelVertex* vertices,
     143             :     const size_t* blendIndices,
     144             :     const CMatrix3D newPoseMatrices[]) {};
     145             : 
     146             : 
     147           0 : static void SkinPointsAndNormalsFallback(
     148             :         size_t numVertices,
     149             :         const VertexArrayIterator<CVector3D>& Position,
     150             :         const VertexArrayIterator<CVector3D>& Normal,
     151             :         const SModelVertex* vertices,
     152             :         const size_t* blendIndices,
     153             :         const CMatrix3D newPoseMatrices[])
     154             : {
     155             :     // To avoid some performance overhead, get the raw vertex array pointers
     156           0 :     char* PositionData = Position.GetData();
     157           0 :     size_t PositionStride = Position.GetStride();
     158           0 :     char* NormalData = Normal.GetData();
     159           0 :     size_t NormalStride = Normal.GetStride();
     160             : 
     161           0 :     for (size_t j = 0; j < numVertices; ++j)
     162             :     {
     163           0 :         const SModelVertex& vtx = vertices[j];
     164             : 
     165           0 :         CVector3D pos = newPoseMatrices[blendIndices[j]].Transform(vtx.m_Coords);
     166           0 :         CVector3D norm = newPoseMatrices[blendIndices[j]].Rotate(vtx.m_Norm);
     167             : 
     168             :         // If there was more than one influence, the result is probably not going
     169             :         // to be of unit length (since it's a weighted sum of several independent
     170             :         // unit vectors), so we need to normalise it.
     171             :         // (It's fairly common to only have one influence, so it seems sensible to
     172             :         // optimise that case a bit.)
     173           0 :         if (vtx.m_Blend.m_Bone[1] != 0xff) // if more than one influence
     174           0 :             norm.Normalize();
     175             : 
     176           0 :         memcpy(PositionData + PositionStride*j, &pos.X, 3*sizeof(float));
     177           0 :         memcpy(NormalData + NormalStride*j, &norm.X, 3*sizeof(float));
     178             :     }
     179           0 : }
     180             : 
     181             : #if COMPILER_HAS_SSE
     182           0 : static void SkinPointsAndNormalsSSE(
     183             :         size_t numVertices,
     184             :         const VertexArrayIterator<CVector3D>& Position,
     185             :         const VertexArrayIterator<CVector3D>& Normal,
     186             :         const SModelVertex* vertices,
     187             :         const size_t* blendIndices,
     188             :         const CMatrix3D newPoseMatrices[])
     189             : {
     190             :     // To avoid some performance overhead, get the raw vertex array pointers
     191           0 :     char* PositionData = Position.GetData();
     192           0 :     size_t PositionStride = Position.GetStride();
     193           0 :     char* NormalData = Normal.GetData();
     194           0 :     size_t NormalStride = Normal.GetStride();
     195             : 
     196             :     // Must be aligned correctly for SSE
     197           0 :     ASSERT((intptr_t)newPoseMatrices % 16 == 0);
     198           0 :     ASSERT((intptr_t)PositionData % 16 == 0);
     199           0 :     ASSERT((intptr_t)PositionStride % 16 == 0);
     200           0 :     ASSERT((intptr_t)NormalData % 16 == 0);
     201           0 :     ASSERT((intptr_t)NormalStride % 16 == 0);
     202             : 
     203           0 :     __m128 col0, col1, col2, col3, vec0, vec1, vec2;
     204             : 
     205           0 :     for (size_t j = 0; j < numVertices; ++j)
     206             :     {
     207           0 :         const SModelVertex& vtx = vertices[j];
     208           0 :         const CMatrix3D& mtx = newPoseMatrices[blendIndices[j]];
     209             : 
     210             :         // Loads matrix to xmm registers.
     211           0 :         const float* data = mtx.AsFloatArray().data();
     212           0 :         col0 = _mm_load_ps(data);
     213           0 :         col1 = _mm_load_ps(data + 4);
     214           0 :         col2 = _mm_load_ps(data + 8);
     215           0 :         col3 = _mm_load_ps(data + 12);
     216             : 
     217             :         // Loads and computes vertex coordinates.
     218           0 :         vec0 = _mm_load1_ps(&vtx.m_Coords.X);   // v0 = [x, x, x, x]
     219           0 :         vec0 = _mm_mul_ps(col0, vec0);          // v0 = [_11*x, _21*x, _31*x, _41*x]
     220           0 :         vec1 = _mm_load1_ps(&vtx.m_Coords.Y);   // v1 = [y, y, y, y]
     221           0 :         vec1 = _mm_mul_ps(col1, vec1);          // v1 = [_12*y, _22*y, _32*y, _42*y]
     222           0 :         vec0 = _mm_add_ps(vec0, vec1);          // v0 = [_11*x + _12*y, ...]
     223           0 :         vec1 = _mm_load1_ps(&vtx.m_Coords.Z);   // v1 = [z, z, z, z]
     224           0 :         vec1 = _mm_mul_ps(col2, vec1);          // v1 = [_13*z, _23*z, _33*z, _43*z]
     225           0 :         vec1 = _mm_add_ps(vec1, col3);          // v1 = [_13*z + _14, ...]
     226           0 :         vec0 = _mm_add_ps(vec0, vec1);          // v0 = [_11*x + _12*y + _13*z + _14, ...]
     227           0 :         _mm_store_ps((float*)(PositionData + PositionStride*j), vec0);
     228             : 
     229             :         // Loads and computes normal vectors.
     230           0 :         vec0 = _mm_load1_ps(&vtx.m_Norm.X);     // v0 = [x, x, x, x]
     231           0 :         vec0 = _mm_mul_ps(col0, vec0);          // v0 = [_11*x, _21*x, _31*x, _41*x]
     232           0 :         vec1 = _mm_load1_ps(&vtx.m_Norm.Y);     // v1 = [y, y, y, y]
     233           0 :         vec1 = _mm_mul_ps(col1, vec1);          // v1 = [_12*y, _22*y, _32*y, _42*y]
     234           0 :         vec0 = _mm_add_ps(vec0, vec1);          // v0 = [_11*x + _12*y, ...]
     235           0 :         vec1 = _mm_load1_ps(&vtx.m_Norm.Z);     // v1 = [z, z, z, z]
     236           0 :         vec1 = _mm_mul_ps(col2, vec1);          // v1 = [_13*z, _23*z, _33*z, _43*z]
     237           0 :         vec0 = _mm_add_ps(vec0, vec1);          // v0 = [_11*x + _12*y + _13*z, ...]
     238             : 
     239             :         // If there was more than one influence, the result is probably not going
     240             :         // to be of unit length (since it's a weighted sum of several independent
     241             :         // unit vectors), so we need to normalise it.
     242             :         // (It's fairly common to only have one influence, so it seems sensible to
     243             :         // optimise that case a bit.)
     244           0 :         if (vtx.m_Blend.m_Bone[1] != 0xff) // if more than one influence
     245             :         {
     246             :             // Normalization.
     247             :             // vec1 = [x*x, y*y, z*z, ?*?]
     248           0 :             vec1 = _mm_mul_ps(vec0, vec0);
     249             :             // vec2 = [y*y, z*z, x*x, ?*?]
     250           0 :             vec2 = _mm_shuffle_ps(vec1, vec1, _MM_SHUFFLE(3, 0, 2, 1));
     251           0 :             vec1 = _mm_add_ps(vec1, vec2);
     252             :             // vec2 = [z*z, x*x, y*y, ?*?]
     253           0 :             vec2 = _mm_shuffle_ps(vec2, vec2, _MM_SHUFFLE(3, 0, 2, 1));
     254           0 :             vec1 = _mm_add_ps(vec1, vec2);
     255             :             // rsqrt(a) = 1 / sqrt(a)
     256           0 :             vec1 = _mm_rsqrt_ps(vec1);
     257           0 :             vec0 = _mm_mul_ps(vec0, vec1);
     258             :         }
     259           0 :         _mm_store_ps((float*)(NormalData + NormalStride*j), vec0);
     260             :     }
     261           0 : }
     262             : #endif
     263             : 
     264           0 : void CModelDef::BlendBoneMatrices(
     265             :         CMatrix3D boneMatrices[])
     266             : {
     267           0 :     for (size_t i = 0; i < m_NumBlends; ++i)
     268             :     {
     269           0 :         const SVertexBlend& blend = m_pBlends[i];
     270           0 :         CMatrix3D& boneMatrix = boneMatrices[m_NumBones + 1 + i];
     271             : 
     272             :         // Note: there is a special case of joint influence, in which the vertex
     273             :         //  is influenced by the bind-shape matrix instead of a particular bone,
     274             :         //  which we indicate by setting the bone ID to the total number of bones.
     275             :         //  It should be blended with the world space transform and we have already
     276             :         //  set up this matrix in boneMatrices.
     277             :         //  (see https://gitea.wildfiregames.com/0ad/0ad/issues/1012)
     278             : 
     279           0 :         boneMatrix.Blend(boneMatrices[blend.m_Bone[0]], blend.m_Weight[0]);
     280           0 :         for (size_t j = 1; j < SVertexBlend::SIZE && blend.m_Bone[j] != 0xFF; ++j)
     281           0 :             boneMatrix.AddBlend(boneMatrices[blend.m_Bone[j]], blend.m_Weight[j]);
     282             :     }
     283           0 : }
     284             : 
     285             : // CModelDef Constructor
     286           8 : CModelDef::CModelDef() :
     287           8 :     m_NumVertices(0), m_NumUVsPerVertex(0), m_pVertices(0), m_NumFaces(0), m_pFaces(0),
     288           8 :     m_NumBones(0), m_Bones(0), m_InverseBindBoneMatrices(NULL),
     289           8 :     m_NumBlends(0), m_pBlends(0), m_pBlendIndices(0),
     290           8 :     m_Name(L"[not loaded]")
     291             : {
     292           8 : }
     293             : 
     294             : // CModelDef Destructor
     295           8 : CModelDef::~CModelDef()
     296             : {
     297           8 :     for(RenderDataMap::iterator it = m_RenderData.begin(); it != m_RenderData.end(); ++it)
     298           0 :         delete it->second;
     299           8 :     delete[] m_pVertices;
     300           8 :     delete[] m_pFaces;
     301           8 :     delete[] m_Bones;
     302           8 :     delete[] m_InverseBindBoneMatrices;
     303           8 :     delete[] m_pBlends;
     304           8 :     delete[] m_pBlendIndices;
     305          16 : }
     306             : 
     307             : // FindPropPoint: find and return pointer to prop point matching given name;
     308             : // return null if no match (case insensitive search)
     309           0 : const SPropPoint* CModelDef::FindPropPoint(const char* name) const
     310             : {
     311           0 :     for (size_t i = 0; i < m_PropPoints.size(); ++i)
     312           0 :         if (m_PropPoints[i].m_Name == name)
     313             :             return &m_PropPoints[i];
     314             : 
     315             :     return 0;
     316             : }
     317             : 
     318             : // Load: read and return a new CModelDef initialised with data from given file
     319           7 : CModelDef* CModelDef::Load(const VfsPath& filename, const VfsPath& name)
     320             : {
     321           7 :     CFileUnpacker unpacker;
     322             : 
     323             :     // read everything in from file
     324           7 :     unpacker.Read(filename,"PSMD");
     325             : 
     326             :     // check version
     327           7 :     if (unpacker.GetVersion()<FILE_READ_VERSION) {
     328           0 :         throw PSERROR_File_InvalidVersion();
     329             :     }
     330             : 
     331           7 :     std::unique_ptr<CModelDef> mdef = std::make_unique<CModelDef>();
     332           7 :     mdef->m_Name = name;
     333             : 
     334             :     // now unpack everything
     335           7 :     mdef->m_NumVertices = unpacker.UnpackSize();
     336             : 
     337             :     // versions prior to 4 only support 1 UV set, 4 and later store it here
     338           7 :     if (unpacker.GetVersion() <= 3)
     339             :     {
     340           3 :         mdef->m_NumUVsPerVertex = 1;
     341             :     }
     342             :     else
     343             :     {
     344           4 :         mdef->m_NumUVsPerVertex = unpacker.UnpackSize();
     345             :     }
     346             : 
     347         757 :     mdef->m_pVertices = new SModelVertex[mdef->m_NumVertices];
     348           7 :     mdef->m_UVCoordinates.reserve(mdef->m_NumVertices * mdef->m_NumUVsPerVertex);
     349             : 
     350         757 :     for (size_t i = 0; i < mdef->m_NumVertices; ++i)
     351             :     {
     352         750 :         unpacker.UnpackRaw(&mdef->m_pVertices[i].m_Coords, 12);
     353         750 :         unpacker.UnpackRaw(&mdef->m_pVertices[i].m_Norm, 12);
     354             : 
     355        1500 :         for (size_t s = 0; s < mdef->m_NumUVsPerVertex; ++s)
     356             :         {
     357         750 :             float uv[2];
     358         750 :             unpacker.UnpackRaw(&uv[0], 8);
     359         750 :             mdef->m_UVCoordinates.emplace_back(uv[0], uv[1]);
     360             :         }
     361             : 
     362         750 :         unpacker.UnpackRaw(&mdef->m_pVertices[i].m_Blend, sizeof(SVertexBlend));
     363             :     }
     364             : 
     365           7 :     mdef->m_NumFaces = unpacker.UnpackSize();
     366           7 :     mdef->m_pFaces=new SModelFace[mdef->m_NumFaces];
     367           7 :     unpacker.UnpackRaw(mdef->m_pFaces,sizeof(SModelFace)*mdef->m_NumFaces);
     368             : 
     369           7 :     mdef->m_NumBones = unpacker.UnpackSize();
     370           7 :     if (mdef->m_NumBones)
     371             :     {
     372           0 :         mdef->m_Bones=new CBoneState[mdef->m_NumBones];
     373           0 :         unpacker.UnpackRaw(mdef->m_Bones,mdef->m_NumBones*sizeof(CBoneState));
     374             : 
     375           0 :         mdef->m_pBlendIndices = new size_t[mdef->m_NumVertices];
     376           0 :         std::vector<SVertexBlend> blends;
     377           0 :         for (size_t i = 0; i < mdef->m_NumVertices; i++)
     378             :         {
     379           0 :             const SVertexBlend &blend = mdef->m_pVertices[i].m_Blend;
     380           0 :             if (blend.m_Bone[1] == 0xFF)
     381             :             {
     382           0 :                 mdef->m_pBlendIndices[i] = blend.m_Bone[0];
     383             :             }
     384             :             else
     385             :             {
     386             :                 // If there's already a vertex using the same blend as this, then
     387             :                 // reuse its entry from blends; otherwise add the new one to blends
     388             :                 size_t j;
     389           0 :                 for (j = 0; j < blends.size(); j++)
     390             :                 {
     391           0 :                     if (blend == blends[j]) break;
     392             :                 }
     393           0 :                 if (j >= blends.size())
     394           0 :                     blends.push_back(blend);
     395             :                 // This index is offset by one to allow the special case of a
     396             :                 //  weighted influence relative to the bind-shape rather than
     397             :                 //  a particular bone. See comment in BlendBoneMatrices.
     398           0 :                 mdef->m_pBlendIndices[i] = mdef->m_NumBones + 1 + j;
     399             :             }
     400             :         }
     401             : 
     402           0 :         mdef->m_NumBlends = blends.size();
     403           0 :         mdef->m_pBlends = new SVertexBlend[mdef->m_NumBlends];
     404           0 :         std::copy(blends.begin(), blends.end(), mdef->m_pBlends);
     405           0 :     }
     406             : 
     407           7 :     if (unpacker.GetVersion() >= 2)
     408             :     {
     409             :         // versions >=2 also have prop point data
     410           7 :         size_t numPropPoints = unpacker.UnpackSize();
     411           7 :         mdef->m_PropPoints.resize(numPropPoints);
     412           7 :         if (numPropPoints)
     413             :         {
     414           8 :             for (size_t i = 0; i < numPropPoints; i++)
     415             :             {
     416           4 :                 unpacker.UnpackString(mdef->m_PropPoints[i].m_Name);
     417           4 :                 unpacker.UnpackRaw(&mdef->m_PropPoints[i].m_Position.X, sizeof(mdef->m_PropPoints[i].m_Position));
     418           4 :                 unpacker.UnpackRaw(&mdef->m_PropPoints[i].m_Rotation.m_V.X, sizeof(mdef->m_PropPoints[i].m_Rotation));
     419           4 :                 unpacker.UnpackRaw(&mdef->m_PropPoints[i].m_BoneIndex, sizeof(mdef->m_PropPoints[i].m_BoneIndex));
     420             : 
     421             :                 // build prop point transform
     422           4 :                 mdef->m_PropPoints[i].m_Transform.SetIdentity();
     423           4 :                 mdef->m_PropPoints[i].m_Transform.Rotate(mdef->m_PropPoints[i].m_Rotation);
     424           4 :                 mdef->m_PropPoints[i].m_Transform.Translate(mdef->m_PropPoints[i].m_Position);
     425             :             }
     426             :         }
     427             :     }
     428             : 
     429           7 :     if (unpacker.GetVersion() <= 2)
     430             :     {
     431             :         // Versions <=2 don't include the default 'root' prop point, so add it here
     432             : 
     433           0 :         SPropPoint prop;
     434           0 :         prop.m_Name = "root";
     435           0 :         prop.m_Transform.SetIdentity();
     436           0 :         prop.m_BoneIndex = 0xFF;
     437             : 
     438           0 :         mdef->m_PropPoints.push_back(prop);
     439           0 :     }
     440             : 
     441           7 :     if (unpacker.GetVersion() <= 2)
     442             :     {
     443             :         // Versions <=2 store the vertexes relative to the bind pose. That
     444             :         // isn't useful when you want to do correct skinning, so later versions
     445             :         // store them in world space. So, fix the old models by skinning each
     446             :         // vertex:
     447             : 
     448           0 :         if (mdef->m_NumBones) // only do skinned models
     449             :         {
     450           0 :             std::vector<CMatrix3D> bindPose (mdef->m_NumBones);
     451             : 
     452           0 :             for (size_t i = 0; i < mdef->m_NumBones; ++i)
     453             :             {
     454           0 :                 bindPose[i].SetIdentity();
     455           0 :                 bindPose[i].Rotate(mdef->m_Bones[i].m_Rotation);
     456           0 :                 bindPose[i].Translate(mdef->m_Bones[i].m_Translation);
     457             :             }
     458             : 
     459           0 :             for (size_t i = 0; i < mdef->m_NumVertices; ++i)
     460             :             {
     461           0 :                 mdef->m_pVertices[i].m_Coords = SkinPoint(mdef->m_pVertices[i], &bindPose[0]);
     462           0 :                 mdef->m_pVertices[i].m_Norm = SkinNormal(mdef->m_pVertices[i], &bindPose[0]);
     463             :             }
     464           0 :         }
     465             :     }
     466             : 
     467             :     // Compute the inverse bind-pose matrices, needed by the skinning code
     468           7 :     mdef->m_InverseBindBoneMatrices = new CMatrix3D[mdef->m_NumBones];
     469           7 :     CBoneState* defpose = mdef->GetBones();
     470           7 :     for (size_t i = 0; i < mdef->m_NumBones; ++i)
     471             :     {
     472           0 :         mdef->m_InverseBindBoneMatrices[i].SetIdentity();
     473           0 :         mdef->m_InverseBindBoneMatrices[i].Translate(-defpose[i].m_Translation);
     474           0 :         mdef->m_InverseBindBoneMatrices[i].Rotate(defpose[i].m_Rotation.GetInverse());
     475             :     }
     476             : 
     477          14 :     return mdef.release();
     478           7 : }
     479             : 
     480             : // Save: write the given CModelDef to the given file
     481           0 : void CModelDef::Save(const VfsPath& filename, const CModelDef* mdef)
     482             : {
     483           0 :     CFilePacker packer(FILE_VERSION, "PSMD");
     484             : 
     485             :     // pack everything up
     486           0 :     const size_t numVertices = mdef->GetNumVertices();
     487           0 :     packer.PackSize(numVertices);
     488           0 :     packer.PackRaw(mdef->GetVertices(), sizeof(SModelVertex) * numVertices);
     489             : 
     490           0 :     const size_t numFaces = mdef->GetNumFaces();
     491           0 :     packer.PackSize(numFaces);
     492           0 :     packer.PackRaw(mdef->GetFaces(), sizeof(SModelFace) * numFaces);
     493             : 
     494           0 :     const size_t numBones = mdef->m_NumBones;
     495           0 :     packer.PackSize(numBones);
     496           0 :     if (numBones)
     497           0 :         packer.PackRaw(mdef->m_Bones, sizeof(CBoneState) * numBones);
     498             : 
     499           0 :     const size_t numPropPoints = mdef->m_PropPoints.size();
     500           0 :     packer.PackSize(numPropPoints);
     501           0 :     for (size_t i = 0; i < numPropPoints; i++)
     502             :     {
     503           0 :         packer.PackString(mdef->m_PropPoints[i].m_Name);
     504           0 :         packer.PackRaw(&mdef->m_PropPoints[i].m_Position.X, sizeof(mdef->m_PropPoints[i].m_Position));
     505           0 :         packer.PackRaw(&mdef->m_PropPoints[i].m_Rotation.m_V.X, sizeof(mdef->m_PropPoints[i].m_Rotation));
     506           0 :         packer.PackRaw(&mdef->m_PropPoints[i].m_BoneIndex, sizeof(mdef->m_PropPoints[i].m_BoneIndex));
     507             :     }
     508             : 
     509             :     // flush everything out to file
     510           0 :     packer.Write(filename);
     511           0 : }
     512             : 
     513             : // SetRenderData: Set the render data object for the given key,
     514           0 : void CModelDef::SetRenderData(const void* key, CModelDefRPrivate* data)
     515             : {
     516           0 :     delete m_RenderData[key];
     517           0 :     m_RenderData[key] = data;
     518           0 : }
     519             : 
     520             : // GetRenderData: Get the render data object for the given key,
     521             : // or 0 if no such object exists.
     522             : // Reference count of the render data object is automatically increased.
     523           0 : CModelDefRPrivate* CModelDef::GetRenderData(const void* key) const
     524             : {
     525           0 :     RenderDataMap::const_iterator it = m_RenderData.find(key);
     526             : 
     527           0 :     if (it != m_RenderData.end())
     528           0 :         return it->second;
     529             : 
     530             :     return 0;
     531             : }
     532             : 
     533          10 : void ModelDefActivateFastImpl()
     534             : {
     535             : #if COMPILER_HAS_SSE
     536          10 :     if (SDL_HasSSE())
     537             :     {
     538          10 :         CModelDef::SkinPointsAndNormals = SkinPointsAndNormalsSSE;
     539          10 :         return;
     540             :     }
     541             : #endif
     542           0 :     CModelDef::SkinPointsAndNormals = SkinPointsAndNormalsFallback;
     543             : }

Generated by: LCOV version 1.16