b6a59774636b24df6df623f58f7c241d2c2e1e9b
[blender-staging.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 #include "BLI_arithb.h"
51
52 bool BL_MeshDeformer::Apply(RAS_IPolyMaterial*)
53 {
54         size_t i;
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(list<RAS_MeshMaterial>::iterator mit= m_pMeshObject->GetFirstMaterial();
61                         mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
62                         if(!mit->m_slots[(void*)m_gameobj])
63                                 continue;
64
65                         RAS_MeshSlot *slot = *mit->m_slots[(void*)m_gameobj];
66                         RAS_MeshSlot::iterator it;
67
68                         // for each array
69                         for(slot->begin(it); !slot->end(it); slot->next(it)) {
70                                 //      For each vertex
71                                 for(i=it.startvertex; i<it.endvertex; i++) {
72                                         RAS_TexVert& v = it.vertex[i];
73                                         v.SetXYZ(m_bmesh->mvert[v.getOrigIndex()].co);
74                                 }
75                         }
76                 }
77
78                 m_lastDeformUpdate = m_gameobj->GetLastFrame();
79
80                 return true;
81         }
82
83         return false;
84 }
85
86 BL_MeshDeformer::~BL_MeshDeformer()
87 {       
88         if (m_transverts)
89                 delete [] m_transverts;
90         if (m_transnors)
91                 delete [] m_transnors;
92 }
93  
94 void BL_MeshDeformer::Relink(GEN_Map<class GEN_HashedPtr, void*>*map)
95 {
96         void **h_obj = (*map)[m_gameobj];
97
98         if (h_obj)
99                 m_gameobj = (BL_DeformableGameObject*)(*h_obj);
100         else
101                 m_gameobj = NULL;
102 }
103
104 /**
105  * @warning This function is expensive!
106  */
107 void BL_MeshDeformer::RecalcNormals()
108 {
109         /* We don't normalize for performance, not doing it for faces normals
110          * gives area-weight normals which often look better anyway, and use
111          * GL_NORMALIZE so we don't have to do per vertex normalization either
112          * since the GPU can do it faster */
113         list<RAS_MeshMaterial>::iterator mit;
114         RAS_MeshSlot::iterator it;
115         size_t i;
116
117         /* set vertex normals to zero */
118         memset(m_transnors, 0, sizeof(float)*3*m_bmesh->totvert);
119
120         /* add face normals to vertices. */
121         for(mit = m_pMeshObject->GetFirstMaterial();
122                 mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
123                 if(!mit->m_slots[(void*)m_gameobj])
124                         continue;
125
126                 RAS_MeshSlot *slot = *mit->m_slots[(void*)m_gameobj];
127
128                 for(slot->begin(it); !slot->end(it); slot->next(it)) {
129                         int nvert = (int)it.array->m_type;
130
131                         for(i=0; i<it.totindex; i+=nvert) {
132                                 RAS_TexVert& v1 = it.vertex[it.index[i]];
133                                 RAS_TexVert& v2 = it.vertex[it.index[i+1]];
134                                 RAS_TexVert& v3 = it.vertex[it.index[i+2]];
135                                 RAS_TexVert *v4 = NULL;
136
137                                 const float *co1 = v1.getXYZ();
138                                 const float *co2 = v2.getXYZ();
139                                 const float *co3 = v3.getXYZ();
140                                 const float *co4 = NULL;
141                                 
142                                 /* compute face normal */
143                                 float fnor[3], n1[3], n2[3];
144
145                                 if(nvert == 4) {
146                                         v4 = &it.vertex[it.index[i+3]];
147                                         co4 = v4->getXYZ();
148
149                                         n1[0]= co1[0]-co3[0];
150                                         n1[1]= co1[1]-co3[1];
151                                         n1[2]= co1[2]-co3[2];
152
153                                         n2[0]= co2[0]-co4[0];
154                                         n2[1]= co2[1]-co4[1];
155                                         n2[2]= co2[2]-co4[2];
156                                 }
157                                 else {
158                                         n1[0]= co1[0]-co2[0];
159                                         n2[0]= co2[0]-co3[0];
160                                         n1[1]= co1[1]-co2[1];
161
162                                         n2[1]= co2[1]-co3[1];
163                                         n1[2]= co1[2]-co2[2];
164                                         n2[2]= co2[2]-co3[2];
165                                 }
166
167                                 fnor[0]= n1[1]*n2[2] - n1[2]*n2[1];
168                                 fnor[1]= n1[2]*n2[0] - n1[0]*n2[2];
169                                 fnor[2]= n1[0]*n2[1] - n1[1]*n2[0];
170                                 Normalize(fnor);
171
172                                 /* add to vertices for smooth normals */
173                                 float *vn1 = m_transnors[v1.getOrigIndex()];
174                                 float *vn2 = m_transnors[v2.getOrigIndex()];
175                                 float *vn3 = m_transnors[v3.getOrigIndex()];
176
177                                 vn1[0] += fnor[0]; vn1[1] += fnor[1]; vn1[2] += fnor[2];
178                                 vn2[0] += fnor[0]; vn2[1] += fnor[1]; vn2[2] += fnor[2];
179                                 vn3[0] += fnor[0]; vn3[1] += fnor[1]; vn3[2] += fnor[2];
180
181                                 if(v4) {
182                                         float *vn4 = m_transnors[v4->getOrigIndex()];
183                                         vn4[0] += fnor[0]; vn4[1] += fnor[1]; vn4[2] += fnor[2];
184                                 }
185
186                                 /* in case of flat - just assign, the vertices are split */
187                                 if(v1.getFlag() & RAS_TexVert::FLAT) {
188                                         v1.SetNormal(fnor);
189                                         v2.SetNormal(fnor);
190                                         v3.SetNormal(fnor);
191                                         if(v4)
192                                                 v4->SetNormal(fnor);
193                                 }
194                         }
195                 }
196         }
197
198         /* assign smooth vertex normals */
199         for(mit = m_pMeshObject->GetFirstMaterial();
200                 mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
201                 if(!mit->m_slots[(void*)m_gameobj])
202                         continue;
203
204                 RAS_MeshSlot *slot = *mit->m_slots[(void*)m_gameobj];
205
206                 for(slot->begin(it); !slot->end(it); slot->next(it)) {
207                         for(i=it.startvertex; i<it.endvertex; i++) {
208                                 RAS_TexVert& v = it.vertex[i];
209
210                                 if(!(v.getFlag() & RAS_TexVert::FLAT))
211                                         v.SetNormal(m_transnors[v.getOrigIndex()]); //.safe_normalized()
212                         }
213                 }
214         }
215 }
216
217 void BL_MeshDeformer::VerifyStorage()
218 {
219         /* Ensure that we have the right number of verts assigned */
220         if (m_tvtot!=m_bmesh->totvert){
221                 if (m_transverts)
222                         delete [] m_transverts;
223                 if (m_transnors)
224                         delete [] m_transnors;
225                 
226                 m_transverts=new float[m_bmesh->totvert][3];
227                 m_transnors=new float[m_bmesh->totvert][3];
228                 m_tvtot = m_bmesh->totvert;
229         }
230 }
231