diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-10-10 01:11:51 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-10-10 01:11:51 +0400 |
commit | 7306eb84f07c92a5bced22f7f38dd7de1770c425 (patch) | |
tree | bbf9ba1bdbeae8ce5be45972314ed32905e3a9a5 /source/blender/blenkernel | |
parent | 9d70e050a27ce4700e0e16dd120c343bd5ecc2a6 (diff) |
move NavMesh draw code out of being a modifier and into DerivedMesh drawing hack (which IMHO is less bad then mis-using a modifier only to override drawing calls).
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_mesh.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/DerivedMesh.c | 186 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh.c | 16 |
4 files changed, 206 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h index 95490b1aff6..052e18a98ea 100644 --- a/source/blender/blenkernel/BKE_mesh.h +++ b/source/blender/blenkernel/BKE_mesh.h @@ -161,6 +161,8 @@ int BKE_mesh_validate_dm(struct DerivedMesh *dm); void BKE_mesh_calc_edges(struct Mesh *mesh, int update); +void BKE_mesh_ensure_navmesh(struct Mesh *me); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt index 91749143008..856810b048a 100644 --- a/source/blender/blenkernel/CMakeLists.txt +++ b/source/blender/blenkernel/CMakeLists.txt @@ -359,6 +359,8 @@ if(WITH_GAMEENGINE) intern/navmesh_conversion.c BKE_navmesh_conversion.h ) + + add_definitions(-DWITH_GAMEENGINE) endif() ## Warnings as errors, this is too strict! diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c index 5e462238f31..9c90ec23d3c 100644 --- a/source/blender/blenkernel/intern/DerivedMesh.c +++ b/source/blender/blenkernel/intern/DerivedMesh.c @@ -62,6 +62,10 @@ #include "BKE_multires.h" #include "BKE_armature.h" +#ifdef WITH_GAMEENGINE +#include "BKE_navmesh_conversion.h" +#endif + #include "BLO_sys_types.h" // for intptr_t support #include "GL/glew.h" @@ -73,6 +77,8 @@ extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */ +static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm); + /////////////////////////////////// /////////////////////////////////// @@ -2105,6 +2111,18 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos add_orco_dm(ob, NULL, *deform_r, NULL, CD_ORCO); } +#ifdef WITH_GAMEENGINE + /* NavMesh - this is a hack but saves having a NavMesh modifier */ + if (ob->body_type == OB_BODY_TYPE_NAVMESH && finaldm->type == DM_TYPE_CDDM) { + DerivedMesh *tdm; + tdm= navmesh_dm_createNavMeshForVisualization(finaldm); + if (finaldm != tdm) { + finaldm->release(finaldm); + finaldm= tdm; + } + } +#endif /* WITH_GAMEENGINE */ + *final_r = finaldm; if(orcodm) @@ -2939,3 +2957,171 @@ void DM_set_object_boundbox(Object *ob, DerivedMesh *dm) boundbox_set_from_min_max(ob->bb, min, max); } + +/* --- NAVMESH (begin) --- */ +#ifdef WITH_GAMEENGINE + +BM_INLINE int navmesh_bit(int a, int b) +{ + return (a & (1 << b)) >> b; +} + +BM_INLINE void navmesh_intToCol(int i, float* col) +{ + int r = navmesh_bit(i, 0) + navmesh_bit(i, 3) * 2 + 1; + int g = navmesh_bit(i, 1) + navmesh_bit(i, 4) * 2 + 1; + int b = navmesh_bit(i, 2) + navmesh_bit(i, 5) * 2 + 1; + col[0] = 1 - r*63.0f/255.0f; + col[1] = 1 - g*63.0f/255.0f; + col[2] = 1 - b*63.0f/255.0f; +} + +static void navmesh_drawColored(DerivedMesh *dm) +{ + int a, glmode; + MVert *mvert = (MVert *)CustomData_get_layer(&dm->vertData, CD_MVERT); + MFace *mface = (MFace *)CustomData_get_layer(&dm->faceData, CD_MFACE); + int* polygonIdx = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST); + const float BLACK_COLOR[3] = {0.f, 0.f, 0.f}; + float col[3]; + + if (!polygonIdx) + return; + + /* + //UI_ThemeColor(TH_WIRE); + glDisable(GL_LIGHTING); + glLineWidth(2.0); + dm->drawEdges(dm, 0, 1); + glLineWidth(1.0); + glEnable(GL_LIGHTING);*/ + + glDisable(GL_LIGHTING); + if(GPU_buffer_legacy(dm) ) { + DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" ); + //glShadeModel(GL_SMOOTH); + glBegin(glmode = GL_QUADS); + for(a = 0; a < dm->numFaceData; a++, mface++) { + int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES; + int polygonIdx = *(int*)CustomData_get(&dm->faceData, a, CD_RECAST); + if (polygonIdx<=0) + memcpy(col, BLACK_COLOR, 3*sizeof(float)); + else + navmesh_intToCol(polygonIdx, col); + + if(new_glmode != glmode) { + glEnd(); + glBegin(glmode = new_glmode); + } + glColor3fv(col); + glVertex3fv(mvert[mface->v1].co); + glVertex3fv(mvert[mface->v2].co); + glVertex3fv(mvert[mface->v3].co); + if(mface->v4) { + glVertex3fv(mvert[mface->v4].co); + } + } + glEnd(); + } + glEnable(GL_LIGHTING); +} + +static void navmesh_DM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr)) +{ + (void) setDrawOptions; + + navmesh_drawColored(dm); +} + +static void navmesh_DM_drawFacesSolid(DerivedMesh *dm, + float (*partial_redraw_planes)[4], + int UNUSED(fast), int (*setMaterial)(int, void *attribs)) +{ + (void) partial_redraw_planes; + (void) setMaterial; + + //drawFacesSolid_original(dm, partial_redraw_planes, fast, setMaterial); + navmesh_drawColored(dm); +} + +static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm) +{ + DerivedMesh *result; + int maxFaces = dm->getNumFaces(dm); + int *recastData; + int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0; + float* verts=NULL; + unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL; + int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL; + int res; + + result = CDDM_copy(dm); + if (!CustomData_has_layer(&result->faceData, CD_RECAST)) + { + int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST); + CustomData_add_layer_named(&result->faceData, CD_RECAST, CD_DUPLICATE, + sourceRecastData, maxFaces, "recastData"); + } + recastData = (int*)CustomData_get_layer(&result->faceData, CD_RECAST); + result->drawFacesTex = navmesh_DM_drawFacesTex; + result->drawFacesSolid = navmesh_DM_drawFacesSolid; + + + //process mesh + res = buildNavMeshDataByDerivedMesh(dm, &vertsPerPoly, &nverts, &verts, &ndtris, &dtris, + &npolys, &dmeshes, &polys, &dtrisToPolysMap, &dtrisToTrisMap, + &trisToFacesMap); + if (res) + { + size_t polyIdx; + + //invalidate concave polygon + for (polyIdx=0; polyIdx<(size_t)npolys; polyIdx++) + { + unsigned short* poly = &polys[polyIdx*2*vertsPerPoly]; + if (!polyIsConvex(poly, vertsPerPoly, verts)) + { + //set negative polygon idx to all faces + unsigned short *dmesh = &dmeshes[4*polyIdx]; + unsigned short tbase = dmesh[2]; + unsigned short tnum = dmesh[3]; + unsigned short ti; + + for (ti=0; ti<tnum; ti++) + { + unsigned short triidx = dtrisToTrisMap[tbase+ti]; + unsigned short faceidx = trisToFacesMap[triidx]; + if (recastData[faceidx]>0) + recastData[faceidx] = -recastData[faceidx]; + } + } + } + + } + else + { + printf("Error during creation polygon infos\n"); + } + + //clean up + if (verts!=NULL) + MEM_freeN(verts); + if (dtris!=NULL) + MEM_freeN(dtris); + if (dmeshes!=NULL) + MEM_freeN(dmeshes); + if (polys!=NULL) + MEM_freeN(polys); + if (dtrisToPolysMap!=NULL) + MEM_freeN(dtrisToPolysMap); + if (dtrisToTrisMap!=NULL) + MEM_freeN(dtrisToTrisMap); + if (trisToFacesMap!=NULL) + MEM_freeN(trisToFacesMap); + + return result; +} + +#endif /* WITH_GAMEENGINE */ + +/* --- NAVMESH (end) --- */ diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c index 810e7c285e8..2c7653b6e1c 100644 --- a/source/blender/blenkernel/intern/mesh.c +++ b/source/blender/blenkernel/intern/mesh.c @@ -1606,3 +1606,19 @@ void mesh_translate(Mesh *me, float offset[3], int do_keys) } } } + + +void BKE_mesh_ensure_navmesh(Mesh *me) +{ + if (!CustomData_has_layer(&me->fdata, CD_RECAST)) { + int i; + int numFaces = me->totface; + int* recastData; + CustomData_add_layer_named(&me->fdata, CD_RECAST, CD_CALLOC, NULL, numFaces, "recastData"); + recastData = (int*)CustomData_get_layer(&me->fdata, CD_RECAST); + for (i=0; i<numFaces; i++) { + recastData[i] = i+1; + } + CustomData_add_layer_named(&me->fdata, CD_RECAST, CD_REFERENCE, recastData, numFaces, "recastData"); + } +} |