~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to source/gameengine/Converter/BL_SkinDeformer.cpp

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: BL_SkinDeformer.cpp 16736 2008-09-26 02:27:59Z erwin $
 
3
 *
 
4
 * ***** BEGIN GPL LICENSE BLOCK *****
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 *
 
20
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
21
 * All rights reserved.
 
22
 *
 
23
 * The Original Code is: all of this file.
 
24
 *
 
25
 * Contributor(s): none yet.
 
26
 *
 
27
 * ***** END GPL LICENSE BLOCK *****
 
28
 */
 
29
 
 
30
#ifdef WIN32
 
31
#pragma warning (disable : 4786)
 
32
#endif //WIN32
 
33
 
 
34
#include "BL_SkinDeformer.h"
 
35
#include "GEN_Map.h"
 
36
#include "STR_HashedString.h"
 
37
#include "RAS_IPolygonMaterial.h"
 
38
#include "BL_SkinMeshObject.h"
 
39
 
 
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"
 
48
 
 
49
extern "C"{
 
50
        #include "BKE_lattice.h"
 
51
}
 
52
 #include "BKE_utildefines.h"
 
53
 
 
54
#include "BLI_blenlib.h"
 
55
#include "BLI_arithb.h"
 
56
 
 
57
#define __NLA_DEFNORMALS
 
58
//#undef __NLA_DEFNORMALS
 
59
 
 
60
BL_SkinDeformer::BL_SkinDeformer(BL_DeformableGameObject *gameobj,
 
61
                                                                struct Object *bmeshobj, 
 
62
                                                                class BL_SkinMeshObject *mesh,
 
63
                                                                BL_ArmatureObject* arma)
 
64
                                                        :       //
 
65
                                                        BL_MeshDeformer(gameobj, bmeshobj, mesh),
 
66
                                                        m_armobj(arma),
 
67
                                                        m_lastArmaUpdate(-1),
 
68
                                                        m_defbase(&bmeshobj->defbase),
 
69
                                                        m_releaseobject(false),
 
70
                                                        m_poseApplied(false)
 
71
{
 
72
        Mat4CpyMat4(m_obmat, bmeshobj->obmat);
 
73
};
 
74
 
 
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,
 
80
        bool release_object,
 
81
        BL_ArmatureObject* arma)        :       
 
82
                BL_MeshDeformer(gameobj, bmeshobj_old, mesh),
 
83
                m_armobj(arma),
 
84
                m_lastArmaUpdate(-1),
 
85
                m_defbase(&bmeshobj_old->defbase),
 
86
                m_releaseobject(release_object)
 
87
        {
 
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);
 
94
        }
 
95
 
 
96
BL_SkinDeformer::~BL_SkinDeformer()
 
97
{
 
98
        if(m_releaseobject && m_armobj)
 
99
                m_armobj->Release();
 
100
}
 
101
 
 
102
void BL_SkinDeformer::Relink(GEN_Map<class GEN_HashedPtr, void*>*map)
 
103
{
 
104
        if (m_armobj) {
 
105
                void **h_obj = (*map)[m_armobj];
 
106
 
 
107
                if (h_obj)
 
108
                        SetArmature( (BL_ArmatureObject*)(*h_obj) );
 
109
                else
 
110
                        m_armobj=NULL;
 
111
        }
 
112
 
 
113
        BL_MeshDeformer::Relink(map);
 
114
}
 
115
 
 
116
bool BL_SkinDeformer::Apply(RAS_IPolyMaterial *mat)
 
117
{
 
118
        RAS_MeshSlot::iterator it;
 
119
        RAS_MeshMaterial *mmat;
 
120
        RAS_MeshSlot *slot;
 
121
        size_t i;
 
122
 
 
123
        // update the vertex in m_transverts
 
124
        Update();
 
125
 
 
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])
 
132
                return true;
 
133
 
 
134
        slot = *mmat->m_slots[(void*)m_gameobj];
 
135
 
 
136
        // for each array
 
137
        for(slot->begin(it); !slot->end(it); slot->next(it)) {
 
138
                // for each vertex
 
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()]);
 
143
                }
 
144
        }
 
145
 
 
146
        return true;
 
147
}
 
148
 
 
149
RAS_Deformer *BL_SkinDeformer::GetReplica(class KX_GameObject* replica)
 
150
{
 
151
        BL_SkinDeformer *result;
 
152
 
 
153
        result = new BL_SkinDeformer(*this);
 
154
        result->ProcessReplica();
 
155
        return result;
 
156
}
 
157
 
 
158
void BL_SkinDeformer::ProcessReplica()
 
159
{
 
160
}
 
161
 
 
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)
 
165
{
 
166
        /* See if the armature has been updated for this frame */
 
167
        if (PoseUpdated()){     
 
168
                float obmat[4][4];      // the original object matrice 
 
169
 
 
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();
 
173
 
 
174
                /* store verts locally */
 
175
                VerifyStorage();
 
176
        
 
177
                /* duplicate */
 
178
                for (int v =0; v<m_bmesh->totvert; v++)
 
179
                        VECCOPY(m_transverts[v], m_bmesh->mvert[v].co);
 
180
 
 
181
                m_armobj->ApplyPose();
 
182
 
 
183
                // save matrix first
 
184
                Mat4CpyMat4(obmat, m_objMesh->obmat);
 
185
                // set reference matrix
 
186
                Mat4CpyMat4(m_objMesh->obmat, m_obmat);
 
187
 
 
188
                armature_deform_verts( par_arma, m_objMesh, NULL, m_transverts, NULL, m_bmesh->totvert, ARM_DEF_VGROUP, NULL, NULL );
 
189
                
 
190
                // restore matrix 
 
191
                Mat4CpyMat4(m_objMesh->obmat, obmat);
 
192
 
 
193
#ifdef __NLA_DEFNORMALS
 
194
                RecalcNormals();
 
195
#endif
 
196
 
 
197
                /* Update the current frame */
 
198
                m_lastArmaUpdate=m_armobj->GetLastFrame();
 
199
 
 
200
                m_armobj->RestorePose();
 
201
 
 
202
                /* indicate that the m_transverts and normals are up to date */
 
203
                return true;
 
204
        }
 
205
 
 
206
        return false;
 
207
}
 
208
 
 
209
/* XXX note: I propose to drop this function */
 
210
void BL_SkinDeformer::SetArmature(BL_ArmatureObject *armobj)
 
211
{
 
212
        // only used to set the object now
 
213
        m_armobj = armobj;
 
214
}