4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 /** \file gameengine/Converter/BL_SkinDeformer.cpp
35 #if defined(WIN32) && !defined(FREE_WINDOWS)
36 #pragma warning (disable : 4786)
39 // Eigen2 stuff used for BGEDeformVerts
43 #include "BL_SkinDeformer.h"
45 #include "STR_HashedString.h"
46 #include "RAS_IPolygonMaterial.h"
47 #include "RAS_MeshObject.h"
49 //#include "BL_ArmatureController.h"
50 #include "DNA_armature_types.h"
51 #include "DNA_action_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "BLI_utildefines.h"
55 #include "BKE_armature.h"
56 #include "BKE_action.h"
57 #include "MT_Point3.h"
60 #include "BKE_lattice.h"
61 #include "BKE_deform.h"
65 #include "BLI_blenlib.h"
68 #define __NLA_DEFNORMALS
69 //#undef __NLA_DEFNORMALS
71 BL_SkinDeformer::BL_SkinDeformer(BL_DeformableGameObject *gameobj,
72 struct Object *bmeshobj,
73 class RAS_MeshObject *mesh,
74 BL_ArmatureObject* arma)
76 BL_MeshDeformer(gameobj, bmeshobj, mesh),
79 //m_defbase(&bmeshobj->defbase),
80 m_releaseobject(false),
86 copy_m4_m4(m_obmat, bmeshobj->obmat);
89 BL_SkinDeformer::BL_SkinDeformer(
90 BL_DeformableGameObject *gameobj,
91 struct Object *bmeshobj_old, // Blender object that owns the new mesh
92 struct Object *bmeshobj_new, // Blender object that owns the original mesh
93 class RAS_MeshObject *mesh,
96 BL_ArmatureObject* arma) :
97 BL_MeshDeformer(gameobj, bmeshobj_old, mesh),
100 //m_defbase(&bmeshobj_old->defbase),
101 m_releaseobject(release_object),
102 m_recalcNormal(recalc_normal),
103 m_copyNormals(false),
106 // this is needed to ensure correct deformation of mesh:
107 // the deformation is done with Blender's armature_deform_verts() function
108 // that takes an object as parameter and not a mesh. The object matrice is used
109 // in the calculation, so we must use the matrix of the original object to
110 // simulate a pure replacement of the mesh.
111 copy_m4_m4(m_obmat, bmeshobj_new->obmat);
114 BL_SkinDeformer::~BL_SkinDeformer()
116 if(m_releaseobject && m_armobj)
119 delete [] m_dfnrToPC;
122 void BL_SkinDeformer::Relink(CTR_Map<class CTR_HashedPtr, void*>*map)
125 void **h_obj = (*map)[m_armobj];
128 m_armobj = (BL_ArmatureObject*)(*h_obj);
133 BL_MeshDeformer::Relink(map);
136 bool BL_SkinDeformer::Apply(RAS_IPolyMaterial *mat)
138 RAS_MeshSlot::iterator it;
139 RAS_MeshMaterial *mmat;
141 size_t i, nmat, imat;
143 // update the vertex in m_transverts
148 // the vertex cache is unique to this deformer, no need to update it
149 // if it wasn't updated! We must update all the materials at once
150 // because we will not get here again for the other material
151 nmat = m_pMeshObject->NumMaterials();
152 for (imat=0; imat<nmat; imat++) {
153 mmat = m_pMeshObject->GetMeshMaterial(imat);
154 if(!mmat->m_slots[(void*)m_gameobj])
157 slot = *mmat->m_slots[(void*)m_gameobj];
160 for(slot->begin(it); !slot->end(it); slot->next(it)) {
162 // copy the untransformed data from the original mvert
163 for(i=it.startvertex; i<it.endvertex; i++) {
164 RAS_TexVert& v = it.vertex[i];
165 v.SetXYZ(m_transverts[v.getOrigIndex()]);
167 v.SetNormal(m_transnors[v.getOrigIndex()]);
173 m_copyNormals = false;
178 RAS_Deformer *BL_SkinDeformer::GetReplica()
180 BL_SkinDeformer *result;
182 result = new BL_SkinDeformer(*this);
183 /* there is m_armobj that must be fixed but we cannot do it now, it will be done in Relink */
184 result->ProcessReplica();
188 void BL_SkinDeformer::ProcessReplica()
190 BL_MeshDeformer::ProcessReplica();
191 m_lastArmaUpdate = -1;
192 m_releaseobject = false;
196 void BL_SkinDeformer::BlenderDeformVerts()
198 float obmat[4][4]; // the original object matrix
199 Object* par_arma = m_armobj->GetArmatureObject();
202 copy_m4_m4(obmat, m_objMesh->obmat);
203 // set reference matrix
204 copy_m4_m4(m_objMesh->obmat, m_obmat);
206 armature_deform_verts( par_arma, m_objMesh, NULL, m_transverts, NULL, m_bmesh->totvert, ARM_DEF_VGROUP, NULL, NULL );
209 copy_m4_m4(m_objMesh->obmat, obmat);
211 #ifdef __NLA_DEFNORMALS
217 void BL_SkinDeformer::BGEDeformVerts()
219 Object *par_arma = m_armobj->GetArmatureObject();
220 MDeformVert *dverts = m_bmesh->dvert;
222 int numGroups = BLI_countlist(&m_objMesh->defbase);
227 if (m_dfnrToPC == NULL)
229 m_dfnrToPC = new bPoseChannel*[numGroups];
231 for (i=0, dg=(bDeformGroup*)m_objMesh->defbase.first;
233 ++i, dg=(bDeformGroup*)dg->next)
235 m_dfnrToPC[i] = get_pose_channel(par_arma->pose, dg->name);
237 if (m_dfnrToPC[i] && m_dfnrToPC[i]->bone->flag & BONE_NO_DEFORM)
238 m_dfnrToPC[i] = NULL;
243 for (int i=0; i<m_bmesh->totvert; ++i)
245 float contrib = 0.f, weight, max_weight=0.f;
246 bPoseChannel *pchan=NULL;
248 Eigen::Map<Eigen::Vector3f> norm(m_transnors[i]);
249 Eigen::Vector4f vec(0, 0, 0, 1);
250 Eigen::Matrix4f norm_chan_mat;
251 Eigen::Vector4f co(m_transverts[i][0],
258 if (!dvert->totweight)
261 for (int j=0; j<dvert->totweight; ++j)
263 int index = dvert->dw[j].def_nr;
265 if (index < numGroups && (pchan=m_dfnrToPC[index]))
267 weight = dvert->dw[j].weight;
271 Eigen::Vector4f cop(co);
272 Eigen::Matrix4f chan_mat = Eigen::Matrix4f::Map((float*)pchan->chan_mat);
274 // Update Vertex Position
276 vec += (cop - co)*weight;
278 // Save the most influential channel so we can use it to update the vertex normal
279 if (weight > max_weight)
282 norm_chan_mat = chan_mat;
291 // Update Vertex Normal
292 norm = norm_chan_mat.corner<3, 3>(Eigen::TopLeft)*norm;
294 if (contrib > 0.0001f)
300 m_transverts[i][0] = co[0];
301 m_transverts[i][1] = co[1];
302 m_transverts[i][2] = co[2];
304 m_copyNormals = true;
307 bool BL_SkinDeformer::UpdateInternal(bool shape_applied)
309 /* See if the armature has been updated for this frame */
313 /* store verts locally */
317 for (int v =0; v<m_bmesh->totvert; v++)
319 VECCOPY(m_transverts[v], m_bmesh->mvert[v].co);
320 VECCOPY(m_transnors[v], m_bmesh->mvert[v].no);
324 m_armobj->ApplyPose();
326 switch (m_armobj->GetVertDeformType())
328 case ARM_VDEF_BGE_CPU:
331 case ARM_VDEF_BLENDER:
333 BlenderDeformVerts();
336 /* Update the current frame */
337 m_lastArmaUpdate=m_armobj->GetLastFrame();
339 m_armobj->RestorePose();
340 /* dynamic vertex, cannot use display list */
342 /* indicate that the m_transverts and normals are up to date */
349 bool BL_SkinDeformer::Update(void)
351 return UpdateInternal(false);
354 /* XXX note: I propose to drop this function */
355 void BL_SkinDeformer::SetArmature(BL_ArmatureObject *armobj)
357 // only used to set the object now