Game Development Reference
In-Depth Information
void Triangulate (const vector<Vertex2>& vertexArray,
const map<Vertex2,int>& vertexMap
vector<int>& terrIndices, vector<int>& terrAdjacencies)
{
Delaunay2 triangulation(vertexArray);
vector<int> indices = triangulation.GetIndices();
vector<int> adjacencies = triangulation.GetAdjacencies();
int size = terrIndices.size(), deltaSize = indices.size();
terrIndices.resize(size + deltaSize);
terrAdjacencies.resize(size + deltaSize);
for (int j0 = size, j1 = 0; j1 < deltaSize; ++j0, ++j1) {
int j2 = indices[j1];
terrIndices[j0] = vertexMap[vertexArray[j2]];
j2 = adjacencies[j1];
terrAdjacencies[j0] = (j2 >= 0 ? j2 + size/3 : -1);
}
}
Listing 10.8. The Delaunay triangulation of a tile.
void Stitch (const vector<Vertex2>& verts, vector<int>& inds,
vector<int>& adjs)
{
typedef map<Edge2,pair<int,int>> SharedMap;
SharedMap shared;
int numTriangles = indices.size()/3;
for (int t0 = 0; t0 < numTriangles; ++t0) {
for (int j0 = 0, b0 = 3*t0; j0 < 3; ++j0) {
if (terrAdjacencies[b0 + j0] == -1) {
int i0 = inds[b0 + j0], i1 = inds[b0 + ((j0+1)%3)];
Vertex2 v0 = verts[i0], v1 = verts[i1];
SharedMap::iterator itr = shared.find(Edge2(v0,v1));
if (itr != shared.end()) { // update the adjacencies
itr->second.second = t0; int t1 = itr->second.first;
for (int j1 = 0, b1 = 3*t1; j1 < 3; ++j1) {
int i2 = inds[b1 + j1]; Vertex2 v2 = verts[i2];
if(v2!=v0&&v2!=v1){
j1 = (j1 + 1) % 3;
adjs[b1 + j1] = t0;
adjs[b0 + j0] = t1;
break;
}
}
}
else { // shared edge encountered first time
shared[edge] = make_pair(t0,-1);
}
}
}
}
}
Listing 10.9. Delaunay triangulation of tiles followed by stitching.
Search WWH ::




Custom Search