diff --git a/DebugUtils/Include/DebugDraw.h b/DebugUtils/Include/DebugDraw.h index 8c95416..6bd0b62 100644 --- a/DebugUtils/Include/DebugDraw.h +++ b/DebugUtils/Include/DebugDraw.h @@ -37,6 +37,8 @@ struct duDebugDraw virtual void depthMask(bool state) = 0; + virtual void texture(bool state) = 0; + // Begin drawing primitives. // Params: // prim - (in) primitive type to draw, one of rcDebugDrawPrimitives. @@ -56,6 +58,18 @@ struct duDebugDraw // color - (in) color of the verts. virtual void vertex(const float x, const float y, const float z, unsigned int color) = 0; + // Submit a vertex + // Params: + // pos - (in) position of the verts. + // color - (in) color of the verts. + virtual void vertex(const float* pos, unsigned int color, const float* uv) = 0; + + // Submit a vertex + // Params: + // x,y,z - (in) position of the verts. + // color - (in) color of the verts. + virtual void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) = 0; + // End drawing primitives. virtual void end() = 0; }; diff --git a/DebugUtils/Include/RecastDebugDraw.h b/DebugUtils/Include/RecastDebugDraw.h index 8c81a3e..13126a9 100644 --- a/DebugUtils/Include/RecastDebugDraw.h +++ b/DebugUtils/Include/RecastDebugDraw.h @@ -19,8 +19,8 @@ #ifndef RECAST_DEBUGDRAW_H #define RECAST_DEBUGDRAW_H -void duDebugDrawTriMesh(struct duDebugDraw* dd, const float* verts, int nverts, const int* tris, const float* normals, int ntris, const unsigned char* flags); -void duDebugDrawTriMeshSlope(struct duDebugDraw* dd, const float* verts, int nverts, const int* tris, const float* normals, int ntris, const float walkableSlopeAngle); +void duDebugDrawTriMesh(struct duDebugDraw* dd, const float* verts, int nverts, const int* tris, const float* normals, int ntris, const unsigned char* flags, const float texScale); +void duDebugDrawTriMeshSlope(struct duDebugDraw* dd, const float* verts, int nverts, const int* tris, const float* normals, int ntris, const float walkableSlopeAngle, const float texScale); void duDebugDrawHeightfieldSolid(struct duDebugDraw* dd, const struct rcHeightfield& hf); void duDebugDrawHeightfieldWalkable(struct duDebugDraw* dd, const struct rcHeightfield& hf); @@ -35,6 +35,7 @@ void duDebugDrawHeightfieldLayers(duDebugDraw* dd, const struct rcHeightfieldLay void duDebugDrawHeightfieldLayersRegions(duDebugDraw* dd, const struct rcHeightfieldLayerSet& lset); void duDebugDrawLayerContours(duDebugDraw* dd, const struct rcLayerContourSet& lcset); +void duDebugDrawLayerPolyMesh(duDebugDraw* dd, const struct rcLayerPolyMesh& lmesh); void duDebugDrawRegionConnections(struct duDebugDraw* dd, const struct rcContourSet& cset, const float alpha = 1.0f); diff --git a/DebugUtils/Source/RecastDebugDraw.cpp b/DebugUtils/Source/RecastDebugDraw.cpp index a44069f..23eeb15 100644 --- a/DebugUtils/Source/RecastDebugDraw.cpp +++ b/DebugUtils/Source/RecastDebugDraw.cpp @@ -24,33 +24,62 @@ void duDebugDrawTriMesh(duDebugDraw* dd, const float* verts, int /*nverts*/, const int* tris, const float* normals, int ntris, - const unsigned char* flags) + const unsigned char* flags, const float texScale) { if (!dd) return; if (!verts) return; if (!tris) return; if (!normals) return; + float uva[2]; + float uvb[2]; + float uvc[2]; + + const unsigned int unwalkable = duRGBA(192,128,0,255); + + dd->texture(true); + dd->begin(DU_DRAW_TRIS); for (int i = 0; i < ntris*3; i += 3) { + const float* norm = &normals[i]; unsigned int color; - unsigned char a = (unsigned char)(150*(2+normals[i+0]+normals[i+1])/4); + unsigned char a = (unsigned char)(220*(2+norm[0]+norm[1])/4); if (flags && !flags[i/3]) - color = duRGBA(a,a/4,a/16,255); + color = duLerpCol(duRGBA(a,a,a,255), unwalkable, 64); else color = duRGBA(a,a,a,255); - - dd->vertex(&verts[tris[i+0]*3], color); - dd->vertex(&verts[tris[i+1]*3], color); - dd->vertex(&verts[tris[i+2]*3], color); + + const float* va = &verts[tris[i+0]*3]; + const float* vb = &verts[tris[i+1]*3]; + const float* vc = &verts[tris[i+2]*3]; + + int ax = 0, ay = 0; + if (rcAbs(norm[1]) > rcAbs(norm[ax])) + ax = 1; + if (rcAbs(norm[2]) > rcAbs(norm[ax])) + ax = 2; + ax = (1<vertex(va, color, uva); + dd->vertex(vb, color, uvb); + dd->vertex(vc, color, uvc); } dd->end(); + dd->texture(false); } void duDebugDrawTriMeshSlope(duDebugDraw* dd, const float* verts, int /*nverts*/, const int* tris, const float* normals, int ntris, - const float walkableSlopeAngle) + const float walkableSlopeAngle, const float texScale) { if (!dd) return; if (!verts) return; @@ -58,23 +87,52 @@ void duDebugDrawTriMeshSlope(duDebugDraw* dd, const float* verts, int /*nverts*/ if (!normals) return; const float walkableThr = cosf(walkableSlopeAngle/180.0f*DU_PI); + + float uva[2]; + float uvb[2]; + float uvc[2]; + + dd->texture(true); + const unsigned int unwalkable = duRGBA(192,128,0,255); + dd->begin(DU_DRAW_TRIS); for (int i = 0; i < ntris*3; i += 3) { const float* norm = &normals[i]; unsigned int color; - unsigned char a = (unsigned char)(255*(2+normals[i+0]+normals[i+1])/4); + unsigned char a = (unsigned char)(220*(2+norm[0]+norm[1])/4); if (norm[1] < walkableThr) - color = duRGBA(a,a/4,a/16,255); + color = duLerpCol(duRGBA(a,a,a,255), unwalkable, 64); else color = duRGBA(a,a,a,255); - dd->vertex(&verts[tris[i+0]*3], color); - dd->vertex(&verts[tris[i+1]*3], color); - dd->vertex(&verts[tris[i+2]*3], color); + const float* va = &verts[tris[i+0]*3]; + const float* vb = &verts[tris[i+1]*3]; + const float* vc = &verts[tris[i+2]*3]; + + int ax = 0, ay = 0; + if (rcAbs(norm[1]) > rcAbs(norm[ax])) + ax = 1; + if (rcAbs(norm[2]) > rcAbs(norm[ax])) + ax = 2; + ax = (1<vertex(va, color, uva); + dd->vertex(vb, color, uvb); + dd->vertex(vc, color, uvc); } dd->end(); + + dd->texture(false); } void duDebugDrawHeightfieldSolid(duDebugDraw* dd, const rcHeightfield& hf) @@ -498,7 +556,9 @@ void duDebugDrawLayerContours(duDebugDraw* dd, const struct rcLayerContourSet& l { const rcLayerContour& c = lcset.conts[i]; unsigned int color = 0; - + + color = duIntToCol(i, a); + for (int j = 0; j < c.nverts; ++j) { const int k = (j+1) % c.nverts; @@ -510,7 +570,6 @@ void duDebugDrawLayerContours(duDebugDraw* dd, const struct rcLayerContourSet& l const float bx = orig[0] + vb[0]*cs; const float by = orig[1] + (vb[1]+1+(i&1))*ch; const float bz = orig[2] + vb[2]*cs; - color = duIntToCol(vb[3], a); dd->vertex(ax,ay,az,color); dd->vertex(bx,by,bz,duDarkenCol(color)); } @@ -526,12 +585,10 @@ void duDebugDrawLayerContours(duDebugDraw* dd, const struct rcLayerContourSet& l for (int j = 0; j < c.nverts; ++j) { - const int k = (j+1) % c.nverts; const unsigned char* va = &c.verts[j*4]; - const unsigned char* vb = &c.verts[k*4]; - color = duDarkenCol(duIntToCol(va[3], a)); - if (va[3] != vb[3]) + color = duDarkenCol(duIntToCol(i, a)); + if (va[3]) color = duRGBA(255,255,255,a); float fx = orig[0] + va[0]*cs; @@ -543,6 +600,118 @@ void duDebugDrawLayerContours(duDebugDraw* dd, const struct rcLayerContourSet& l dd->end(); } +void duDebugDrawLayerPolyMesh(duDebugDraw* dd, const struct rcLayerPolyMesh& lmesh) +{ + if (!dd) return; + + const int nvp = lmesh.nvp; + const float cs = lmesh.cs; + const float ch = lmesh.ch; + const float* orig = lmesh.bmin; + + dd->begin(DU_DRAW_TRIS); + + for (int i = 0; i < lmesh.npolys; ++i) + { + const unsigned short* p = &lmesh.polys[i*nvp*2]; + + unsigned int color; + if (lmesh.areas[i] == RC_WALKABLE_AREA) + color = duRGBA(0,192,255,64); + else if (lmesh.areas[i] == RC_NULL_AREA) + color = duRGBA(0,0,0,64); + else + color = duIntToCol(lmesh.areas[i], 255); + + unsigned short vi[3]; + for (int j = 2; j < nvp; ++j) + { + if (p[j] == RC_MESH_NULL_IDX) break; + vi[0] = p[0]; + vi[1] = p[j-1]; + vi[2] = p[j]; + for (int k = 0; k < 3; ++k) + { + const unsigned short* v = &lmesh.verts[vi[k]*3]; + const float x = orig[0] + v[0]*cs; + const float y = orig[1] + (v[1]+1)*ch; + const float z = orig[2] + v[2]*cs; + dd->vertex(x,y,z, color); + } + } + } + dd->end(); + + // Draw neighbours edges + const unsigned int coln = duRGBA(0,48,64,32); + dd->begin(DU_DRAW_LINES, 1.5f); + for (int i = 0; i < lmesh.npolys; ++i) + { + const unsigned short* p = &lmesh.polys[i*nvp*2]; + for (int j = 0; j < nvp; ++j) + { + if (p[j] == RC_MESH_NULL_IDX) break; + if (p[nvp+j] == RC_MESH_NULL_IDX) continue; + int vi[2]; + vi[0] = p[j]; + if (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) + vi[1] = p[0]; + else + vi[1] = p[j+1]; + for (int k = 0; k < 2; ++k) + { + const unsigned short* v = &lmesh.verts[vi[k]*3]; + const float x = orig[0] + v[0]*cs; + const float y = orig[1] + (v[1]+1)*ch + 0.1f; + const float z = orig[2] + v[2]*cs; + dd->vertex(x, y, z, coln); + } + } + } + dd->end(); + + // Draw boundary edges + const unsigned int colb = duRGBA(0,48,64,220); + dd->begin(DU_DRAW_LINES, 2.5f); + for (int i = 0; i < lmesh.npolys; ++i) + { + const unsigned short* p = &lmesh.polys[i*nvp*2]; + for (int j = 0; j < nvp; ++j) + { + if (p[j] == RC_MESH_NULL_IDX) break; + if (p[nvp+j] != RC_MESH_NULL_IDX) continue; + int vi[2]; + vi[0] = p[j]; + if (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) + vi[1] = p[0]; + else + vi[1] = p[j+1]; + for (int k = 0; k < 2; ++k) + { + const unsigned short* v = &lmesh.verts[vi[k]*3]; + const float x = orig[0] + v[0]*cs; + const float y = orig[1] + (v[1]+1)*ch + 0.1f; + const float z = orig[2] + v[2]*cs; + dd->vertex(x, y, z, colb); + } + } + } + dd->end(); + + dd->begin(DU_DRAW_POINTS, 3.0f); + const unsigned int colv = duRGBA(0,0,0,220); + for (int i = 0; i < lmesh.nverts; ++i) + { + const unsigned short* v = &lmesh.verts[i*3]; + const float x = orig[0] + v[0]*cs; + const float y = orig[1] + (v[1]+1)*ch + 0.1f; + const float z = orig[2] + v[2]*cs; + dd->vertex(x,y,z, colv); + } + dd->end(); +} + + static void getContourCenter(const rcContour* cont, const float* orig, float cs, float ch, float* center) { center[0] = 0; diff --git a/Recast/Include/Recast.h b/Recast/Include/Recast.h index 945a5f3..f1952b9 100644 --- a/Recast/Include/Recast.h +++ b/Recast/Include/Recast.h @@ -779,6 +779,28 @@ bool rcBuildLayerContours(rcContext* ctx, rcLayerContourSet& lcset); +struct rcLayerPolyMesh +{ + unsigned short* verts; // Vertices of the mesh, 3 elements per vertex. + unsigned short* polys; // Polygons of the mesh, nvp*2 elements per polygon. + unsigned short* flags; // Per polygon flags. + unsigned char* areas; // Area ID of polygons. + int nverts; // Number of vertices. + int npolys; // Number of polygons. + int maxpolys; // Number of allocated polygons. + int nvp; // Max number of vertices per polygon. + float bmin[3], bmax[3]; // Bounding box of the mesh. + float cs, ch; // Cell size and height. +}; + +rcLayerPolyMesh* rcAllocLayerPolyMesh(); +void rcFreeLayerPolyMesh(rcLayerPolyMesh* lmesh); + +bool rcBuildLayerPolyMesh(rcContext* ctx, + rcLayerContourSet& lcset, + const int maxVertsPerPoly, + rcLayerPolyMesh& lmesh); + // Builds simplified contours from the regions outlines. diff --git a/Recast/Source/Recast.cpp b/Recast/Source/Recast.cpp index 733eaaf..ded1c1c 100644 --- a/Recast/Source/Recast.cpp +++ b/Recast/Source/Recast.cpp @@ -132,6 +132,23 @@ void rcFreeLayerContourSet(rcLayerContourSet* cset) rcFree(cset); } +rcLayerPolyMesh* rcAllocLayerPolyMesh() +{ + rcLayerPolyMesh* lmesh = (rcLayerPolyMesh*)rcAlloc(sizeof(rcLayerPolyMesh), RC_ALLOC_PERM); + memset(lmesh, 0, sizeof(rcLayerPolyMesh)); + return lmesh; +} + +void rcFreeLayerPolyMesh(rcLayerPolyMesh* lmesh) +{ + if (!lmesh) return; + rcFree(lmesh->verts); + rcFree(lmesh->polys); + rcFree(lmesh->flags); + rcFree(lmesh->areas); + rcFree(lmesh); +} + rcContourSet* rcAllocContourSet() diff --git a/Recast/Source/RecastLayers.cpp b/Recast/Source/RecastLayers.cpp index f30a151..ce928ad 100644 --- a/Recast/Source/RecastLayers.cpp +++ b/Recast/Source/RecastLayers.cpp @@ -694,12 +694,10 @@ bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf, return true; } -inline bool isConnected(rcHeightfieldLayer& layer, const int ia, const int ib, const int walkableClimb) -{ - if (layer.areas[ia] != layer.areas[ib]) return false; - if (rcAbs((int)layer.heights[ia] - (int)layer.heights[ib]) > walkableClimb) return false; - return true; -} + + + +// Runtime stuff.... struct rcMonotoneRegion { @@ -709,6 +707,13 @@ struct rcMonotoneRegion unsigned char regId; }; +inline bool isConnected(rcHeightfieldLayer& layer, const int ia, const int ib, const int walkableClimb) +{ + if (layer.areas[ia] != layer.areas[ib]) return false; + if (rcAbs((int)layer.heights[ia] - (int)layer.heights[ib]) > walkableClimb) return false; + return true; +} + static bool canMerge(unsigned char oldRegId, unsigned char newRegId, const rcMonotoneRegion* regs, const int nregs) { int count = 0; @@ -757,13 +762,13 @@ bool rcBuildLayerRegions(rcContext* ctx, rcHeightfieldLayer& layer, const int wa memset(sweeps,0,sizeof(rcLayerSweepSpan)*nsweeps); // Partition walkable area into monotone regions. - int prevCount[256]; + unsigned char prevCount[256]; unsigned char regId = 0; for (int y = 0; y < h; ++y) { if (regId > 0) - memset(prevCount,0,sizeof(int)*regId); + memset(prevCount,0,sizeof(unsigned char)*regId); unsigned char sweepId = 0; for (int x = 0; x < w; ++x) @@ -822,7 +827,7 @@ bool rcBuildLayerRegions(rcContext* ctx, rcHeightfieldLayer& layer, const int wa { // If the neighbour is set and there is only one continuous connection to it, // the sweep will be merged with the previous one, else new region is created. - if (sweeps[i].nei != 0xff && prevCount[sweeps[i].nei] == (int)sweeps[i].ns) + if (sweeps[i].nei != 0xff && (unsigned short)prevCount[sweeps[i].nei] == sweeps[i].ns) { sweeps[i].id = sweeps[i].nei; } @@ -944,22 +949,21 @@ bool rcBuildLayerRegions(rcContext* ctx, rcHeightfieldLayer& layer, const int wa return true; } -static bool allocVert(rcLayerContour& cont, int& cverts) -{ - if (cont.nverts+1 > cverts) - { - cverts = !cverts ? 16 : cverts*2; - unsigned char* nv = (unsigned char*)rcAlloc(cverts*4, RC_ALLOC_TEMP); - if (!nv) return false; - if (cont.nverts) - memcpy(nv, cont.verts, cont.nverts*4); - rcFree(cont.verts); - cont.verts = nv; - } - return true; -} -static bool addVertex(rcLayerContour& cont, int x, int y, int z, int r, int& cverts) + +struct rcTempContour +{ + inline rcTempContour() : verts(0), poly(0) {} + inline ~rcTempContour() { rcFree(verts); rcFree(poly); } + unsigned char* verts; + int nverts; + int cverts; + unsigned short* poly; + int npoly; + int cpoly; +}; + +static bool appendVertex(rcTempContour& cont, const int x, const int y, const int z, const int r) { // Try to merge with existing segments. if (cont.nverts > 1) @@ -973,7 +977,6 @@ static bool addVertex(rcLayerContour& cont, int x, int y, int z, int r, int& cve // The verts are aligned aling x-axis, update z. pb[1] = (unsigned char)y; pb[2] = (unsigned char)z; - pb[3] = (unsigned char)r; return true; } else if (pa[2] == pb[2] && (int)pb[2] == z) @@ -981,14 +984,13 @@ static bool addVertex(rcLayerContour& cont, int x, int y, int z, int r, int& cve // The verts are aligned aling z-axis, update x. pb[0] = (unsigned char)x; pb[1] = (unsigned char)y; - pb[3] = (unsigned char)r; return true; } } } // Add new point. - if (!allocVert(cont, cverts)) + if (cont.nverts+1 > cont.cverts) return false; unsigned char* v = &cont.verts[cont.nverts*4]; @@ -1030,20 +1032,15 @@ static unsigned char getNeighbourReg(rcHeightfieldLayer& layer, return layer.regs[ib]; } -static int getCornerHeight(rcHeightfieldLayer& layer, - const int x, const int y, const int dir, - const int walkableClimb) -{ - return layer.heights[x+y*layer.width]; -} - -static bool walkContour(int x, int y, rcHeightfieldLayer& layer, +static bool walkContour(rcHeightfieldLayer& layer, const unsigned char* cons, - const int walkableClimb, rcLayerContour& cont) + int x, int y, const int walkableClimb, + rcTempContour& cont) { const int w = layer.width; const int h = layer.height; - int cverts = cont.nverts; + + cont.nverts = 0; int startX = x; int startY = y; @@ -1078,7 +1075,6 @@ static bool walkContour(int x, int y, rcHeightfieldLayer& layer, { // Solid edge. int px = x; - int py = getCornerHeight(layer, x, y, dir, walkableClimb); int pz = y; switch(dir) { @@ -1088,7 +1084,7 @@ static bool walkContour(int x, int y, rcHeightfieldLayer& layer, } // Try to merge with previous vertex. - if (!addVertex(cont,px,py,pz,rn,cverts)) + if (!appendVertex(cont, px, (int)layer.heights[x+y*w], pz,rn)) return false; ndir = (dir+1) & 0x3; // Rotate CW @@ -1125,27 +1121,6 @@ static float distancePtSeg(const int x, const int z, const int px, const int pz, const int qx, const int qz) { - /* float pqx = (float)(qx - px); - float pqy = (float)(qy - py); - float pqz = (float)(qz - pz); - float dx = (float)(x - px); - float dy = (float)(y - py); - float dz = (float)(z - pz); - float d = pqx*pqx + pqy*pqy + pqz*pqz; - float t = pqx*dx + pqy*dy + pqz*dz; - if (d > 0) - t /= d; - if (t < 0) - t = 0; - else if (t > 1) - t = 1; - - dx = px + t*pqx - x; - dy = py + t*pqy - y; - dz = pz + t*pqz - z; - - return dx*dx + dy*dy + dz*dz;*/ - float pqx = (float)(qx - px); float pqz = (float)(qz - pz); float dx = (float)(x - px); @@ -1165,12 +1140,9 @@ static float distancePtSeg(const int x, const int z, return dx*dx + dz*dz; } -static bool simplifyContour(rcLayerContour& cont, const float maxError) +static void simplifyContour(rcTempContour& cont, const float maxError) { - int* poly = (int*)rcAlloc(sizeof(int)*cont.nverts, RC_ALLOC_TEMP); - if (!poly) - return false; - int npoly = 0; + cont.npoly = 0; for (int i = 0; i < cont.nverts; ++i) { @@ -1179,9 +1151,9 @@ static bool simplifyContour(rcLayerContour& cont, const float maxError) unsigned char ra = cont.verts[j*4+3]; unsigned char rb = cont.verts[i*4+3]; if (ra != rb) - poly[npoly++] = i; + cont.poly[cont.npoly++] = i; } - if (npoly < 2) + if (cont.npoly < 2) { // If there is no transitions at all, // create some initial points for the simplification process. @@ -1209,24 +1181,24 @@ static bool simplifyContour(rcLayerContour& cont, const float maxError) uri = i; } } - npoly = 0; - poly[npoly++] = lli; - poly[npoly++] = uri; + cont.npoly = 0; + cont.poly[cont.npoly++] = lli; + cont.poly[cont.npoly++] = uri; } // Add points until all raw points are within // error tolerance to the simplified shape. - for (int i = 0; i < npoly; ) + for (int i = 0; i < cont.npoly; ) { - int ii = (i+1) % npoly; + int ii = (i+1) % cont.npoly; - const int ai = poly[i]; - const int ax = cont.verts[ai*4+0]; - const int az = cont.verts[ai*4+2]; + const int ai = (int)cont.poly[i]; + const int ax = (int)cont.verts[ai*4+0]; + const int az = (int)cont.verts[ai*4+2]; - const int bi = poly[ii]; - const int bx = cont.verts[bi*4+0]; - const int bz = cont.verts[bi*4+2]; + const int bi = (int)cont.poly[ii]; + const int bx = (int)cont.verts[bi*4+0]; + const int bz = (int)cont.verts[bi*4+2]; // Find maximum deviation from the segment. float maxd = 0; @@ -1266,10 +1238,10 @@ static bool simplifyContour(rcLayerContour& cont, const float maxError) // add new point, else continue to next segment. if (maxi != -1 && maxd > (maxError*maxError)) { - npoly++; - for (int j = npoly-1; j > i; --j) - poly[j] = poly[j-1]; - poly[i+1] = maxi; + cont.npoly++; + for (int j = cont.npoly-1; j > i; --j) + cont.poly[j] = cont.poly[j-1]; + cont.poly[i+1] = (unsigned short)maxi; } else { @@ -1279,15 +1251,15 @@ static bool simplifyContour(rcLayerContour& cont, const float maxError) // Remap vertices int start = 0; - for (int i = 1; i < npoly; ++i) - if (poly[i] < poly[start]) + for (int i = 1; i < cont.npoly; ++i) + if (cont.poly[i] < cont.poly[start]) start = i; cont.nverts = 0; - for (int i = 0; i < npoly; ++i) + for (int i = 0; i < cont.npoly; ++i) { - const int j = (start+i) % npoly; - unsigned char* src = &cont.verts[poly[j]*4]; + const int j = (start+i) % cont.npoly; + unsigned char* src = &cont.verts[cont.poly[j]*4]; unsigned char* dst = &cont.verts[cont.nverts*4]; dst[0] = src[0]; dst[1] = src[1]; @@ -1295,12 +1267,53 @@ static bool simplifyContour(rcLayerContour& cont, const float maxError) dst[3] = src[3]; cont.nverts++; } - - rcFree(poly); - - return true; } +static int getCornerHeight(rcHeightfieldLayer& layer, const unsigned char* cons, + const int x, const int y, const int z, const int walkableClimb, + bool& shouldRemove) +{ + const int w = layer.width; + const int h = layer.height; + +// unsigned char ic[4]; +// unsigned char ia[4]; + int n = 0; + + unsigned char ic = 0xff; + int ih = 0; + + for (int dz = -1; dz <= 0; ++dz) + { + for (int dx = -1; dx <= 0; ++dx) + { + const int px = x+dx; + const int pz = z+dz; + if (px >= 0 && pz >= 0 && px < w && pz < h) + { + const int idx = px + pz*w; + const int hh = (int)layer.heights[idx]; + if (rcAbs(hh-y) <= walkableClimb) + { + ih = rcMax(ih, hh); + ic &= cons[idx]; + //ia[n] = layer.areas[idx]; + n++; + } + } + } + } + + shouldRemove = false; + if (n > 1 && (ic == 1 || ic == 2 || ic == 4 || ic == 8)) + { + shouldRemove = true; + } + + return ih; +} + + // TODO: move this somewhere else, once the layer meshing is done. bool rcBuildLayerContours(rcContext* ctx, rcHeightfieldLayer& layer, @@ -1335,29 +1348,27 @@ bool rcBuildLayerContours(rcContext* ctx, } memset(cons,0,sizeof(unsigned char)*w*h); - -/* if (portal->dir == 0 || portal->dir == 2) + // Allocate temp buffer for contour tracing. + const int maxTempVerts = (w*h*2)*2; // Twice around the layer. + rcTempContour temp; + temp.nverts = 0; + temp.cverts = maxTempVerts; + temp.npoly = 0; + temp.cpoly = maxTempVerts; + + temp.verts = (unsigned char*)rcAlloc(sizeof(unsigned char)*temp.cverts, RC_ALLOC_TEMP); + if (!cons) { - const int xx = portal->dir == 0 ? (int)portal->pos : (int)portal->pos+1; - const float fx = layer->bmin[0] + xx*cs; - const float fya = layer->bmin[1] + (layer->ymin)*ch; - const float fyb = layer->bmin[1] + (layer->ymin)*ch; - const float fza = layer->bmin[2] + portal->smin*cs; - const float fzb = layer->bmin[2] + portal->smax*cs; - dd->vertex(fx, fya+h, fza, pcol); - dd->vertex(fx, fyb+h, fzb, pcol); + ctx->log(RC_LOG_ERROR, "rcBuildLayerContours: Out of memory 'temp.verts' (%d).", temp.cverts); + return false; } - else if (portal->dir == 3 || portal->dir == 1) + + temp.poly = (unsigned short*)rcAlloc(sizeof(unsigned short)*temp.cpoly, RC_ALLOC_TEMP); + if (!cons) { - const int yy = portal->dir == 3 ? (int)portal->pos : (int)portal->pos+1; - const float fxa = layer->bmin[0] + portal->smin*cs; - const float fxb = layer->bmin[0] + portal->smax*cs; - const float fya = layer->bmin[1] + (layer->ymin)*ch; - const float fyb = layer->bmin[1] + (layer->ymin)*ch; - const float fz = layer->bmin[2] + yy*cs; - dd->vertex(fxa, fya+h, fz, pcol); - dd->vertex(fxb, fyb+h, fz, pcol); - }*/ + ctx->log(RC_LOG_ERROR, "rcBuildLayerContours: Out of memory 'temp.poly' (%d).", temp.cpoly); + return false; + } // Paint portals for (int i = 0; i < layer.nportals; ++i) @@ -1376,22 +1387,7 @@ bool rcBuildLayerContours(rcContext* ctx, cons[j + (int)portal->pos*w] |= mask; } } - -/* printf("cons:\n"); - for (int y = 0; y < h; ++y) - { - for (int x = 0; x < w; ++x) - { - unsigned char c = cons[x+y*w]; - if (c == 0) - printf("."); - else - printf("%x",c); - } - printf("\n"); - }*/ - - + // Find contours. for (int y = 0; y < h; ++y) { @@ -1410,18 +1406,35 @@ bool rcBuildLayerContours(rcContext* ctx, cont.reg = ri; cont.area = layer.areas[idx]; - if (!walkContour(x, y, layer, cons, walkableClimb, cont)) + if (!walkContour(layer, cons, x, y, walkableClimb, temp)) { - ctx->log(RC_LOG_ERROR, "rcBuildLayerContours: Failed to walk contour."); + ctx->log(RC_LOG_ERROR, "rcBuildLayerContours: Failed to walk contour (nverts=%d cverts=%d).", temp.nverts, temp.cverts); return false; } - if (!simplifyContour(cont, maxError)) - { - ctx->log(RC_LOG_ERROR, "rcBuildLayerContours: Failed to simplify contour."); - return false; - } + simplifyContour(temp, maxError); + // Store contour. + cont.nverts = temp.nverts; + if (cont.nverts > 0) + { + cont.verts = (unsigned char*)rcAlloc(sizeof(unsigned char)*4*temp.nverts, RC_ALLOC_PERM); + if (!cont.verts) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerContours: Out of memory 'cont.verts' (%d).", temp.nverts); + return false; + } + + for (int i = 0; i < temp.nverts; ++i) + { + bool shouldRemove; + unsigned char* v = &temp.verts[i*4]; + v[1] = getCornerHeight(layer, cons, (int)v[0], (int)v[1], (int)v[2], walkableClimb, shouldRemove); + v[3] = shouldRemove ? 1 : 0; + } + + memcpy(cont.verts, temp.verts, sizeof(unsigned char)*4*temp.nverts); + } } } @@ -1429,3 +1442,1089 @@ bool rcBuildLayerContours(rcContext* ctx, } + + + + + +struct rcEdge +{ + unsigned short vert[2]; + unsigned short polyEdge[2]; + unsigned short poly[2]; +}; + +static bool buildMeshAdjacency(unsigned short* polys, const int npolys, + const int nverts, const int vertsPerPoly) +{ + // Based on code by Eric Lengyel from: + // http://www.terathon.com/code/edges.php + + int maxEdgeCount = npolys*vertsPerPoly; + unsigned short* firstEdge = (unsigned short*)rcAlloc(sizeof(unsigned short)*(nverts + maxEdgeCount), RC_ALLOC_TEMP); + if (!firstEdge) + return false; + unsigned short* nextEdge = firstEdge + nverts; + int edgeCount = 0; + + rcEdge* edges = (rcEdge*)rcAlloc(sizeof(rcEdge)*maxEdgeCount, RC_ALLOC_TEMP); + if (!edges) + { + rcFree(firstEdge); + return false; + } + + for (int i = 0; i < nverts; i++) + firstEdge[i] = RC_MESH_NULL_IDX; + + for (int i = 0; i < npolys; ++i) + { + unsigned short* t = &polys[i*vertsPerPoly*2]; + for (int j = 0; j < vertsPerPoly; ++j) + { + unsigned short v0 = t[j]; + unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RC_MESH_NULL_IDX) ? t[0] : t[j+1]; + if (v0 < v1) + { + rcEdge& edge = edges[edgeCount]; + edge.vert[0] = v0; + edge.vert[1] = v1; + edge.poly[0] = (unsigned short)i; + edge.polyEdge[0] = (unsigned short)j; + edge.poly[1] = (unsigned short)i; + edge.polyEdge[1] = 0; + // Insert edge + nextEdge[edgeCount] = firstEdge[v0]; + firstEdge[v0] = (unsigned short)edgeCount; + edgeCount++; + } + } + } + + for (int i = 0; i < npolys; ++i) + { + unsigned short* t = &polys[i*vertsPerPoly*2]; + for (int j = 0; j < vertsPerPoly; ++j) + { + unsigned short v0 = t[j]; + unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RC_MESH_NULL_IDX) ? t[0] : t[j+1]; + if (v0 > v1) + { + for (unsigned short e = firstEdge[v1]; e != RC_MESH_NULL_IDX; e = nextEdge[e]) + { + rcEdge& edge = edges[e]; + if (edge.vert[1] == v0 && edge.poly[0] == edge.poly[1]) + { + edge.poly[1] = (unsigned short)i; + edge.polyEdge[1] = (unsigned short)j; + break; + } + } + } + } + } + + // Store adjacency + for (int i = 0; i < edgeCount; ++i) + { + const rcEdge& e = edges[i]; + if (e.poly[0] != e.poly[1]) + { + unsigned short* p0 = &polys[e.poly[0]*vertsPerPoly*2]; + unsigned short* p1 = &polys[e.poly[1]*vertsPerPoly*2]; + p0[vertsPerPoly + e.polyEdge[0]] = e.poly[1]; + p1[vertsPerPoly + e.polyEdge[1]] = e.poly[0]; + } + } + + rcFree(firstEdge); + rcFree(edges); + + return true; +} + + +static const int VERTEX_BUCKET_COUNT2 = (1<<8); + +inline int computeVertexHash2(int x, int y, int z) +{ + const unsigned int h1 = 0x8da6b343; // Large multiplicative constants; + const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes + const unsigned int h3 = 0xcb1ab31f; + unsigned int n = h1 * x + h2 * y + h3 * z; + return (int)(n & (VERTEX_BUCKET_COUNT2-1)); +} + +static unsigned short addVertex(unsigned short x, unsigned short y, unsigned short z, + unsigned short* verts, unsigned short* firstVert, unsigned short* nextVert, int& nv) +{ + int bucket = computeVertexHash2(x, 0, z); + unsigned short i = firstVert[bucket]; + + while (i != RC_MESH_NULL_IDX) + { + const unsigned short* v = &verts[i*3]; + if (v[0] == x && (rcAbs(v[1] - y) <= 2) && v[2] == z) + return i; + i = nextVert[i]; // next + } + + // Could not find, create new. + i = nv; nv++; + unsigned short* v = &verts[i*3]; + v[0] = x; + v[1] = y; + v[2] = z; + nextVert[i] = firstVert[bucket]; + firstVert[bucket] = i; + + return (unsigned short)i; +} + +inline int prev(int i, int n) { return i-1 >= 0 ? i-1 : n-1; } +inline int next(int i, int n) { return i+1 < n ? i+1 : 0; } + +inline int area2(const unsigned char* a, const unsigned char* b, const unsigned char* c) +{ + return ((int)b[0] - (int)a[0]) * ((int)c[2] - (int)a[2]) - ((int)c[0] - (int)a[0]) * ((int)b[2] - (int)a[2]); +} + +// Exclusive or: true iff exactly one argument is true. +// The arguments are negated to ensure that they are 0/1 +// values. Then the bitwise Xor operator may apply. +// (This idea is due to Michael Baldwin.) +inline bool xorb(bool x, bool y) +{ + return !x ^ !y; +} + +// Returns true iff c is strictly to the left of the directed +// line through a to b. +inline bool left(const unsigned char* a, const unsigned char* b, const unsigned char* c) +{ + return area2(a, b, c) < 0; +} + +inline bool leftOn(const unsigned char* a, const unsigned char* b, const unsigned char* c) +{ + return area2(a, b, c) <= 0; +} + +inline bool collinear(const unsigned char* a, const unsigned char* b, const unsigned char* c) +{ + return area2(a, b, c) == 0; +} + +// Returns true iff ab properly intersects cd: they share +// a point interior to both segments. The properness of the +// intersection is ensured by using strict leftness. +static bool intersectProp(const unsigned char* a, const unsigned char* b, + const unsigned char* c, const unsigned char* d) +{ + // Eliminate improper cases. + if (collinear(a,b,c) || collinear(a,b,d) || + collinear(c,d,a) || collinear(c,d,b)) + return false; + + return xorb(left(a,b,c), left(a,b,d)) && xorb(left(c,d,a), left(c,d,b)); +} + +// Returns T iff (a,b,c) are collinear and point c lies +// on the closed segement ab. +static bool between(const unsigned char* a, const unsigned char* b, const unsigned char* c) +{ + if (!collinear(a, b, c)) + return false; + // If ab not vertical, check betweenness on x; else on y. + if (a[0] != b[0]) + return ((a[0] <= c[0]) && (c[0] <= b[0])) || ((a[0] >= c[0]) && (c[0] >= b[0])); + else + return ((a[2] <= c[2]) && (c[2] <= b[2])) || ((a[2] >= c[2]) && (c[2] >= b[2])); +} + +// Returns true iff segments ab and cd intersect, properly or improperly. +static bool intersect(const unsigned char* a, const unsigned char* b, + const unsigned char* c, const unsigned char* d) +{ + if (intersectProp(a, b, c, d)) + return true; + else if (between(a, b, c) || between(a, b, d) || + between(c, d, a) || between(c, d, b)) + return true; + else + return false; +} + +static bool vequal(const unsigned char* a, const unsigned char* b) +{ + return a[0] == b[0] && a[2] == b[2]; +} + +// Returns T iff (v_i, v_j) is a proper internal *or* external +// diagonal of P, *ignoring edges incident to v_i and v_j*. +static bool diagonalie(int i, int j, int n, const unsigned char* verts, const unsigned short* indices) +{ + const unsigned char* d0 = &verts[(indices[i] & 0x7fff) * 4]; + const unsigned char* d1 = &verts[(indices[j] & 0x7fff) * 4]; + + // For each edge (k,k+1) of P + for (int k = 0; k < n; k++) + { + int k1 = next(k, n); + // Skip edges incident to i or j + if (!((k == i) || (k1 == i) || (k == j) || (k1 == j))) + { + const unsigned char* p0 = &verts[(indices[k] & 0x7fff) * 4]; + const unsigned char* p1 = &verts[(indices[k1] & 0x7fff) * 4]; + + if (vequal(d0, p0) || vequal(d1, p0) || vequal(d0, p1) || vequal(d1, p1)) + continue; + + if (intersect(d0, d1, p0, p1)) + return false; + } + } + return true; +} + +// Returns true iff the diagonal (i,j) is strictly internal to the +// polygon P in the neighborhood of the i endpoint. +static bool inCone(int i, int j, int n, const unsigned char* verts, const unsigned short* indices) +{ + const unsigned char* pi = &verts[(indices[i] & 0x7fff) * 4]; + const unsigned char* pj = &verts[(indices[j] & 0x7fff) * 4]; + const unsigned char* pi1 = &verts[(indices[next(i, n)] & 0x7fff) * 4]; + const unsigned char* pin1 = &verts[(indices[prev(i, n)] & 0x7fff) * 4]; + + // If P[i] is a convex vertex [ i+1 left or on (i-1,i) ]. + if (leftOn(pin1, pi, pi1)) + return left(pi, pj, pin1) && left(pj, pi, pi1); + // Assume (i-1,i,i+1) not collinear. + // else P[i] is reflex. + return !(leftOn(pi, pj, pi1) && leftOn(pj, pi, pin1)); +} + +// Returns T iff (v_i, v_j) is a proper internal +// diagonal of P. +static bool diagonal(int i, int j, int n, const unsigned char* verts, const unsigned short* indices) +{ + return inCone(i, j, n, verts, indices) && diagonalie(i, j, n, verts, indices); +} + +static int triangulate(int n, const unsigned char* verts, unsigned short* indices, unsigned short* tris) +{ + int ntris = 0; + unsigned short* dst = tris; + + // The last bit of the index is used to indicate if the vertex can be removed. + for (int i = 0; i < n; i++) + { + int i1 = next(i, n); + int i2 = next(i1, n); + if (diagonal(i, i2, n, verts, indices)) + indices[i1] |= 0x8000; + } + + while (n > 3) + { + int minLen = -1; + int mini = -1; + for (int i = 0; i < n; i++) + { + int i1 = next(i, n); + if (indices[i1] & 0x8000) + { + const unsigned char* p0 = &verts[(indices[i] & 0x7fff) * 4]; + const unsigned char* p2 = &verts[(indices[next(i1, n)] & 0x7fff) * 4]; + + const int dx = (int)p2[0] - (int)p0[0]; + const int dz = (int)p2[2] - (int)p0[2]; + const int len = dx*dx + dz*dz; + if (minLen < 0 || len < minLen) + { + minLen = len; + mini = i; + } + } + } + + if (mini == -1) + { + // Should not happen. + /* printf("mini == -1 ntris=%d n=%d\n", ntris, n); + for (int i = 0; i < n; i++) + { + printf("%d ", indices[i] & 0x0fffffff); + } + printf("\n");*/ + return -ntris; + } + + int i = mini; + int i1 = next(i, n); + int i2 = next(i1, n); + + *dst++ = indices[i] & 0x7fff; + *dst++ = indices[i1] & 0x7fff; + *dst++ = indices[i2] & 0x7fff; + ntris++; + + // Removes P[i1] by copying P[i+1]...P[n-1] left one index. + n--; + for (int k = i1; k < n; k++) + indices[k] = indices[k+1]; + + if (i1 >= n) i1 = 0; + i = prev(i1,n); + // Update diagonal flags. + if (diagonal(prev(i, n), i1, n, verts, indices)) + indices[i] |= 0x8000; + else + indices[i] &= 0x7fff; + + if (diagonal(i, next(i1, n), n, verts, indices)) + indices[i1] |= 0x8000; + else + indices[i1] &= 0x7fff; + } + + // Append the remaining triangle. + *dst++ = indices[0] & 0x7fff; + *dst++ = indices[1] & 0x7fff; + *dst++ = indices[2] & 0x7fff; + ntris++; + + return ntris; +} + +static const int MAX_VERTS_PER_POLY = 6; +static const int MAX_REM_EDGES = 48; + + +static int countPolyVerts(const unsigned short* p, const int nvp) +{ + for (int i = 0; i < nvp; ++i) + if (p[i] == RC_MESH_NULL_IDX) + return i; + return nvp; +} + +inline bool uleft(const unsigned short* a, const unsigned short* b, const unsigned short* c) +{ + return ((int)b[0] - (int)a[0]) * ((int)c[2] - (int)a[2]) - + ((int)c[0] - (int)a[0]) * ((int)b[2] - (int)a[2]) < 0; +} + +static int getPolyMergeValue(unsigned short* pa, unsigned short* pb, + const unsigned short* verts, int& ea, int& eb, + const int nvp) +{ + const int na = countPolyVerts(pa, nvp); + const int nb = countPolyVerts(pb, nvp); + + // If the merged polygon would be too big, do not merge. + if (na+nb-2 > nvp) + return -1; + + // Check if the polygons share an edge. + ea = -1; + eb = -1; + + for (int i = 0; i < na; ++i) + { + unsigned short va0 = pa[i]; + unsigned short va1 = pa[(i+1) % na]; + if (va0 > va1) + rcSwap(va0, va1); + for (int j = 0; j < nb; ++j) + { + unsigned short vb0 = pb[j]; + unsigned short vb1 = pb[(j+1) % nb]; + if (vb0 > vb1) + rcSwap(vb0, vb1); + if (va0 == vb0 && va1 == vb1) + { + ea = i; + eb = j; + break; + } + } + } + + // No common edge, cannot merge. + if (ea == -1 || eb == -1) + return -1; + + // Check to see if the merged polygon would be convex. + unsigned short va, vb, vc; + + va = pa[(ea+na-1) % na]; + vb = pa[ea]; + vc = pb[(eb+2) % nb]; + if (!uleft(&verts[va*3], &verts[vb*3], &verts[vc*3])) + return -1; + + va = pb[(eb+nb-1) % nb]; + vb = pb[eb]; + vc = pa[(ea+2) % na]; + if (!uleft(&verts[va*3], &verts[vb*3], &verts[vc*3])) + return -1; + + va = pa[ea]; + vb = pa[(ea+1)%na]; + + int dx = (int)verts[va*3+0] - (int)verts[vb*3+0]; + int dy = (int)verts[va*3+2] - (int)verts[vb*3+2]; + + return dx*dx + dy*dy; +} + +static void mergePolys(unsigned short* pa, unsigned short* pb, int ea, int eb, const int nvp) +{ + unsigned short tmp[MAX_VERTS_PER_POLY*2]; + + const int na = countPolyVerts(pa, nvp); + const int nb = countPolyVerts(pb, nvp); + + // Merge polygons. + memset(tmp, 0xff, sizeof(unsigned short)*nvp); + int n = 0; + // Add pa + for (int i = 0; i < na-1; ++i) + tmp[n++] = pa[(ea+1+i) % na]; + // Add pb + for (int i = 0; i < nb-1; ++i) + tmp[n++] = pb[(eb+1+i) % nb]; + + memcpy(pa, tmp, sizeof(unsigned short)*nvp); +} + + +static void pushFront(unsigned short v, unsigned short* arr, int& an) +{ + an++; + for (int i = an-1; i > 0; --i) + arr[i] = arr[i-1]; + arr[0] = v; +} + +static void pushBack(unsigned short v, unsigned short* arr, int& an) +{ + arr[an] = v; + an++; +} + +static bool canRemoveVertex(rcContext* ctx, rcLayerPolyMesh& mesh, const unsigned short rem) +{ + const int nvp = mesh.nvp; + + // Count number of polygons to remove. + int numRemovedVerts = 0; + int numTouchedVerts = 0; + int numRemainingEdges = 0; + for (int i = 0; i < mesh.npolys; ++i) + { + unsigned short* p = &mesh.polys[i*nvp*2]; + const int nv = countPolyVerts(p, nvp); + int numRemoved = 0; + int numVerts = 0; + for (int j = 0; j < nv; ++j) + { + if (p[j] == rem) + { + numTouchedVerts++; + numRemoved++; + } + numVerts++; + } + if (numRemoved) + { + numRemovedVerts += numRemoved; + numRemainingEdges += numVerts-(numRemoved+1); + } + } + + // There would be too few edges remaining to create a polygon. + // This can happen for example when a tip of a triangle is marked + // as deletion, but there are no other polys that share the vertex. + // In this case, the vertex should not be removed. + if (numRemainingEdges <= 2) + return false; + + // Check that there is enough memory for the test. + const int maxEdges = numTouchedVerts*2; + if (maxEdges > MAX_REM_EDGES) + return false; + + // Find edges which share the removed vertex. + unsigned short edges[MAX_REM_EDGES]; + int nedges = 0; + + for (int i = 0; i < mesh.npolys; ++i) + { + unsigned short* p = &mesh.polys[i*nvp*2]; + const int nv = countPolyVerts(p, nvp); + + // Collect edges which touches the removed vertex. + for (int j = 0, k = nv-1; j < nv; k = j++) + { + if (p[j] == rem || p[k] == rem) + { + // Arrange edge so that a=rem. + int a = p[j], b = p[k]; + if (b == rem) + rcSwap(a,b); + + // Check if the edge exists + bool exists = false; + for (int k = 0; k < nedges; ++k) + { + unsigned short* e = &edges[k*3]; + if (e[1] == b) + { + // Exists, increment vertex share count. + e[2]++; + exists = true; + } + } + // Add new edge. + if (!exists) + { + unsigned short* e = &edges[nedges*3]; + e[0] = a; + e[1] = b; + e[2] = 1; + nedges++; + } + } + } + } + + // There should be no more than 2 open edges. + // This catches the case that two non-adjacent polygons + // share the removed vertex. In that case, do not remove the vertex. + int numOpenEdges = 0; + for (int i = 0; i < nedges; ++i) + { + if (edges[i*3+2] < 2) + numOpenEdges++; + } + if (numOpenEdges > 2) + return false; + + return true; +} + +static bool removeVertex(rcContext* ctx, rcLayerPolyMesh& mesh, const unsigned short rem, const int maxTris) +{ + const int nvp = mesh.nvp; + + // Count number of polygons to remove. + int numRemovedVerts = 0; + for (int i = 0; i < mesh.npolys; ++i) + { + unsigned short* p = &mesh.polys[i*nvp*2]; + const int nv = countPolyVerts(p, nvp); + for (int j = 0; j < nv; ++j) + { + if (p[j] == rem) + numRemovedVerts++; + } + } + + int nedges = 0; + unsigned short edges[MAX_REM_EDGES*3]; + int nhole = 0; + unsigned short hole[MAX_REM_EDGES]; + int nharea = 0; + unsigned short harea[MAX_REM_EDGES]; + + for (int i = 0; i < mesh.npolys; ++i) + { + unsigned short* p = &mesh.polys[i*nvp*2]; + const int nv = countPolyVerts(p, nvp); + bool hasRem = false; + for (int j = 0; j < nv; ++j) + if (p[j] == rem) hasRem = true; + if (hasRem) + { + // Collect edges which does not touch the removed vertex. + for (int j = 0, k = nv-1; j < nv; k = j++) + { + if (p[j] != rem && p[k] != rem) + { + if (nedges >= MAX_REM_EDGES) + return false; + unsigned short* e = &edges[nedges*3]; + e[0] = p[k]; + e[1] = p[j]; + e[2] = mesh.areas[i]; + nedges++; + } + } + // Remove the polygon. + unsigned short* p2 = &mesh.polys[(mesh.npolys-1)*nvp*2]; + memcpy(p,p2,sizeof(unsigned short)*nvp); + memset(p+nvp,0xff,sizeof(unsigned short)*nvp); + mesh.areas[i] = mesh.areas[mesh.npolys-1]; + mesh.npolys--; + --i; + } + } + + // Remove vertex. + for (int i = (int)rem; i < mesh.nverts; ++i) + { + mesh.verts[i*3+0] = mesh.verts[(i+1)*3+0]; + mesh.verts[i*3+1] = mesh.verts[(i+1)*3+1]; + mesh.verts[i*3+2] = mesh.verts[(i+1)*3+2]; + } + mesh.nverts--; + + // Adjust indices to match the removed vertex layout. + for (int i = 0; i < mesh.npolys; ++i) + { + unsigned short* p = &mesh.polys[i*nvp*2]; + const int nv = countPolyVerts(p, nvp); + for (int j = 0; j < nv; ++j) + if (p[j] > rem) p[j]--; + } + for (int i = 0; i < nedges; ++i) + { + if (edges[i*3+0] > rem) edges[i*3+0]--; + if (edges[i*3+1] > rem) edges[i*3+1]--; + } + + if (nedges == 0) + return true; + + // Start with one vertex, keep appending connected + // segments to the start and end of the hole. + pushBack(edges[0], hole, nhole); + pushBack(edges[2], harea, nharea); + + while (nedges) + { + bool match = false; + + for (int i = 0; i < nedges; ++i) + { + const unsigned short ea = edges[i*3+0]; + const unsigned short eb = edges[i*3+1]; + const unsigned short a = edges[i*3+2]; + bool add = false; + if (hole[0] == eb) + { + // The segment matches the beginning of the hole boundary. + if (nhole >= MAX_REM_EDGES) + return false; + pushFront(ea, hole, nhole); + pushFront(a, harea, nharea); + add = true; + } + else if (hole[nhole-1] == ea) + { + // The segment matches the end of the hole boundary. + if (nhole >= MAX_REM_EDGES) + return false; + pushBack(eb, hole, nhole); + pushBack(a, harea, nharea); + add = true; + } + if (add) + { + // The edge segment was added, remove it. + edges[i*3+0] = edges[(nedges-1)*3+0]; + edges[i*3+1] = edges[(nedges-1)*3+1]; + edges[i*3+2] = edges[(nedges-1)*3+2]; + --nedges; + match = true; + --i; + } + } + + if (!match) + break; + } + + + unsigned short tris[MAX_REM_EDGES*3]; + unsigned char tverts[MAX_REM_EDGES*3]; + unsigned short tpoly[MAX_REM_EDGES*3]; + + // Generate temp vertex array for triangulation. + for (int i = 0; i < nhole; ++i) + { + const unsigned short pi = hole[i]; + tverts[i*4+0] = (unsigned char)mesh.verts[pi*3+0]; + tverts[i*4+1] = (unsigned char)mesh.verts[pi*3+1]; + tverts[i*4+2] = (unsigned char)mesh.verts[pi*3+2]; + tverts[i*4+3] = 0; + tpoly[i] = (unsigned short)i; + } + + // Triangulate the hole. + int ntris = triangulate(nhole, tverts, tpoly, tris); + if (ntris < 0) + { + ntris = -ntris; + ctx->log(RC_LOG_WARNING, "removeVertex: triangulate() returned bad results."); + } + + if (ntris > MAX_REM_EDGES) + return false; + + unsigned short polys[MAX_REM_EDGES*MAX_VERTS_PER_POLY]; + unsigned char pareas[MAX_REM_EDGES]; + + // Build initial polygons. + int npolys = 0; + memset(polys, 0xff, ntris*nvp*sizeof(unsigned short)); + for (int j = 0; j < ntris; ++j) + { + unsigned short* t = &tris[j*3]; + if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2]) + { + polys[npolys*nvp+0] = hole[t[0]]; + polys[npolys*nvp+1] = hole[t[1]]; + polys[npolys*nvp+2] = hole[t[2]]; + pareas[npolys] = (unsigned char)harea[t[0]]; + npolys++; + } + } + if (!npolys) + return true; + + // Merge polygons. + if (nvp > 3) + { + for (;;) + { + // Find best polygons to merge. + int bestMergeVal = 0; + int bestPa = 0, bestPb = 0, bestEa = 0, bestEb = 0; + + for (int j = 0; j < npolys-1; ++j) + { + unsigned short* pj = &polys[j*nvp]; + for (int k = j+1; k < npolys; ++k) + { + unsigned short* pk = &polys[k*nvp]; + int ea, eb; + int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb, nvp); + if (v > bestMergeVal) + { + bestMergeVal = v; + bestPa = j; + bestPb = k; + bestEa = ea; + bestEb = eb; + } + } + } + + if (bestMergeVal > 0) + { + // Found best, merge. + unsigned short* pa = &polys[bestPa*nvp]; + unsigned short* pb = &polys[bestPb*nvp]; + mergePolys(pa, pb, bestEa, bestEb, nvp); + memcpy(pb, &polys[(npolys-1)*nvp], sizeof(unsigned short)*nvp); + pareas[bestPb] = pareas[npolys-1]; + npolys--; + } + else + { + // Could not merge any polygons, stop. + break; + } + } + } + + // Store polygons. + for (int i = 0; i < npolys; ++i) + { + if (mesh.npolys >= maxTris) break; + unsigned short* p = &mesh.polys[mesh.npolys*nvp*2]; + memset(p,0xff,sizeof(unsigned short)*nvp*2); + for (int j = 0; j < nvp; ++j) + p[j] = polys[i*nvp+j]; + mesh.areas[mesh.npolys] = pareas[i]; + mesh.npolys++; + if (mesh.npolys > maxTris) + { + ctx->log(RC_LOG_ERROR, "removeVertex: Too many polygons %d (max:%d).", mesh.npolys, maxTris); + return false; + } + } + + return true; +} + + + + + +bool rcBuildLayerPolyMesh(rcContext* ctx, + rcLayerContourSet& lcset, + const int maxVertsPerPoly, + rcLayerPolyMesh& mesh) +{ + rcAssert(ctx); + + const int nvp = rcMin(maxVertsPerPoly, MAX_VERTS_PER_POLY); + +// ctx->startTimer(RC_TIMER_BUILD_POLYMESH); + + rcVcopy(mesh.bmin, lcset.bmin); + rcVcopy(mesh.bmax, lcset.bmax); + mesh.cs = lcset.cs; + mesh.ch = lcset.ch; + + int maxVertices = 0; + int maxTris = 0; + int maxVertsPerCont = 0; + for (int i = 0; i < lcset.nconts; ++i) + { + // Skip null contours. + if (lcset.conts[i].nverts < 3) continue; + maxVertices += lcset.conts[i].nverts; + maxTris += lcset.conts[i].nverts - 2; + maxVertsPerCont = rcMax(maxVertsPerCont, lcset.conts[i].nverts); + } + + if (maxVertices >= 0xfffe) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Too many vertices %d.", maxVertices); + return false; + } + + rcScopedDelete vflags = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxVertices, RC_ALLOC_TEMP); + if (!vflags) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'vflags' (%d).", maxVertices); + return false; + } + memset(vflags, 0, maxVertices); + + mesh.verts = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertices*3, RC_ALLOC_PERM); + if (!mesh.verts) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'mesh.verts' (%d).", maxVertices); + return false; + } + mesh.polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxTris*nvp*2*2, RC_ALLOC_PERM); + if (!mesh.polys) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'mesh.polys' (%d).", maxTris*nvp*2); + return false; + } + mesh.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxTris, RC_ALLOC_PERM); + if (!mesh.areas) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'mesh.areas' (%d).", maxTris); + return false; + } + mesh.flags = (unsigned short*)rcAlloc(sizeof(unsigned short)*mesh.npolys, RC_ALLOC_PERM); + if (!mesh.flags) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'mesh.flags' (%d).", mesh.npolys); + return false; + } + // Just allocate and clean the mesh flags array. The user is resposible for filling it. + memset(mesh.flags, 0, sizeof(unsigned short) * mesh.npolys); + + + mesh.nverts = 0; + mesh.npolys = 0; + mesh.nvp = nvp; + mesh.maxpolys = maxTris; + + memset(mesh.verts, 0, sizeof(unsigned short)*maxVertices*3); + memset(mesh.polys, 0xff, sizeof(unsigned short)*maxTris*nvp*2); + memset(mesh.areas, 0, sizeof(unsigned char)*maxTris); + + unsigned short firstVert[VERTEX_BUCKET_COUNT2]; + for (int i = 0; i < VERTEX_BUCKET_COUNT2; ++i) + firstVert[i] = RC_MESH_NULL_IDX; + + rcScopedDelete nextVert = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertices, RC_ALLOC_TEMP); + if (!nextVert) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'nextVert' (%d).", maxVertices); + return false; + } + memset(nextVert, 0, sizeof(unsigned short)*maxVertices); + + rcScopedDelete indices = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertsPerCont, RC_ALLOC_TEMP); + if (!indices) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'indices' (%d).", maxVertsPerCont); + return false; + } + rcScopedDelete tris = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertsPerCont*3, RC_ALLOC_TEMP); + if (!tris) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'tris' (%d).", maxVertsPerCont*3); + return false; + } + rcScopedDelete polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertsPerCont*nvp, RC_ALLOC_TEMP); + if (!polys) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Out of memory 'polys' (%d).", maxVertsPerCont*nvp); + return false; + } + + for (int i = 0; i < lcset.nconts; ++i) + { + rcLayerContour& cont = lcset.conts[i]; + + // Skip null contours. + if (cont.nverts < 3) + continue; + + // Triangulate contour + for (int j = 0; j < cont.nverts; ++j) + indices[j] = (unsigned short)j; + + int ntris = triangulate(cont.nverts, cont.verts, &indices[0], &tris[0]); + if (ntris <= 0) + { + ctx->log(RC_LOG_WARNING, "rcBuildLayerPolyMesh: Bad triangulation Contour %d.", i); + ntris = -ntris; + } + + // Add and merge vertices. + for (int j = 0; j < cont.nverts; ++j) + { + const unsigned char* v = &cont.verts[j*4]; + indices[j] = addVertex((unsigned short)v[0], (unsigned short)v[1], (unsigned short)v[2], + mesh.verts, firstVert, nextVert, mesh.nverts); + if (v[3]) + { + // This vertex should be removed. + vflags[indices[j]] = 1; + } + } + + // Build initial polygons. + int npolys = 0; + memset(polys, 0xff, sizeof(unsigned short) * maxVertsPerCont * nvp); + for (int j = 0; j < ntris; ++j) + { + const unsigned short* t = &tris[j*3]; + if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2]) + { + polys[npolys*nvp+0] = indices[t[0]]; + polys[npolys*nvp+1] = indices[t[1]]; + polys[npolys*nvp+2] = indices[t[2]]; + npolys++; + } + } + if (!npolys) + continue; + + // Merge polygons. + if (nvp > 3) + { + for(;;) + { + // Find best polygons to merge. + int bestMergeVal = 0; + int bestPa = 0, bestPb = 0, bestEa = 0, bestEb = 0; + + for (int j = 0; j < npolys-1; ++j) + { + unsigned short* pj = &polys[j*nvp]; + for (int k = j+1; k < npolys; ++k) + { + unsigned short* pk = &polys[k*nvp]; + int ea, eb; + int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb, nvp); + if (v > bestMergeVal) + { + bestMergeVal = v; + bestPa = j; + bestPb = k; + bestEa = ea; + bestEb = eb; + } + } + } + + if (bestMergeVal > 0) + { + // Found best, merge. + unsigned short* pa = &polys[bestPa*nvp]; + unsigned short* pb = &polys[bestPb*nvp]; + mergePolys(pa, pb, bestEa, bestEb, nvp); + memcpy(pb, &polys[(npolys-1)*nvp], sizeof(unsigned short)*nvp); + npolys--; + } + else + { + // Could not merge any polygons, stop. + break; + } + } + } + + // Store polygons. + for (int j = 0; j < npolys; ++j) + { + unsigned short* p = &mesh.polys[mesh.npolys*nvp*2]; + unsigned short* q = &polys[j*nvp]; + for (int k = 0; k < nvp; ++k) + p[k] = q[k]; + mesh.areas[mesh.npolys] = cont.area; + mesh.npolys++; + if (mesh.npolys > maxTris) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Too many polygons %d (max:%d).", mesh.npolys, maxTris); + return false; + } + } + } + + + // Remove edge vertices. + for (int i = 0; i < mesh.nverts; ++i) + { + if (vflags[i]) + { + if (!canRemoveVertex(ctx, mesh, (unsigned short)i)) + continue; + if (!removeVertex(ctx, mesh, (unsigned short)i, maxTris)) + { + // Failed to remove vertex + ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Failed to remove edge vertex %d.", i); + return false; + } + // Remove vertex + // Note: mesh.nverts is already decremented inside removeVertex()! + for (int j = i; j < mesh.nverts; ++j) + vflags[j] = vflags[j+1]; + --i; + } + } + + // Calculate adjacency. + if (!buildMeshAdjacency(mesh.polys, mesh.npolys, mesh.nverts, nvp)) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: Adjacency failed."); + return false; + } + + if (mesh.nverts > 0xffff) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: The resulting mesh has too many vertices %d (max %d). Data can be corrupted.", mesh.nverts, 0xffff); + } + if (mesh.npolys > 0xffff) + { + ctx->log(RC_LOG_ERROR, "rcBuildLayerPolyMesh: The resulting mesh has too many polygons %d (max %d). Data can be corrupted.", mesh.npolys, 0xffff); + } + +// ctx->stopTimer(RC_TIMER_BUILD_POLYMESH); + + return true; +} diff --git a/Recast/Source/RecastMesh.cpp b/Recast/Source/RecastMesh.cpp index 4b33c10..e5319c0 100644 --- a/Recast/Source/RecastMesh.cpp +++ b/Recast/Source/RecastMesh.cpp @@ -195,7 +195,7 @@ inline bool collinear(const int* a, const int* b, const int* c) // Returns true iff ab properly intersects cd: they share // a point interior to both segments. The properness of the // intersection is ensured by using strict leftness. -bool intersectProp(const int* a, const int* b, const int* c, const int* d) +static bool intersectProp(const int* a, const int* b, const int* c, const int* d) { // Eliminate improper cases. if (collinear(a,b,c) || collinear(a,b,d) || @@ -470,6 +470,7 @@ static void mergePolys(unsigned short* pa, unsigned short* pb, int ea, int eb, memcpy(pa, tmp, sizeof(unsigned short)*nvp); } + static void pushFront(int v, int* arr, int& an) { an++; @@ -1165,11 +1166,11 @@ bool rcBuildPolyMesh(rcContext* ctx, rcContourSet& cset, int nvp, rcPolyMesh& me if (mesh.nverts > 0xffff) { - ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: The resulting mesh has too many vertices %d (max %d). Data can be corrupted.", mesh.nverts, 0xffff); + ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: The resulting mesh has too many vertices %d (max %d). Data can be corrupted.", mesh.nverts, 0xffff); } if (mesh.npolys > 0xffff) { - ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: The resulting mesh has too many polygons %d (max %d). Data can be corrupted.", mesh.npolys, 0xffff); + ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: The resulting mesh has too many polygons %d (max %d). Data can be corrupted.", mesh.npolys, 0xffff); } ctx->stopTimer(RC_TIMER_BUILD_POLYMESH); diff --git a/RecastDemo/Include/SampleInterfaces.h b/RecastDemo/Include/SampleInterfaces.h index bacbb20..a0ea6ef 100644 --- a/RecastDemo/Include/SampleInterfaces.h +++ b/RecastDemo/Include/SampleInterfaces.h @@ -65,9 +65,12 @@ class DebugDrawGL : public duDebugDraw { public: virtual void depthMask(bool state); + virtual void texture(bool state); virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f); virtual void vertex(const float* pos, unsigned int color); virtual void vertex(const float x, const float y, const float z, unsigned int color); + virtual void vertex(const float* pos, unsigned int color, const float* uv); + virtual void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v); virtual void end(); }; diff --git a/RecastDemo/Include/Sample_TileMesh.h b/RecastDemo/Include/Sample_TileMesh.h index 027cb84..71b56c5 100644 --- a/RecastDemo/Include/Sample_TileMesh.h +++ b/RecastDemo/Include/Sample_TileMesh.h @@ -44,6 +44,8 @@ protected: rcHeightfieldLayerSet* m_lset; rcLayerContourSet* m_lcsets[MAX_LAYERS]; int m_nlcsets; + rcLayerPolyMesh* m_lmeshes[MAX_LAYERS]; + int m_nlmeshes; enum DrawMode { @@ -68,6 +70,7 @@ protected: DRAWMODE_HEIGHFIELD_LAYERS, DRAWMODE_LAYER_CONTOURS, + DRAWMODE_LAYER_MESHES, MAX_DRAWMODE }; diff --git a/RecastDemo/Source/Sample.cpp b/RecastDemo/Source/Sample.cpp index 50d745b..2794701 100644 --- a/RecastDemo/Source/Sample.cpp +++ b/RecastDemo/Source/Sample.cpp @@ -82,7 +82,7 @@ void Sample::handleRender() // Draw mesh duDebugDrawTriMesh(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), - m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0); + m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0, 1.0f); // Draw bounds const float* bmin = m_geom->getMeshBoundsMin(); const float* bmax = m_geom->getMeshBoundsMax(); diff --git a/RecastDemo/Source/SampleInterfaces.cpp b/RecastDemo/Source/SampleInterfaces.cpp index d3d96d6..af7f6da 100644 --- a/RecastDemo/Source/SampleInterfaces.cpp +++ b/RecastDemo/Source/SampleInterfaces.cpp @@ -139,11 +139,64 @@ const char* BuildContext::getLogText(const int i) const //////////////////////////////////////////////////////////////////////////////////////////////////// +class GLCheckerTexture +{ + unsigned int m_texId; +public: + GLCheckerTexture() : m_texId(0) + { + } + + ~GLCheckerTexture() + { + if (m_texId != 0) + glDeleteTextures(1, &m_texId); + } + void bind() + { + if (m_texId == 0) + { + // Create checker pattern. + const unsigned int col0 = duRGBA(215,215,215,255); + const unsigned int col1 = duRGBA(255,255,255,255); + static const int TSIZE = 32; + unsigned int data[TSIZE*TSIZE]; + for (int y = 0; y < TSIZE; ++y) + for (int x = 0; x < TSIZE; ++x) + data[x+y*TSIZE] = (x==0 || y==0) ? col0 : col1; + glGenTextures(1, &m_texId); + glBindTexture(GL_TEXTURE_2D, m_texId); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TSIZE,TSIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + } + else + { + glBindTexture(GL_TEXTURE_2D, m_texId); + } + } +}; +GLCheckerTexture g_tex; + + void DebugDrawGL::depthMask(bool state) { glDepthMask(state ? GL_TRUE : GL_FALSE); } +void DebugDrawGL::texture(bool state) +{ + if (state) + { + glEnable(GL_TEXTURE_2D); + g_tex.bind(); + } + else + { + glDisable(GL_TEXTURE_2D); + } +} + void DebugDrawGL::begin(duDebugDrawPrimitives prim, float size) { switch (prim) @@ -177,6 +230,20 @@ void DebugDrawGL::vertex(const float x, const float y, const float z, unsigned i glVertex3f(x,y,z); } +void DebugDrawGL::vertex(const float* pos, unsigned int color, const float* uv) +{ + glColor4ubv((GLubyte*)&color); + glTexCoord2fv(uv); + glVertex3fv(pos); +} + +void DebugDrawGL::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) +{ + glColor4ubv((GLubyte*)&color); + glTexCoord2f(u,v); + glVertex3f(x,y,z); +} + void DebugDrawGL::end() { glEnd(); diff --git a/RecastDemo/Source/Sample_SoloMeshSimple.cpp b/RecastDemo/Source/Sample_SoloMeshSimple.cpp index 2a376be..eb192ae 100644 --- a/RecastDemo/Source/Sample_SoloMeshSimple.cpp +++ b/RecastDemo/Source/Sample_SoloMeshSimple.cpp @@ -215,19 +215,14 @@ void Sample_SoloMeshSimple::handleRender() glEnable(GL_FOG); glDepthMask(GL_TRUE); - if (m_drawMode == DRAWMODE_MESH) + const float texScale = 1.0f / (m_cellSize * 10.0f); + + if (m_drawMode != DRAWMODE_NAVMESH_TRANS) { // Draw mesh duDebugDrawTriMeshSlope(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), - m_agentMaxSlope); - m_geom->drawOffMeshConnections(&dd); - } - else if (m_drawMode != DRAWMODE_NAVMESH_TRANS) - { - // Draw mesh - duDebugDrawTriMesh(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), - m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0); + m_agentMaxSlope, texScale); m_geom->drawOffMeshConnections(&dd); } diff --git a/RecastDemo/Source/Sample_SoloMeshTiled.cpp b/RecastDemo/Source/Sample_SoloMeshTiled.cpp index 773f8b4..51a7024 100644 --- a/RecastDemo/Source/Sample_SoloMeshTiled.cpp +++ b/RecastDemo/Source/Sample_SoloMeshTiled.cpp @@ -347,19 +347,14 @@ void Sample_SoloMeshTiled::handleRender() glEnable(GL_FOG); glDepthMask(GL_TRUE); - if (m_drawMode == DRAWMODE_MESH) + const float texScale = 1.0f / (m_cellSize * 10.0f); + + if (m_drawMode != DRAWMODE_NAVMESH_TRANS) { // Draw mesh duDebugDrawTriMeshSlope(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), - m_agentMaxSlope); - m_geom->drawOffMeshConnections(&dd); - } - else if (m_drawMode != DRAWMODE_NAVMESH_TRANS) - { - // Draw mesh - duDebugDrawTriMesh(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), - m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0); + m_agentMaxSlope, texScale); m_geom->drawOffMeshConnections(&dd); } diff --git a/RecastDemo/Source/Sample_TempObstacles.cpp b/RecastDemo/Source/Sample_TempObstacles.cpp index 332ad34..f5d0804 100644 --- a/RecastDemo/Source/Sample_TempObstacles.cpp +++ b/RecastDemo/Source/Sample_TempObstacles.cpp @@ -1460,20 +1460,23 @@ void Sample_TempObstacles::handleRender() DebugDrawGL dd; + const float texScale = 1.0f / (m_cellSize * 10.0f); + // Draw mesh if (m_drawMode == DRAWMODE_MESH) { // Draw mesh duDebugDrawTriMeshSlope(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), - m_agentMaxSlope); + m_agentMaxSlope, texScale); m_geom->drawOffMeshConnections(&dd); } else if (m_drawMode != DRAWMODE_NAVMESH_TRANS) { // Draw mesh duDebugDrawTriMesh(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), - m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0); + m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0, + texScale); m_geom->drawOffMeshConnections(&dd); } diff --git a/RecastDemo/Source/Sample_TileMesh.cpp b/RecastDemo/Source/Sample_TileMesh.cpp index 3c6dba0..a7e1f79 100644 --- a/RecastDemo/Source/Sample_TileMesh.cpp +++ b/RecastDemo/Source/Sample_TileMesh.cpp @@ -183,6 +183,7 @@ Sample_TileMesh::Sample_TileMesh() : m_dmesh(0), m_lset(0), m_nlcsets(0), + m_nlmeshes(0), m_drawMode(DRAWMODE_NAVMESH), m_maxTiles(0), m_maxPolysPerTile(0), @@ -197,6 +198,7 @@ Sample_TileMesh::Sample_TileMesh() : memset(m_tileBmax, 0, sizeof(m_tileBmax)); memset(m_lcsets, 0, sizeof(m_lcsets)); + memset(m_lmeshes, 0, sizeof(m_lmeshes)); setTool(new NavMeshTileTool); } @@ -225,12 +227,20 @@ void Sample_TileMesh::cleanup() rcFreeHeightfieldLayerSet(m_lset); m_lset = 0; + for (int i = 0; i < MAX_LAYERS; ++i) { rcFreeLayerContourSet(m_lcsets[i]); m_lcsets[i] = 0; } m_nlcsets = 0; + + for (int i = 0; i < MAX_LAYERS; ++i) + { + rcFreeLayerPolyMesh(m_lmeshes[i]); + m_lmeshes[i] = 0; + } + m_nlmeshes = 0; } @@ -481,6 +491,7 @@ void Sample_TileMesh::handleDebugMode() valid[DRAWMODE_POLYMESH_DETAIL] = m_dmesh != 0; valid[DRAWMODE_HEIGHFIELD_LAYERS] = m_lset != 0; valid[DRAWMODE_LAYER_CONTOURS] = m_nlcsets != 0; + valid[DRAWMODE_LAYER_MESHES] = m_nlmeshes != 0; } int unavail = 0; @@ -528,10 +539,14 @@ void Sample_TileMesh::handleDebugMode() if (imguiCheck("Poly Mesh Detail", m_drawMode == DRAWMODE_POLYMESH_DETAIL, valid[DRAWMODE_POLYMESH_DETAIL])) m_drawMode = DRAWMODE_POLYMESH_DETAIL; + imguiSeparatorLine(); + if (imguiCheck("Heighfield Layers", m_drawMode == DRAWMODE_HEIGHFIELD_LAYERS, valid[DRAWMODE_HEIGHFIELD_LAYERS])) m_drawMode = DRAWMODE_HEIGHFIELD_LAYERS; if (imguiCheck("Layer Contours", m_drawMode == DRAWMODE_LAYER_CONTOURS, valid[DRAWMODE_LAYER_CONTOURS])) m_drawMode = DRAWMODE_LAYER_CONTOURS; + if (imguiCheck("Layer Meshes", m_drawMode == DRAWMODE_LAYER_MESHES, valid[DRAWMODE_LAYER_MESHES])) + m_drawMode = DRAWMODE_LAYER_MESHES; if (unavail) { @@ -548,23 +563,20 @@ void Sample_TileMesh::handleRender() DebugDrawGL dd; + const float texScale = 1.0f / (m_cellSize * 10.0f); + // Draw mesh - if (m_drawMode == DRAWMODE_MESH) + if (m_drawMode != DRAWMODE_NAVMESH_TRANS) { // Draw mesh duDebugDrawTriMeshSlope(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), - m_agentMaxSlope); - m_geom->drawOffMeshConnections(&dd); - } - else if (m_drawMode != DRAWMODE_NAVMESH_TRANS) - { - // Draw mesh - duDebugDrawTriMesh(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), - m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0); + m_agentMaxSlope, texScale); m_geom->drawOffMeshConnections(&dd); } + + /* duDebugDrawTriMesh(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(), m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0); m_geom->drawOffMeshConnections(&dd);*/ @@ -690,6 +702,14 @@ void Sample_TileMesh::handleRender() duDebugDrawLayerContours(&dd, *m_lcsets[i]); glDepthMask(GL_TRUE); } + + if (m_nlmeshes && m_drawMode == DRAWMODE_LAYER_MESHES) + { + glDepthMask(GL_FALSE); + for (int i = 0; i < m_nlmeshes; ++i) + duDebugDrawLayerPolyMesh(&dd, *m_lmeshes[i]); + glDepthMask(GL_TRUE); + } m_geom->drawConvexVolumes(&dd); @@ -1137,6 +1157,7 @@ unsigned char* Sample_TileMesh::buildTileMesh(const int tx, const int ty, const } m_nlcsets = 0; + m_nlmeshes = 0; for (int i = 0; i < m_lset->nlayers; ++i) { rcBuildLayerRegions(m_ctx, m_lset->layers[i], m_cfg.walkableClimb); @@ -1146,7 +1167,16 @@ unsigned char* Sample_TileMesh::buildTileMesh(const int tx, const int ty, const m_cfg.walkableClimb, m_cfg.maxSimplificationError, *m_lcsets[m_nlcsets])) break; + + m_lmeshes[m_nlmeshes] = rcAllocLayerPolyMesh(); + if (!rcBuildLayerPolyMesh(m_ctx, *m_lcsets[m_nlcsets], + m_cfg.maxVertsPerPoly, + *m_lmeshes[m_nlmeshes])) + break; + m_nlcsets++; + m_nlmeshes++; + } } diff --git a/RecastDemo/Source/main.cpp b/RecastDemo/Source/main.cpp index f008a8a..6efcb5a 100644 --- a/RecastDemo/Source/main.cpp +++ b/RecastDemo/Source/main.cpp @@ -162,10 +162,10 @@ int main(int /*argc*/, char** /*argv*/) glEnable(GL_CULL_FACE); - float fogCol[4] = { 0.32f,0.25f,0.25f,1 }; + float fogCol[4] = { 0.32f, 0.31f, 0.30f, 1.0f }; glEnable(GL_FOG); glFogi(GL_FOG_MODE, GL_LINEAR); - glFogf(GL_FOG_START, camr*0.2f); + glFogf(GL_FOG_START, camr*0.1f); glFogf(GL_FOG_END, camr*1.25f); glFogfv(GL_FOG_COLOR, fogCol); @@ -667,7 +667,7 @@ int main(int /*argc*/, char** /*argv*/) } rx = 45; ry = -45; - glFogf(GL_FOG_START, camr*0.2f); + glFogf(GL_FOG_START, camr*0.1f); glFogf(GL_FOG_END, camr*1.25f); } @@ -743,7 +743,7 @@ int main(int /*argc*/, char** /*argv*/) } rx = 45; ry = -45; - glFogf(GL_FOG_START, camr*0.2f); + glFogf(GL_FOG_START, camr*0.1f); glFogf(GL_FOG_END, camr*1.25f); } } @@ -857,7 +857,7 @@ int main(int /*argc*/, char** /*argv*/) } rx = 45; ry = -45; - glFogf(GL_FOG_START, camr*0.2f); + glFogf(GL_FOG_START, camr*0.1f); glFogf(GL_FOG_END, camr*1.25f); }