Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DetourTileNavMeshBuilder.cpp « Source « Detour « recastnavigation « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95dd34b04f6348d56752ba3c73a603a5caac3d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Copyright (c) 2009 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would be
//    appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
//    misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DetourTileNavMesh.h"
#include "DetourCommon.h"

bool dtCreateNavMeshTileData(const unsigned short* verts, const int nverts,
							 const unsigned short* polys, const int npolys, const int nvp,
							 const unsigned short* dmeshes, const float* dverts, const int ndverts,
							 const unsigned char* dtris, const int ndtris, 
							 const float* bmin, const float* bmax, float cs, float ch, int tileSize, int walkableClimb,
							 unsigned char** outData, int* outDataSize)
{
	if (nvp != DT_TILE_VERTS_PER_POLYGON)
		return false;
	if (nverts >= 0xffff)
		return false;
	
	if (!nverts)
		return false;
	if (!npolys)
		return false;
	if (!dmeshes || !dverts || ! dtris)
		return false;
	
	// Find portal edges which are at tile borders.
	int nedges = 0;
	int nportals = 0;
	for (int i = 0; i < npolys; ++i)
	{
		const unsigned short* p = &polys[i*2*nvp];
		for (int j = 0; j < nvp; ++j)
		{
			if (p[j] == 0xffff) break;
			int nj = j+1;
			if (nj >= nvp || p[nj] == 0xffff) nj = 0;
			const unsigned short* va = &verts[p[j]*3];
			const unsigned short* vb = &verts[p[nj]*3];
			
			nedges++;
			
			if (va[0] == tileSize && vb[0] == tileSize)
				nportals++; // x+
			else if (va[2] == tileSize && vb[2]  == tileSize)
				nportals++; // z+
			else if (va[0] == 0 && vb[0] == 0)
				nportals++; // x-
			else if (va[2] == 0 && vb[2] == 0)
				nportals++; // z-
		}
	}

	const int maxLinks = nedges + nportals*2;
	
	
	// Find unique detail vertices.
	int uniqueDetailVerts = 0;
	if (dmeshes)
	{
		for (int i = 0; i < npolys; ++i)
		{
			const unsigned short* p = &polys[i*nvp*2];
			int ndv = dmeshes[i*4+1];
			int nv = 0;
			for (int j = 0; j < nvp; ++j)
			{
				if (p[j] == 0xffff) break;
				nv++;
			}
			ndv -= nv;
			uniqueDetailVerts += ndv;
		}
	}
	
	// Calculate data size
	const int headerSize = sizeof(dtTileHeader);
	const int vertsSize = sizeof(float)*3*nverts;
	const int polysSize = sizeof(dtTilePoly)*npolys;
	const int linksSize = sizeof(dtTileLink)*maxLinks;
	const int detailMeshesSize = sizeof(dtTilePolyDetail)*npolys;
	const int detailVertsSize = sizeof(float)*3*uniqueDetailVerts;
	const int detailTrisSize = sizeof(unsigned char)*4*ndtris;
	
	const int dataSize = headerSize + vertsSize + polysSize + linksSize +
						 detailMeshesSize + detailVertsSize + detailTrisSize;
	unsigned char* data = new unsigned char[dataSize];
	if (!data)
		return false;
	memset(data, 0, dataSize);
	
	unsigned char* d = data;
	dtTileHeader* header = (dtTileHeader*)d; d += headerSize;
	float* navVerts = (float*)d; d += vertsSize;
	dtTilePoly* navPolys = (dtTilePoly*)d; d += polysSize;
	d += linksSize;
	dtTilePolyDetail* navDMeshes = (dtTilePolyDetail*)d; d += detailMeshesSize;
	float* navDVerts = (float*)d; d += detailVertsSize;
	unsigned char* navDTris = (unsigned char*)d; d += detailTrisSize;
	
	
	// Store header
	header->magic = DT_TILE_NAVMESH_MAGIC;
	header->version = DT_TILE_NAVMESH_VERSION;
	header->npolys = npolys;
	header->nverts = nverts;
	header->maxlinks = maxLinks;
	header->bmin[0] = bmin[0];
	header->bmin[1] = bmin[1];
	header->bmin[2] = bmin[2];
	header->bmax[0] = bmax[0];
	header->bmax[1] = bmax[1];
	header->bmax[2] = bmax[2];
	header->ndmeshes = npolys;
	header->ndverts = uniqueDetailVerts;
	header->ndtris = ndtris;
	
	// Store vertices
	for (int i = 0; i < nverts; ++i)
	{
		const unsigned short* iv = &verts[i*3];
		float* v = &navVerts[i*3];
		v[0] = bmin[0] + iv[0] * cs;
		v[1] = bmin[1] + iv[1] * ch;
		v[2] = bmin[2] + iv[2] * cs;
	}
	
	// Store polygons
	const unsigned short* src = polys;
	for (int i = 0; i < npolys; ++i)
	{
		dtTilePoly* p = &navPolys[i];
		p->nv = 0;
		for (int j = 0; j < nvp; ++j)
		{
			if (src[j] == 0xffff) break;
			p->v[j] = src[j];
			p->n[j] = (src[nvp+j]+1) & 0xffff;
			p->nv++;
		}
		src += nvp*2;
	}

	// Store portal edges.
	for (int i = 0; i < npolys; ++i)
	{
		dtTilePoly* poly = &navPolys[i];
		for (int j = 0; j < poly->nv; ++j)
		{
			int nj = j+1;
			if (nj >= poly->nv) nj = 0;

			const unsigned short* va = &verts[poly->v[j]*3];
			const unsigned short* vb = &verts[poly->v[nj]*3];
						
			if (va[0] == tileSize && vb[0] == tileSize) // x+
				poly->n[j] = 0x8000 | 0;
			else if (va[2] == tileSize && vb[2]  == tileSize) // z+
				poly->n[j] = 0x8000 | 1;
			else if (va[0] == 0 && vb[0] == 0) // x-
				poly->n[j] = 0x8000 | 2;
			else if (va[2] == 0 && vb[2] == 0) // z-
				poly->n[j] = 0x8000 | 3;
		}
	}

	// Store detail meshes and vertices.
	// The nav polygon vertices are stored as the first vertices on each mesh.
	// We compress the mesh data by skipping them and using the navmesh coordinates.
	unsigned short vbase = 0;
	for (int i = 0; i < npolys; ++i)
	{
		dtTilePolyDetail& dtl = navDMeshes[i];
		const int vb = dmeshes[i*4+0];
		const int ndv = dmeshes[i*4+1];
		const int nv = navPolys[i].nv;
		dtl.vbase = vbase;
		dtl.nverts = ndv-nv;
		dtl.tbase = dmeshes[i*4+2];
		dtl.ntris = dmeshes[i*4+3];
		// Copy vertices except the first 'nv' verts which are equal to nav poly verts.
		if (ndv-nv)
		{
			memcpy(&navDVerts[vbase*3], &dverts[(vb+nv)*3], sizeof(float)*3*(ndv-nv));
			vbase += ndv-nv;
		}
	}
	// Store triangles.
	memcpy(navDTris, dtris, sizeof(unsigned char)*4*ndtris);
	
	*outData = data;
	*outDataSize = dataSize;
	
	return true;
}