예제 #1
0
        /// <summary>
        /// Get World Matrix For NifGeometry
        /// </summary>
        /// <param name="nifGeom"></param>
        /// <param name="UnitFactor"></param>
        /// <param name="heightCalculator"></param>
        /// <returns></returns>
        protected static Matrix ComputeWorldMatrix(NifGeometry nifGeom, float UnitFactor, TerrainHeightCalculator heightCalculator)
        {
            if (nifGeom == null)
            {
                throw new ArgumentNullException("nifGeom");
            }

            // Find Ground
            float nifHeight = nifGeom.Z * UnitFactor;

            if (nifGeom.OnGround)
            {
                nifHeight = heightCalculator[nifGeom.X, nifGeom.Y] * UnitFactor;
            }

            // Get Translation (XY Inverted), Scale, Rotation
            Matrix translation;

            ZoneDrawingExtensions.CreateTranslation(nifGeom.X * UnitFactor, nifHeight, nifGeom.Y * UnitFactor, out translation);
            Matrix scale;

            ZoneDrawingExtensions.CreateScale(nifGeom.Scale * UnitFactor, out scale);
            Matrix rotation;

            ZoneDrawingExtensions.CreateRotation(new Vector3(nifGeom.RotationX, nifGeom.RotationY, nifGeom.RotationZ), nifGeom.Angle, out rotation);

            Matrix result = Matrix.Identity;

            // Flip if needed
            if (nifGeom.Flip)
            {
                Matrix flip;
                ZoneDrawingExtensions.CreateScale(-1f, 1f, 1f, out flip);
                result = flip;
            }

            Matrix intermediateResult;

            // Combine Scale, Rotation, Invertion Rotation, Translation
            ZoneDrawingExtensions.Mult(ref result, ref scale, out intermediateResult);
            result = intermediateResult;
            ZoneDrawingExtensions.Mult(ref result, ref rotation, out intermediateResult);
            result = intermediateResult;
            ZoneDrawingExtensions.Mult(ref result, ref RotationMatrix, out intermediateResult);
            result = intermediateResult;
            ZoneDrawingExtensions.Mult(ref result, ref translation, out intermediateResult);
            result = intermediateResult;

            // Combine Matrix with Parent Matrix
            if (nifGeom.RelativeTo != null)
            {
                Matrix relativeMatrix = ComputeWorldMatrix(nifGeom.RelativeTo, UnitFactor, heightCalculator);
                ZoneDrawingExtensions.Mult(ref result, ref relativeMatrix, out intermediateResult);
                result = intermediateResult;
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Get World Matrix For NifGeometry
        /// </summary>
        /// <param name="nifGeom"></param>
        /// <param name="UnitFactor"></param>
        /// <returns></returns>
        protected static Matrix ComputeWorldMatrix(NifGeometry nifGeom, float UnitFactor)
        {
            if (nifGeom == null)
                throw new ArgumentNullException("nifGeom");

            // Get Translation (XY Inverted), Scale, Rotation
            Matrix translation;
            ZoneDrawingExtensions.CreateTranslation(nifGeom.X * UnitFactor, nifGeom.Y * UnitFactor, nifGeom.Z * UnitFactor, out translation);
            Matrix scale;
            ZoneDrawingExtensions.CreateScale(nifGeom.Scale * UnitFactor, out scale);
            Matrix rotation;
            ZoneDrawingExtensions.CreateRotation(new Vector3(nifGeom.RotationX, nifGeom.RotationY, nifGeom.RotationZ * -1f), nifGeom.Angle , out rotation);

            Matrix result = Matrix.Identity;
            // Flip if needed
            if (nifGeom.Flip)
            {
                Matrix flip;
                ZoneDrawingExtensions.CreateScale(-1f, 1f, 1f, out flip);
                result = flip;
            }

            Matrix intermediateResult;
            // Combine Scale, Rotation, Translation, Invertion Rotation
            ZoneDrawingExtensions.Mult(ref result, ref scale, out intermediateResult);
            result = intermediateResult;
            ZoneDrawingExtensions.Mult(ref result, ref rotation, out intermediateResult);
            result = intermediateResult;
            ZoneDrawingExtensions.Mult(ref result, ref translation, out intermediateResult);
            result = intermediateResult;
            ZoneDrawingExtensions.Mult(ref result, ref RotationMatrix, out intermediateResult);
            result = intermediateResult;

            // Combine Matrix with Parent Matrix
            if (nifGeom.RelativeTo != null)
            {
                Matrix relativeMatrix = ComputeWorldMatrix(nifGeom.RelativeTo, UnitFactor);
                ZoneDrawingExtensions.Mult(ref result, ref relativeMatrix, out intermediateResult);
                result = intermediateResult;
            }

            return result;
        }