/// <summary> /// Constructor to set up mesh /// </summary> public TerrainMesh(GraphicsDevice graphicsDevice, TerrainPatch patch, int level) { terrainPatch = patch; mipLevel = level; int scale = 1; for (int i = mipLevel; i > 0; i--) { scale *= 2; } meshSize = (short)(TerrainPatch.patchSize / scale + 1); // Create vertex buffer for this mipmap vertices = new VertexPositionNormal[meshSize * meshSize]; patchVertexBuffer = new DynamicVertexBuffer(graphicsDevice, VertexPositionNormal.vertexDeclaration, vertices.Length, BufferUsage.WriteOnly); }
/* * public void BuildMeshData(GraphicsDevice graphicsDevice) * { * // Iterate backwards because we need to grab neighboring edges for normals * for (int i = (int)(gridSize.X * gridSize.Y) - 1; i >= 0; i--) * { * int x = i % (int)gridSize.X; * int y = i / (int)gridSize.Y; * * Vector2 offset = new Vector2(x, y); * TerrainPatch currentPatch = new TerrainPatch(graphicsDevice, offset); * * // find south and east neighbors * if (y < gridSize.Y - 1) currentPatch.neighbors[1] = terrainPatches[y + 1, x]; * if (x < gridSize.X - 1) currentPatch.neighbors[3] = terrainPatches[y, x + 1]; * * currentPatch.UpdateMap(heightData, scale, heightScale, mapPosition, indices); * * terrainPatches[y, x] = currentPatch; * visiblePatches.Add(terrainPatches[y, x]); * } * * // Second pass, find its other neighbors * /* * for (int y = 0; y < gridSize.Y; y++) * { * for (int x = 0; x < gridSize.X; x++) * { * TerrainPatch currentPatch = terrainPatches[x, y]; * * // Find north and west neighbors * if (y > 0) currentPatch.neighbors[0] = terrainPatches[y - 1, x]; * if (x > 0) currentPatch.neighbors[2] = terrainPatches[y, x - 1]; * } * } * } */ /// <summary> /// Build the array of terrain patches to draw with /// </summary> public bool BuildMeshData(GraphicsDevice graphicsDevice) { // Iterate backwards because we need to grab neighboring edges for normals for (int i = 0; i < tilesToBuild; i++) { int x = nextPatch % (int)gridSize.X; int y = nextPatch / (int)gridSize.Y; Vector2 offset = new Vector2(x, y); TerrainPatch currentPatch = new TerrainPatch(graphicsDevice, offset); // find south and east neighbors if (y < gridSize.Y - 1) { currentPatch.neighbors[1] = terrainPatches[y + 1, x]; } if (x < gridSize.X - 1) { currentPatch.neighbors[3] = terrainPatches[y, x + 1]; } currentPatch.UpdateMap(heightData, scale, heightScale, mapPosition, indices); terrainPatches[y, x] = currentPatch; visiblePatches.Add(terrainPatches[y, x]); nextPatch--; patchesBuilt++; // Finish updating this frame if (patchesBuilt == (gridSize.X * gridSize.Y)) { // All patches have been built return(true); } } return(false); }