- split the data structures that actually constitute a mesh
[blender.git] / source / gameengine / Converter / BL_SkinDeformer.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #ifdef WIN32
38 #pragma warning (disable : 4786)
39 #endif //WIN32
40
41 #include "GEN_Map.h"
42 #include "STR_HashedString.h"
43 #include "RAS_IPolygonMaterial.h"
44 #include "BL_SkinMeshObject.h"
45
46 //#include "BL_ArmatureController.h"
47 #include "BL_SkinDeformer.h"
48 #include "DNA_armature_types.h"
49 #include "DNA_action_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_meshdata_types.h"
52 #include "BKE_armature.h"
53 #include "BKE_action.h"
54 #include "MT_Point3.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_arithb.h"
58
59 #define __NLA_DEFNORMALS
60 //#undef __NLA_DEFNORMALS
61
62 BL_SkinDeformer::~BL_SkinDeformer()
63 {
64 };
65
66 bool BL_SkinDeformer::Apply(RAS_IPolyMaterial *mat)
67 {
68         int                     i, j, index;
69         vecVertexArray  array;
70 #ifdef __NLA_OLDDEFORM
71         vecMVertArray   mvarray;
72 #else
73         vecIndexArrays  mvarray;
74 #endif
75         vecMDVertArray  dvarray;
76         vecIndexArrays  diarray;
77
78         RAS_TexVert *tv;
79 #ifdef __NLA_OLDDEFORM
80         MVert   *mvert;
81         MDeformVert     *dvert;
82 #endif
83         MT_Point3 pt;
84 //      float co[3];
85
86         if (!m_armobj)
87                 return false;
88
89         Update();
90
91         array = m_pMeshObject->GetVertexCache(mat);
92 #ifdef __NLA_OLDDEFORM
93         dvarray = m_pMeshObject->GetDVertCache(mat);
94 #endif
95         mvarray = m_pMeshObject->GetMVertCache(mat);
96         diarray = m_pMeshObject->GetDIndexCache(mat);
97         
98
99         // For each array
100         for (i=0; i<array.size(); i++){
101                 //      For each vertex
102                 for (j=0; j<array[i]->size(); j++){
103
104                         tv = &((*array[i])[j]);
105                         
106                         index = ((*diarray[i])[j]);
107 #ifdef __NLA_OLDDEFORM
108                         pt = tv->xyz();
109                         mvert = ((*mvarray[i])[index]);
110                         dvert = ((*dvarray[i])[index]);
111 #endif
112                         
113                         //      Copy the untransformed data from the original mvert
114 #ifdef __NLA_OLDDEFORM
115                         co[0]=mvert->co[0];
116                         co[1]=mvert->co[1];
117                         co[2]=mvert->co[2];
118
119                         //      Do the deformation
120                         GB_calc_armature_deform(co, dvert);
121                         tv->SetXYZ(co);
122 #else
123                         //      Set the data
124                         tv->SetXYZ(m_transverts[((*mvarray[i])[index])]);
125 #ifdef __NLA_DEFNORMALS
126
127                         tv->SetNormal(m_transnors[((*mvarray[i])[index])]);
128 #endif
129 #endif
130                 }
131         }
132         
133         return true;
134 }
135
136 RAS_Deformer *BL_SkinDeformer::GetReplica()
137 {
138         BL_SkinDeformer *result;
139
140         result = new BL_SkinDeformer(*this);
141         result->ProcessReplica();
142         return result;
143 }
144
145 void BL_SkinDeformer::ProcessReplica()
146 {
147 }
148
149 void BL_SkinDeformer::Update(void)
150 {
151
152         /* See if the armature has been updated for this frame */
153         if (m_lastUpdate!=m_armobj->GetLastFrame()){    
154                 
155                 /* Do all of the posing necessary */
156                 GB_init_armature_deform (m_defbase, m_premat, m_postmat);
157                 m_armobj->ApplyPose();
158                 precalc_armature_posemats (m_armobj->GetArmature());
159                 for (Bone *curBone=(Bone*)m_armobj->GetArmature()->bonebase.first; curBone; curBone=(Bone*)curBone->next)
160                         precalc_bone_defmat(curBone);
161                 
162                 VerifyStorage();
163
164                 /* Transform the verts & store locally */
165                 for (int v =0; v<m_bmesh->totvert; v++){
166                         float co[3];
167
168                         co[0]=m_bmesh->mvert[v].co[0];
169                         co[1]=m_bmesh->mvert[v].co[1];
170                         co[2]=m_bmesh->mvert[v].co[2];
171                         GB_calc_armature_deform(co, &m_bmesh->dvert[v]);
172
173                         m_transverts[v]=MT_Point3(co);
174                 }
175                 
176                 RecalcNormals();
177                 
178
179                 /* Update the current frame */
180                 m_lastUpdate=m_armobj->GetLastFrame();
181         }
182 }
183
184 void BL_SkinDeformer::SetArmature(BL_ArmatureObject *armobj)
185 {
186         m_armobj = armobj;
187
188         for (bDeformGroup *dg=(bDeformGroup*)m_defbase->first; dg; dg=(bDeformGroup*)dg->next)
189                         dg->data = (void*)get_named_bone(m_armobj->GetArmature(), dg->name);
190                 
191                 GB_validate_defgroups(m_bmesh, m_defbase);
192 }