BGE modifier: generate correct physic shape, share static derived mesh, share display...
[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::ProcessReplica()
95 {
96         m_transverts = NULL;
97         m_transnors = NULL;
98         m_tvtot = 0;
99         m_bDynamic=false;
100         m_lastDeformUpdate = -1;
101 }
102
103 void BL_MeshDeformer::Relink(GEN_Map<class GEN_HashedPtr, void*>*map)
104 {
105         void **h_obj = (*map)[m_gameobj];
106
107         if (h_obj)
108                 m_gameobj = (BL_DeformableGameObject*)(*h_obj);
109         else
110                 m_gameobj = NULL;
111 }
112
113 /**
114  * @warning This function is expensive!
115  */
116 void BL_MeshDeformer::RecalcNormals()
117 {
118         /* We don't normalize for performance, not doing it for faces normals
119          * gives area-weight normals which often look better anyway, and use
120          * GL_NORMALIZE so we don't have to do per vertex normalization either
121          * since the GPU can do it faster */
122         list<RAS_MeshMaterial>::iterator mit;
123         RAS_MeshSlot::iterator it;
124         size_t i;
125
126         /* set vertex normals to zero */
127         memset(m_transnors, 0, sizeof(float)*3*m_bmesh->totvert);
128
129         /* add face normals to vertices. */
130         for(mit = m_pMeshObject->GetFirstMaterial();
131                 mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
132                 if(!mit->m_slots[(void*)m_gameobj])
133                         continue;
134
135                 RAS_MeshSlot *slot = *mit->m_slots[(void*)m_gameobj];
136
137                 for(slot->begin(it); !slot->end(it); slot->next(it)) {
138                         int nvert = (int)it.array->m_type;
139
140                         for(i=0; i<it.totindex; i+=nvert) {
141                                 RAS_TexVert& v1 = it.vertex[it.index[i]];
142                                 RAS_TexVert& v2 = it.vertex[it.index[i+1]];
143                                 RAS_TexVert& v3 = it.vertex[it.index[i+2]];
144                                 RAS_TexVert *v4 = NULL;
145
146                                 const float *co1 = v1.getXYZ();
147                                 const float *co2 = v2.getXYZ();
148                                 const float *co3 = v3.getXYZ();
149                                 const float *co4 = NULL;
150                                 
151                                 /* compute face normal */
152                                 float fnor[3], n1[3], n2[3];
153
154                                 if(nvert == 4) {
155                                         v4 = &it.vertex[it.index[i+3]];
156                                         co4 = v4->getXYZ();
157
158                                         n1[0]= co1[0]-co3[0];
159                                         n1[1]= co1[1]-co3[1];
160                                         n1[2]= co1[2]-co3[2];
161
162                                         n2[0]= co2[0]-co4[0];
163                                         n2[1]= co2[1]-co4[1];
164                                         n2[2]= co2[2]-co4[2];
165                                 }
166                                 else {
167                                         n1[0]= co1[0]-co2[0];
168                                         n2[0]= co2[0]-co3[0];
169                                         n1[1]= co1[1]-co2[1];
170
171                                         n2[1]= co2[1]-co3[1];
172                                         n1[2]= co1[2]-co2[2];
173                                         n2[2]= co2[2]-co3[2];
174                                 }
175
176                                 fnor[0]= n1[1]*n2[2] - n1[2]*n2[1];
177                                 fnor[1]= n1[2]*n2[0] - n1[0]*n2[2];
178                                 fnor[2]= n1[0]*n2[1] - n1[1]*n2[0];
179                                 Normalize(fnor);
180
181                                 /* add to vertices for smooth normals */
182                                 float *vn1 = m_transnors[v1.getOrigIndex()];
183                                 float *vn2 = m_transnors[v2.getOrigIndex()];
184                                 float *vn3 = m_transnors[v3.getOrigIndex()];
185
186                                 vn1[0] += fnor[0]; vn1[1] += fnor[1]; vn1[2] += fnor[2];
187                                 vn2[0] += fnor[0]; vn2[1] += fnor[1]; vn2[2] += fnor[2];
188                                 vn3[0] += fnor[0]; vn3[1] += fnor[1]; vn3[2] += fnor[2];
189
190                                 if(v4) {
191                                         float *vn4 = m_transnors[v4->getOrigIndex()];
192                                         vn4[0] += fnor[0]; vn4[1] += fnor[1]; vn4[2] += fnor[2];
193                                 }
194
195                                 /* in case of flat - just assign, the vertices are split */
196                                 if(v1.getFlag() & RAS_TexVert::FLAT) {
197                                         v1.SetNormal(fnor);
198                                         v2.SetNormal(fnor);
199                                         v3.SetNormal(fnor);
200                                         if(v4)
201                                                 v4->SetNormal(fnor);
202                                 }
203                         }
204                 }
205         }
206
207         /* assign smooth vertex normals */
208         for(mit = m_pMeshObject->GetFirstMaterial();
209                 mit != m_pMeshObject->GetLastMaterial(); ++ mit) {
210                 if(!mit->m_slots[(void*)m_gameobj])
211                         continue;
212
213                 RAS_MeshSlot *slot = *mit->m_slots[(void*)m_gameobj];
214
215                 for(slot->begin(it); !slot->end(it); slot->next(it)) {
216                         for(i=it.startvertex; i<it.endvertex; i++) {
217                                 RAS_TexVert& v = it.vertex[i];
218
219                                 if(!(v.getFlag() & RAS_TexVert::FLAT))
220                                         v.SetNormal(m_transnors[v.getOrigIndex()]); //.safe_normalized()
221                         }
222                 }
223         }
224 }
225
226 void BL_MeshDeformer::VerifyStorage()
227 {
228         /* Ensure that we have the right number of verts assigned */
229         if (m_tvtot!=m_bmesh->totvert){
230                 if (m_transverts)
231                         delete [] m_transverts;
232                 if (m_transnors)
233                         delete [] m_transnors;
234                 
235                 m_transverts=new float[m_bmesh->totvert][3];
236                 m_transnors=new float[m_bmesh->totvert][3];
237                 m_tvtot = m_bmesh->totvert;
238         }
239 }
240