Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // initialize our vertex count for height (h) and width (w)
- int w = x_quad_count + 1;
- int h = y_quad_count + 1;
- // total number of vertices
- int count = w * h;
- // initialize vertices array
- Vector3[] vertices = new Vector3[count];
- // invariant: we have processed i vertices thus far
- for (int i = 0; i != count; ++i)
- {
- int x = i % w; // the x index
- int y = i / w; // the y index
- // map x and y to the range [0.0 .. 1.0]
- float a = (float)x / (w - 1);
- float b = (float)y / (h - 1);
- // map a and b to size, then make it relative to origin.
- // (origin will be in the top-left, looking down at the plan)
- a = (a * (size.X)) + origin.X;
- b = (b * (size.Y)) + origin.Y;
- // assign calculated position
- vertices[i] = new Vector3(a, b, origin.Z);
- }
- int polygon_count = (w - 1) * (h - 1);
- // index_count = (2) triangle indices per polygon
- // plus (3) indices for each polygon row beggining;
- int index_count = 2 * polygon_count + ((h - 1) * 3);
- ushort[] indices = new ushort[index_count];
- int j = 0;
- for (int i = 0; i != polygon_count; ++i)
- {
- /* Polygon Vertex Layout
- * _________
- * p1 |\ | p3
- * | \ |
- * | \ |
- * p0 | \| p2
- * ‾‾‾‾‾‾‾‾‾
- * poly-strip = p0, p1, p2, p3
- *
- */
- int x = i % (w - 1); // the x polygon-index
- int y = i / (w - 1); // the y polygon-index
- int vertex_index = y * w;
- int p_1 = vertex_index + x;
- int p_0 = p_1 + w;
- // row start
- if (x == 0)
- {
- indices[j++] = (ushort)(0xFFFF);
- indices[j++] = (ushort)(p_0);
- indices[j++] = (ushort)(p_1);
- }
- indices[j++] = (ushort)(p_0 + 1);
- indices[j++] = (ushort)(p_1 + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement