示例#1
0
        /// <summary>
        /// Method to rebuild the normals and tangents of the loaded model.
        /// </summary>
        public void rebuildNormals()
        {
            // Generates a list of adjacent faces used for generating normals.
            int[] adjacency = new int[model.NumberFaces * 3];
            model.GenerateAdjacency(0, adjacency);

            model.ComputeNormals(adjacency);

            //model.ComputeTangentFrame(TangentOptions.GenerateInPlace | TangentOptions.CalculateNormals);

            TangentBuilder.CalculateTangents(model);
        }
示例#2
0
        /// <summary>
        /// Method to load in a directx mesh.
        /// </summary>
        /// <param name="filename">Path of the model to load.</param>
        /// <returns></returns>
        public Mesh LoadModel(string filename)
        {
            Mesh mesh;

            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
            };

            // Load the mesh from file.
            mesh = Mesh.FromFile(filename, MeshFlags.Managed | MeshFlags.Use32Bit, device);

            // Clone the mesh.
            mesh = mesh.Clone(MeshFlags.Managed | MeshFlags.Use32Bit, vElements, device);

            // Generates a list of adjacent faces used for generating normals.
            int[] adjacency = new int[mesh.NumberFaces * 3];
            mesh.GenerateAdjacency(0, adjacency);

            //mesh.ComputeTangentFrame(TangentOptions.GenerateInPlace | TangentOptions.CalculateNormals);
            mesh.ComputeNormals(adjacency);

            // Create the variables needed to convert the mesh to right handed coordinates.
            GraphicsStream gStream  = mesh.LockVertexBuffer(LockFlags.None);
            List <Vertex>  vertices = new List <Vertex>();
            Vertex         vertex;

            // Convert the mesh to right handed coordinates.
            for (int i = 0; i < mesh.NumberVertices; i++)
            {
                vertex = (Vertex)gStream.Read(typeof(Vertex));

                vertex.Position.Z *= -1.0f;
                vertex.Normal      = Vector3.TransformNormal(vertex.Normal, Matrix.Scaling(1.0f, 1.0f, -1.0f));

                vertices.Add(vertex);
            }

            // Set the vertex buffer
            mesh.SetVertexBufferData(vertices.ToArray(), LockFlags.None);

            // Calculate the tangents
            TangentBuilder.CalculateTangents(mesh);

            return(mesh);
        }
示例#3
0
        /// <summary>
        /// Copies over the vertex and index buffers and calls the function within
        /// the model class to build a Mesh class.
        /// </summary>
        private void buildMesh()
        {
            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
            };

            // Creates a new mesh
            try
            {
                mesh = new Mesh(faceList.Count, vertexInfo.Count, MeshFlags.Managed | MeshFlags.Use32Bit, vElements, device);

                mesh.SetVertexBufferData(vertexInfo.ToArray(), LockFlags.None);
                mesh.SetIndexBufferData(faceList.ToArray(), LockFlags.None);
            }
            catch (DirectXException)
            {
                MessageBox.Show("A problem occured with the model", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                faceList.Clear();
                vertexInfo.Clear();
                vertexList.Clear();
                mesh = new Mesh(1, 1, MeshFlags.Managed, vElements, device);
                return;
            }

            // Try loop to generate normals (if needed), tangents and binormals.
            try
            {
                // Generates a list of adjacent faces used for generating normals.
                int[] adjacency = new int[mesh.NumberFaces * 3];
                mesh.GenerateAdjacency(0, adjacency);

                if (!hasNormals)
                {
                    mesh.ComputeNormals(adjacency);
                }

                TangentBuilder.CalculateTangents(mesh);
            }
            catch (DirectXException dxe)
            {
                Console.WriteLine(dxe);
            }
        }
示例#4
0
        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);
        }