2
* $Id: BL_SkinDeformer.cpp 16736 2008-09-26 02:27:59Z erwin $
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
31
#pragma warning (disable : 4786)
34
#include "BL_SkinDeformer.h"
36
#include "STR_HashedString.h"
37
#include "RAS_IPolygonMaterial.h"
38
#include "BL_SkinMeshObject.h"
40
//#include "BL_ArmatureController.h"
41
#include "DNA_armature_types.h"
42
#include "DNA_action_types.h"
43
#include "DNA_mesh_types.h"
44
#include "DNA_meshdata_types.h"
45
#include "BKE_armature.h"
46
#include "BKE_action.h"
47
#include "MT_Point3.h"
50
#include "BKE_lattice.h"
52
#include "BKE_utildefines.h"
54
#include "BLI_blenlib.h"
55
#include "BLI_arithb.h"
57
#define __NLA_DEFNORMALS
58
//#undef __NLA_DEFNORMALS
60
BL_SkinDeformer::BL_SkinDeformer(BL_DeformableGameObject *gameobj,
61
struct Object *bmeshobj,
62
class BL_SkinMeshObject *mesh,
63
BL_ArmatureObject* arma)
65
BL_MeshDeformer(gameobj, bmeshobj, mesh),
68
m_defbase(&bmeshobj->defbase),
69
m_releaseobject(false),
72
Mat4CpyMat4(m_obmat, bmeshobj->obmat);
75
BL_SkinDeformer::BL_SkinDeformer(
76
BL_DeformableGameObject *gameobj,
77
struct Object *bmeshobj_old, // Blender object that owns the new mesh
78
struct Object *bmeshobj_new, // Blender object that owns the original mesh
79
class BL_SkinMeshObject *mesh,
81
BL_ArmatureObject* arma) :
82
BL_MeshDeformer(gameobj, bmeshobj_old, mesh),
85
m_defbase(&bmeshobj_old->defbase),
86
m_releaseobject(release_object)
88
// this is needed to ensure correct deformation of mesh:
89
// the deformation is done with Blender's armature_deform_verts() function
90
// that takes an object as parameter and not a mesh. The object matrice is used
91
// in the calculation, so we must use the matrix of the original object to
92
// simulate a pure replacement of the mesh.
93
Mat4CpyMat4(m_obmat, bmeshobj_new->obmat);
96
BL_SkinDeformer::~BL_SkinDeformer()
98
if(m_releaseobject && m_armobj)
102
void BL_SkinDeformer::Relink(GEN_Map<class GEN_HashedPtr, void*>*map)
105
void **h_obj = (*map)[m_armobj];
108
SetArmature( (BL_ArmatureObject*)(*h_obj) );
113
BL_MeshDeformer::Relink(map);
116
bool BL_SkinDeformer::Apply(RAS_IPolyMaterial *mat)
118
RAS_MeshSlot::iterator it;
119
RAS_MeshMaterial *mmat;
123
// update the vertex in m_transverts
126
// The vertex cache can only be updated for this deformer:
127
// Duplicated objects with more than one ploymaterial (=multiple mesh slot per object)
128
// share the same mesh (=the same cache). As the rendering is done per polymaterial
129
// cycling through the objects, the entire mesh cache cannot be updated in one shot.
130
mmat = m_pMeshObject->GetMeshMaterial(mat);
131
if(!mmat->m_slots[(void*)m_gameobj])
134
slot = *mmat->m_slots[(void*)m_gameobj];
137
for(slot->begin(it); !slot->end(it); slot->next(it)) {
139
// copy the untransformed data from the original mvert
140
for(i=it.startvertex; i<it.endvertex; i++) {
141
RAS_TexVert& v = it.vertex[i];
142
v.SetXYZ(m_transverts[v.getOrigIndex()]);
149
RAS_Deformer *BL_SkinDeformer::GetReplica(class KX_GameObject* replica)
151
BL_SkinDeformer *result;
153
result = new BL_SkinDeformer(*this);
154
result->ProcessReplica();
158
void BL_SkinDeformer::ProcessReplica()
162
//void where_is_pose (Object *ob);
163
//void armature_deform_verts(Object *armOb, Object *target, float (*vertexCos)[3], int numVerts, int deformflag);
164
bool BL_SkinDeformer::Update(void)
166
/* See if the armature has been updated for this frame */
168
float obmat[4][4]; // the original object matrice
170
/* XXX note: where_is_pose() (from BKE_armature.h) calculates all matrices needed to start deforming */
171
/* but it requires the blender object pointer... */
172
Object* par_arma = m_armobj->GetArmatureObject();
174
/* store verts locally */
178
for (int v =0; v<m_bmesh->totvert; v++)
179
VECCOPY(m_transverts[v], m_bmesh->mvert[v].co);
181
m_armobj->ApplyPose();
184
Mat4CpyMat4(obmat, m_objMesh->obmat);
185
// set reference matrix
186
Mat4CpyMat4(m_objMesh->obmat, m_obmat);
188
armature_deform_verts( par_arma, m_objMesh, NULL, m_transverts, NULL, m_bmesh->totvert, ARM_DEF_VGROUP, NULL, NULL );
191
Mat4CpyMat4(m_objMesh->obmat, obmat);
193
#ifdef __NLA_DEFNORMALS
197
/* Update the current frame */
198
m_lastArmaUpdate=m_armobj->GetLastFrame();
200
m_armobj->RestorePose();
202
/* indicate that the m_transverts and normals are up to date */
209
/* XXX note: I propose to drop this function */
210
void BL_SkinDeformer::SetArmature(BL_ArmatureObject *armobj)
212
// only used to set the object now