Blender V2.61 - r43446

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