Blender V2.61 - r43446

BL_ShapeDeformer.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #if defined(WIN32) && !defined(FREE_WINDOWS)
00034 #pragma warning (disable : 4786)
00035 #endif //WIN32
00036 
00037 #include "MEM_guardedalloc.h"
00038 #include "BL_ShapeDeformer.h"
00039 #include "CTR_Map.h"
00040 #include "STR_HashedString.h"
00041 #include "RAS_IPolygonMaterial.h"
00042 #include "RAS_MeshObject.h"
00043 
00044 //#include "BL_ArmatureController.h"
00045 #include "DNA_anim_types.h"
00046 #include "DNA_armature_types.h"
00047 #include "DNA_action_types.h"
00048 #include "DNA_key_types.h"
00049 #include "DNA_mesh_types.h"
00050 #include "DNA_meshdata_types.h"
00051 #include "BKE_armature.h"
00052 #include "BKE_action.h"
00053 #include "BKE_key.h"
00054 #include "BKE_ipo.h"
00055 #include "MT_Point3.h"
00056 
00057 extern "C"{
00058     #include "BKE_lattice.h"
00059     #include "BKE_animsys.h"
00060 }
00061  
00062 
00063 #include "BLI_blenlib.h"
00064 #include "BLI_math.h"
00065 
00066 #define __NLA_DEFNORMALS
00067 //#undef __NLA_DEFNORMALS
00068 
00069 BL_ShapeDeformer::BL_ShapeDeformer(BL_DeformableGameObject *gameobj,
00070                                    Object *bmeshobj,
00071                                    RAS_MeshObject *mesh)
00072     :
00073       BL_SkinDeformer(gameobj,bmeshobj, mesh),
00074       m_useShapeDrivers(false),
00075       m_lastShapeUpdate(-1)
00076 {
00077     m_key = m_bmesh->key;
00078     m_bmesh->key = copy_key(m_key);
00079 };
00080 
00081 /* this second constructor is needed for making a mesh deformable on the fly. */
00082 BL_ShapeDeformer::BL_ShapeDeformer(BL_DeformableGameObject *gameobj,
00083                 Object *bmeshobj_old,
00084                 Object *bmeshobj_new,
00085                 RAS_MeshObject *mesh,
00086                 bool release_object,
00087                 bool recalc_normal,
00088                 BL_ArmatureObject* arma)
00089                 :
00090                     BL_SkinDeformer(gameobj, bmeshobj_old, bmeshobj_new, mesh, release_object, recalc_normal, arma),
00091                     m_useShapeDrivers(false),
00092                     m_lastShapeUpdate(-1)
00093 {
00094     m_key = m_bmesh->key;
00095     m_bmesh->key = copy_key(m_key);
00096 };
00097 
00098 BL_ShapeDeformer::~BL_ShapeDeformer()
00099 {
00100     if (m_key && m_bmesh->key)
00101     {
00102         free_key(m_bmesh->key);
00103         m_bmesh->key = m_key;
00104     }
00105 };
00106 
00107 RAS_Deformer *BL_ShapeDeformer::GetReplica()
00108 {
00109     BL_ShapeDeformer *result;
00110 
00111     result = new BL_ShapeDeformer(*this);
00112     result->ProcessReplica();
00113     return result;
00114 }
00115 
00116 void BL_ShapeDeformer::ProcessReplica()
00117 {
00118     BL_SkinDeformer::ProcessReplica();
00119     m_lastShapeUpdate = -1;
00120 }
00121 
00122 bool BL_ShapeDeformer::LoadShapeDrivers(Object* arma)
00123 {
00124     // This used to check if we had drivers from this armature,
00125     // now we just assume we want to use shape drivers
00126     // and let the animsys handle things.
00127     m_useShapeDrivers = true;
00128 
00129     return true;
00130 }
00131 
00132 bool BL_ShapeDeformer::ExecuteShapeDrivers(void)
00133 {
00134     if (m_useShapeDrivers && PoseUpdated()) {
00135         // the shape drivers use the bone matrix as input. Must 
00136         // update the matrix now
00137         m_armobj->ApplyPose();
00138 
00139         // We don't need an actual time, just use 0
00140         BKE_animsys_evaluate_animdata(NULL, &GetKey()->id, GetKey()->adt, 0.f, ADT_RECALC_DRIVERS);
00141 
00142         ForceUpdate();
00143         m_armobj->RestorePose();
00144         m_bDynamic = true;
00145         return true;
00146     }
00147     return false;
00148 }
00149 
00150 bool BL_ShapeDeformer::Update(void)
00151 {
00152     bool bShapeUpdate = false;
00153     bool bSkinUpdate = false;
00154 
00155     ExecuteShapeDrivers();
00156 
00157     /* See if the object shape has changed */
00158     if (m_lastShapeUpdate != m_gameobj->GetLastFrame()) {
00159         /* the key coefficient have been set already, we just need to blend the keys */
00160         Object* blendobj = m_gameobj->GetBlendObject();
00161         
00162         // make sure the vertex weight cache is in line with this object
00163         m_pMeshObject->CheckWeightCache(blendobj);
00164 
00165         /* we will blend the key directly in m_transverts array: it is used by armature as the start position */
00166         /* m_bmesh->key can be NULL in case of Modifier deformer */
00167         if (m_bmesh->key) {
00168             /* store verts locally */
00169             VerifyStorage();
00170 
00171             do_rel_key(0, m_bmesh->totvert, m_bmesh->totvert, (char *)(float *)m_transverts, m_bmesh->key, NULL, 0); /* last arg is ignored */
00172             m_bDynamic = true;
00173         }
00174 
00175         // Don't release the weight array as in Blender, it will most likely be reusable on next frame 
00176         // The weight array are ultimately deleted when the skin mesh is destroyed
00177            
00178         /* Update the current frame */
00179         m_lastShapeUpdate=m_gameobj->GetLastFrame();
00180 
00181         // As we have changed, the mesh, the skin deformer must update as well.
00182         // This will force the update
00183         BL_SkinDeformer::ForceUpdate();
00184         bShapeUpdate = true;
00185     }
00186     // check for armature deform
00187     bSkinUpdate = BL_SkinDeformer::UpdateInternal(bShapeUpdate && m_bDynamic);
00188 
00189     // non dynamic deformer = Modifer without armature and shape keys, no need to create storage
00190     if (!bSkinUpdate && bShapeUpdate && m_bDynamic) {
00191         // this means that there is no armature, we still need to 
00192         // update the normal (was not done after shape key calculation)
00193 
00194 #ifdef __NLA_DEFNORMALS
00195         if (m_recalcNormal)
00196             RecalcNormals();
00197 #endif
00198         bSkinUpdate = true;
00199     }
00200     return bSkinUpdate;
00201 }
00202 
00203 Key *BL_ShapeDeformer::GetKey()
00204 {
00205     return m_bmesh->key;
00206 }
00207 
00208 void BL_ShapeDeformer::SetKey(Key *key)
00209 {
00210     m_bmesh->key = key;
00211 }