8f9142458883ee599bb2dbcc4acc38ce22d67eac
[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
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 numVerts, numEdges, numFaces;
129         int maxVerts = dm->getNumVerts(dm);
130         int maxEdges = dm->getNumEdges(dm);
131         int maxFaces = dm->getNumFaces(dm);
132
133         result = CDDM_copy(dm);
134         int *recastData = (int*)CustomData_get_layer(&dm->faceData, CD_PROP_INT);
135         CustomData_add_layer_named(&result->faceData, CD_PROP_INT, CD_DUPLICATE, 
136                         recastData, maxFaces, "recastData");
137         recastData = (int*)CustomData_get_layer(&result->faceData, CD_PROP_INT);
138         result->drawFacesTex =  navDM_drawFacesTex;
139         result->drawFacesSolid = navDM_drawFacesSolid;
140         
141         
142         //process mesh
143         int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0; 
144         float* verts=NULL;
145         unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL;
146         int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
147
148         bool res  = buildNavMeshDataByDerivedMesh(dm, vertsPerPoly, nverts, verts, ndtris, dtris,
149                                                                                 npolys, dmeshes, polys, dtrisToPolysMap, dtrisToTrisMap,
150                                                                                 trisToFacesMap);
151         if (res)
152         {
153                 //invalidate concave polygon
154                 for (size_t polyIdx=0; polyIdx<(size_t)npolys; polyIdx++)
155                 {
156                         unsigned short* poly = &polys[polyIdx*2*vertsPerPoly];
157                         if (!polyIsConvex(poly, vertsPerPoly, verts))
158                         {
159                                 //set negative polygon idx to all faces
160                                 unsigned short *dmesh = &dmeshes[4*polyIdx];
161                                 unsigned short tbase = dmesh[2];
162                                 unsigned short tnum = dmesh[3];
163                                 for (unsigned short ti=0; ti<tnum; ti++)
164                                 {
165                                         unsigned short triidx = dtrisToTrisMap[tbase+ti];
166                                         unsigned short faceidx = trisToFacesMap[triidx];
167                                         if (recastData[triidx]>0)
168                                                 recastData[triidx] = -recastData[triidx];
169                                 }                               
170                         }
171                 }
172
173         }
174         else
175         {
176                 printf("Error during creation polygon infos\n");
177         }
178
179         //clean up
180         if (verts!=NULL)
181                 delete verts;
182         if (dtris!=NULL)
183                 delete dtris;
184         if (dmeshes!=NULL)
185                 delete dmeshes;
186         if (polys!=NULL)
187                 delete polys;
188         if (dtrisToPolysMap!=NULL)
189                 delete dtrisToPolysMap;
190         if (dtrisToTrisMap!=NULL)
191                 delete dtrisToTrisMap;  
192         if (trisToFacesMap!=NULL)
193                 delete trisToFacesMap;          
194
195         return result;
196 }
197
198 /*
199 static int isDisabled(ModifierData *md, int useRenderParams)
200 {
201         NavMeshModifierData *amd = (NavMeshModifierData*) md;
202         return false; 
203 }*/
204
205
206
207 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
208                                                                   int useRenderParams, int isFinalCalc)
209 {
210         DerivedMesh *result = NULL;
211         NavMeshModifierData *nmmd = (NavMeshModifierData*) md;
212
213         if (ob->body_type==OB_BODY_TYPE_NAVMESH)
214                 result = createNavMeshForVisualization(nmmd, derivedData);
215         
216         return result;
217 }
218
219
220 ModifierTypeInfo modifierType_NavMesh = {
221         /* name */              "NavMesh",
222         /* structName */        "NavMeshModifierData",
223         /* structSize */        sizeof(NavMeshModifierData),
224         /* type */              eModifierTypeType_Constructive,
225         /* flags */             (ModifierTypeFlag) (eModifierTypeFlag_AcceptsMesh
226                                                         | eModifierTypeFlag_NoUserAdd),
227         /* copyData */          copyData,
228         /* deformVerts */       0,
229         /* deformVertsEM */     0,
230         /* deformMatricesEM */  0,
231         /* applyModifier */     applyModifier,
232         /* applyModifierEM */   0,
233         /* initData */          initData,
234         /* requiredDataMask */  0,
235         /* freeData */          0,
236         /* isDisabled */        0,
237         /* updateDepgraph */    0,
238         /* dependsOnTime */     0,
239         /* foreachObjectLink */ 0,
240         /* foreachIDLink */     0,
241 };
242
243 };