Advertisement
Guest User

Untitled

a guest
Apr 15th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. // initialize our vertex count for height (h) and width (w)
  2.             int w = x_quad_count + 1;
  3.             int h = y_quad_count + 1;
  4.  
  5.             // total number of vertices
  6.             int count = w * h;
  7.  
  8.             // initialize vertices array
  9.             Vector3[] vertices = new Vector3[count];
  10.  
  11.             // invariant: we have processed i vertices thus far
  12.             for (int i = 0; i != count; ++i)
  13.             {
  14.                 int x = i % w;    // the x index
  15.                 int y = i / w;    // the y index
  16.  
  17.                 // map x and y to the range [0.0 .. 1.0]
  18.                 float a = (float)x / (w - 1);
  19.                 float b = (float)y / (h - 1);
  20.  
  21.                 // map a and b to size, then make it relative to origin.
  22.                 // (origin will be in the top-left, looking down at the plan)
  23.                 a = (a * (size.X)) + origin.X;
  24.                 b = (b * (size.Y)) + origin.Y;
  25.  
  26.                 // assign calculated position
  27.                 vertices[i] = new Vector3(a, b, origin.Z);                
  28.             }
  29.  
  30.             int polygon_count = (w - 1) * (h - 1);
  31.  
  32.             // index_count = (2) triangle indices per polygon
  33.             // plus (3) indices for each polygon row beggining;    
  34.             int index_count = 2 * polygon_count + ((h - 1) * 3);
  35.  
  36.             ushort[] indices = new ushort[index_count];
  37.             int j = 0;
  38.             for (int i = 0; i != polygon_count; ++i)
  39.             {
  40.                 /*  Polygon Vertex Layout
  41.                  *      _________
  42.                  *  p1  |\      | p3
  43.                  *      |  \    |
  44.                  *      |    \  |
  45.                  *  p0  |      \| p2
  46.                  *      ‾‾‾‾‾‾‾‾‾
  47.                  *  poly-strip = p0, p1, p2, p3
  48.                  *                  
  49.                  */
  50.  
  51.                 int x = i % (w - 1);    // the x polygon-index
  52.                 int y = i / (w - 1);    // the y polygon-index
  53.  
  54.                 int vertex_index = y * w;
  55.                 int p_1 = vertex_index + x;
  56.                 int p_0 = p_1 + w;
  57.  
  58.                 // row start
  59.                 if (x == 0)
  60.                 {
  61.                     indices[j++] = (ushort)(0xFFFF);
  62.                     indices[j++] = (ushort)(p_0);
  63.                     indices[j++] = (ushort)(p_1);
  64.                 }
  65.  
  66.                 indices[j++] = (ushort)(p_0 + 1);
  67.                 indices[j++] = (ushort)(p_1 + 1);
  68.  
  69.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement