Пример #1
0
        /// <summary>
        /// This function is used to scale a list of vertices to the specified
        /// size
        /// </summary>
        /// <param name="listToScale">The list of vertices that should be scaled. Ref is to prevent memory leaks.</param>
        /// <param name="scale">The multiplier that should be used to scale the vertices.</param>
        static public void rotateVertexList(ref List <VertexPositionColoredNormal> listToScale, float scale)
        {
            if (listToScale == null)
            {
                return;
            }

            //Create the matrix needed to scale each vertex
            Matrix scaleMatrix = Matrix.CreateScale(scale);

            for (int i = 0; i < listToScale.Count; i++)              //got through all the vertices in the list
            {
                VertexPositionColoredNormal vertex = listToScale[i]; //open the vertex for editing
                //apply the final matrix to the podition of the vertex
                vertex.Position = Vector3.Transform(vertex.Position, scaleMatrix);
                listToScale[i]  = vertex;//save the new vertex
            }
        }
Пример #2
0
        /// <summary>
        /// Centres a VertexPositionColoredNormal list around a given x and y point in a Vector3.
        /// The z will be moved to have a minimum of the specified point.
        /// </summary>
        /// <param name="listToCentre">The list of vertices to centre. Ref is to prevent memory leaks</param>
        /// <param name="centrePosition">The position they should be centred anround.</param>
        static public void centreVertexList(ref List <VertexPositionColoredNormal> listToCentre, Vector3 centrePosition)
        {
            if (listToCentre == null)
            {
                return;
            }

            float xMax    = float.NaN;        //the maximum x coordinate of the model
            float xMin    = float.NaN;        //the minimum x coordinate of the model
            float xCentre = 0.0f;             //the centre x coordinate of the model
            float xTarget = centrePosition.X; //the target centre x coordinate of the model
            float xMove   = 0.0f;             //the amount that the models needs to move to reach the target position

            float yMax    = float.NaN;        //the maximum y coordinate of the model
            float yMin    = float.NaN;        //the minimum y coordinate of the model
            float yCentre = 0.0f;             //the centre y coordinate of the model
            float yTarget = centrePosition.Y; //the target centre y coordinate of the model
            float yMove   = 0.0f;

            float zMin    = float.NaN;                                   //the minimum y coordinate of the model
            float zTarget = centrePosition.Z;                            //the target minimum y coordinate of the model
            float zMove   = 0.0f;                                        //the amount that the models needs to move to reach the target position

            foreach (VertexPositionColoredNormal vertex in listToCentre) //cycle through all the vertices in the list
            {
                Vector3 vector = vertex.Position;                        //the position of the current vertice

                //just intitialise the values if it has not been done yet
                //float.NaN is used to avoid a situation were either the min or max is actually the default value if were to use a real number
                if (float.IsNaN(xMax))
                {
                    xMax = vector.X;
                }
                if (float.IsNaN(xMin))
                {
                    xMin = vector.X;
                }
                if (float.IsNaN(yMax))
                {
                    yMax = vector.Y;
                }
                if (float.IsNaN(yMin))
                {
                    yMin = vector.Y;
                }
                if (float.IsNaN(zMin))
                {
                    zMin = vector.Z;
                }

                xMax = Math.Max(xMax, vector.X); //set this position to the maximum if it is larger than the previous one
                xMin = Math.Min(xMin, vector.X); //set this position to the minimum if it is smaller than the previous one

                yMax = Math.Max(yMax, vector.Y); //set this position to the maximum if it is larger than the previous one
                yMin = Math.Min(yMin, vector.Y); //set this position to the minimum if it is smaller than the previous one

                zMin = Math.Min(zMin, vector.Z); //set this position to the minimum if it is smaller than the previous one
            }

            xCentre = (xMax + xMin) / 2; //calculate the centre x position
            xMove   = xTarget - xCentre; //calculate the amount that has to be moved in the x axis

            yCentre = (yMax + yMin) / 2; //calculate the centre y position
            yMove   = yTarget - yCentre; //calculate the amount that has to be moved in the y axis

            zMove = zTarget - zMin;      //calculate the amount that has to be moved in the z axis

            /*
             * //Give the caller the centreposition that can be used to move the model back to its previous position
             * if (oldCentrePosition != null)
             *  oldCentrePosition = new Vector3(xCentre, yCentre, zMin);*/

            for (int i = 0; i < listToCentre.Count; i++)              //got through all the vertices in the list
            {
                VertexPositionColoredNormal vertex = listToCentre[i]; //Open the vertex for editing
                Vector3 vector = vertex.Position;                     //Oen the vector of thevertex for editing
                //move the model to the target positions
                vector.X       += xMove;
                vector.Y       += yMove;
                vector.Z       += zMove;
                vertex.Position = vector; //Store the new vector in the vertex
                listToCentre[i] = vertex; //store the new vertex in the list
            }
        }
