Blender  V2.59
KX_SoftBodyDeformer.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: KX_SoftBodyDeformer.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 "MT_assert.h"
00040 
00041 #include "KX_ConvertPhysicsObject.h"
00042 #include "KX_SoftBodyDeformer.h"
00043 #include "RAS_MeshObject.h"
00044 #include "CTR_Map.h"
00045 #include "CTR_HashedPtr.h"
00046 
00047 #ifdef USE_BULLET
00048 
00049 #include "CcdPhysicsEnvironment.h"
00050 #include "CcdPhysicsController.h"
00051 #include "BulletSoftBody/btSoftBody.h"
00052 
00053 #include "KX_BulletPhysicsController.h"
00054 #include "btBulletDynamicsCommon.h"
00055 
00056 void KX_SoftBodyDeformer::Relink(CTR_Map<class CTR_HashedPtr, void*>*map)
00057 {
00058         void **h_obj = (*map)[m_gameobj];
00059 
00060         if (h_obj) {
00061                 m_gameobj = (BL_DeformableGameObject*)(*h_obj);
00062                 m_pMeshObject = m_gameobj->GetMesh(0);
00063         } else {
00064                 m_gameobj = NULL;
00065                 m_pMeshObject = NULL;
00066         }
00067 }
00068 
00069 bool KX_SoftBodyDeformer::Apply(class RAS_IPolyMaterial *polymat)
00070 {
00071         KX_BulletPhysicsController* ctrl = (KX_BulletPhysicsController*) m_gameobj->GetPhysicsController();
00072         if (!ctrl)
00073                 return false;
00074 
00075         btSoftBody* softBody= ctrl->GetSoftBody();
00076         if (!softBody)
00077                 return false;
00078 
00079         //printf("apply\n");
00080         RAS_MeshSlot::iterator it;
00081         RAS_MeshMaterial *mmat;
00082         RAS_MeshSlot *slot;
00083         size_t i;
00084 
00085         // update the vertex in m_transverts
00086         Update();
00087 
00088         // The vertex cache can only be updated for this deformer:
00089         // Duplicated objects with more than one ploymaterial (=multiple mesh slot per object)
00090         // share the same mesh (=the same cache). As the rendering is done per polymaterial
00091         // cycling through the objects, the entire mesh cache cannot be updated in one shot.
00092         mmat = m_pMeshObject->GetMeshMaterial(polymat);
00093         if(!mmat->m_slots[(void*)m_gameobj])
00094                 return true;
00095 
00096         slot = *mmat->m_slots[(void*)m_gameobj];
00097 
00098         // for each array
00099         for(slot->begin(it); !slot->end(it); slot->next(it)) 
00100         {
00101                 btSoftBody::tNodeArray&   nodes(softBody->m_nodes);
00102 
00103                 int index = 0;
00104                 for(i=it.startvertex; i<it.endvertex; i++,index++) {
00105                         RAS_TexVert& v = it.vertex[i];
00106                         btAssert(v.getSoftBodyIndex() >= 0);
00107 
00108                         MT_Point3 pt (
00109                                 nodes[v.getSoftBodyIndex()].m_x.getX(),
00110                                 nodes[v.getSoftBodyIndex()].m_x.getY(),
00111                                 nodes[v.getSoftBodyIndex()].m_x.getZ());
00112                         v.SetXYZ(pt);
00113 
00114                         MT_Vector3 normal (
00115                                 nodes[v.getSoftBodyIndex()].m_n.getX(),
00116                                 nodes[v.getSoftBodyIndex()].m_n.getY(),
00117                                 nodes[v.getSoftBodyIndex()].m_n.getZ());
00118                         v.SetNormal(normal);
00119 
00120                 }
00121         }
00122         return true;
00123 }
00124 
00125 #endif