Exemple #1
0
        public void Multiply(Matrix34 *m)
        {
            Matrix34 m2 = this;

            float *s1 = (float *)&m2, s2 = (float *)m;

            fixed(float *p = _data)
            {
                int   index = 0;
                float val;

                for (int b = 0; b < 12; b += 4)
                {
                    for (int a = 0; a < 4; a++)
                    {
                        val = 0.0f;
                        for (int x = b, y = a; y < 12; y += 4)
                        {
                            val += s1[x++] * s2[y];
                        }

                        p[index++] = val;
                    }
                }

                p[3]  += s1[3];
                p[7]  += s1[7];
                p[11] += s1[11];
            }
        }
Exemple #2
0
        public static Matrix34 ScaleMatrix(float x, float y, float z)
        {
            Matrix34 m = new Matrix34();
            float *  p = (float *)&m;

            p[0]  = x;
            p[5]  = y;
            p[10] = z;
            return(m);
        }
Exemple #3
0
        public static Matrix34 TranslationMatrix(float x, float y, float z)
        {
            Matrix34 m = new Matrix34();
            float *  p = (float *)&m;

            p[3]  = x;
            p[7]  = y;
            p[11] = z;
            p[0]  = p[5] = p[10] = 1.0f;
            return(m);
        }
Exemple #4
0
        public static Matrix34 EnvironmentTexMtx()
        {
            Matrix34 m = Identity;

            m[0]  = 0.5f;
            m[3]  = 0.5f;
            m[5]  = -0.5f;
            m[7]  = 0.5f;
            m[10] = 0.0f;
            m[11] = 1.0f;
            return(m);
        }
Exemple #5
0
        public static Matrix34 TextureMatrix(TextureFrameState state)
        {
            Matrix34 m = Identity;

            if (state.Flags != 7)
            {
                MtxArray[state.Indirect ? 0 : 1 + ((int)state.MatrixMode).Clamp(0, 2) * 7 + state.Flags]((float *)&m,
                                                                                                         state);
            }

            return(m);
        }
Exemple #6
0
        public Matrix34 GetTranslation()
        {
            Matrix34 m = Identity;
            float *  p = (float *)&m;

            fixed(float *s = _data)
            {
                p[3]  = s[3];
                p[7]  = s[7];
                p[11] = s[11];
            }

            return(m);
        }
Exemple #7
0
        public static Matrix34 RotationMatrix(float x, float y, float z)
        {
            float cosx = (float)Math.Cos(x / 180.0f * Math.PI);
            float sinx = (float)Math.Sin(x / 180.0f * Math.PI);
            float cosy = (float)Math.Cos(y / 180.0f * Math.PI);
            float siny = (float)Math.Sin(y / 180.0f * Math.PI);
            float cosz = (float)Math.Cos(z / 180.0f * Math.PI);
            float sinz = (float)Math.Sin(z / 180.0f * Math.PI);

            Matrix34 m = Identity;
            float *  p = (float *)&m;

            p[5]  = cosx;
            p[6]  = -sinx;
            p[9]  = sinx;
            p[10] = cosx;

            Matrix34 m2 = Identity;
            float *  p2 = (float *)&m2;

            p2[0]  = cosy;
            p2[2]  = siny;
            p2[8]  = -siny;
            p2[10] = cosy;

            Matrix34 m3 = Identity;
            float *  p3 = (float *)&m3;

            p3[0] = cosz;
            p3[1] = -sinz;
            p3[4] = sinz;
            p3[5] = cosz;

            m.Multiply(&m2);
            m.Multiply(&m3);

            //p[0] = cosy * cosz;
            //p[1] = cosy * sinz;
            //p[2] = -siny;
            //p[4] = (sinx * siny * cosz - cosx * sinz);
            //p[5] = (sinx * siny * sinz + cosx * cosz);
            //p[6] = sinx * cosy;
            //p[8] = (cosx * siny * cosz + sinx * sinz);
            //p[9] = (cosx * siny * sinz - sinx * cosz);
            //p[10] = cosx * cosy;

            return(m);
        }
