Blender V2.61 - r43446

BL_ModifierDeformer.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_ModifierDeformer.h"
00039 #include "CTR_Map.h"
00040 #include "STR_HashedString.h"
00041 #include "RAS_IPolygonMaterial.h"
00042 #include "RAS_MeshObject.h"
00043 #include "PHY_IGraphicController.h"
00044 
00045 //#include "BL_ArmatureController.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 "DNA_ipo_types.h"
00052 #include "DNA_curve_types.h"
00053 #include "DNA_modifier_types.h"
00054 #include "DNA_scene_types.h"
00055 #include "BLI_utildefines.h"
00056 #include "BKE_armature.h"
00057 #include "BKE_action.h"
00058 #include "BKE_key.h"
00059 #include "BKE_ipo.h"
00060 #include "MT_Point3.h"
00061 
00062 extern "C"{
00063     #include "BKE_customdata.h"
00064     #include "BKE_DerivedMesh.h"
00065     #include "BKE_lattice.h"
00066     #include "BKE_modifier.h"
00067 }
00068  
00069 
00070 #include "BLI_blenlib.h"
00071 #include "BLI_math.h"
00072 
00073 #define __NLA_DEFNORMALS
00074 //#undef __NLA_DEFNORMALS
00075 
00076 
00077 BL_ModifierDeformer::~BL_ModifierDeformer()
00078 {
00079     if (m_dm) {
00080         // deformedOnly is used as a user counter
00081         if (--m_dm->deformedOnly == 0) {
00082             m_dm->needsFree = 1;
00083             m_dm->release(m_dm);
00084         }
00085     }
00086 };
00087 
00088 RAS_Deformer *BL_ModifierDeformer::GetReplica()
00089 {
00090     BL_ModifierDeformer *result;
00091 
00092     result = new BL_ModifierDeformer(*this);
00093     result->ProcessReplica();
00094     return result;
00095 }
00096 
00097 void BL_ModifierDeformer::ProcessReplica()
00098 {
00099     /* Note! - This is not inherited from PyObjectPlus */
00100     BL_ShapeDeformer::ProcessReplica();
00101     if (m_dm)
00102         // by default try to reuse mesh, deformedOnly is used as a user count
00103         m_dm->deformedOnly++;
00104     // this will force an update and if the mesh cannot be reused, a new one will be created
00105     m_lastModifierUpdate = -1;
00106 }
00107 
00108 bool BL_ModifierDeformer::HasCompatibleDeformer(Object *ob)
00109 {
00110     if (!ob->modifiers.first)
00111         return false;
00112     // soft body cannot use mesh modifiers
00113     if ((ob->gameflag & OB_SOFT_BODY) != 0)
00114         return false;
00115     ModifierData* md;
00116     for (md = (ModifierData*)ob->modifiers.first; md; md = (ModifierData*)md->next) {
00117         if (modifier_dependsOnTime(md))
00118             continue;
00119         if (!(md->mode & eModifierMode_Realtime))
00120             continue;
00121         /* armature modifier are handled by SkinDeformer, not ModifierDeformer */
00122         if (md->type == eModifierType_Armature )
00123             continue;
00124         return true;
00125     }
00126     return false;
00127 }
00128 
00129 bool BL_ModifierDeformer::HasArmatureDeformer(Object *ob)
00130 {
00131     if (!ob->modifiers.first)
00132         return false;
00133 
00134     ModifierData* md = (ModifierData*)ob->modifiers.first;
00135     if(md->type == eModifierType_Armature )
00136         return true;
00137 
00138     return false;
00139 }
00140 
00141 // return a deformed mesh that supports mapping (with a valid CD_ORIGINDEX layer)
00142 struct DerivedMesh* BL_ModifierDeformer::GetPhysicsMesh()
00143 {
00144     // we need to compute the deformed mesh taking into account the current
00145     // shape and skin deformers, we cannot just call mesh_create_derived_physics()
00146     // because that would use the m_transvers already deformed previously by BL_ModifierDeformer::Update(),
00147     // so restart from scratch by forcing a full update the shape/skin deformers 
00148     // (will do nothing if there is no such deformer)
00149     BL_ShapeDeformer::ForceUpdate();
00150     BL_ShapeDeformer::Update();
00151     // now apply the modifiers but without those that don't support mapping
00152     Object* blendobj = m_gameobj->GetBlendObject();
00153     /* hack: the modifiers require that the mesh is attached to the object
00154        It may not be the case here because of replace mesh actuator */
00155     Mesh *oldmesh = (Mesh*)blendobj->data;
00156     blendobj->data = m_bmesh;
00157     DerivedMesh *dm = mesh_create_derived_physics(m_scene, blendobj, m_transverts, CD_MASK_MESH);
00158     /* restore object data */
00159     blendobj->data = oldmesh;
00160     /* m_transverts is correct here (takes into account deform only modifiers) */
00161     /* the derived mesh returned by this function must be released by the caller !!! */
00162     return dm;
00163 }
00164 
00165 bool BL_ModifierDeformer::Update(void)
00166 {
00167     bool bShapeUpdate = BL_ShapeDeformer::Update();
00168 
00169     if (bShapeUpdate || m_lastModifierUpdate != m_gameobj->GetLastFrame()) {
00170         // static derived mesh are not updated
00171         if (m_dm == NULL || m_bDynamic) {
00172             /* execute the modifiers */
00173             Object* blendobj = m_gameobj->GetBlendObject();
00174             /* hack: the modifiers require that the mesh is attached to the object
00175                It may not be the case here because of replace mesh actuator */
00176             Mesh *oldmesh = (Mesh*)blendobj->data;
00177             blendobj->data = m_bmesh;
00178             /* execute the modifiers */     
00179             DerivedMesh *dm = mesh_create_derived_no_virtual(m_scene, blendobj, m_transverts, CD_MASK_MESH);
00180             /* restore object data */
00181             blendobj->data = oldmesh;
00182             /* free the current derived mesh and replace, (dm should never be NULL) */
00183             if (m_dm != NULL) {
00184                 // HACK! use deformedOnly as a user counter
00185                 if (--m_dm->deformedOnly == 0) {
00186                     m_dm->needsFree = 1;
00187                     m_dm->release(m_dm);
00188                 }
00189             }
00190             m_dm = dm;
00191             // get rid of temporary data
00192             m_dm->needsFree = 0;
00193             m_dm->release(m_dm);
00194             // HACK! use deformedOnly as a user counter
00195             m_dm->deformedOnly = 1;
00196             /* update the graphic controller */
00197             PHY_IGraphicController *ctrl = m_gameobj->GetGraphicController();
00198             if (ctrl) {
00199                 float min_r[3], max_r[3];
00200                 INIT_MINMAX(min_r, max_r);
00201                 m_dm->getMinMax(m_dm, min_r, max_r);
00202                 ctrl->setLocalAabb(min_r, max_r);
00203             }
00204         }
00205         m_lastModifierUpdate=m_gameobj->GetLastFrame();
00206         bShapeUpdate = true;
00207     }
00208     return bShapeUpdate;
00209 }
00210 
00211 bool BL_ModifierDeformer::Apply(RAS_IPolyMaterial *mat)
00212 {
00213     if (!Update())
00214         return false;
00215 
00216     // drawing is based on derived mesh, must set it in the mesh slots
00217     int nmat = m_pMeshObject->NumMaterials();
00218     for (int imat=0; imat<nmat; imat++) {
00219         RAS_MeshMaterial *mmat = m_pMeshObject->GetMeshMaterial(imat);
00220         RAS_MeshSlot **slot = mmat->m_slots[(void*)m_gameobj];
00221         if(!slot || !*slot)
00222             continue;
00223         (*slot)->m_pDerivedMesh = m_dm;
00224     }
00225     return true;
00226 }