public void CreateGroundPlane(float minValue, float size, float uvScale) { VertexElement[] vElements = new VertexElement[] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), new VertexElement(0, 12, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0), new VertexElement(0, 20, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0), new VertexElement(0, 32, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Tangent, 0), VertexElement.VertexDeclarationEnd }; model = new Mesh(2, 4, MeshFlags.Managed | MeshFlags.Use32Bit, vElements, device); Vertex[] vertexList = new Vertex[4]; // Initialize the values for the 4 vertexes. vertexList[0].Position = new Vector3(-size, minValue, -size); vertexList[0].Normal = new Vector3(0, 1.0f, 0); vertexList[0].TexCoord = new Vector2(0, 0); vertexList[1].Position = new Vector3(-size, minValue, size); vertexList[1].Normal = new Vector3(0, 1.0f, 0); vertexList[1].TexCoord = new Vector2(0, uvScale); vertexList[2].Position = new Vector3(size, minValue, -size); vertexList[2].Normal = new Vector3(0, 1.0f, 0); vertexList[2].TexCoord = new Vector2(uvScale, 0); vertexList[3].Position = new Vector3(size, minValue, size); vertexList[3].Normal = new Vector3(0, 1.0f, 0); vertexList[3].TexCoord = new Vector2(uvScale, uvScale); int[] indexList = { 0, 3, 2, 1, 3, 0 }; model.SetIndexBufferData(indexList, LockFlags.None); model.SetVertexBufferData(vertexList, LockFlags.None); TangentBuilder.CalculateTangents(model); }
/// <summary> /// Calculates if a vertex value is already in the list of vertices. /// </summary> /// <param name="hash">The index into the vertex list.</param> /// <param name="vertex">The vertex to look for.</param> /// <returns>The index into the array of vertices.</returns> private int addVertex(int hash, Vertex vertex) { bool foundInList = false; int index = 0; // Checks to see if the hash value is part of the table. if (table.ContainsKey(hash)) { // Checks if the vertex at the index of the hash value is equal to the // value of the vertex passed into the function. if (table[hash].Equals(vertex)) { // Sets the index to the hash and that it was found. index = hash; foundInList = true; } } if (!foundInList) { index = vertexInfo.Count; // Sets the index to the last value in the list. vertexInfo.Add(vertex); // Adds the vertex to the list. table.Add(index, vertex); // Adds the vertex to a hash list. } return (int)index; }
/// <summary> /// Treats the buffered string as face information. /// </summary> private void addFace() { string[] face1, face2, face3, face4; // Info for up to 4 faces. Vertex[] vertices = new Vertex[4]; // Array of 4 Vertex's bool isQuad = false; // bool for handling quad faces. string[] values = buffer.Split(enumerator, StringSplitOptions.RemoveEmptyEntries); // Splits up the buffer string by spaces. // Splits up the face information even more (puts 1/1/1 into three spots in an array) face1 = values[1].Split('/'); face2 = values[2].Split('/'); face3 = values[3].Split('/'); face4 = face3; // so face4 isn't null. // Checks if the length is consistant with a quad face. if (values.Length == 5) { // Makes sure the 5th array isn't empty if (!values[4].Equals("")) { // Splits up the data for the 4th value of the quad. face4 = values[4].Split('/'); // sets quad to true. isQuad = true; } } // Sets the position vectors for the first three values. vertices[0].Position = vertexList[Math.Abs(Int32.Parse(face1[0])) - 1]; vertices[1].Position = vertexList[Math.Abs(Int32.Parse(face2[0])) - 1]; vertices[2].Position = vertexList[Math.Abs(Int32.Parse(face3[0])) - 1]; // Sets the fourth position vector if the face described in the file is a quad. if (isQuad) vertices[3].Position = vertexList[Math.Abs(Int32.Parse(face4[0])) - 1]; // Handles if there is texture coordinates. if (face1.Length >= 2) { if (!face1[1].Equals("")) { // Sets the texture coordinate vectors for the first three values. vertices[0].TexCoord = uvList[Math.Abs(Int32.Parse(face1[1])) - 1]; vertices[1].TexCoord = uvList[Math.Abs(Int32.Parse(face2[1])) - 1]; vertices[2].TexCoord = uvList[Math.Abs(Int32.Parse(face3[1])) - 1]; // Sets the fourth texture coordinate vector if the face described in the file is a quad. if (isQuad) vertices[3].TexCoord = uvList[Math.Abs(Int32.Parse(face4[1])) - 1]; } } // Handles if there is normals. if (face1.Length == 3) { if (!face1[2].Equals("")) { // Sets the normal vectors for the first three values. vertices[0].Normal = normalList[Math.Abs(Int32.Parse(face1[2])) - 1]; vertices[1].Normal = normalList[Math.Abs(Int32.Parse(face2[2])) - 1]; vertices[2].Normal = normalList[Math.Abs(Int32.Parse(face3[2])) - 1]; // Sets the fourth normal vector if the face described in the file is a quad. if (isQuad) vertices[3].Normal = normalList[Math.Abs(Int32.Parse(face4[2])) - 1]; } } // Handles adding the face information to the face list. if (!isQuad) { // Calls the addVertex method to get the vertex index to be used for the index buffer. faceList.Add(addVertex(Math.Abs(Int32.Parse(face1[0])), vertices[0])); faceList.Add(addVertex(Math.Abs(Int32.Parse(face2[0])), vertices[1])); faceList.Add(addVertex(Math.Abs(Int32.Parse(face3[0])), vertices[2])); } // Adds two triangles if the face is a quad. else { // Calls the addVertex method to get the vertex index to be used for the index buffer. faceList.Add(addVertex(Math.Abs(Int32.Parse(face1[0])), vertices[0])); faceList.Add(addVertex(Math.Abs(Int32.Parse(face2[0])), vertices[1])); faceList.Add(addVertex(Math.Abs(Int32.Parse(face4[0])), vertices[3])); faceList.Add(addVertex(Math.Abs(Int32.Parse(face2[0])), vertices[1])); faceList.Add(addVertex(Math.Abs(Int32.Parse(face3[0])), vertices[2])); faceList.Add(addVertex(Math.Abs(Int32.Parse(face4[0])), vertices[3])); } }