For the Terrain Editor<\/em> I wanted to give the user full control over how large and detailed the terrain would be. \u00a0To do this I take in four arguments that define the terrain to be generated. \u00a0I take in the size of the grid both Width<\/em> and Length<\/em>. \u00a0Then I took in the number of Rows<\/em> and Columns<\/em>.<\/p>\n Once the basic parameters have been specified the number of triangles must be calculated. \u00a0To calculate the number of triangles that will be required to make up the grid with the specified parameters we use the following equation:<\/p>\n Number Of Triangles = (Number Of Rows – 1) * (Number Of Columns – 1) * 2<\/em><\/p>\n This is done first, because DirectX 9 only supports a maximum of 65533 triangles. \u00a0So some form of error prevention or alert should execute if the number of triangles exceeds 65533. \u00a0Instead of outputting an error message, I recalculate the maximum number of rows and columns with respect to the original ratio passed in.<\/p>\n Next, the amount of space between each vertex will need to be calculated based on the parameters passed in by the user. \u00a0This is \u00a0a simple equation to just divide the size by the number of spaces between vertices.<\/p>\n X-Axis Spacing = Width \/ (Number of Columns – 1)<\/em><\/p>\n Z-Axis Spacing = Length \/ (Number of Rows – 1)<\/em><\/p>\n In order to generate a grid aligned in the center of the level an offset starting point will need to be calculated. \u00a0This is another simple equation moving the starting point over by half the width and length of the grid.<\/p>\n X-Axis Start = Width * -0.5<\/em><\/p>\n Z-Axis Start = Length * 0.5<\/em><\/p>\n Now comes the fun part, setting the vertex position, normal, and texture coordinates. \u00a0The vertices’ position are calculated using the starting points and spacing calculated earlier.<\/p>\n Vertex Position X = (X-Axis Start) + (X-Axis Spacing) * Current Column Iterator<\/em><\/p>\n Vertex Position Z = -(Z-Axis Start) + (Z-Axis Spacing) * Current Row Iterator<\/em><\/p>\n Vertex Position Y = 0.0<\/em><\/p>\n Every vertex Y position should start at 0.0 so that the grid starts as a flat terrain. \u00a0Since the grid starts as a flat terrain every vertex normal will be:<\/p>\n Vector3(0.0, 1.0, 0.0)<\/em><\/p>\n Now lets move onto calculating the texture coordinates. \u00a0The texture coordinates is calculated using the previous position generated from the equation above and the dimensions established by the user:<\/p>\n Texture Position X = ((Vertex Position X) + (0.5*Width)) \/ Width<\/em><\/p>\n Texture Position Y = ((Vertex Position Z) – (0.5*Length)) \/ -Length<\/em><\/p>\n Finally, we need to generate the indices for our grid. \u00a0The 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.<\/p>\n rows = Number Of Rows<\/em><\/p>\n cols = Number Of Columns<\/em><\/p>\n r = Current Row Iterator<\/em><\/p>\n c =Current Column Iterator<\/em><\/p>\n Index[ k + 2] = r * cols + c \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/Bottom Left<\/em><\/p>\n Index[k + 1] \u00a0= r * cols + c + 1 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/Bottom Right<\/em><\/p>\n Index[k] \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= (r+1) * cols + c \u00a0 \u00a0 \u00a0 \u00a0 \/\/Top Left<\/em><\/p>\n Index[k + 5] = (r + 1) * cols + c \u00a0 \u00a0 \u00a0 \u00a0\/\/Top Left<\/em><\/p>\n Index[k + 4] = r * cols + c + 1 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/Bottom Right<\/em><\/p>\n Index[k + 3] = (r+1) * cols + c + 1 \u00a0 \/\/Top Right<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"For the Terrain Editor I wanted to give the user full control over how large and detailed the terrain would be. \u00a0To do this I …","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts\/501"}],"collection":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":23,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":571,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts\/501\/revisions\/571"}],"wp:attachment":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Vertex Position<\/h4>\n
Vertex Normal<\/h4>\n
Vertex Texture Coordinates<\/h4>\n
First Triangle:<\/h5>\n
Second Triangle:<\/h5>\n