svn merge -r 15392:15551 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Converter / BL_MeshDeformer.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  * Simple deformation controller that restores a mesh to its rest position
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #ifdef WIN32
36 // This warning tells us about truncation of __long__ stl-generated names.
37 // It can occasionally cause DevStudio to have internal compiler warnings.
38 #pragma warning( disable : 4786 )     
39 #endif
40
41 #include "RAS_IPolygonMaterial.h"
42 #include "BL_DeformableGameObject.h"
43 #include "BL_MeshDeformer.h"
44 #include "BL_SkinMeshObject.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meshdata_types.h"
47
48 #include "GEN_Map.h"
49 #include "STR_HashedString.h"
50
51 bool BL_MeshDeformer::Apply(RAS_IPolyMaterial*)
52 {
53         size_t i, j;
54         float *co;
55
56         // only apply once per frame if the mesh is actually modified
57         if(m_pMeshObject->MeshModified() &&
58            m_lastDeformUpdate != m_gameobj->GetLastFrame()) {
59                 // For each material
60                 for(RAS_MaterialBucket::Set::iterator mit = m_pMeshObject->GetFirstMaterial();
61                         mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
62                         RAS_IPolyMaterial *mat = (*mit)->GetPolyMaterial();
63
64                         vecVertexArray& vertexarrays = m_pMeshObject->GetVertexCache(mat);
65
66                         // For each array
67                         for (i=0; i<vertexarrays.size(); i++){
68                                 KX_VertexArray& vertexarray = (*vertexarrays[i]);
69
70                                 //      For each vertex
71                                 for (j=0; j<vertexarray.size(); j++){
72                                         RAS_TexVert& v = vertexarray[j];
73                                         co = m_bmesh->mvert[v.getOrigIndex()].co;
74                                         v.SetXYZ(MT_Point3(co));
75                                 }
76                         }
77                 }
78
79                 m_lastDeformUpdate = m_gameobj->GetLastFrame();
80
81                 return true;
82         }
83
84         return false;
85 }
86
87 BL_MeshDeformer::~BL_MeshDeformer()
88 {       
89         if (m_transverts)
90                 delete [] m_transverts;
91         if (m_transnors)
92                 delete [] m_transnors;
93 };
94
95 /**
96  * @warning This function is expensive!
97  */
98 void BL_MeshDeformer::RecalcNormals()
99 {
100         /* We don't normalize for performance, not doing it for faces normals
101          * gives area-weight normals which often look better anyway, and use
102          * GL_NORMALIZE so we don't have to do per vertex normalization either
103          * since the GPU can do it faster */
104         size_t i, j;
105
106         /* set vertex normals to zero */
107         memset(m_transnors, 0, sizeof(float)*3*m_bmesh->totvert);
108
109         /* add face normals to vertices. */
110         for(RAS_MaterialBucket::Set::iterator mit = m_pMeshObject->GetFirstMaterial();
111                 mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
112                 RAS_IPolyMaterial *mat = (*mit)->GetPolyMaterial();
113
114                 const vecIndexArrays& indexarrays = m_pMeshObject->GetIndexCache(mat);
115                 vecVertexArray& vertexarrays = m_pMeshObject->GetVertexCache(mat);
116
117                 for (i=0; i<indexarrays.size(); i++) {
118                         KX_VertexArray& vertexarray = (*vertexarrays[i]);
119                         const KX_IndexArray& indexarray = (*indexarrays[i]);
120                         int nvert = mat->UsesTriangles()? 3: 4;
121
122                         for(j=0; j<indexarray.size(); j+=nvert) {
123                                 RAS_TexVert& v1 = vertexarray[indexarray[j]];
124                                 RAS_TexVert& v2 = vertexarray[indexarray[j+1]];
125                                 RAS_TexVert& v3 = vertexarray[indexarray[j+2]];
126                                 RAS_TexVert *v4 = NULL;
127
128                                 const float *co1 = v1.getLocalXYZ();
129                                 const float *co2 = v2.getLocalXYZ();
130                                 const float *co3 = v3.getLocalXYZ();
131                                 const float *co4 = NULL;
132                                 
133                                 /* compute face normal */
134                                 float fnor[3], n1[3], n2[3];
135
136                                 if(nvert == 4) {
137                                         v4 = &vertexarray[indexarray[j+3]];
138                                         co4 = v4->getLocalXYZ();
139
140                                         n1[0]= co1[0]-co3[0];
141                                         n1[1]= co1[1]-co3[1];
142                                         n1[2]= co1[2]-co3[2];
143
144                                         n2[0]= co2[0]-co4[0];
145                                         n2[1]= co2[1]-co4[1];
146                                         n2[2]= co2[2]-co4[2];
147                                 }
148                                 else {
149                                         n1[0]= co1[0]-co2[0];
150                                         n2[0]= co2[0]-co3[0];
151                                         n1[1]= co1[1]-co2[1];
152
153                                         n2[1]= co2[1]-co3[1];
154                                         n1[2]= co1[2]-co2[2];
155                                         n2[2]= co2[2]-co3[2];
156                                 }
157
158                                 fnor[0]= n1[1]*n2[2] - n1[2]*n2[1];
159                                 fnor[1]= n1[2]*n2[0] - n1[0]*n2[2];
160                                 fnor[2]= n1[0]*n2[1] - n1[1]*n2[0];
161
162                                 /* add to vertices for smooth normals */
163                                 float *vn1 = m_transnors[v1.getOrigIndex()];
164                                 float *vn2 = m_transnors[v2.getOrigIndex()];
165                                 float *vn3 = m_transnors[v3.getOrigIndex()];
166
167                                 vn1[0] += fnor[0]; vn1[1] += fnor[1]; vn1[2] += fnor[2];
168                                 vn2[0] += fnor[0]; vn2[1] += fnor[1]; vn2[2] += fnor[2];
169                                 vn3[0] += fnor[0]; vn3[1] += fnor[1]; vn3[2] += fnor[2];
170
171                                 if(v4) {
172                                         float *vn4 = m_transnors[v4->getOrigIndex()];
173                                         vn4[0] += fnor[0]; vn4[1] += fnor[1]; vn4[2] += fnor[2];
174                                 }
175
176                                 /* in case of flat - just assign, the vertices are split */
177                                 if(v1.getFlag() & TV_CALCFACENORMAL) {
178                                         v1.SetNormal(fnor);
179                                         v2.SetNormal(fnor);
180                                         v3.SetNormal(fnor);
181                                         if(v4)
182                                                 v4->SetNormal(fnor);
183                                 }
184                         }
185                 }
186         }
187
188         /* assign smooth vertex normals */
189         for(RAS_MaterialBucket::Set::iterator mit = m_pMeshObject->GetFirstMaterial();
190                 mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
191                 RAS_IPolyMaterial *mat = (*mit)->GetPolyMaterial();
192
193                 vecVertexArray& vertexarrays = m_pMeshObject->GetVertexCache(mat);
194
195                 for (i=0; i<vertexarrays.size(); i++) {
196                         KX_VertexArray& vertexarray = (*vertexarrays[i]);
197                         
198                         for(j=0; j<vertexarray.size(); j++) {
199                                 RAS_TexVert& v = vertexarray[j];
200
201                                 if(!(v.getFlag() & TV_CALCFACENORMAL))
202                                         v.SetNormal(m_transnors[v.getOrigIndex()]); //.safe_normalized()
203                         }
204                 }
205         }
206 }
207
208 void BL_MeshDeformer::VerifyStorage()
209 {
210         /* Ensure that we have the right number of verts assigned */
211         if (m_tvtot!=m_bmesh->totvert){
212                 if (m_transverts)
213                         delete [] m_transverts;
214                 if (m_transnors)
215                         delete [] m_transnors;
216                 
217                 m_transverts=new float[m_bmesh->totvert][3];
218                 m_transnors=new float[m_bmesh->totvert][3];
219                 m_tvtot = m_bmesh->totvert;
220         }
221 }
222