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