BL_ModifierDeformer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file gameengine/Converter/BL_ModifierDeformer.cpp
  28. * \ingroup bgeconv
  29. */
  30. #ifdef _MSC_VER
  31. # pragma warning (disable:4786)
  32. #endif
  33. #include "MEM_guardedalloc.h"
  34. #include "BL_ModifierDeformer.h"
  35. #include "CTR_Map.h"
  36. #include "STR_HashedString.h"
  37. #include "RAS_IPolygonMaterial.h"
  38. #include "RAS_MeshObject.h"
  39. #include "PHY_IGraphicController.h"
  40. #include "DNA_armature_types.h"
  41. #include "DNA_action_types.h"
  42. #include "DNA_key_types.h"
  43. #include "DNA_mesh_types.h"
  44. #include "DNA_meshdata_types.h"
  45. #include "DNA_curve_types.h"
  46. #include "DNA_modifier_types.h"
  47. #include "DNA_scene_types.h"
  48. #include "BLI_utildefines.h"
  49. #include "BKE_armature.h"
  50. #include "BKE_action.h"
  51. #include "BKE_key.h"
  52. #include "BKE_ipo.h"
  53. #include "MT_Point3.h"
  54. extern "C"{
  55. #include "BKE_customdata.h"
  56. #include "BKE_DerivedMesh.h"
  57. #include "BKE_lattice.h"
  58. #include "BKE_modifier.h"
  59. }
  60. #include "BLI_blenlib.h"
  61. #include "BLI_math.h"
  62. BL_ModifierDeformer::~BL_ModifierDeformer()
  63. {
  64. if (m_dm) {
  65. // deformedOnly is used as a user counter
  66. if (--m_dm->deformedOnly == 0) {
  67. m_dm->needsFree = 1;
  68. m_dm->release(m_dm);
  69. }
  70. }
  71. };
  72. RAS_Deformer *BL_ModifierDeformer::GetReplica()
  73. {
  74. BL_ModifierDeformer *result;
  75. result = new BL_ModifierDeformer(*this);
  76. result->ProcessReplica();
  77. return result;
  78. }
  79. void BL_ModifierDeformer::ProcessReplica()
  80. {
  81. /* Note! - This is not inherited from PyObjectPlus */
  82. BL_ShapeDeformer::ProcessReplica();
  83. if (m_dm)
  84. // by default try to reuse mesh, deformedOnly is used as a user count
  85. m_dm->deformedOnly++;
  86. // this will force an update and if the mesh cannot be reused, a new one will be created
  87. m_lastModifierUpdate = -1;
  88. }
  89. bool BL_ModifierDeformer::HasCompatibleDeformer(Object *ob)
  90. {
  91. if (!ob->modifiers.first)
  92. return false;
  93. // soft body cannot use mesh modifiers
  94. if ((ob->gameflag & OB_SOFT_BODY) != 0)
  95. return false;
  96. ModifierData* md;
  97. for (md = (ModifierData *)ob->modifiers.first; md; md = md->next) {
  98. if (modifier_dependsOnTime(md))
  99. continue;
  100. if (!(md->mode & eModifierMode_Realtime))
  101. continue;
  102. /* armature modifier are handled by SkinDeformer, not ModifierDeformer */
  103. if (md->type == eModifierType_Armature )
  104. continue;
  105. return true;
  106. }
  107. return false;
  108. }
  109. bool BL_ModifierDeformer::HasArmatureDeformer(Object *ob)
  110. {
  111. if (!ob->modifiers.first)
  112. return false;
  113. ModifierData* md = (ModifierData*)ob->modifiers.first;
  114. if (md->type == eModifierType_Armature )
  115. return true;
  116. return false;
  117. }
  118. // return a deformed mesh that supports mapping (with a valid CD_ORIGINDEX layer)
  119. struct DerivedMesh* BL_ModifierDeformer::GetPhysicsMesh()
  120. {
  121. /* we need to compute the deformed mesh taking into account the current
  122. * shape and skin deformers, we cannot just call mesh_create_derived_physics()
  123. * because that would use the m_transvers already deformed previously by BL_ModifierDeformer::Update(),
  124. * so restart from scratch by forcing a full update the shape/skin deformers
  125. * (will do nothing if there is no such deformer) */
  126. BL_ShapeDeformer::ForceUpdate();
  127. BL_ShapeDeformer::Update();
  128. // now apply the modifiers but without those that don't support mapping
  129. Object* blendobj = m_gameobj->GetBlendObject();
  130. /* hack: the modifiers require that the mesh is attached to the object
  131. * It may not be the case here because of replace mesh actuator */
  132. Mesh *oldmesh = (Mesh*)blendobj->data;
  133. blendobj->data = m_bmesh;
  134. DerivedMesh *dm = mesh_create_derived_physics(m_scene, blendobj, m_transverts, CD_MASK_MESH);
  135. /* restore object data */
  136. blendobj->data = oldmesh;
  137. /* m_transverts is correct here (takes into account deform only modifiers) */
  138. /* the derived mesh returned by this function must be released by the caller !!! */
  139. return dm;
  140. }
  141. bool BL_ModifierDeformer::Update(void)
  142. {
  143. bool bShapeUpdate = BL_ShapeDeformer::Update();
  144. if (bShapeUpdate || m_lastModifierUpdate != m_gameobj->GetLastFrame()) {
  145. // static derived mesh are not updated
  146. if (m_dm == NULL || m_bDynamic) {
  147. /* execute the modifiers */
  148. Object* blendobj = m_gameobj->GetBlendObject();
  149. /* hack: the modifiers require that the mesh is attached to the object
  150. * It may not be the case here because of replace mesh actuator */
  151. Mesh *oldmesh = (Mesh*)blendobj->data;
  152. blendobj->data = m_bmesh;
  153. /* execute the modifiers */
  154. DerivedMesh *dm = mesh_create_derived_no_virtual(m_scene, blendobj, m_transverts, CD_MASK_MESH);
  155. /* restore object data */
  156. blendobj->data = oldmesh;
  157. /* free the current derived mesh and replace, (dm should never be NULL) */
  158. if (m_dm != NULL) {
  159. // HACK! use deformedOnly as a user counter
  160. if (--m_dm->deformedOnly == 0) {
  161. m_dm->needsFree = 1;
  162. m_dm->release(m_dm);
  163. }
  164. }
  165. m_dm = dm;
  166. // get rid of temporary data
  167. m_dm->needsFree = 0;
  168. m_dm->release(m_dm);
  169. // HACK! use deformedOnly as a user counter
  170. m_dm->deformedOnly = 1;
  171. DM_update_materials(m_dm, blendobj);
  172. /* update the graphic controller */
  173. PHY_IGraphicController *ctrl = m_gameobj->GetGraphicController();
  174. if (ctrl) {
  175. float min[3], max[3];
  176. INIT_MINMAX(min, max);
  177. m_dm->getMinMax(m_dm, min, max);
  178. ctrl->SetLocalAabb(min, max);
  179. }
  180. }
  181. m_lastModifierUpdate=m_gameobj->GetLastFrame();
  182. bShapeUpdate = true;
  183. int nmat = m_pMeshObject->NumMaterials();
  184. for (int imat=0; imat<nmat; imat++) {
  185. RAS_MeshMaterial *mmat = m_pMeshObject->GetMeshMaterial(imat);
  186. RAS_MeshSlot **slot = mmat->m_slots[(void*)m_gameobj];
  187. if (!slot || !*slot)
  188. continue;
  189. (*slot)->m_pDerivedMesh = m_dm;
  190. }
  191. }
  192. return bShapeUpdate;
  193. }
  194. bool BL_ModifierDeformer::Apply(RAS_IPolyMaterial *mat)
  195. {
  196. if (!Update())
  197. return false;
  198. return true;
  199. }