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