For the Terrain Editor I wanted to give the user full control over how large and detailed the terrain would be. To do this I take in four arguments that define the terrain to be generated. I take in the size of the grid both Width and Length. Then I took in the number of Rows and Columns.
Once the basic parameters have been specified the number of triangles must be calculated. To calculate the number of triangles that will be required to make up the grid with the specified parameters we use the following equation:
Number Of Triangles = (Number Of Rows – 1) * (Number Of Columns – 1) * 2
This is done first, because DirectX 9 only supports a maximum of 65533 triangles. So some form of error prevention or alert should execute if the number of triangles exceeds 65533. Instead of outputting an error message, I recalculate the maximum number of rows and columns with respect to the original ratio passed in.
Next, the amount of space between each vertex will need to be calculated based on the parameters passed in by the user. This is a simple equation to just divide the size by the number of spaces between vertices.
X-Axis Spacing = Width / (Number of Columns – 1)
Z-Axis Spacing = Length / (Number of Rows – 1)
In order to generate a grid aligned in the center of the level an offset starting point will need to be calculated. This is another simple equation moving the starting point over by half the width and length of the grid.
X-Axis Start = Width * -0.5
Z-Axis Start = Length * 0.5
Vertex Position
Now comes the fun part, setting the vertex position, normal, and texture coordinates. The vertices’ position are calculated using the starting points and spacing calculated earlier.
Vertex Position X = (X-Axis Start) + (X-Axis Spacing) * Current Column Iterator
Vertex Position Z = -(Z-Axis Start) + (Z-Axis Spacing) * Current Row Iterator
Vertex Position Y = 0.0
Vertex Normal
Every vertex Y position should start at 0.0 so that the grid starts as a flat terrain. Since the grid starts as a flat terrain every vertex normal will be:
Vector3(0.0, 1.0, 0.0)
Vertex Texture Coordinates
Now lets move onto calculating the texture coordinates. The texture coordinates is calculated using the previous position generated from the equation above and the dimensions established by the user:
Texture Position X = ((Vertex Position X) + (0.5*Width)) / Width
Texture Position Y = ((Vertex Position Z) – (0.5*Length)) / -Length
Finally, we need to generate the indices for our grid. The equation I use generated two triangles in a quad like shape per iteration. Where ‘k’ is the current index position and is incremented by 6 for every iteration.
rows = Number Of Rows
cols = Number Of Columns
r = Current Row Iterator
c =Current Column Iterator
First Triangle:
Index[ k + 2] = r * cols + c //Bottom Left
Index[k + 1] = r * cols + c + 1 //Bottom Right
Index[k] = (r+1) * cols + c //Top Left
Second Triangle:
Index[k + 5] = (r + 1) * cols + c //Top Left
Index[k + 4] = r * cols + c + 1 //Bottom Right
Index[k + 3] = (r+1) * cols + c + 1 //Top Right