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