Exemple #8
0
        public static Matrix34 RotationMatrixRX(float x)
        {
            Matrix34 m = new Matrix34();
            float *  p = (float *)&m;

            float cosx = (float)Math.Cos(x / 180.0f * Math.PI);
            float sinx = (float)Math.Sin(x / 180.0f * Math.PI);

            p[0]  = 1.0f;
            p[5]  = cosx;
            p[6]  = sinx;
            p[9]  = -sinx;
            p[10] = cosx;

            return(m);
        }
Exemple #9
0
        public static Matrix34 RotationMatrixRY(float y)
        {
            Matrix34 m = new Matrix34();
            float *  p = (float *)&m;

            float cosy = (float)Math.Cos(y / 180.0f * Math.PI);
            float siny = (float)Math.Sin(y / 180.0f * Math.PI);

            p[5] = 1.0f;

            p[0]  = cosy;
            p[2]  = -siny;
            p[8]  = siny;
            p[10] = cosy;

            return(m);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            Matrix34 m = new Matrix34();

            string s = value?.ToString() ?? "";

            string[] arr = s.Split(LanguageCheck.DecimalDelimiters, StringSplitOptions.RemoveEmptyEntries);

            if (arr.Length == 12)
            {
                float.TryParse(arr[0], out m._data[0]);
                float.TryParse(arr[1], out m._data[1]);
                float.TryParse(arr[2], out m._data[2]);
                float.TryParse(arr[3], out m._data[3]);
                float.TryParse(arr[4], out m._data[4]);
                float.TryParse(arr[5], out m._data[5]);
                float.TryParse(arr[6], out m._data[6]);
                float.TryParse(arr[7], out m._data[7]);
                float.TryParse(arr[8], out m._data[8]);
                float.TryParse(arr[9], out m._data[9]);
                float.TryParse(arr[10], out m._data[10]);
                float.TryParse(arr[11], out m._data[11]);
            }
            else if (arr.Length == 1 && float.TryParse(arr[0], out float f))
            {
                m._data[0]  = f;
                m._data[1]  = f;
                m._data[2]  = f;
                m._data[3]  = f;
                m._data[4]  = f;
                m._data[5]  = f;
                m._data[6]  = f;
                m._data[7]  = f;
                m._data[8]  = f;
                m._data[9]  = f;
                m._data[10] = f;
                m._data[11] = f;
            }

            return(m);
        }
Exemple #11
0
        /// <summary>
        /// This function returns a texture matrix
        /// that will aim the texture to the midpoint between the active camera
        /// and the given reference camera or light.
        /// </summary>
        public static Matrix EnvSpecMap(
            int refCam,
            int refLight,
            SCN0Node node,
            ModelPanelViewport v,
            float frame)
        {
            // Normal environmental map when neither the light nor the camera is specified.
            Matrix34 finalMtx = EnvironmentTexMtx();

            GLCamera cam = v.Camera;
            Vector3  vLook, camUp, camLook;
            Matrix   camMtx    = cam._matrixInverse;
            Matrix   invCamMtx = cam._matrix;

            Matrix34 m34 = (Matrix34)camMtx;

            camLook._x = -m34[8];
            camLook._y = -m34[9];
            camLook._z = -m34[10];

            if (refLight >= 0)
            {
                Vector3 lgtLook = GetLightLook(node, refLight, invCamMtx, v, frame, out bool specEnabled);

                // Specular light is already set as a vector taking the center position.
                if (!specEnabled)
                {
                    vLook = GetHalfAngle(camLook, lgtLook);
                }
                else
                {
                    vLook = -lgtLook;
                }

                if (Math.Abs(vLook._x) < 0.000001f &&
                    Math.Abs(vLook._z) < 0.000001f)
                {
                    camUp._x = camUp._y = 0.0f;
                    if (vLook._y <= 0.0f)
                    {
                        // Look straight down
                        camUp._z = -1.0f;
                    }
                    else
                    {
                        // Look straight up
                        camUp._z = 1.0f;
                    }
                }
                else
                {
                    camUp._x = camUp._z = 0.0f;
                    camUp._y = 1.0f;
                }
            }
            else if (refCam >= 0)
            {
                SCN0CameraNode camNode = null;

                if (node?.CameraGroup != null && refCam < node.CameraGroup.Children.Count)
                {
                    camNode = (SCN0CameraNode)node.CameraGroup.Children[refCam];
                }
                else
                {
                    camNode = new SCN0CameraNode();
                }

                camNode.GetModelViewMatrix(frame, out Matrix cM, out Matrix cMInv);

                // Map from the midpoint of the view camera and the specified camera.
                Matrix34 lgtCam = (Matrix34)cM;
                camUp._x = lgtCam[4];
                camUp._y = lgtCam[5];
                camUp._z = lgtCam[6];
                Vector3 lgtLook = new Vector3(-lgtCam[8], -lgtCam[9], -lgtCam[10]);

                vLook = GetHalfAngle(camLook, lgtLook);
            }
            else
            {
                return((Matrix)finalMtx);
            }

            vLook.Normalize();
            Vector3 vRight, vUp;

            vUp = (vRight = vLook.Cross(camUp).Normalize()).Cross(vLook);
            m34 = new Matrix34(
                vRight._x, vRight._y, vRight._z, 0.0f,
                vUp._x, vUp._y, vUp._z, 0.0f,
                vLook._x, vLook._y, vLook._z, 0.0f);

            m34     = (Matrix34)((Matrix)m34 * invCamMtx);
            m34[3]  = 0.0f;
            m34[7]  = 0.0f;
            m34[11] = 0.0f;

            return((Matrix)(finalMtx * m34));
        }
