KX_SoftBodyDeformer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/KX_SoftBodyDeformer.cpp
  28. * \ingroup bgeconv
  29. */
  30. #ifdef _MSC_VER
  31. # pragma warning (disable:4786)
  32. #endif //WIN32
  33. #include "MT_assert.h"
  34. #include "KX_SoftBodyDeformer.h"
  35. #include "RAS_MeshObject.h"
  36. #include "CTR_Map.h"
  37. #include "CTR_HashedPtr.h"
  38. #ifdef WITH_BULLET
  39. #include "CcdPhysicsEnvironment.h"
  40. #include "CcdPhysicsController.h"
  41. #include "BulletSoftBody/btSoftBody.h"
  42. #include "btBulletDynamicsCommon.h"
  43. void KX_SoftBodyDeformer::Relink(CTR_Map<class CTR_HashedPtr, void*>*map)
  44. {
  45. void **h_obj = (*map)[m_gameobj];
  46. if (h_obj) {
  47. m_gameobj = (BL_DeformableGameObject*)(*h_obj);
  48. m_pMeshObject = m_gameobj->GetMesh(0);
  49. } else {
  50. m_gameobj = NULL;
  51. m_pMeshObject = NULL;
  52. }
  53. }
  54. bool KX_SoftBodyDeformer::Apply(class RAS_IPolyMaterial *polymat)
  55. {
  56. CcdPhysicsController* ctrl = (CcdPhysicsController*) m_gameobj->GetPhysicsController();
  57. if (!ctrl)
  58. return false;
  59. btSoftBody* softBody= ctrl->GetSoftBody();
  60. if (!softBody)
  61. return false;
  62. //printf("apply\n");
  63. RAS_MeshSlot::iterator it;
  64. RAS_MeshMaterial *mmat;
  65. RAS_MeshSlot *slot;
  66. size_t i;
  67. // update the vertex in m_transverts
  68. Update();
  69. // The vertex cache can only be updated for this deformer:
  70. // Duplicated objects with more than one ploymaterial (=multiple mesh slot per object)
  71. // share the same mesh (=the same cache). As the rendering is done per polymaterial
  72. // cycling through the objects, the entire mesh cache cannot be updated in one shot.
  73. mmat = m_pMeshObject->GetMeshMaterial(polymat);
  74. if (!mmat->m_slots[(void*)m_gameobj])
  75. return true;
  76. slot = *mmat->m_slots[(void*)m_gameobj];
  77. // for each array
  78. for (slot->begin(it); !slot->end(it); slot->next(it))
  79. {
  80. btSoftBody::tNodeArray& nodes(softBody->m_nodes);
  81. int index = 0;
  82. for (i=it.startvertex; i<it.endvertex; i++,index++) {
  83. RAS_TexVert& v = it.vertex[i];
  84. btAssert(v.getSoftBodyIndex() >= 0);
  85. MT_Point3 pt (
  86. nodes[v.getSoftBodyIndex()].m_x.getX(),
  87. nodes[v.getSoftBodyIndex()].m_x.getY(),
  88. nodes[v.getSoftBodyIndex()].m_x.getZ());
  89. v.SetXYZ(pt);
  90. MT_Vector3 normal (
  91. nodes[v.getSoftBodyIndex()].m_n.getX(),
  92. nodes[v.getSoftBodyIndex()].m_n.getY(),
  93. nodes[v.getSoftBodyIndex()].m_n.getZ());
  94. v.SetNormal(normal);
  95. }
  96. }
  97. return true;
  98. }
  99. #endif