2010-04-06 01:09:34 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
#include "maths/Bound.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"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Profile.h"
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-02-11 13:57:19 +01:00
|
|
|
#include "ps/CLogger.h"
|
2009-11-03 22:46:35 +01:00
|
|
|
#define LOG_CATEGORY L"graphics"
|
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)
|
2006-12-15 17:09:30 +01:00
|
|
|
: m_Parent(NULL), m_Flags(0), m_Anim(NULL), m_AnimTime(0),
|
|
|
|
m_BoneMatrices(NULL), m_InverseBindBoneMatrices(NULL),
|
2007-03-01 19:52:53 +01:00
|
|
|
m_PositionValid(false), m_ShadingColor(1,1,1,1),
|
|
|
|
m_SkeletonAnimManager(skeletonAnimManager)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Destructor
|
|
|
|
CModel::~CModel()
|
|
|
|
{
|
2005-10-26 03:03:28 +02:00
|
|
|
// Detach us from our parent
|
|
|
|
if (m_Parent)
|
|
|
|
{
|
|
|
|
for(std::vector<Prop>::iterator iter = m_Parent->m_Props.begin();
|
|
|
|
iter != m_Parent->m_Props.end();
|
|
|
|
++iter)
|
|
|
|
{
|
|
|
|
if (iter->m_Model == this)
|
|
|
|
{
|
|
|
|
m_Parent->m_Props.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Parent = 0;
|
|
|
|
}
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
ReleaseData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReleaseData: delete anything allocated by the model
|
|
|
|
void CModel::ReleaseData()
|
|
|
|
{
|
|
|
|
delete[] m_BoneMatrices;
|
2006-12-15 17:09:30 +01:00
|
|
|
delete[] m_InverseBindBoneMatrices;
|
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
|
|
|
{
|
2005-10-30 22:31:10 +01:00
|
|
|
m_Props[i].m_Model->m_Parent = 0;
|
2004-05-30 02:46:58 +02:00
|
|
|
delete m_Props[i].m_Model;
|
|
|
|
}
|
|
|
|
m_Props.clear();
|
2004-12-12 19:40:00 +01:00
|
|
|
m_pModelDef = CModelDefPtr();
|
2004-12-16 13:01:47 +01:00
|
|
|
|
|
|
|
Handle h = m_Texture.GetHandle();
|
2005-09-02 04:54:02 +02:00
|
|
|
ogl_tex_free(h);
|
2004-12-16 13:01:47 +01:00
|
|
|
m_Texture.SetHandle(0);
|
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)
|
|
|
|
{
|
2004-05-30 02:46:58 +02:00
|
|
|
// allocate matrices for bone transformations
|
2006-12-15 17:09:30 +01:00
|
|
|
m_BoneMatrices = new CMatrix3D[numBones];
|
|
|
|
m_InverseBindBoneMatrices = new CMatrix3D[numBones];
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
// store default pose until animation assigned
|
2006-12-15 17:09:30 +01:00
|
|
|
CBoneState* defpose = modeldef->GetBones();
|
|
|
|
for (size_t i = 0; i < numBones; ++i)
|
|
|
|
{
|
|
|
|
m_BoneMatrices[i].SetIdentity();
|
|
|
|
m_BoneMatrices[i].Rotate(defpose[i].m_Rotation);
|
|
|
|
m_BoneMatrices[i].Translate(defpose[i].m_Translation);
|
|
|
|
|
|
|
|
m_InverseBindBoneMatrices[i].SetIdentity();
|
|
|
|
m_InverseBindBoneMatrices[i].Translate(-defpose[i].m_Translation);
|
|
|
|
m_InverseBindBoneMatrices[i].Rotate(defpose[i].m_Rotation.GetInverse());
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
m_PositionValid = true;
|
|
|
|
|
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))
|
2005-02-10 00:19:48 +01:00
|
|
|
CalcObjectBounds();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_Anim->m_ObjectBounds.IsEmpty())
|
|
|
|
CalcAnimatedObjectBound(m_Anim->m_AnimDef, m_Anim->m_ObjectBounds);
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert(! 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;
|
|
|
|
}
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
m_ObjectBounds.Transform(GetTransform(),m_Bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CalcObjectBounds: calculate object space bounds of this model, based solely on vertex positions
|
|
|
|
void CModel::CalcObjectBounds()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CalcAnimatedObjectBound: calculate bounds encompassing all vertex positions for given animation
|
|
|
|
void CModel::CalcAnimatedObjectBound(CSkeletonAnimDef* anim,CBound& result)
|
|
|
|
{
|
|
|
|
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();
|
2005-10-26 03:03:28 +02:00
|
|
|
CModel* oldparent = m_Parent;
|
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
result += CModelDef::SkinPoint(verts[i], GetAnimatedBoneMatrices(), GetInverseBindBoneMatrices());
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BuildAnimation: load raw animation frame animation from given file, and build a
|
|
|
|
// animation specific to this model
|
2010-04-06 01:09:34 +02:00
|
|
|
CSkeletonAnim* CModel::BuildAnimation(const VfsPath& pathname, const char* 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;
|
|
|
|
anim->m_ActionPos = actionpos * anim->m_AnimDef->GetDuration();
|
|
|
|
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-04-06 01:09:34 +02:00
|
|
|
// Update: update this model by the given time, in msec
|
2004-05-30 02:46:58 +02:00
|
|
|
void CModel::Update(float time)
|
|
|
|
{
|
2007-02-01 15:46:14 +01:00
|
|
|
if (m_Anim && m_Anim->m_AnimDef && m_BoneMatrices)
|
|
|
|
{
|
2007-02-10 04:09:52 +01:00
|
|
|
float oldAnimTime = m_AnimTime;
|
2004-05-30 02:46:58 +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
|
2010-04-06 01:09:34 +02:00
|
|
|
m_AnimTime += time;
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2007-02-01 15:46:14 +01:00
|
|
|
float duration = m_Anim->m_AnimDef->GetDuration();
|
|
|
|
if (m_AnimTime > duration)
|
|
|
|
{
|
2006-03-17 04:59:49 +01:00
|
|
|
if (m_Flags & MODELFLAG_NOLOOPANIMATION)
|
2007-02-10 04:09:52 +01:00
|
|
|
{
|
|
|
|
if (m_NextAnim)
|
|
|
|
SetAnimation(m_NextAnim);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Changing to no animation - probably becoming a corpse.
|
|
|
|
// Make sure the last displayed frame is the final frame
|
|
|
|
// of the animation.
|
|
|
|
float nearlyEnd = duration - 1.f; // 1 msec
|
2007-02-25 22:11:57 +01:00
|
|
|
if (fabs(oldAnimTime - nearlyEnd) < 1.f)
|
2007-02-10 04:09:52 +01:00
|
|
|
SetAnimation(NULL);
|
|
|
|
else
|
|
|
|
m_AnimTime = nearlyEnd;
|
|
|
|
}
|
|
|
|
}
|
2005-05-27 02:38:30 +02:00
|
|
|
else
|
2007-02-01 15:46:14 +01:00
|
|
|
m_AnimTime = fmod(m_AnimTime, duration);
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark vertices as dirty
|
|
|
|
SetDirty(RENDERDATA_UPDATE_VERTICES);
|
|
|
|
|
|
|
|
// mark matrices as dirty
|
2005-10-26 03:03:28 +02:00
|
|
|
InvalidatePosition();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update props
|
2007-02-01 15:46:14 +01:00
|
|
|
for (size_t i = 0; i < m_Props.size(); ++i)
|
2004-05-30 02:46:58 +02:00
|
|
|
m_Props[i].m_Model->Update(time);
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CModel::NeedsNewAnim(float time) const
|
|
|
|
{
|
|
|
|
// TODO: fix UnitAnimation so it correctly loops animated props
|
|
|
|
|
|
|
|
if (m_Anim && m_Anim->m_AnimDef && m_BoneMatrices)
|
|
|
|
{
|
|
|
|
float duration = m_Anim->m_AnimDef->GetDuration();
|
2010-04-06 01:09:34 +02:00
|
|
|
if (m_AnimTime + time > duration)
|
2007-02-10 04:09:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-06 01:09:34 +02:00
|
|
|
void CModel::CheckActionTriggers(float time, bool& action, bool& action2) const
|
|
|
|
{
|
|
|
|
if (m_Anim && m_Anim->m_AnimDef && m_BoneMatrices)
|
|
|
|
{
|
|
|
|
if (m_AnimTime <= m_Anim->m_ActionPos && m_AnimTime + time > m_Anim->m_ActionPos)
|
|
|
|
action = true;
|
|
|
|
|
|
|
|
if (m_AnimTime <= m_Anim->m_ActionPos2 && m_AnimTime + time > m_Anim->m_ActionPos2)
|
|
|
|
action2 = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
debug_assert(!m_Parent || m_Parent->m_PositionValid);
|
|
|
|
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.
|
|
|
|
debug_assert(m_PositionValid);
|
|
|
|
return;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-26 03:03:28 +02:00
|
|
|
if (m_Anim && m_BoneMatrices)
|
|
|
|
{
|
|
|
|
PROFILE( "generating bone matrices" );
|
|
|
|
|
|
|
|
debug_assert(m_pModelDef->GetNumBones() == m_Anim->m_AnimDef->GetNumKeys());
|
|
|
|
|
2007-03-01 01:14:35 +01:00
|
|
|
m_Anim->m_AnimDef->BuildBoneMatrices(m_AnimTime, m_BoneMatrices, !(m_Flags & MODELFLAG_NOLOOPANIMATION));
|
2005-10-26 03:03:28 +02:00
|
|
|
|
|
|
|
const CMatrix3D& transform=GetTransform();
|
|
|
|
for (size_t i=0;i<m_pModelDef->GetNumBones();i++) {
|
|
|
|
m_BoneMatrices[i].Concatenate(transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
proptransform.Concatenate(m_BoneMatrices[prop.m_Point->m_BoneIndex]);
|
|
|
|
else
|
|
|
|
proptransform.Concatenate(m_Transform);
|
|
|
|
|
|
|
|
prop.m_Model->SetTransform(proptransform);
|
|
|
|
prop.m_Model->ValidatePosition();
|
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-04-06 01:09:34 +02:00
|
|
|
bool CModel::SetAnimation(CSkeletonAnim* anim, bool once, CSkeletonAnim* next)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2005-02-11 13:57:19 +01:00
|
|
|
m_Anim=NULL; // in case something fails
|
|
|
|
|
|
|
|
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)
|
2005-05-27 02:38:30 +02:00
|
|
|
{
|
2004-11-11 08:09:32 +01:00
|
|
|
m_Flags |= MODELFLAG_NOLOOPANIMATION;
|
2005-05-27 02:38:30 +02:00
|
|
|
m_NextAnim = next;
|
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2006-03-17 04:59:49 +01:00
|
|
|
if (!m_BoneMatrices && anim->m_AnimDef) {
|
2004-05-30 02:46:58 +02:00
|
|
|
// not boned, can't animate
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-17 04:59:49 +01:00
|
|
|
if (m_BoneMatrices && !anim->m_AnimDef) {
|
|
|
|
// boned, but animation isn't valid
|
|
|
|
// (e.g. the default (static) idle animation on an animated unit)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, L"Mismatch between model's skeleton and animation's skeleton (%lu model bones != %lu animation keys)",
|
2009-10-05 18:15:51 +02:00
|
|
|
(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_NextAnim = source->m_NextAnim;
|
|
|
|
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
|
2006-04-24 00:22:18 +02:00
|
|
|
void CModel::AddProp(SPropPoint* point, CModel* model, CObjectEntry* objectentry)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
// position model according to prop point position
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2009-02-18 11:36:27 +01:00
|
|
|
// RemoveProp: remove any props from the given point
|
2004-05-30 02:46:58 +02:00
|
|
|
void CModel::RemoveProp(SPropPoint* point)
|
|
|
|
{
|
2009-02-18 11:36:27 +01:00
|
|
|
for (size_t i = 0; i < m_Props.size(); i++) {
|
|
|
|
if (m_Props[i].m_Point == point) {
|
2005-12-26 11:57:25 +01:00
|
|
|
delete m_Props[i].m_Model;
|
|
|
|
// (when a prop is removed it will automatically remove its prop point)
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Clone: return a clone of this model
|
|
|
|
CModel* CModel::Clone() const
|
|
|
|
{
|
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);
|
|
|
|
clone->SetTexture(m_Texture);
|
2005-05-21 03:40:32 +02:00
|
|
|
if (m_Texture.GetHandle())
|
|
|
|
h_add_ref(m_Texture.GetHandle());
|
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);
|
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++) {
|
2004-05-30 02:46:58 +02:00
|
|
|
// eek! TODO, RC - need to investigate shallow clone here
|
2006-04-24 00:22:18 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
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-02-10 00:19:48 +01:00
|
|
|
InvalidateBounds();
|
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
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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;
|
2007-02-01 15:46:14 +01:00
|
|
|
if(m_Material.GetTexture().Trim(PS_TRIM_BOTH).length() > 0)
|
2005-03-22 18:09:36 +01:00
|
|
|
{
|
2007-02-01 15:46:14 +01:00
|
|
|
// [TODO: uh, shouldn't this be doing something?]
|
2005-03-22 18:09:36 +01:00
|
|
|
}
|
2004-10-30 23:57:46 +02:00
|
|
|
}
|
2005-04-07 06:29:07 +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
|
|
|
void CModel::SetPlayerID(size_t id)
|
2005-04-07 06:29:07 +02:00
|
|
|
{
|
|
|
|
m_Material.SetPlayerColor(id);
|
|
|
|
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::SetPlayerColor(const CColor& colour)
|
2005-04-07 06:29:07 +02:00
|
|
|
{
|
|
|
|
m_Material.SetPlayerColor(colour);
|
|
|
|
}
|
2005-09-06 10:25:41 +02:00
|
|
|
|
2010-03-17 23:51:47 +01:00
|
|
|
void CModel::SetShadingColor(const CColor& colour)
|
2005-09-06 10:25:41 +02:00
|
|
|
{
|
|
|
|
m_ShadingColor = colour;
|
2006-07-10 01:12:37 +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);
|
2006-07-10 01:12:37 +02:00
|
|
|
}
|
2005-09-06 10:25:41 +02:00
|
|
|
}
|