/// <summary> /// Constructor for randomly generating terrian /// </summary> public Terrain() { //FileStream fs = new FileStream("heightdata.raw", FileMode.Open, FileAccess.Read); //BinaryReader r = new BinaryReader(fs); rand = new Random(); width = rand.Next(2, 100);//64;//rand.Next(2, 50); tall = rand.Next(2, 100);//64;//rand.Next(2, 50); world = Matrix.Identity; Height = new int[width, tall]; var vertices = new CustomVertex.VertexPositionColor[width * tall]; var indicies = new short[(width - 1) * (tall - 1) * 3]; for (int i = 0; i < width; i++) { for (int j = 0; j < tall; j++) { //height[width - 1 - j, tall - 1 - i] = (int)(r.ReadByte() / 50); Height[i, j] = rand.Next(0, 3); } } for (int i = 0; i < width; i++) { for (int j = 0; j < tall; j++) { vertices[i + j * width].Position = new Vector3(i, Height[i, j], j); vertices[i + j * width].Color = Color.White.ToArgb(); } } for (int i = 0; i < width - 1; i++) { for (int j = 0; j < tall - 1; j++) { indicies[(i + j * (width - 1)) * 3] = (short)((i + 1) + (j + 1) * width); indicies[(i + j * (width - 1)) * 3 + 1] = (short)((i + 1) + j * width); indicies[(i + j * (width - 1)) * 3 + 2] = (short)(i + j * width); } } mesh = new Mesh(DeviceManager.LocalDevice, indicies.Length, vertices.Length, MeshFlags.Managed, CustomVertex.VertexPositionColor.Format); mesh.LockVertexBuffer(LockFlags.Discard).WriteRange<CustomVertex.VertexPositionColor>(vertices); mesh.UnlockVertexBuffer(); mesh.LockIndexBuffer(LockFlags.Discard).WriteRange<short>(indicies); mesh.UnlockIndexBuffer(); mesh.Optimize(MeshOptimizeFlags.AttributeSort | MeshOptimizeFlags.Compact); //r.Dispose(); //fs.Dispose(); }
/// <summary> /// For creating shape objects /// </summary> /// <param name="type">the name of the object you wish to create</param> public MeshClass(MeshType type) { if (type == MeshType.Cube) { objectMesh = Mesh.CreateBox(DeviceManager.LocalDevice, 1f, 1f, 1f); objectMesh.ComputeNormals(); objectMesh.Optimize(MeshOptimizeFlags.Compact); ApplyColor(Color.White); } else if (type == MeshType.Triangle) { var ShapeVertices = new CustomVertex.VertexPositionColor[] { new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, 1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, 1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, -1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, -1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(0f, 1f, 0f) }, }; var ShapeIndices = new short[] { 0, 2, 1, // base 1, 2, 3, 0, 1, 4, // sides 1, 3, 4, 3, 2, 4, 2, 0, 4, }; objectMesh = new Mesh(DeviceManager.LocalDevice, ShapeIndices.Length, ShapeVertices.Length, MeshFlags.Managed, VertexFormat.Position | VertexFormat.Diffuse); objectMesh.LockVertexBuffer(LockFlags.None).WriteRange<CustomVertex.VertexPositionColor>(ShapeVertices); objectMesh.UnlockVertexBuffer(); objectMesh.LockIndexBuffer(LockFlags.None).WriteRange<short>(ShapeIndices); objectMesh.UnlockIndexBuffer(); Mesh other = objectMesh.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, objectMesh.VertexFormat | VertexFormat.Normal | VertexFormat.Texture2); objectMesh.Dispose(); objectMesh = null; other.ComputeNormals(); objectMesh = other.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, other.VertexFormat); other.Dispose(); objectMesh.Optimize(MeshOptimizeFlags.Compact); } ObjectPosition = Vector3.Zero; ObjectRotate = Vector3.Zero; ObjectScale = new Vector3(1, 1, 1); world = Matrix.Translation(ObjectPosition); Name = type.ToString(); IsShapeObject = true; }