SHARE
TWEET

XNA Terrain - Inefficient

sammarks Aug 20th, 2011 661 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         /// <summary>
  2.         /// Initializes the Terrain object.
  3.         /// </summary>
  4.         /// <param name="heightmap">The imported heightmap to use for rendering</param>
  5.         /// <param name="camera">The game's camera</param>
  6.         public TerrainManager(Texture2D heightmap, Camera camera)
  7.         {
  8.             this.camera = camera;
  9.             this.heightmap = heightmap;
  10.  
  11.             Color[] tempC = new Color[heightmap.Width * heightmap.Height];
  12.             heightmap.GetData<Color>(tempC);
  13.             colors = new Color[heightmap.Width, heightmap.Height];
  14.             for (int x = 0; x < heightmap.Width; x++)
  15.                 for (int y = 0; y < heightmap.Height; y++)
  16.                     colors[x, y] = tempC[x + y * heightmap.Width];
  17.         }
  18.  
  19.         private void UpdateViewComponents()
  20.         {
  21.             userPosition = new Vector2(camera.GetPosition().X,
  22.                 camera.GetPosition().Z) / terrainScale;
  23.             Vector2 upperLeft = new Vector2(
  24.                 userPosition.X - (viewDistance / 2), userPosition.Y + (viewDistance / 2));
  25.             Vector2 upperRight = new Vector2(
  26.                 userPosition.X + (viewDistance / 2), userPosition.Y + (viewDistance / 2));
  27.             Vector2 lowerLeft = new Vector2(
  28.                 userPosition.X - (viewDistance / 2), userPosition.Y - (viewDistance / 2));
  29.             Vector2 lowerRight = new Vector2(
  30.                 userPosition.X + (viewDistance / 2), userPosition.Y - (viewDistance / 2));
  31.  
  32.             Build();
  33.         }
  34.  
  35.         int width = 0, height = 0;
  36.  
  37.         private void Build()
  38.         {
  39.             if (viewDistance < 128) viewDistance = 128;
  40.  
  41.             width = int.Parse(viewDistance.ToString()) * 2;
  42.             height = int.Parse(viewDistance.ToString()) * 2;
  43.             userPosition = DeNegifyPoint(userPosition);
  44.  
  45.             int rectPosX = int.Parse((userPosition.X - viewDistance).ToString());
  46.             int rectPosY = int.Parse((userPosition.Y + viewDistance).ToString());
  47.             if (rectPosX < 0) rectPosX = 0;
  48.             if (rectPosY < 0) rectPosY = 0;
  49.             if (rectPosX > heightmap.Width) rectPosX = heightmap.Width;
  50.             if (rectPosY > heightmap.Height) rectPosY = heightmap.Height;
  51.             int section = int.Parse((viewDistance / 4).ToString());
  52.  
  53.             // Begin the onslaught of for loops. Let's hope to god this is efficient.
  54.             PrepareVerticesAndIndexes();
  55.             AddVertsAndIndexes(rectPosX, rectPosY, 8);
  56.             AddVertsAndIndexes(rectPosX + section, rectPosY + section, 4);
  57.             AddVertsAndIndexes(rectPosX + (section * 2),
  58.                 rectPosY + (section * 2), 2);
  59.             AddVertsAndIndexes(
  60.                 rectPosX + (section * 3),
  61.                 rectPosY + (section * 3), 1);
  62.         }
  63.  
  64.         // Drawing variables.
  65.         VertexPositionColorTexture[] vertices;
  66.         int[] indexes;
  67.         int counter = 0;
  68.  
  69.         public void Draw()
  70.         {
  71.            
  72.         }
  73.  
  74.         private void PrepareVerticesAndIndexes()
  75.         {
  76.             vertices = new VertexPositionColorTexture[width * height];
  77.             indexes = new int[width * height * 3];
  78.             counter = 0;
  79.         }
  80.  
  81.         private void AddVertsAndIndexes(int offsetX, int offsetY, int sectionSpace)
  82.         {
  83.             float section = viewDistance / 4;
  84.  
  85.             for (int i = 0; i < section / sectionSpace; i++)
  86.             {
  87.                 // Top and Bottom
  88.                 for (int x = 0; x < width; x += sectionSpace)
  89.                 {
  90.                     vertices[x + i * width].Position = new Vector3(
  91.                         x + offsetX, 0, -(i + offsetY));
  92.                     vertices[x + i * width].Color = Color.ForestGreen;
  93.  
  94.                     int figX = x + offsetX;
  95.                     int figY = i + offsetY;
  96.                     int lowerLeft = figX + figY * width;
  97.                     int lowerRight = (figX + 1) + figY * width;
  98.                     int topLeft = figX + (figY + 1) * width;
  99.                     int topRight = (figX + 1) + (figY + 1) * width;
  100.  
  101.                     indexes[counter++] = topLeft;
  102.                     indexes[counter++] = lowerRight;
  103.                     indexes[counter++] = lowerLeft;
  104.  
  105.                     indexes[counter++] = topLeft;
  106.                     indexes[counter++] = topRight;
  107.                     indexes[counter++] = lowerRight;
  108.                 }
  109.  
  110.                 // Left and Right
  111.                 for (int y = 0; y < height; y += sectionSpace)
  112.                 {
  113.                     vertices[i + y * width].Position = new Vector3(
  114.                         i + offsetX, 0, -(y + offsetY));
  115.                     vertices[i + y * width].Color = Color.ForestGreen;
  116.  
  117.                     int figX = i + offsetX;
  118.                     int figY = y + offsetY;
  119.                     int lowerLeft = figX + figY * width;
  120.                     int lowerRight = (figX + 1) + figY * width;
  121.                     int topLeft = figX + (figY + 1) * width;
  122.                     int topRight = (figX + 1) + (figY + 1) * width;
  123.  
  124.                     indexes[counter++] = topLeft;
  125.                     indexes[counter++] = lowerRight;
  126.                     indexes[counter++] = lowerLeft;
  127.  
  128.                     indexes[counter++] = topLeft;
  129.                     indexes[counter++] = topRight;
  130.                     indexes[counter++] = lowerRight;
  131.                 }
  132.             }
  133.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top