Exemplo n.º 1
0
        public static CMatrix4x4 GetRotateZ(float r)
        {
            CMatrix4x4 rm = new CMatrix4x4();

            rm.Identity();
            rm[0, 0] = (float)(System.Math.Cos(r));
            rm[0, 1] = (float)(-System.Math.Sin(r));
            //
            rm[1, 0] = (float)(System.Math.Sin(r));
            rm[1, 1] = (float)(System.Math.Cos(r));
            return(rm);
        }
        public static CMatrix4x4 GetRotateX(float r)
        {
            CMatrix4x4 rm = new CMatrix4x4();
            rm.Identity();
            rm[1, 1] = (float)(System.Math.Cos(r));
            rm[1, 2] = (float)(-System.Math.Sin(r));
            //

            rm[2, 1] = (float)(System.Math.Sin(r));
            rm[2, 2] = (float)(System.Math.Cos(r));
            return rm;
        }
 /// <summary>
 /// 获取旋转矩阵
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="z"></param>
 /// <param name="r">弧度</param>
 /// <returns></returns>
 public static CMatrix4x4 GetRotate(float x, float y, float z, float r)
 {
     CMatrix4x4 rm = new CMatrix4x4();
     rm.Identity();
     rm[0, 0] = (float)(x * x * (1 - System.Math.Cos(r)) + System.Math.Cos(r));
     rm[0, 1] = (float)(x * y * (1 - System.Math.Cos(r)) +  z * System.Math.Sin(r));
     rm[0, 2] = (float)(x * z * (1 - System.Math.Cos(r)) - y * System.Math.Sin(r));
     //
     rm[1, 0] = (float)(y * x * (1 - System.Math.Cos(r)) - z * System.Math.Sin(r));
     rm[1, 1] = (float)(y * y * (1 - System.Math.Cos(r)) + System.Math.Cos(r));
     rm[1, 2] = (float)(y * z * (1 - System.Math.Cos(r)) + z * System.Math.Sin(r));
     //
     rm[2, 0] = (float)(z * x * (1 - System.Math.Cos(r)) + z * System.Math.Sin(r));
     rm[2, 1] = (float)(z * y * (1 - System.Math.Cos(r)) - System.Math.Sin(r));
     rm[2, 2] = (float)(z * z * (1 - System.Math.Cos(r)) + z * System.Math.Cos(r));
     return rm;
 }
Exemplo n.º 4
0
        /// <summary>
        /// 获取旋转矩阵
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <param name="r">弧度</param>
        /// <returns></returns>
        public static CMatrix4x4 GetRotate(float x, float y, float z, float r)
        {
            CMatrix4x4 rm = new CMatrix4x4();

            rm.Identity();
            rm[0, 0] = (float)(x * x * (1 - System.Math.Cos(r)) + System.Math.Cos(r));
            rm[0, 1] = (float)(x * y * (1 - System.Math.Cos(r)) + z * System.Math.Sin(r));
            rm[0, 2] = (float)(x * z * (1 - System.Math.Cos(r)) - y * System.Math.Sin(r));
            //
            rm[1, 0] = (float)(y * x * (1 - System.Math.Cos(r)) - z * System.Math.Sin(r));
            rm[1, 1] = (float)(y * y * (1 - System.Math.Cos(r)) + System.Math.Cos(r));
            rm[1, 2] = (float)(y * z * (1 - System.Math.Cos(r)) + z * System.Math.Sin(r));
            //
            rm[2, 0] = (float)(z * x * (1 - System.Math.Cos(r)) + z * System.Math.Sin(r));
            rm[2, 1] = (float)(z * y * (1 - System.Math.Cos(r)) - System.Math.Sin(r));
            rm[2, 2] = (float)(z * z * (1 - System.Math.Cos(r)) + z * System.Math.Cos(r));
            return(rm);
        }
        public static void Test()
        {
            CVector3D a = new CVector3D(1, 2, 1,1);
            CVector3D b = new CVector3D(5, 6, 0,1);
            CVector3D c = new CVector3D(1, 2, 3, 1);

            float r1 = CVector3D.Dot(a,b);
            CVector3D r2 = a - b;
            CVector3D r3 = CVector3D.Cross(a,b);
            Console.WriteLine("a dot b:{0}", r1);
            Console.WriteLine("a - b:({0},{1},{2},{3})", r2.x, r2.y, r2.z, r2.w);
            Console.WriteLine("a X b:({0},{1},{2},{3})", r3.x, r3.y, r3.z, r3.w);
            //
            CMatrix4x4 mat1 = new CMatrix4x4(1,2,3,4,
                                            1,2,3,4,
                                            1,2,3,4,
                                            0,0,0,1);
            CMatrix4x4 mat2 = new CMatrix4x4(1, 2, 3, 4,
                                            1, 2, 3, 4,
                                            1, 2, 3, 4,
                                            1, 2, 3, 4);
            CMatrix4x4 mat3 = new CMatrix4x4();
            mat3.Identity();
            CMatrix4x4 mat4 = new CMatrix4x4(1, 0, 0, 0,
                                           0, 1, 0, 0,
                                           0, 0, 1, 0,
                                           1, 2, 3, 1);
            CMatrix4x4 matr1 = mat1 * mat3;
            Console.WriteLine("mat1 * mat3:");
            showMat(matr1);
            CMatrix4x4 matr2 = mat1 * mat2;
            Console.WriteLine("mat1 * mat2:");
            showMat(matr2);
            CVector3D r4 = a * mat1;
            Console.WriteLine("a * mat1:({0},{1},{2},{3})", r4.x, r4.y, r4.z, r4.w);

            CVector3D r5 = a * mat4;
            Console.WriteLine("a * mat4:({0},{1},{2},{3})", r5.x, r5.y, r5.z, r5.w);
        }
Exemplo n.º 6
0
 private void timer1_Tick(object sender, EventArgs e)
 {
     lock (_frameBuff)
     {
         ClearBuff();
         CMatrix4x4 m = new CMatrix4x4();
         m.Identity();
         m[3, 2] = 5;
         rot += 0.1f;
         m = MathUntil.GetRotateY(rot) * m;
         CMatrix4x4 v = MathUntil.GetView(new CVector3D(0, 0, 0, 1), new CVector3D(0, 0, 1, 1), new CVector3D(0, 1, 0, 1));
         CMatrix4x4 p = MathUntil.GetProjection((float)System.Math.PI / 4, this.MaximumSize.Width / (float)this.MaximumSize.Height, 1f, 500f);
         //
         Draw(m, v, p);
         //
         if (g1 == null)
         {
             g1 = this.CreateGraphics();
         }
         g1.Clear(System.Drawing.Color.Black);
         g1.DrawImage(_frameBuff, 0, 0);
     }
     this.Invalidate();
 }