Blender  V2.59
BL_ModifierDeformer.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: BL_ModifierDeformer.cpp 36523 2011-05-06 20:18:42Z blendix $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00035 #if defined(WIN32) && !defined(FREE_WINDOWS)
00036 #pragma warning (disable : 4786)
00037 #endif //WIN32
00038 
00039 #include "MEM_guardedalloc.h"
00040 #include "BL_ModifierDeformer.h"
00041 #include "CTR_Map.h"
00042 #include "STR_HashedString.h"
00043 #include "RAS_IPolygonMaterial.h"
00044 #include "RAS_MeshObject.h"
00045 #include "PHY_IGraphicController.h"
00046 
00047 //#include "BL_ArmatureController.h"
00048 #include "DNA_armature_types.h"
00049 #include "DNA_action_types.h"
00050 #include "DNA_key_types.h"
00051 #include "DNA_mesh_types.h"
00052 #include "DNA_meshdata_types.h"
00053 #include "DNA_ipo_types.h"
00054 #include "DNA_curve_types.h"
00055 #include "DNA_modifier_types.h"
00056 #include "DNA_scene_types.h"
00057 #include "BLI_utildefines.h"
00058 #include "BKE_armature.h"
00059 #include "BKE_action.h"
00060 #include "BKE_key.h"
00061 #include "BKE_ipo.h"
00062 #include "MT_Point3.h"
00063 
00064 extern "C"{
00065         #include "BKE_customdata.h"
00066         #include "BKE_DerivedMesh.h"
00067         #include "BKE_lattice.h"
00068         #include "BKE_modifier.h"
00069 }
00070  
00071 
00072 #include "BLI_blenlib.h"
00073 #include "BLI_math.h"
00074 
00075 #define __NLA_DEFNORMALS
00076 //#undef __NLA_DEFNORMALS
00077 
00078 
00079 BL_ModifierDeformer::~BL_ModifierDeformer()
00080 {
00081         if (m_dm) {
00082                 // deformedOnly is used as a user counter
00083                 if (--m_dm->deformedOnly == 0) {
00084                         m_dm->needsFree = 1;
00085                         m_dm->release(m_dm);
00086                 }
00087         }
00088 };
00089 
00090 RAS_Deformer *BL_ModifierDeformer::GetReplica()
00091 {
00092         BL_ModifierDeformer *result;
00093 
00094         result = new BL_ModifierDeformer(*this);
00095         result->ProcessReplica();
00096         return result;
00097 }
00098 
00099 void BL_ModifierDeformer::ProcessReplica()
00100 {
00101         /* Note! - This is not inherited from PyObjectPlus */
00102         BL_ShapeDeformer::ProcessReplica();
00103         if (m_dm)
00104                 // by default try to reuse mesh, deformedOnly is used as a user count
00105                 m_dm->deformedOnly++;
00106         // this will force an update and if the mesh cannot be reused, a new one will be created
00107         m_lastModifierUpdate = -1;
00108 }
00109 
00110 bool BL_ModifierDeformer::HasCompatibleDeformer(Object *ob)
00111 {
00112         if (!ob->modifiers.first)
00113                 return false;
00114         // soft body cannot use mesh modifiers
00115         if ((ob->gameflag & OB_SOFT_BODY) != 0)
00116                 return false;
00117         ModifierData* md;
00118         for (md = (ModifierData*)ob->modifiers.first; md; md = (ModifierData*)md->next) {
00119                 if (modifier_dependsOnTime(md))
00120                         continue;
00121                 if (!(md->mode & eModifierMode_Realtime))
00122                         continue;
00123                 /* armature modifier are handled by SkinDeformer, not ModifierDeformer */
00124                 if (md->type == eModifierType_Armature )
00125                         continue;
00126                 return true;
00127         }
00128         return false;
00129 }
00130 
00131 bool BL_ModifierDeformer::HasArmatureDeformer(Object *ob)
00132 {
00133         if (!ob->modifiers.first)
00134                 return false;
00135 
00136         ModifierData* md = (ModifierData*)ob->modifiers.first;
00137         if(md->type == eModifierType_Armature )
00138                 return true;
00139 
00140         return false;
00141 }
00142 
00143 // return a deformed mesh that supports mapping (with a valid CD_ORIGINDEX layer)
00144 struct DerivedMesh* BL_ModifierDeformer::GetPhysicsMesh()
00145 {
00146         // we need to compute the deformed mesh taking into account the current
00147         // shape and skin deformers, we cannot just call mesh_create_derived_physics()
00148         // because that would use the m_transvers already deformed previously by BL_ModifierDeformer::Update(),
00149         // so restart from scratch by forcing a full update the shape/skin deformers 
00150         // (will do nothing if there is no such deformer)
00151         BL_ShapeDeformer::ForceUpdate();
00152         BL_ShapeDeformer::Update();
00153         // now apply the modifiers but without those that don't support mapping
00154         Object* blendobj = m_gameobj->GetBlendObject();
00155         /* hack: the modifiers require that the mesh is attached to the object
00156            It may not be the case here because of replace mesh actuator */
00157         Mesh *oldmesh = (Mesh*)blendobj->data;
00158         blendobj->data = m_bmesh;
00159         DerivedMesh *dm = mesh_create_derived_physics(m_scene, blendobj, m_transverts, CD_MASK_MESH);
00160         /* restore object data */
00161         blendobj->data = oldmesh;
00162         /* m_transverts is correct here (takes into account deform only modifiers) */
00163         /* the derived mesh returned by this function must be released by the caller !!! */
00164         return dm;
00165 }
00166 
00167 bool BL_ModifierDeformer::Update(void)
00168 {
00169         bool bShapeUpdate = BL_ShapeDeformer::Update();
00170 
00171         if (bShapeUpdate || m_lastModifierUpdate != m_gameobj->GetLastFrame()) {
00172                 // static derived mesh are not updated
00173                 if (m_dm == NULL || m_bDynamic) {
00174                         /* execute the modifiers */
00175                         Object* blendobj = m_gameobj->GetBlendObject();
00176                         /* hack: the modifiers require that the mesh is attached to the object
00177                            It may not be the case here because of replace mesh actuator */
00178                         Mesh *oldmesh = (Mesh*)blendobj->data;
00179                         blendobj->data = m_bmesh;
00180                         /* execute the modifiers */             
00181                         DerivedMesh *dm = mesh_create_derived_no_virtual(m_scene, blendobj, m_transverts, CD_MASK_MESH);
00182                         /* restore object data */
00183                         blendobj->data = oldmesh;
00184                         /* free the current derived mesh and replace, (dm should never be NULL) */
00185                         if (m_dm != NULL) {
00186                                 // HACK! use deformedOnly as a user counter
00187                                 if (--m_dm->deformedOnly == 0) {
00188                                         m_dm->needsFree = 1;
00189                                         m_dm->release(m_dm);
00190                                 }
00191                         }
00192                         m_dm = dm;
00193                         // get rid of temporary data
00194                         m_dm->needsFree = 0;
00195                         m_dm->release(m_dm);
00196                         // HACK! use deformedOnly as a user counter
00197                         m_dm->deformedOnly = 1;
00198                         /* update the graphic controller */
00199                         PHY_IGraphicController *ctrl = m_gameobj->GetGraphicController();
00200                         if (ctrl) {
00201                                 float min_r[3], max_r[3];
00202                                 INIT_MINMAX(min_r, max_r);
00203                                 m_dm->getMinMax(m_dm, min_r, max_r);
00204                                 ctrl->setLocalAabb(min_r, max_r);
00205                         }
00206                 }
00207                 m_lastModifierUpdate=m_gameobj->GetLastFrame();
00208                 bShapeUpdate = true;
00209         }
00210         return bShapeUpdate;
00211 }
00212 
00213 bool BL_ModifierDeformer::Apply(RAS_IPolyMaterial *mat)
00214 {
00215         if (!Update())
00216                 return false;
00217 
00218         // drawing is based on derived mesh, must set it in the mesh slots
00219         int nmat = m_pMeshObject->NumMaterials();
00220         for (int imat=0; imat<nmat; imat++) {
00221                 RAS_MeshMaterial *mmat = m_pMeshObject->GetMeshMaterial(imat);
00222                 RAS_MeshSlot **slot = mmat->m_slots[(void*)m_gameobj];
00223                 if(!slot || !*slot)
00224                         continue;
00225                 (*slot)->m_pDerivedMesh = m_dm;
00226         }
00227         return true;
00228 }