Exemple #12
0
        public static Matrix EnvLightMap(int refLight, SCN0Node node, ModelPanelViewport v, float frame)
        {
            Matrix   m         = Matrix.Identity;
            GLCamera cam       = v.Camera;
            Matrix   camMtx    = cam._matrix;
            Matrix   invCamMtx = cam._matrixInverse;

            Matrix34 envMtx = new Matrix34(
                0.5f, 0.0f, 0.0f, 0.5f,
                0.0f, -0.5f, 0.0f, 0.5f,
                0.0f, 0.0f, 0.0f, 1.0f);

            //If no light is referenced, use the BrawlCrate built-in light
            if (refLight < 0 || node?.LightGroup != null && refLight >= node.LightGroup.Children.Count)
            {
                refLight = 0;
                node     = null;
            }

            // The light position and direction needs to be transformed with the camera's inverse matrix.
            Vector3 vLook, camUp, vRight, vUp;

            vLook = GetLightLook(node, refLight, invCamMtx, v, frame, out bool specEnabled).Normalize();

            // Calculate without using a target because the margin of error for calculations must be taken into account when the light is far away.
            // Take the absolute value as a measure against transformation margin.
            if (Math.Abs(vLook._x) < 0.000001f &&
                Math.Abs(vLook._z) < 0.000001f)
            {
                camUp._x = camUp._y = 0.0f;
                if (vLook._y <= 0.0f)
                {
                    // Look straight down
                    camUp._z = -1.0f;
                }
                else
                {
                    // Look straight up
                    camUp._z = 1.0f;
                }
            }
            else
            {
                camUp._x = camUp._z = 0.0f;
                camUp._y = 1.0f;
            }

            vUp = (vRight = vLook.Cross(camUp).Normalize()).Cross(vLook);

            Matrix34 m34 = new Matrix34(
                vRight._x, vRight._y, vRight._z, 0.0f,
                vUp._x, vUp._y, vUp._z, 0.0f,
                -vLook._x, -vLook._y, -vLook._z, 0.0f);

            m34 = (Matrix34)((Matrix)m34 * invCamMtx);

            m34[3]  = 0.0f;
            m34[7]  = 0.0f;
            m34[11] = 0.0f;

            return((Matrix)(envMtx * m34));

            //return (Matrix)envMtx * Matrix.RotationMatrix(new Vector3().LookatAngles((Vector3)v._posLight) * Maths._rad2degf);
        }
Exemple #13
0
        internal void Scale(float x, float y, float z)
        {
            Matrix34 m = ScaleMatrix(x, y, z);

            Multiply(&m);
        }
Exemple #14
0
        internal void Rotate(float x, float y, float z)
        {
            Matrix34 m = RotationMatrix(x, y, z);

            Multiply(&m);
        }
Exemple #15
0
        public void Translate(float x, float y, float z)
        {
            Matrix34 m = TranslationMatrix(x, y, z);

            Multiply(&m);
        }