2012-03-05 01:42:34 +01:00
|
|
|
/* Copyright (C) 2012 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
|
|
|
/*
|
|
|
|
* Mesh object with texture and skinning information
|
2007-05-07 18:33:24 +02:00
|
|
|
*/
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
#include "Model.h"
|
2004-12-12 20:43:55 +01:00
|
|
|
#include "ModelDef.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Quaternion.h"
|
2011-11-25 07:36:13 +01:00
|
|
|
#include "maths/BoundingBoxAligned.h"
|
2004-05-30 02:46:58 +02:00
|
|
|
#include "SkeletonAnim.h"
|
|
|
|
#include "SkeletonAnimDef.h"
|
|
|
|
#include "SkeletonAnimManager.h"
|
2004-11-08 23:02:01 +01:00
|
|
|
#include "MeshManager.h"
|
2006-07-10 01:12:37 +02:00
|
|
|
#include "ObjectEntry.h"
|
2005-08-12 19:06:53 +02:00
|
|
|
#include "lib/res/graphics/ogl_tex.h"
|
2005-04-03 07:02:00 +02:00
|
|
|
#include "lib/res/h_mgr.h"
|
2011-11-10 00:11:28 +01:00
|
|
|
#include "lib/sysdep/rtl.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Profile.h"
|
2005-02-11 13:57:19 +01:00
|
|
|
#include "ps/CLogger.h"
|
2012-04-12 17:43:59 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2005-02-11 13:57:19 +01:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructor
|
2007-03-01 19:52:53 +01:00
|
|
|
CModel::CModel(CSkeletonAnimManager& skeletonAnimManager)
|
2011-03-13 20:22:05 +01:00
|
|
|
: m_Flags(0), m_Anim(NULL), m_AnimTime(0),
|
2012-04-12 17:43:59 +02:00
|
|
|
m_BoneMatrices(NULL),
|
2010-06-05 02:49:14 +02:00
|
|
|
m_AmmoPropPoint(NULL), m_AmmoLoadedProp(0),
|
2007-03-01 19:52:53 +01:00
|
|
|
m_SkeletonAnimManager(skeletonAnimManager)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Destructor
|
|
|
|
CModel::~CModel()
|
|
|
|
{
|
|
|
|
ReleaseData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReleaseData: delete anything allocated by the model
|
|
|
|
void CModel::ReleaseData()
|
|
|
|
{
|
2011-11-10 00:11:28 +01:00
|
|
|
rtl_FreeAligned(m_BoneMatrices);
|
2011-03-13 20:22:05 +01:00
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
2004-05-30 02:46:58 +02:00
|
|
|
delete m_Props[i].m_Model;
|
|
|
|
m_Props.clear();
|
2011-03-13 20:22:05 +01:00
|
|
|
|
2004-12-12 19:40:00 +01:00
|
|
|
m_pModelDef = CModelDefPtr();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// InitModel: setup model from given geometry
|
2008-07-17 16:23:51 +02:00
|
|
|
bool CModel::InitModel(const CModelDefPtr& modeldef)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
// clean up any existing data first
|
|
|
|
ReleaseData();
|
|
|
|
|
|
|
|
m_pModelDef = modeldef;
|
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
size_t numBones = modeldef->GetNumBones();
|
|
|
|
if (numBones != 0)
|
|
|
|
{
|
2011-07-13 01:48:05 +02:00
|
|
|
size_t numBlends = modeldef->GetNumBlends();
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
// allocate matrices for bone transformations
|
2012-03-05 01:42:34 +01:00
|
|
|
// (one extra matrix is used for the special case of bind-shape relative weighting)
|
|
|
|
m_BoneMatrices = (CMatrix3D*)rtl_AllocateAligned(sizeof(CMatrix3D) * (numBones + 1 + numBlends), 16);
|
2012-04-12 17:43:59 +02:00
|
|
|
for (size_t i = 0; i < numBones + 1 + numBlends; ++i)
|
2011-07-13 01:48:05 +02:00
|
|
|
{
|
|
|
|
m_BoneMatrices[i].SetIdentity();
|
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
m_PositionValid = true;
|
2011-07-13 01:48:05 +02:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CalcBound: calculate the world space bounds of this model
|
|
|
|
void CModel::CalcBounds()
|
|
|
|
{
|
2005-02-10 00:19:48 +01:00
|
|
|
// Need to calculate the object bounds first, if that hasn't already been done
|
2006-03-17 04:59:49 +01:00
|
|
|
if (! (m_Anim && m_Anim->m_AnimDef))
|
2011-07-13 01:48:05 +02:00
|
|
|
{
|
|
|
|
if (m_ObjectBounds.IsEmpty())
|
2011-11-25 07:36:13 +01:00
|
|
|
CalcStaticObjectBounds();
|
2011-07-13 01:48:05 +02:00
|
|
|
}
|
2005-02-10 00:19:48 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_Anim->m_ObjectBounds.IsEmpty())
|
2011-11-25 07:36:13 +01:00
|
|
|
CalcAnimatedObjectBounds(m_Anim->m_AnimDef, m_Anim->m_ObjectBounds);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(! m_Anim->m_ObjectBounds.IsEmpty()); // (if this happens, it'll be recalculating the bounds every time)
|
2005-02-10 00:19:48 +01:00
|
|
|
m_ObjectBounds = m_Anim->m_ObjectBounds;
|
|
|
|
}
|
|
|
|
|
2011-07-05 02:12:00 +02:00
|
|
|
// Ensure the transform is set correctly before we use it
|
|
|
|
ValidatePosition();
|
|
|
|
|
2011-11-25 07:36:13 +01:00
|
|
|
// Now transform the object-space bounds to world-space bounds
|
|
|
|
m_ObjectBounds.Transform(GetTransform(), m_WorldBounds);
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CalcObjectBounds: calculate object space bounds of this model, based solely on vertex positions
|
2011-11-25 07:36:13 +01:00
|
|
|
void CModel::CalcStaticObjectBounds()
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
m_ObjectBounds.SetEmpty();
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
size_t numverts=m_pModelDef->GetNumVertices();
|
2004-05-30 02:46:58 +02:00
|
|
|
SModelVertex* verts=m_pModelDef->GetVertices();
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
for (size_t i=0;i<numverts;i++) {
|
2004-05-30 02:46:58 +02:00
|
|
|
m_ObjectBounds+=verts[i].m_Coords;
|
2011-07-05 02:12:00 +02:00
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CalcAnimatedObjectBound: calculate bounds encompassing all vertex positions for given animation
|
2011-11-25 07:36:13 +01:00
|
|
|
void CModel::CalcAnimatedObjectBounds(CSkeletonAnimDef* anim, CBoundingBoxAligned& result)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
result.SetEmpty();
|
|
|
|
|
2005-02-10 00:19:48 +01:00
|
|
|
// Set the current animation on which to perform calculations (if it's necessary)
|
|
|
|
if (anim != m_Anim->m_AnimDef)
|
|
|
|
{
|
|
|
|
CSkeletonAnim dummyanim;
|
|
|
|
dummyanim.m_AnimDef=anim;
|
|
|
|
if (!SetAnimation(&dummyanim)) return;
|
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
size_t numverts=m_pModelDef->GetNumVertices();
|
2004-05-30 02:46:58 +02:00
|
|
|
SModelVertex* verts=m_pModelDef->GetVertices();
|
2005-02-10 00:19:48 +01:00
|
|
|
|
|
|
|
// Remove any transformations, so that we calculate the bounding box
|
|
|
|
// at the origin. The box is later re-transformed onto the object, without
|
|
|
|
// having to recalculate the size of the box.
|
|
|
|
CMatrix3D transform, oldtransform = GetTransform();
|
2011-03-13 20:22:05 +01:00
|
|
|
CModelAbstract* oldparent = m_Parent;
|
2005-10-26 03:03:28 +02:00
|
|
|
|
|
|
|
m_Parent = 0;
|
2005-02-10 00:19:48 +01:00
|
|
|
transform.SetIdentity();
|
2005-10-26 03:03:28 +02:00
|
|
|
CRenderableObject::SetTransform(transform);
|
2005-02-10 00:19:48 +01:00
|
|
|
|
2005-05-01 21:09:13 +02:00
|
|
|
// Following seems to stomp over the current animation time - which, unsurprisingly,
|
|
|
|
// introduces artefacts in the currently playing animation. Save it here and restore it
|
|
|
|
// at the end.
|
|
|
|
float AnimTime = m_AnimTime;
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
// iterate through every frame of the animation
|
2005-10-26 03:03:28 +02:00
|
|
|
for (size_t j=0;j<anim->GetNumFrames();j++) {
|
|
|
|
m_PositionValid = false;
|
|
|
|
ValidatePosition();
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
// extend bounds by vertex positions at the frame
|
2006-12-15 17:09:30 +01:00
|
|
|
for (size_t i=0;i<numverts;i++)
|
|
|
|
{
|
2011-07-13 01:48:05 +02:00
|
|
|
result += CModelDef::SkinPoint(verts[i], GetAnimatedBoneMatrices());
|
2006-12-15 17:09:30 +01:00
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
// advance to next frame
|
2005-10-26 03:03:28 +02:00
|
|
|
m_AnimTime += anim->GetFrameTime();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2005-02-10 00:19:48 +01:00
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
m_PositionValid = false;
|
|
|
|
m_Parent = oldparent;
|
2005-02-10 00:19:48 +01:00
|
|
|
SetTransform(oldtransform);
|
2005-05-01 21:09:13 +02:00
|
|
|
m_AnimTime = AnimTime;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 14:26:31 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2011-11-25 07:36:13 +01:00
|
|
|
const CBoundingBoxAligned CModel::GetWorldBoundsRec()
|
2011-04-29 14:26:31 +02:00
|
|
|
{
|
2011-11-25 07:36:13 +01:00
|
|
|
CBoundingBoxAligned bounds = GetWorldBounds();
|
2011-04-29 14:26:31 +02:00
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
2011-11-25 07:36:13 +01:00
|
|
|
bounds += m_Props[i].m_Model->GetWorldBoundsRec();
|
2011-04-29 14:26:31 +02:00
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
2011-11-25 07:36:13 +01:00
|
|
|
const CBoundingBoxAligned CModel::GetObjectSelectionBoundsRec()
|
|
|
|
{
|
|
|
|
CBoundingBoxAligned objBounds = GetObjectBounds(); // updates the (children-not-included) object-space bounds if necessary
|
|
|
|
|
|
|
|
// now extend these bounds to include the props' selection bounds (if any)
|
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
|
|
|
{
|
|
|
|
const Prop& prop = m_Props[i];
|
|
|
|
if (prop.m_Hidden)
|
|
|
|
continue; // prop is hidden from rendering, so it also shouldn't be used for selection
|
|
|
|
|
|
|
|
CBoundingBoxAligned propSelectionBounds = prop.m_Model->GetObjectSelectionBoundsRec();
|
|
|
|
if (propSelectionBounds.IsEmpty())
|
|
|
|
continue; // submodel does not wish to participate in selection box, exclude it
|
|
|
|
|
|
|
|
// We have the prop's bounds in its own object-space; now we need to transform them so they can be properly added
|
|
|
|
// to the bounds in our object-space. For that, we need the transform of the prop attachment point.
|
|
|
|
//
|
|
|
|
// We have the prop point information; however, it's not trivial to compute its exact location in our object-space
|
|
|
|
// since it may or may not be attached to a bone (see SPropPoint), which in turn may or may not be in the middle of
|
|
|
|
// an animation. The bone matrices might be of interest, but they're really only meant to be used for the animation
|
|
|
|
// system and are quite opaque to use from the outside (see @ref ValidatePosition).
|
|
|
|
//
|
|
|
|
// However, a nice side effect of ValidatePosition is that it also computes the absolute world-space transform of
|
|
|
|
// our props and sets it on their respective models. In particular, @ref ValidatePosition will compute the prop's
|
|
|
|
// world-space transform as either
|
|
|
|
//
|
|
|
|
// T' = T x B x O
|
|
|
|
// or
|
|
|
|
// T' = T x O
|
|
|
|
//
|
|
|
|
// where T' is the prop's world-space transform, T is our world-space transform, O is the prop's local
|
|
|
|
// offset/rotation matrix, and B is an optional transformation matrix of the bone the prop is attached to
|
|
|
|
// (taking into account animation and everything).
|
|
|
|
//
|
|
|
|
// From this, it is clear that either O or B x O is the object-space transformation matrix of the prop. So,
|
|
|
|
// all we need to do is apply our own inverse world-transform T^(-1) to T' to get our desired result. Luckily,
|
|
|
|
// this is precomputed upon setting the transform matrix (see @ref SetTransform), so it is free to fetch.
|
|
|
|
|
|
|
|
CMatrix3D propObjectTransform = prop.m_Model->GetTransform(); // T'
|
|
|
|
propObjectTransform.Concatenate(GetInvTransform()); // T^(-1) x T'
|
|
|
|
|
|
|
|
// Transform the prop's bounds into our object coordinate space
|
|
|
|
CBoundingBoxAligned transformedPropSelectionBounds;
|
|
|
|
propSelectionBounds.Transform(propObjectTransform, transformedPropSelectionBounds);
|
|
|
|
|
|
|
|
objBounds += transformedPropSelectionBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
return objBounds;
|
|
|
|
}
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BuildAnimation: load raw animation frame animation from given file, and build a
|
|
|
|
// animation specific to this model
|
2011-02-17 21:08:20 +01:00
|
|
|
CSkeletonAnim* CModel::BuildAnimation(const VfsPath& pathname, const CStr& name, float speed, float actionpos, float actionpos2)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
CSkeletonAnimDef* def = m_SkeletonAnimManager.GetAnimation(pathname);
|
2010-04-06 01:09:34 +02:00
|
|
|
if (!def)
|
|
|
|
return NULL;
|
2005-05-21 03:40:32 +02:00
|
|
|
|
2010-04-06 01:09:34 +02:00
|
|
|
CSkeletonAnim* anim = new CSkeletonAnim();
|
2005-05-21 03:40:32 +02:00
|
|
|
anim->m_Name = name;
|
2010-04-06 01:09:34 +02:00
|
|
|
anim->m_AnimDef = def;
|
|
|
|
anim->m_Speed = speed;
|
2010-06-05 02:49:14 +02:00
|
|
|
|
|
|
|
if (actionpos == -1.f)
|
|
|
|
anim->m_ActionPos = -1.f;
|
|
|
|
else
|
|
|
|
anim->m_ActionPos = actionpos * anim->m_AnimDef->GetDuration();
|
|
|
|
|
|
|
|
if (actionpos2 == -1.f)
|
|
|
|
anim->m_ActionPos2 = -1.f;
|
|
|
|
else
|
|
|
|
anim->m_ActionPos2 = actionpos2 * anim->m_AnimDef->GetDuration();
|
2005-05-27 02:38:30 +02:00
|
|
|
|
2005-02-10 00:19:48 +01:00
|
|
|
anim->m_ObjectBounds.SetEmpty();
|
|
|
|
InvalidateBounds();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
return anim;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2010-06-05 02:49:14 +02:00
|
|
|
// Update: update this model to the given time, in msec
|
|
|
|
void CModel::UpdateTo(float time)
|
2007-02-10 04:09:52 +01:00
|
|
|
{
|
2010-06-05 02:49:14 +02:00
|
|
|
// update animation time, but don't calculate bone matrices - do that (lazily) when
|
|
|
|
// something requests them; that saves some calculation work for offscreen models,
|
|
|
|
// and also assures the world space, inverted bone matrices (required for normal
|
|
|
|
// skinning) are up to date with respect to m_Transform
|
|
|
|
m_AnimTime = time;
|
2007-02-10 04:09:52 +01:00
|
|
|
|
2010-06-05 02:49:14 +02:00
|
|
|
// mark vertices as dirty
|
|
|
|
SetDirty(RENDERDATA_UPDATE_VERTICES);
|
2010-06-03 03:29:43 +02:00
|
|
|
|
2010-06-05 02:49:14 +02:00
|
|
|
// mark matrices as dirty
|
|
|
|
InvalidatePosition();
|
2010-04-06 01:09:34 +02:00
|
|
|
}
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2005-10-26 03:03:28 +02:00
|
|
|
// InvalidatePosition
|
|
|
|
void CModel::InvalidatePosition()
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2005-10-26 03:03:28 +02:00
|
|
|
m_PositionValid = false;
|
2005-05-20 19:09:47 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
2005-10-26 03:03:28 +02:00
|
|
|
m_Props[i].m_Model->InvalidatePosition();
|
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ValidatePosition: ensure that current transform and bone matrices are both uptodate
|
|
|
|
void CModel::ValidatePosition()
|
|
|
|
{
|
|
|
|
if (m_PositionValid)
|
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(!m_Parent || m_Parent->m_PositionValid);
|
2005-10-26 03:03:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Parent && !m_Parent->m_PositionValid)
|
|
|
|
{
|
|
|
|
// Make sure we don't base our calculations on
|
|
|
|
// a parent animation state that is out of date.
|
|
|
|
m_Parent->ValidatePosition();
|
|
|
|
|
|
|
|
// Parent will recursively call our validation.
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(m_PositionValid);
|
2005-10-26 03:03:28 +02:00
|
|
|
return;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
if (m_Anim && m_BoneMatrices)
|
|
|
|
{
|
2010-09-24 23:33:07 +02:00
|
|
|
// PROFILE( "generating bone matrices" );
|
2005-10-26 03:03:28 +02:00
|
|
|
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(m_pModelDef->GetNumBones() == m_Anim->m_AnimDef->GetNumKeys());
|
2005-10-26 03:03:28 +02:00
|
|
|
|
2007-03-01 01:14:35 +01:00
|
|
|
m_Anim->m_AnimDef->BuildBoneMatrices(m_AnimTime, m_BoneMatrices, !(m_Flags & MODELFLAG_NOLOOPANIMATION));
|
2011-04-06 15:49:05 +02:00
|
|
|
}
|
|
|
|
else if (m_BoneMatrices)
|
|
|
|
{
|
|
|
|
// Bones but no animation - probably a buggy actor forgot to set up the animation,
|
|
|
|
// so just render it in its bind pose
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_pModelDef->GetNumBones(); i++)
|
|
|
|
{
|
|
|
|
m_BoneMatrices[i].SetIdentity();
|
|
|
|
m_BoneMatrices[i].Rotate(m_pModelDef->GetBones()[i].m_Rotation);
|
|
|
|
m_BoneMatrices[i].Translate(m_pModelDef->GetBones()[i].m_Translation);
|
2005-10-26 03:03:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 17:43:59 +02:00
|
|
|
|
|
|
|
// For CPU skinning, we precompute as much as possible so that the only
|
|
|
|
// per-vertex work is a single matrix*vec multiplication.
|
|
|
|
// For GPU skinning, we try to minimise CPU work by doing most computation
|
|
|
|
// in the vertex shader instead.
|
|
|
|
// Using g_Renderer.m_Options to detect CPU vs GPU is a bit hacky,
|
|
|
|
// and this doesn't allow the setting to change at runtime, but there isn't
|
|
|
|
// an obvious cleaner way to determine what data needs to be computed,
|
|
|
|
// and GPU skinning is a rarely-used experimental feature anyway.
|
|
|
|
bool worldSpaceBoneMatrices = !g_Renderer.m_Options.m_GPUSkinning;
|
|
|
|
bool computeBlendMatrices = !g_Renderer.m_Options.m_GPUSkinning;
|
|
|
|
|
|
|
|
if (m_BoneMatrices && worldSpaceBoneMatrices)
|
|
|
|
{
|
|
|
|
// add world-space transformation to m_BoneMatrices
|
|
|
|
const CMatrix3D transform = GetTransform();
|
|
|
|
for (size_t i = 0; i < m_pModelDef->GetNumBones(); i++)
|
|
|
|
m_BoneMatrices[i].Concatenate(transform);
|
|
|
|
}
|
|
|
|
|
2011-11-25 07:36:13 +01:00
|
|
|
// our own position is now valid; now we can safely update our props' positions without fearing
|
|
|
|
// that doing so will cause a revalidation of this model (see recursion above).
|
2005-10-26 03:03:28 +02:00
|
|
|
m_PositionValid = true;
|
|
|
|
|
|
|
|
// re-position and validate all props
|
|
|
|
for (size_t j = 0; j < m_Props.size(); ++j)
|
|
|
|
{
|
2004-06-19 16:39:24 +02:00
|
|
|
const Prop& prop=m_Props[j];
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
CMatrix3D proptransform = prop.m_Point->m_Transform;;
|
|
|
|
if (prop.m_Point->m_BoneIndex != 0xff)
|
2011-11-25 07:36:13 +01:00
|
|
|
{
|
2012-04-12 17:43:59 +02:00
|
|
|
CMatrix3D boneMatrix = m_BoneMatrices[prop.m_Point->m_BoneIndex];
|
|
|
|
if (!worldSpaceBoneMatrices)
|
|
|
|
boneMatrix.Concatenate(GetTransform());
|
|
|
|
proptransform.Concatenate(boneMatrix);
|
2011-11-25 07:36:13 +01:00
|
|
|
}
|
2005-10-26 03:03:28 +02:00
|
|
|
else
|
2011-11-25 07:36:13 +01:00
|
|
|
{
|
|
|
|
// not relative to any bone; just apply world-space transformation (i.e. relative to object-space origin)
|
2005-10-26 03:03:28 +02:00
|
|
|
proptransform.Concatenate(m_Transform);
|
2011-11-25 07:36:13 +01:00
|
|
|
}
|
2005-10-26 03:03:28 +02:00
|
|
|
|
|
|
|
prop.m_Model->SetTransform(proptransform);
|
|
|
|
prop.m_Model->ValidatePosition();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2011-07-13 01:48:05 +02:00
|
|
|
|
|
|
|
if (m_BoneMatrices)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_pModelDef->GetNumBones(); i++)
|
|
|
|
{
|
2012-04-12 17:43:59 +02:00
|
|
|
m_BoneMatrices[i] = m_BoneMatrices[i] * m_pModelDef->GetInverseBindBoneMatrices()[i];
|
2011-07-13 01:48:05 +02: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 transform instead of a particular bone,
|
|
|
|
// which we indicate with the blending bone ID set to the total number
|
|
|
|
// of bones. But since we're skinning in world space, we use the model's
|
|
|
|
// world space transform and store that matrix in this special index.
|
|
|
|
// (see http://trac.wildfiregames.com/ticket/1012)
|
|
|
|
m_BoneMatrices[m_pModelDef->GetNumBones()] = m_Transform;
|
|
|
|
|
2012-04-12 17:43:59 +02:00
|
|
|
if (computeBlendMatrices)
|
|
|
|
m_pModelDef->BlendBoneMatrices(m_BoneMatrices);
|
2011-07-13 01:48:05 +02:00
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SetAnimation: set the given animation as the current animation on this model;
|
|
|
|
// return false on error, else true
|
2010-06-05 02:49:14 +02:00
|
|
|
bool CModel::SetAnimation(CSkeletonAnim* anim, bool once)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2010-06-05 02:49:14 +02:00
|
|
|
m_Anim = NULL; // in case something fails
|
2005-02-11 13:57:19 +01:00
|
|
|
|
2010-06-05 02:49:14 +02:00
|
|
|
if (anim)
|
|
|
|
{
|
2004-11-11 08:09:32 +01:00
|
|
|
m_Flags &= ~MODELFLAG_NOLOOPANIMATION;
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2005-05-21 03:40:32 +02:00
|
|
|
if (once)
|
2004-11-11 08:09:32 +01:00
|
|
|
m_Flags |= MODELFLAG_NOLOOPANIMATION;
|
|
|
|
|
2010-06-05 02:49:14 +02:00
|
|
|
if (!m_BoneMatrices && anim->m_AnimDef)
|
|
|
|
{
|
2004-05-30 02:46:58 +02:00
|
|
|
// not boned, can't animate
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-05 02:49:14 +02:00
|
|
|
if (m_BoneMatrices && !anim->m_AnimDef)
|
|
|
|
{
|
2006-03-17 04:59:49 +01:00
|
|
|
// boned, but animation isn't valid
|
|
|
|
// (e.g. the default (static) idle animation on an animated unit)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-05 02:49:14 +02:00
|
|
|
if (anim->m_AnimDef && anim->m_AnimDef->GetNumKeys() != m_pModelDef->GetNumBones())
|
|
|
|
{
|
2005-02-10 00:19:48 +01:00
|
|
|
// mismatch between model's skeleton and animation's skeleton
|
2010-06-05 02:49:14 +02:00
|
|
|
LOGERROR(L"Mismatch between model's skeleton and animation's skeleton (%lu model bones != %lu animation keys)",
|
|
|
|
(unsigned long)m_pModelDef->GetNumBones(), (unsigned long)anim->m_AnimDef->GetNumKeys());
|
2004-05-30 02:46:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-10 00:19:48 +01:00
|
|
|
// reset the cached bounds when the animation is changed
|
|
|
|
m_ObjectBounds.SetEmpty();
|
|
|
|
InvalidateBounds();
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
// start anim from beginning
|
2010-04-06 01:09:34 +02:00
|
|
|
m_AnimTime = 0;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
m_Anim = anim;
|
2005-02-11 13:57:19 +01:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-09-27 18:54:23 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CopyAnimation
|
|
|
|
void CModel::CopyAnimationFrom(CModel* source)
|
|
|
|
{
|
|
|
|
m_Anim = source->m_Anim;
|
|
|
|
m_AnimTime = source->m_AnimTime;
|
|
|
|
|
|
|
|
m_Flags &= ~MODELFLAG_CASTSHADOWS;
|
|
|
|
if (source->m_Flags & MODELFLAG_CASTSHADOWS)
|
|
|
|
m_Flags |= MODELFLAG_CASTSHADOWS;
|
|
|
|
|
|
|
|
m_ObjectBounds.SetEmpty();
|
|
|
|
InvalidateBounds();
|
|
|
|
}
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AddProp: add a prop to the model on the given point
|
2011-03-13 20:22:05 +01:00
|
|
|
void CModel::AddProp(const SPropPoint* point, CModelAbstract* model, CObjectEntry* objectentry)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
// position model according to prop point position
|
2011-11-25 07:36:13 +01:00
|
|
|
|
|
|
|
// this next call will invalidate the bounds of "model", which will in turn also invalidate the selection box
|
2004-05-30 02:46:58 +02:00
|
|
|
model->SetTransform(point->m_Transform);
|
2005-10-26 03:03:28 +02:00
|
|
|
model->m_Parent = this;
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
Prop prop;
|
2006-04-24 00:22:18 +02:00
|
|
|
prop.m_Point = point;
|
|
|
|
prop.m_Model = model;
|
|
|
|
prop.m_ObjectEntry = objectentry;
|
2004-05-30 02:46:58 +02:00
|
|
|
m_Props.push_back(prop);
|
|
|
|
}
|
|
|
|
|
2011-03-13 20:22:05 +01:00
|
|
|
void CModel::AddAmmoProp(const SPropPoint* point, CModelAbstract* model, CObjectEntry* objectentry)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2010-06-05 02:49:14 +02:00
|
|
|
AddProp(point, model, objectentry);
|
|
|
|
m_AmmoPropPoint = point;
|
|
|
|
m_AmmoLoadedProp = m_Props.size() - 1;
|
|
|
|
m_Props[m_AmmoLoadedProp].m_Hidden = true;
|
2011-11-25 07:36:13 +01:00
|
|
|
|
|
|
|
// we only need to invalidate the selection box here if it is based on props and their visibilities
|
|
|
|
if (!m_CustomSelectionShape)
|
|
|
|
m_SelectionBoxValid = false;
|
2010-06-05 02:49:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModel::ShowAmmoProp()
|
|
|
|
{
|
|
|
|
if (m_AmmoPropPoint == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Show the ammo prop, hide all others on the same prop point
|
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
|
|
|
if (m_Props[i].m_Point == m_AmmoPropPoint)
|
|
|
|
m_Props[i].m_Hidden = (i != m_AmmoLoadedProp);
|
2011-11-25 07:36:13 +01:00
|
|
|
|
|
|
|
// we only need to invalidate the selection box here if it is based on props and their visibilities
|
|
|
|
if (!m_CustomSelectionShape)
|
|
|
|
m_SelectionBoxValid = false;
|
2010-06-05 02:49:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModel::HideAmmoProp()
|
|
|
|
{
|
|
|
|
if (m_AmmoPropPoint == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Hide the ammo prop, show all others on the same prop point
|
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
|
|
|
if (m_Props[i].m_Point == m_AmmoPropPoint)
|
|
|
|
m_Props[i].m_Hidden = (i == m_AmmoLoadedProp);
|
2011-11-25 07:36:13 +01:00
|
|
|
|
|
|
|
// we only need to invalidate here if the selection box is based on props and their visibilities
|
|
|
|
if (!m_CustomSelectionShape)
|
|
|
|
m_SelectionBoxValid = false;
|
2010-06-05 02:49:14 +02:00
|
|
|
}
|
|
|
|
|
2011-03-13 20:22:05 +01:00
|
|
|
CModelAbstract* CModel::FindFirstAmmoProp()
|
2010-06-05 02:49:14 +02:00
|
|
|
{
|
|
|
|
if (m_AmmoPropPoint)
|
|
|
|
return m_Props[m_AmmoLoadedProp].m_Model;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
|
|
|
{
|
2011-03-13 20:22:05 +01:00
|
|
|
CModel* propModel = m_Props[i].m_Model->ToCModel();
|
|
|
|
if (propModel)
|
|
|
|
{
|
|
|
|
CModelAbstract* model = propModel->FindFirstAmmoProp();
|
|
|
|
if (model)
|
|
|
|
return model;
|
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2010-06-05 02:49:14 +02:00
|
|
|
|
|
|
|
return NULL;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Clone: return a clone of this model
|
2011-03-13 20:22:05 +01:00
|
|
|
CModelAbstract* CModel::Clone() const
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2007-03-01 19:52:53 +01:00
|
|
|
CModel* clone = new CModel(m_SkeletonAnimManager);
|
2005-05-21 03:40:32 +02:00
|
|
|
clone->m_ObjectBounds = m_ObjectBounds;
|
2004-05-30 02:46:58 +02:00
|
|
|
clone->InitModel(m_pModelDef);
|
2005-03-22 18:09:36 +01:00
|
|
|
clone->SetMaterial(m_Material);
|
2004-05-30 02:46:58 +02:00
|
|
|
clone->SetAnimation(m_Anim);
|
2004-10-06 20:45:59 +02:00
|
|
|
clone->SetFlags(m_Flags);
|
2010-06-05 02:49:14 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < m_Props.size(); i++)
|
|
|
|
{
|
2004-05-30 02:46:58 +02:00
|
|
|
// eek! TODO, RC - need to investigate shallow clone here
|
2010-06-05 02:49:14 +02:00
|
|
|
if (m_AmmoPropPoint && i == m_AmmoLoadedProp)
|
|
|
|
clone->AddAmmoProp(m_Props[i].m_Point, m_Props[i].m_Model->Clone(), m_Props[i].m_ObjectEntry);
|
|
|
|
else
|
|
|
|
clone->AddProp(m_Props[i].m_Point, m_Props[i].m_Model->Clone(), m_Props[i].m_ObjectEntry);
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2010-06-05 02:49:14 +02:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SetTransform: set the transform on this object, and reorientate props accordingly
|
2005-10-26 03:03:28 +02:00
|
|
|
void CModel::SetTransform(const CMatrix3D& transform)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
// call base class to set transform on this object
|
|
|
|
CRenderableObject::SetTransform(transform);
|
2005-10-26 03:03:28 +02:00
|
|
|
InvalidatePosition();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2004-10-30 23:57:46 +02:00
|
|
|
|
2005-04-07 06:29:07 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-03-18 17:57:54 +01:00
|
|
|
void CModel::AddFlagsRec(int flags)
|
|
|
|
{
|
|
|
|
m_Flags |= flags;
|
2012-04-08 18:36:23 +02:00
|
|
|
|
|
|
|
if (flags & MODELFLAG_IGNORE_LOS)
|
|
|
|
m_Material.AddShaderDefine("IGNORE_LOS", "1");
|
|
|
|
|
2011-03-18 17:57:54 +01:00
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
|
|
|
if (m_Props[i].m_Model->ToCModel())
|
|
|
|
m_Props[i].m_Model->ToCModel()->AddFlagsRec(flags);
|
|
|
|
}
|
|
|
|
|
2004-10-30 23:57:46 +02:00
|
|
|
void CModel::SetMaterial(const CMaterial &material)
|
|
|
|
{
|
2005-03-22 18:09:36 +01:00
|
|
|
m_Material = material;
|
2004-10-30 23:57:46 +02:00
|
|
|
}
|
2005-04-07 06:29:07 +02:00
|
|
|
|
2011-03-18 17:57:54 +01:00
|
|
|
void CModel::SetPlayerID(player_id_t id)
|
2005-04-07 06:29:07 +02:00
|
|
|
{
|
2011-03-13 20:22:05 +01:00
|
|
|
CModelAbstract::SetPlayerID(id);
|
2010-06-03 03:29:43 +02:00
|
|
|
|
2005-04-07 06:29:07 +02:00
|
|
|
for (std::vector<Prop>::iterator it = m_Props.begin(); it != m_Props.end(); ++it)
|
|
|
|
it->m_Model->SetPlayerID(id);
|
|
|
|
}
|
|
|
|
|
2010-03-17 23:51:47 +01:00
|
|
|
void CModel::SetShadingColor(const CColor& colour)
|
2005-09-06 10:25:41 +02:00
|
|
|
{
|
2011-03-13 20:22:05 +01:00
|
|
|
CModelAbstract::SetShadingColor(colour);
|
|
|
|
|
2010-06-05 02:49:14 +02:00
|
|
|
for (std::vector<Prop>::iterator it = m_Props.begin(); it != m_Props.end(); ++it)
|
2005-09-06 10:25:41 +02:00
|
|
|
it->m_Model->SetShadingColor(colour);
|
|
|
|
}
|