Fix for update_msg.py, wasn’t filtering out prop names identical to prop identifiers...
[blender.git] / source / blender / modifiers / intern / MOD_navmesh.c
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): 
24 *
25 * ***** END GPL LICENSE BLOCK *****
26 *
27 */
28
29 /** \file blender/modifiers/intern/MOD_navmesh.c
30  *  \ingroup modifiers
31  */
32
33
34 #include <math.h>
35
36 #include "DNA_mesh_types.h"
37 #include "DNA_meshdata_types.h"
38
39 #ifdef WITH_GAMEENGINE
40 #  include "recast-capi.h"
41 #  include "BKE_navmesh_conversion.h"
42 #  include "GL/glew.h"
43 #  include "GPU_buffers.h"
44 #  include "GPU_draw.h"
45 #endif
46
47 #include "BLI_math.h"
48 #include "BLI_utildefines.h"
49
50 #include "BKE_cdderivedmesh.h"
51 #include "BKE_mesh.h"
52 #include "BKE_modifier.h"
53 #include "BKE_particle.h"
54 #include "BKE_customdata.h"
55 #include "MEM_guardedalloc.h"
56
57 BM_INLINE int bit(int a, int b)
58 {
59         return (a & (1 << b)) >> b;
60 }
61
62 BM_INLINE void intToCol(int i, float* col)
63 {
64         int     r = bit(i, 0) + bit(i, 3) * 2 + 1;
65         int     g = bit(i, 1) + bit(i, 4) * 2 + 1;
66         int     b = bit(i, 2) + bit(i, 5) * 2 + 1;
67         col[0] = 1 - r*63.0f/255.0f;
68         col[1] = 1 - g*63.0f/255.0f;
69         col[2] = 1 - b*63.0f/255.0f;
70 }
71
72
73 static void initData(ModifierData *UNUSED(md))
74 {
75         /* NavMeshModifierData *nmmd = (NavMeshModifierData*) md; */ /* UNUSED */
76 }
77
78 static void copyData(ModifierData *UNUSED(md), ModifierData *UNUSED(target))
79 {
80         /* NavMeshModifierData *nmmd = (NavMeshModifierData*) md; */
81         /* NavMeshModifierData *tnmmd = (NavMeshModifierData*) target; */
82
83         //.todo - deep copy
84 }
85
86 /*
87 static void (*drawFacesSolid_original)(DerivedMesh *dm, float (*partial_redraw_planes)[4],
88                                            int fast, int (*setMaterial)(int, void *attribs)) = NULL;*/
89
90 #ifdef WITH_GAMEENGINE
91
92 static void drawNavMeshColored(DerivedMesh *dm)
93 {
94         int a, glmode;
95         MVert *mvert = (MVert *)CustomData_get_layer(&dm->vertData, CD_MVERT);
96         MFace *mface = (MFace *)CustomData_get_layer(&dm->faceData, CD_MFACE);
97         int* polygonIdx = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
98         const float BLACK_COLOR[3] = {0.f, 0.f, 0.f};
99         float col[3];
100
101         if (!polygonIdx)
102                 return;
103
104         /*
105         //UI_ThemeColor(TH_WIRE);
106         glDisable(GL_LIGHTING);
107         glLineWidth(2.0);
108         dm->drawEdges(dm, 0, 1);
109         glLineWidth(1.0);
110         glEnable(GL_LIGHTING);*/
111
112         glDisable(GL_LIGHTING);
113         if(GPU_buffer_legacy(dm) ) {
114                 DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" );
115                 //glShadeModel(GL_SMOOTH);
116                 glBegin(glmode = GL_QUADS);
117                 for(a = 0; a < dm->numFaceData; a++, mface++) {
118                         int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
119                         int polygonIdx = *(int*)CustomData_get(&dm->faceData, a, CD_RECAST);
120                         if (polygonIdx<=0)
121                                 memcpy(col, BLACK_COLOR, 3*sizeof(float));
122                         else
123                                 intToCol(polygonIdx, col);
124
125                         if(new_glmode != glmode) {
126                                 glEnd();
127                                 glBegin(glmode = new_glmode);
128                         }
129                         glColor3fv(col);
130                         glVertex3fv(mvert[mface->v1].co);
131                         glVertex3fv(mvert[mface->v2].co);
132                         glVertex3fv(mvert[mface->v3].co);
133                         if(mface->v4) {
134                                 glVertex3fv(mvert[mface->v4].co);
135                         }
136                 }
137                 glEnd();
138         }
139         glEnable(GL_LIGHTING);
140 }
141
142 static void navDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr))
143 {
144         (void) setDrawOptions;
145
146         drawNavMeshColored(dm);
147 }
148
149 static void navDM_drawFacesSolid(DerivedMesh *dm,
150                                                                 float (*partial_redraw_planes)[4],
151                                                                 int UNUSED(fast), int (*setMaterial)(int, void *attribs))
152 {
153         (void) partial_redraw_planes;
154         (void) setMaterial;
155
156         //drawFacesSolid_original(dm, partial_redraw_planes, fast, setMaterial);
157         drawNavMeshColored(dm);
158 }
159 #endif /* WITH_GAMEENGINE */
160
161 static DerivedMesh *createNavMeshForVisualization(NavMeshModifierData *UNUSED(mmd), DerivedMesh *dm)
162 {
163 #ifdef WITH_GAMEENGINE
164         DerivedMesh *result;
165         int maxFaces = dm->getNumFaces(dm);
166         int *recastData;
167         int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0; 
168         float* verts=NULL;
169         unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL;
170         int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
171         int res;
172
173         result = CDDM_copy(dm);
174         if (!CustomData_has_layer(&result->faceData, CD_RECAST)) 
175         {
176                 int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
177                 CustomData_add_layer_named(&result->faceData, CD_RECAST, CD_DUPLICATE, 
178                         sourceRecastData, maxFaces, "recastData");
179         }
180         recastData = (int*)CustomData_get_layer(&result->faceData, CD_RECAST);
181         result->drawFacesTex =  navDM_drawFacesTex;
182         result->drawFacesSolid = navDM_drawFacesSolid;
183         
184         
185         //process mesh
186         res  = buildNavMeshDataByDerivedMesh(dm, &vertsPerPoly, &nverts, &verts, &ndtris, &dtris,
187                                                                                 &npolys, &dmeshes, &polys, &dtrisToPolysMap, &dtrisToTrisMap,
188                                                                                 &trisToFacesMap);
189         if (res)
190         {
191                 size_t polyIdx;
192
193                 //invalidate concave polygon
194                 for (polyIdx=0; polyIdx<(size_t)npolys; polyIdx++)
195                 {
196                         unsigned short* poly = &polys[polyIdx*2*vertsPerPoly];
197                         if (!polyIsConvex(poly, vertsPerPoly, verts))
198                         {
199                                 //set negative polygon idx to all faces
200                                 unsigned short *dmesh = &dmeshes[4*polyIdx];
201                                 unsigned short tbase = dmesh[2];
202                                 unsigned short tnum = dmesh[3];
203                                 unsigned short ti;
204
205                                 for (ti=0; ti<tnum; ti++)
206                                 {
207                                         unsigned short triidx = dtrisToTrisMap[tbase+ti];
208                                         unsigned short faceidx = trisToFacesMap[triidx];
209                                         if (recastData[faceidx]>0)
210                                                 recastData[faceidx] = -recastData[faceidx];
211                                 }                               
212                         }
213                 }
214
215         }
216         else
217         {
218                 printf("Error during creation polygon infos\n");
219         }
220
221         //clean up
222         if (verts!=NULL)
223                 MEM_freeN(verts);
224         if (dtris!=NULL)
225                 MEM_freeN(dtris);
226         if (dmeshes!=NULL)
227                 MEM_freeN(dmeshes);
228         if (polys!=NULL)
229                 MEM_freeN(polys);
230         if (dtrisToPolysMap!=NULL)
231                 MEM_freeN(dtrisToPolysMap);
232         if (dtrisToTrisMap!=NULL)
233                 MEM_freeN(dtrisToTrisMap);
234         if (trisToFacesMap!=NULL)
235                 MEM_freeN(trisToFacesMap);
236
237         return result;
238 #else // WITH_GAMEENGINE
239         return dm;
240 #endif // WITH_GAMEENGINE
241 }
242
243 /*
244 static int isDisabled(ModifierData *md, int useRenderParams)
245 {
246         NavMeshModifierData *amd = (NavMeshModifierData*) md;
247         return false; 
248 }*/
249
250
251
252 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
253                                                                   int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
254 {
255         DerivedMesh *result = NULL;
256         NavMeshModifierData *nmmd = (NavMeshModifierData*) md;
257         int hasRecastData = CustomData_has_layer(&derivedData->faceData, CD_RECAST)>0;
258         if (ob->body_type!=OB_BODY_TYPE_NAVMESH || !hasRecastData )
259         {
260                 //convert to nav mesh object:
261                 //1)set physics type
262                 ob->gameflag &= ~OB_COLLISION;
263                 ob->gameflag |= OB_NAVMESH;
264                 ob->body_type = OB_BODY_TYPE_NAVMESH;
265                 //2)add and init recast data layer
266                 if (!hasRecastData)
267                 {
268                         Mesh* obmesh = (Mesh *)ob->data;
269                         if (obmesh)
270                         {
271                                 int i;
272                                 int numFaces = obmesh->totface;
273                                 int* recastData;
274                                 CustomData_add_layer_named(&obmesh->fdata, CD_RECAST, CD_CALLOC, NULL, numFaces, "recastData");
275                                 recastData = (int*)CustomData_get_layer(&obmesh->fdata, CD_RECAST);
276                                 for (i=0; i<numFaces; i++)
277                                 {
278                                         recastData[i] = i+1;
279                                 }
280                                 CustomData_add_layer_named(&derivedData->faceData, CD_RECAST, CD_REFERENCE, recastData, numFaces, "recastData");
281                         }
282                 }
283         }
284
285         result = createNavMeshForVisualization(nmmd, derivedData);
286         
287         return result;
288 }
289
290
291 ModifierTypeInfo modifierType_NavMesh = {
292         /* name */              "NavMesh",
293         /* structName */        "NavMeshModifierData",
294         /* structSize */        sizeof(NavMeshModifierData),
295         /* type */              eModifierTypeType_Constructive,
296         /* flags */             (ModifierTypeFlag) (eModifierTypeFlag_AcceptsMesh
297                                                         | eModifierTypeFlag_Single),
298         /* copyData */          copyData,
299         /* deformVerts */       0,
300         /* deformMatrices */    0,
301         /* deformVertsEM */     0,
302         /* deformMatricesEM */  0,
303         /* applyModifier */     applyModifier,
304         /* applyModifierEM */   0,
305         /* initData */          initData,
306         /* requiredDataMask */  0,
307         /* freeData */          0,
308         /* isDisabled */        0,
309         /* updateDepgraph */    0,
310         /* dependsOnTime */     0,
311         /* foreachObjectLink */ 0,
312         /* foreachIDLink */     0,
313 };