recastnavigation / recastnavigation

Industry-standard navigation-mesh toolset for games

Home Page:http://recastnav.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trouble getting correct indices from recast result

TheExeQ opened this issue · comments

Im trying to convert the recast result i get to my mesh format. However i can't get it to work right, i looked at the documentation and this it the best i could think of. The verts seem to work but the indices are just completly wrong. Also i noticed that there are plenty of dublicates of the vertices, is it supposed to be like that? and if so why?

		const int nvp = m_pmesh->nvp;
		const float cs = m_pmesh->cs;
		const float ch = m_pmesh->ch;
		const float* orig = m_pmesh->bmin;
		for (int i = 0; i < m_pmesh->npolys; ++i)
		{
			const unsigned short* p = &m_pmesh->polys[i * nvp * 2];

			// Iterate the vertices.
			unsigned short vi[3];  // The vertex indices.
			for (int j = 0; j < nvp; ++j)
			{
				if (p[j] == RC_MESH_NULL_IDX)
					break; // End of vertices.
				if (p[j + nvp] == RC_MESH_NULL_IDX)
				{
					// The edge beginning with this vertex is a solid border.
				}
				else
				{
					// The edge beginning with this vertex connects to 
					// polygon p[j + nvp].
				}

				// Convert to world space.
				const unsigned short* v = &m_pmesh->verts[p[j] * 3];
				const float x = orig[0] + v[0] * cs;
				const float y = orig[1] + v[1] * ch;
				const float z = orig[2] + v[2] * cs;

				// Do something with the vertices.
				vertices.emplace_back(gem::vec3(-x * 100.f, y * 100.f, z * 100.f));
			}

			unsigned int* meshDef = &m_dmesh->meshes[i * 4];
			const unsigned int baseTri = meshDef[2];
			const int ntris = (int)meshDef[3];

			const unsigned char* tris = &m_dmesh->tris[baseTri * 4];

			for (int j = 0; j < ntris; ++j)
			{
				indices.emplace_back(p[tris[j * 4 + 0]]);
				indices.emplace_back(p[tris[j * 4 + 1]]);
				indices.emplace_back(p[tris[j * 4 + 2]]);
			}
		}