2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-06-18 06:46:49 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-06-18 06:46:49 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2011-02-25 13:30:41 +00:00
|
|
|
/** \file gameengine/Converter/BL_ShapeDeformer.cpp
|
|
|
|
* \ingroup bgeconv
|
|
|
|
*/
|
|
|
|
|
2012-10-15 02:15:07 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning (disable:4786)
|
|
|
|
#endif
|
2008-06-18 06:46:49 +00:00
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
#include "BL_ShapeDeformer.h"
|
2011-05-06 20:18:42 +00:00
|
|
|
#include "CTR_Map.h"
|
2008-06-18 06:46:49 +00:00
|
|
|
#include "STR_HashedString.h"
|
|
|
|
#include "RAS_IPolygonMaterial.h"
|
2009-11-24 22:44:29 +00:00
|
|
|
#include "RAS_MeshObject.h"
|
2008-06-18 06:46:49 +00:00
|
|
|
|
2011-06-30 19:33:13 +00:00
|
|
|
#include "DNA_anim_types.h"
|
2008-06-18 06:46:49 +00:00
|
|
|
#include "DNA_armature_types.h"
|
|
|
|
#include "DNA_action_types.h"
|
|
|
|
#include "DNA_key_types.h"
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
#include "BKE_armature.h"
|
|
|
|
#include "BKE_action.h"
|
2012-02-14 07:54:12 +00:00
|
|
|
#include "BKE_global.h"
|
|
|
|
#include "BKE_main.h"
|
2008-06-18 06:46:49 +00:00
|
|
|
#include "BKE_key.h"
|
2014-04-06 23:30:59 +00:00
|
|
|
#include "BKE_fcurve.h"
|
2008-06-24 22:19:00 +00:00
|
|
|
#include "BKE_ipo.h"
|
2013-04-14 18:54:06 +00:00
|
|
|
#include "BKE_library.h"
|
2008-06-18 06:46:49 +00:00
|
|
|
#include "MT_Point3.h"
|
|
|
|
|
|
|
|
extern "C"{
|
|
|
|
#include "BKE_lattice.h"
|
2011-06-30 19:33:13 +00:00
|
|
|
#include "BKE_animsys.h"
|
2008-06-18 06:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "BLI_blenlib.h"
|
2009-11-10 20:43:45 +00:00
|
|
|
#include "BLI_math.h"
|
2008-06-18 06:46:49 +00:00
|
|
|
|
|
|
|
#define __NLA_DEFNORMALS
|
|
|
|
//#undef __NLA_DEFNORMALS
|
|
|
|
|
2011-06-23 19:09:09 +00:00
|
|
|
BL_ShapeDeformer::BL_ShapeDeformer(BL_DeformableGameObject *gameobj,
|
2011-09-01 02:12:53 +00:00
|
|
|
Object *bmeshobj,
|
|
|
|
RAS_MeshObject *mesh)
|
|
|
|
:
|
|
|
|
BL_SkinDeformer(gameobj,bmeshobj, mesh),
|
|
|
|
m_useShapeDrivers(false),
|
|
|
|
m_lastShapeUpdate(-1)
|
2011-06-23 19:09:09 +00:00
|
|
|
{
|
2013-04-14 18:54:06 +00:00
|
|
|
m_key = BKE_key_copy(m_bmesh->key);
|
2011-06-23 19:09:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* this second constructor is needed for making a mesh deformable on the fly. */
|
|
|
|
BL_ShapeDeformer::BL_ShapeDeformer(BL_DeformableGameObject *gameobj,
|
|
|
|
Object *bmeshobj_old,
|
|
|
|
Object *bmeshobj_new,
|
|
|
|
RAS_MeshObject *mesh,
|
|
|
|
bool release_object,
|
|
|
|
bool recalc_normal,
|
|
|
|
BL_ArmatureObject* arma)
|
|
|
|
:
|
|
|
|
BL_SkinDeformer(gameobj, bmeshobj_old, bmeshobj_new, mesh, release_object, recalc_normal, arma),
|
2011-08-09 03:27:05 +00:00
|
|
|
m_useShapeDrivers(false),
|
|
|
|
m_lastShapeUpdate(-1)
|
2011-06-23 19:09:09 +00:00
|
|
|
{
|
2013-04-14 18:54:06 +00:00
|
|
|
m_key = BKE_key_copy(m_bmesh->key);
|
2011-06-23 19:09:09 +00:00
|
|
|
};
|
2008-06-18 06:46:49 +00:00
|
|
|
|
|
|
|
BL_ShapeDeformer::~BL_ShapeDeformer()
|
|
|
|
{
|
2013-04-14 18:54:06 +00:00
|
|
|
if (m_key)
|
2011-06-23 19:09:09 +00:00
|
|
|
{
|
2014-01-15 15:37:03 +00:00
|
|
|
BKE_libblock_free(G.main, m_key);
|
2012-02-14 07:54:12 +00:00
|
|
|
m_key = NULL;
|
2011-06-23 19:09:09 +00:00
|
|
|
}
|
2008-06-18 06:46:49 +00:00
|
|
|
};
|
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
RAS_Deformer *BL_ShapeDeformer::GetReplica()
|
2008-06-18 06:46:49 +00:00
|
|
|
{
|
|
|
|
BL_ShapeDeformer *result;
|
|
|
|
|
|
|
|
result = new BL_ShapeDeformer(*this);
|
|
|
|
result->ProcessReplica();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BL_ShapeDeformer::ProcessReplica()
|
|
|
|
{
|
2009-06-08 20:08:19 +00:00
|
|
|
BL_SkinDeformer::ProcessReplica();
|
|
|
|
m_lastShapeUpdate = -1;
|
2013-04-14 18:54:06 +00:00
|
|
|
|
|
|
|
m_key = BKE_key_copy(m_key);
|
2008-06-18 06:46:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 23:30:59 +00:00
|
|
|
bool BL_ShapeDeformer::LoadShapeDrivers(KX_GameObject* parent)
|
2008-06-24 22:19:00 +00:00
|
|
|
{
|
2014-04-09 23:19:13 +00:00
|
|
|
// Only load shape drivers if we have a key
|
|
|
|
if (GetKey() == NULL) {
|
|
|
|
m_useShapeDrivers = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-06 23:30:59 +00:00
|
|
|
// Fix drivers since BL_ArmatureObject makes copies
|
2014-05-09 23:05:23 +00:00
|
|
|
if (parent->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE && GetKey()->adt) {
|
2014-04-06 23:30:59 +00:00
|
|
|
BL_ArmatureObject *arma = (BL_ArmatureObject*)parent;
|
|
|
|
FCurve *fcu;
|
|
|
|
|
|
|
|
for (fcu = (FCurve*)GetKey()->adt->drivers.first; fcu; fcu = (FCurve*)fcu->next) {
|
|
|
|
|
|
|
|
DriverVar *dvar;
|
|
|
|
for (dvar = (DriverVar*)fcu->driver->variables.first; dvar; dvar = (DriverVar*)dvar->next) {
|
|
|
|
DRIVER_TARGETS_USED_LOOPER(dvar)
|
|
|
|
{
|
|
|
|
if (dtar->id) {
|
|
|
|
if ((Object*)dtar->id == arma->GetOrigArmatureObject())
|
|
|
|
dtar->id = (ID*)arma->GetArmatureObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DRIVER_TARGETS_LOOPER_END
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-30 19:33:13 +00:00
|
|
|
// This used to check if we had drivers from this armature,
|
|
|
|
// now we just assume we want to use shape drivers
|
|
|
|
// and let the animsys handle things.
|
|
|
|
m_useShapeDrivers = true;
|
|
|
|
|
|
|
|
return true;
|
2008-06-24 22:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BL_ShapeDeformer::ExecuteShapeDrivers(void)
|
|
|
|
{
|
2011-06-30 19:33:13 +00:00
|
|
|
if (m_useShapeDrivers && PoseUpdated()) {
|
|
|
|
// We don't need an actual time, just use 0
|
2011-07-28 15:07:32 +00:00
|
|
|
BKE_animsys_evaluate_animdata(NULL, &GetKey()->id, GetKey()->adt, 0.f, ADT_RECALC_DRIVERS);
|
2008-09-17 01:29:54 +00:00
|
|
|
|
2008-06-24 22:19:00 +00:00
|
|
|
ForceUpdate();
|
2009-06-08 20:08:19 +00:00
|
|
|
m_bDynamic = true;
|
2008-06-24 22:19:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-06-18 06:46:49 +00:00
|
|
|
bool BL_ShapeDeformer::Update(void)
|
|
|
|
{
|
|
|
|
bool bShapeUpdate = false;
|
|
|
|
bool bSkinUpdate = false;
|
|
|
|
|
2008-06-24 22:19:00 +00:00
|
|
|
ExecuteShapeDrivers();
|
|
|
|
|
2008-06-18 06:46:49 +00:00
|
|
|
/* See if the object shape has changed */
|
|
|
|
if (m_lastShapeUpdate != m_gameobj->GetLastFrame()) {
|
|
|
|
/* the key coefficient have been set already, we just need to blend the keys */
|
|
|
|
Object* blendobj = m_gameobj->GetBlendObject();
|
|
|
|
|
2009-10-22 09:31:07 +00:00
|
|
|
/* we will blend the key directly in m_transverts array: it is used by armature as the start position */
|
2013-04-14 18:54:06 +00:00
|
|
|
/* m_key can be NULL in case of Modifier deformer */
|
|
|
|
if (m_key) {
|
2013-08-19 10:36:39 +00:00
|
|
|
WeightsArrayCache cache = {0, NULL};
|
|
|
|
float **per_keyblock_weights;
|
|
|
|
|
2009-10-22 09:31:07 +00:00
|
|
|
/* store verts locally */
|
|
|
|
VerifyStorage();
|
|
|
|
|
2013-08-19 10:36:39 +00:00
|
|
|
per_keyblock_weights = BKE_keyblock_get_per_block_weights(blendobj, m_key, &cache);
|
|
|
|
BKE_key_evaluate_relative(0, m_bmesh->totvert, m_bmesh->totvert, (char *)(float *)m_transverts,
|
|
|
|
m_key, NULL, per_keyblock_weights, 0); /* last arg is ignored */
|
|
|
|
BKE_keyblock_free_per_block_weights(m_key, per_keyblock_weights, &cache);
|
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
m_bDynamic = true;
|
|
|
|
}
|
2008-06-18 06:46:49 +00:00
|
|
|
|
|
|
|
// Don't release the weight array as in Blender, it will most likely be reusable on next frame
|
|
|
|
// The weight array are ultimately deleted when the skin mesh is destroyed
|
|
|
|
|
|
|
|
/* Update the current frame */
|
|
|
|
m_lastShapeUpdate=m_gameobj->GetLastFrame();
|
|
|
|
|
|
|
|
// As we have changed, the mesh, the skin deformer must update as well.
|
|
|
|
// This will force the update
|
|
|
|
BL_SkinDeformer::ForceUpdate();
|
|
|
|
bShapeUpdate = true;
|
|
|
|
}
|
|
|
|
// check for armature deform
|
2009-10-22 09:31:07 +00:00
|
|
|
bSkinUpdate = BL_SkinDeformer::UpdateInternal(bShapeUpdate && m_bDynamic);
|
2008-06-18 06:46:49 +00:00
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
// non dynamic deformer = Modifer without armature and shape keys, no need to create storage
|
|
|
|
if (!bSkinUpdate && bShapeUpdate && m_bDynamic) {
|
2009-10-22 09:31:07 +00:00
|
|
|
// this means that there is no armature, we still need to
|
|
|
|
// update the normal (was not done after shape key calculation)
|
2008-06-18 06:46:49 +00:00
|
|
|
|
2008-06-19 14:40:46 +00:00
|
|
|
#ifdef __NLA_DEFNORMALS
|
2009-06-08 20:08:19 +00:00
|
|
|
if (m_recalcNormal)
|
|
|
|
RecalcNormals();
|
2008-06-19 14:40:46 +00:00
|
|
|
#endif
|
2014-05-09 23:03:54 +00:00
|
|
|
|
|
|
|
// We also need to handle transverts now (used to be in BL_SkinDeformer::Apply())
|
|
|
|
UpdateTransverts();
|
2008-06-18 06:46:49 +00:00
|
|
|
bSkinUpdate = true;
|
|
|
|
}
|
2013-04-14 18:54:06 +00:00
|
|
|
|
2008-06-18 06:46:49 +00:00
|
|
|
return bSkinUpdate;
|
|
|
|
}
|
2011-06-23 19:09:09 +00:00
|
|
|
|
|
|
|
Key *BL_ShapeDeformer::GetKey()
|
|
|
|
{
|
2013-04-14 18:54:06 +00:00
|
|
|
return m_key;
|
2011-06-23 19:09:09 +00:00
|
|
|
}
|