Пример #3
0
        /// <summary>
        /// Rotates a VertexPositionColoredNormal list according to given x, y and z rotations.
        /// </summary>
        /// <param name="listToRotate">The list of vertices to rotate. Ref is to prevent memory leaks.</param>
        /// <param name="xRot">The amount of yaw to be applied in Radians</param>
        /// <param name="yRot">The amount of pitch to be applied in Radians</param>
        /// <param name="zRot">The amount of roll to be applied in Radians</param>
        static public void rotateVertexList(ref List <VertexPositionColoredNormal> listToRotate, float xRot, float yRot, float zRot)
        {
            if (listToRotate == null)
            {
                return;
            }

            float xMax    = float.NaN;                                   //the maximum x coordinate of the model
            float xMin    = float.NaN;                                   //the minimum x coordinate of the model
            float xCentre = 0.0f;                                        //the centre x coordinate of the model

            float yMax    = float.NaN;                                   //the maximum y coordinate of the model
            float yMin    = float.NaN;                                   //the minimum y coordinate of the model
            float yCentre = 0.0f;                                        //the centre y coordinate of the model

            float zMax    = float.NaN;                                   //the maximum y coordinate of the model
            float zMin    = float.NaN;                                   //the minimum y coordinate of the model
            float zCentre = 0.0f;                                        //the centre y coordinate of the model

            foreach (VertexPositionColoredNormal vertex in listToRotate) //cycle through all the vertices in the list
            {
                Vector3 vector = vertex.Position;                        //the position of the current vertice

                //just intitialise the values if it has not been done yet
                //float.NaN is used to avoid a situation were either the min or max is actually the default value if were to use a real number
                if (float.IsNaN(xMax))
                {
                    xMax = vector.X;
                }
                if (float.IsNaN(xMin))
                {
                    xMin = vector.X;
                }
                if (float.IsNaN(yMax))
                {
                    yMax = vector.Y;
                }
                if (float.IsNaN(yMin))
                {
                    yMin = vector.Y;
                }
                if (float.IsNaN(zMax))
                {
                    zMax = vector.Z;
                }
                if (float.IsNaN(zMin))
                {
                    zMin = vector.Z;
                }

                xMax = Math.Max(xMax, vector.X); //set this position to the maximum if it is larger than the previous one
                xMin = Math.Min(xMin, vector.X); //set this position to the minimum if it is smaller than the previous one

                yMax = Math.Max(yMax, vector.Y); //set this position to the maximum if it is larger than the previous one
                yMin = Math.Min(yMin, vector.Y); //set this position to the minimum if it is smaller than the previous one

                zMax = Math.Max(zMax, vector.Z); //set this position to the maximum if it is larger than the previous one
                zMin = Math.Min(zMin, vector.Z); //set this position to the minimum if it is smaller than the previous one
            }

            xCentre = (xMax + xMin) / 2; //calculate the centre x position
            yCentre = (yMax + yMin) / 2; //calculate the centre x position
            zCentre = (zMax + zMin) / 2; //calculate the centre x position

            //We need to translate the model sothat its centre is on 0,0,0 before we can rotate it
            Matrix translateToCentreMatrix = Matrix.CreateTranslation(new Vector3(-xCentre, -yCentre, -zCentre));
            //We then create the matrix that we will use to apply the requestes rotation
            Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(xRot, yRot, zRot);
            //We then create the matrix needed to move the model back to its original position
            Matrix translateBackMatrix = Matrix.CreateTranslation(new Vector3(xCentre, yCentre, zCentre));
            //Finally we create the matrix thats first centres it, then rotates it and finally takes it back to its original position
            Matrix matrixToApply = translateToCentreMatrix * rotationMatrix * translateBackMatrix;

            for (int i = 0; i < listToRotate.Count; i++)              //got through all the vertices in the list
            {
                VertexPositionColoredNormal vertex = listToRotate[i]; //Open the vertex for editing
                //apply the final matrix to the vector
                vertex.Position = Vector3.Transform(vertex.Position, matrixToApply);
                listToRotate[i] = vertex;//store the new vertex in the list
            }
        }