remove some unused function args
[blender.git] / source / gameengine / Converter / BL_SkinMeshObject.cpp
1 /**
2  * $Id$
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  * Deformer that supports armature skinning
29  */
30
31 #ifdef WIN32
32 #pragma warning (disable:4786) // get rid of stupid stl-visual compiler debug warning
33 #endif //WIN32
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_key_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40
41 #include "RAS_BucketManager.h"
42 #include "RAS_IPolygonMaterial.h"
43
44 #include "KX_GameObject.h"
45
46 #include "BL_SkinMeshObject.h"
47 #include "BL_DeformableGameObject.h"
48
49 BL_SkinMeshObject::BL_SkinMeshObject(Mesh* mesh)
50  : RAS_MeshObject (mesh)
51
52         m_bDeformed = true;
53
54         if (m_mesh && m_mesh->key)
55         {
56                 KeyBlock *kb;
57                 int count=0;
58                 // initialize weight cache for shape objects
59                 // count how many keys in this mesh
60                 for(kb= (KeyBlock*)m_mesh->key->block.first; kb; kb= (KeyBlock*)kb->next)
61                         count++;
62                 m_cacheWeightIndex.resize(count,-1);
63         }
64 }
65
66 BL_SkinMeshObject::~BL_SkinMeshObject()
67 {
68         if (m_mesh && m_mesh->key) 
69         {
70                 KeyBlock *kb;
71                 // remove the weight cache to avoid memory leak 
72                 for(kb= (KeyBlock*)m_mesh->key->block.first; kb; kb= (KeyBlock*)kb->next) {
73                         if(kb->weights) 
74                                 MEM_freeN(kb->weights);
75                         kb->weights= NULL;
76                 }
77         }
78 }
79
80 void BL_SkinMeshObject::UpdateBuckets(void* clientobj,double* oglmatrix,bool useObjectColor,const MT_Vector4& rgbavec, bool visible, bool culled)
81 {
82         list<RAS_MeshMaterial>::iterator it;
83         list<RAS_MeshSlot*>::iterator sit;
84
85         for(it = m_materials.begin();it!=m_materials.end();++it) {
86                 if(!it->m_slots[clientobj])
87                         continue;
88
89                 RAS_MeshSlot *slot = *it->m_slots[clientobj];
90                 slot->SetDeformer(((BL_DeformableGameObject*)clientobj)->GetDeformer());
91         }
92
93         RAS_MeshObject::UpdateBuckets(clientobj, oglmatrix, useObjectColor, rgbavec, visible, culled);
94 }
95
96 static int get_def_index(Object* ob, const char* vgroup)
97 {
98         bDeformGroup *curdef;
99         int index = 0;
100
101         for (curdef = (bDeformGroup*)ob->defbase.first; curdef; curdef=(bDeformGroup*)curdef->next, index++)
102                 if (!strcmp(curdef->name, vgroup))
103                         return index;
104
105         return -1;
106 }
107
108 void BL_SkinMeshObject::CheckWeightCache(Object* obj)
109 {
110         KeyBlock *kb;
111         int kbindex, defindex;
112         MDeformVert *dvert= NULL;
113         int totvert, i, j;
114         float *weights;
115
116         if (!m_mesh->key)
117                 return;
118
119         for(kbindex=0, kb= (KeyBlock*)m_mesh->key->block.first; kb; kb= (KeyBlock*)kb->next, kbindex++)
120         {
121                 // first check the cases where the weight must be cleared
122                 if (kb->vgroup[0] == 0 ||
123                         m_mesh->dvert == NULL ||
124                         (defindex = get_def_index(obj, kb->vgroup)) == -1) {
125                         if (kb->weights) {
126                                 MEM_freeN(kb->weights);
127                                 kb->weights = NULL;
128                         }
129                         m_cacheWeightIndex[kbindex] = -1;
130                 } else if (m_cacheWeightIndex[kbindex] != defindex) {
131                         // a weight array is required but the cache is not matching
132                         if (kb->weights) {
133                                 MEM_freeN(kb->weights);
134                                 kb->weights = NULL;
135                         }
136
137                         dvert= m_mesh->dvert;
138                         totvert= m_mesh->totvert;
139                 
140                         weights= (float*)MEM_callocN(totvert*sizeof(float), "weights");
141                 
142                         for (i=0; i < totvert; i++, dvert++) {
143                                 for(j=0; j<dvert->totweight; j++) {
144                                         if (dvert->dw[j].def_nr == defindex) {
145                                                 weights[i]= dvert->dw[j].weight;
146                                                 break;
147                                         }
148                                 }
149                         }
150                         kb->weights = weights;
151                         m_cacheWeightIndex[kbindex] = defindex;
152                 }
153         }
154 }
155
156