Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Initializes the Terrain object.
- /// </summary>
- /// <param name="heightmap">The imported heightmap to use for rendering</param>
- /// <param name="camera">The game's camera</param>
- public TerrainManager(Texture2D heightmap, Camera camera)
- {
- this.camera = camera;
- this.heightmap = heightmap;
- Color[] tempC = new Color[heightmap.Width * heightmap.Height];
- heightmap.GetData<Color>(tempC);
- colors = new Color[heightmap.Width, heightmap.Height];
- for (int x = 0; x < heightmap.Width; x++)
- for (int y = 0; y < heightmap.Height; y++)
- colors[x, y] = tempC[x + y * heightmap.Width];
- }
- private void UpdateViewComponents()
- {
- userPosition = new Vector2(camera.GetPosition().X,
- camera.GetPosition().Z) / terrainScale;
- Vector2 upperLeft = new Vector2(
- userPosition.X - (viewDistance / 2), userPosition.Y + (viewDistance / 2));
- Vector2 upperRight = new Vector2(
- userPosition.X + (viewDistance / 2), userPosition.Y + (viewDistance / 2));
- Vector2 lowerLeft = new Vector2(
- userPosition.X - (viewDistance / 2), userPosition.Y - (viewDistance / 2));
- Vector2 lowerRight = new Vector2(
- userPosition.X + (viewDistance / 2), userPosition.Y - (viewDistance / 2));
- Build();
- }
- int width = 0, height = 0;
- private void Build()
- {
- if (viewDistance < 128) viewDistance = 128;
- width = int.Parse(viewDistance.ToString()) * 2;
- height = int.Parse(viewDistance.ToString()) * 2;
- userPosition = DeNegifyPoint(userPosition);
- int rectPosX = int.Parse((userPosition.X - viewDistance).ToString());
- int rectPosY = int.Parse((userPosition.Y + viewDistance).ToString());
- if (rectPosX < 0) rectPosX = 0;
- if (rectPosY < 0) rectPosY = 0;
- if (rectPosX > heightmap.Width) rectPosX = heightmap.Width;
- if (rectPosY > heightmap.Height) rectPosY = heightmap.Height;
- int section = int.Parse((viewDistance / 4).ToString());
- // Begin the onslaught of for loops. Let's hope to god this is efficient.
- PrepareVerticesAndIndexes();
- AddVertsAndIndexes(rectPosX, rectPosY, 8);
- AddVertsAndIndexes(rectPosX + section, rectPosY + section, 4);
- AddVertsAndIndexes(rectPosX + (section * 2),
- rectPosY + (section * 2), 2);
- AddVertsAndIndexes(
- rectPosX + (section * 3),
- rectPosY + (section * 3), 1);
- }
- // Drawing variables.
- VertexPositionColorTexture[] vertices;
- int[] indexes;
- int counter = 0;
- public void Draw()
- {
- }
- private void PrepareVerticesAndIndexes()
- {
- vertices = new VertexPositionColorTexture[width * height];
- indexes = new int[width * height * 3];
- counter = 0;
- }
- private void AddVertsAndIndexes(int offsetX, int offsetY, int sectionSpace)
- {
- float section = viewDistance / 4;
- for (int i = 0; i < section / sectionSpace; i++)
- {
- // Top and Bottom
- for (int x = 0; x < width; x += sectionSpace)
- {
- vertices[x + i * width].Position = new Vector3(
- x + offsetX, 0, -(i + offsetY));
- vertices[x + i * width].Color = Color.ForestGreen;
- int figX = x + offsetX;
- int figY = i + offsetY;
- int lowerLeft = figX + figY * width;
- int lowerRight = (figX + 1) + figY * width;
- int topLeft = figX + (figY + 1) * width;
- int topRight = (figX + 1) + (figY + 1) * width;
- indexes[counter++] = topLeft;
- indexes[counter++] = lowerRight;
- indexes[counter++] = lowerLeft;
- indexes[counter++] = topLeft;
- indexes[counter++] = topRight;
- indexes[counter++] = lowerRight;
- }
- // Left and Right
- for (int y = 0; y < height; y += sectionSpace)
- {
- vertices[i + y * width].Position = new Vector3(
- i + offsetX, 0, -(y + offsetY));
- vertices[i + y * width].Color = Color.ForestGreen;
- int figX = i + offsetX;
- int figY = y + offsetY;
- int lowerLeft = figX + figY * width;
- int lowerRight = (figX + 1) + figY * width;
- int topLeft = figX + (figY + 1) * width;
- int topRight = (figX + 1) + (figY + 1) * width;
- indexes[counter++] = topLeft;
- indexes[counter++] = lowerRight;
- indexes[counter++] = lowerLeft;
- indexes[counter++] = topLeft;
- indexes[counter++] = topRight;
- indexes[counter++] = lowerRight;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement