public static void showVector3(CVector3D v)
 {
     if (isEnable)
     {
         Console.WriteLine("[{0},{1},{2},{3}]", v.x, v.y, v.z, v.w);
     }
 }
Beispiel #2
0
        /// <summary>
        /// 叉乘
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs"></param>
        /// <returns></returns>
        public static CVector3D Cross(CVector3D lhs, CVector3D rhs)
        {
            float x = lhs.y * rhs.z - lhs.z * rhs.y;
            float y = lhs.z * rhs.x - lhs.x * rhs.z;
            float z = lhs.x * rhs.y - lhs.y * rhs.x;

            return(new CVector3D(x, y, z, 1));
        }
 /// <summary>
 /// 向量变换
 /// </summary>
 /// <param name="lhs"></param>
 /// <param name="rhs"></param>
 /// <returns></returns>
 public static CVector3D operator *(CVector3D lhs, CMatrix4x4 rhs)
 {
     CVector3D v = new CVector3D();
     v.x = lhs.x * rhs[0, 0] + lhs.y * rhs[1, 0] + lhs.z * rhs[2, 0] + lhs.w * rhs[3, 0];
     v.y = lhs.x * rhs[0, 1] + lhs.y * rhs[1, 1] + lhs.z * rhs[2, 1] + lhs.w * rhs[3, 1];
     v.z = lhs.x * rhs[0, 2] + lhs.y * rhs[1, 2] + lhs.z * rhs[2, 2] + lhs.w * rhs[3, 2];
     v.w = lhs.x * rhs[0, 3] + lhs.y * rhs[1, 3] + lhs.z * rhs[2, 3] + lhs.w * rhs[3, 3];
     return v;
 }
 public static CVector3D operator -(CVector3D lhs, CVector3D rhs)
 {
     CVector3D v = new CVector3D();
     v.x = lhs.x - rhs.x;
     v.y = lhs.y - rhs.y;
     v.z = lhs.z - rhs.z;
     v.w = 0;
     return v;
 }
 public CVertex(float x, float y, float z, float u, float v, float r, float g, float b)
 {
     point   = new CVector3D(x, y, z, 1);
     color.r = r;
     color.g = g;
     color.b = b;
     onePerZ = 1;
     this.u  = u;
     this.v  = v;
 }
 public CVertex(CVertex v)
 {
     point   = new CVector3D(v.point.x, v.point.y, v.point.z, 1);
     color.r = v.color.r;
     color.g = v.color.g;
     color.b = v.color.b;
     onePerZ = 1;
     this.u  = v.u;
     this.v  = v.v;
 }
Beispiel #7
0
        /// <summary>
        /// 向量变换
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs"></param>
        /// <returns></returns>
        public static CVector3D operator *(CVector3D lhs, CMatrix4x4 rhs)
        {
            CVector3D v = new CVector3D();

            v.x = lhs.x * rhs[0, 0] + lhs.y * rhs[1, 0] + lhs.z * rhs[2, 0] + lhs.w * rhs[3, 0];
            v.y = lhs.x * rhs[0, 1] + lhs.y * rhs[1, 1] + lhs.z * rhs[2, 1] + lhs.w * rhs[3, 1];
            v.z = lhs.x * rhs[0, 2] + lhs.y * rhs[1, 2] + lhs.z * rhs[2, 2] + lhs.w * rhs[3, 2];
            v.w = lhs.x * rhs[0, 3] + lhs.y * rhs[1, 3] + lhs.z * rhs[2, 3] + lhs.w * rhs[3, 3];
            return(v);
        }
Beispiel #8
0
        public static CVector3D operator -(CVector3D lhs, CVector3D rhs)
        {
            CVector3D v = new CVector3D();

            v.x = lhs.x - rhs.x;
            v.y = lhs.y - rhs.y;
            v.z = lhs.z - rhs.z;
            v.w = 0;
            return(v);
        }
 public CVertex(CVertex v)
 {
     point = new CVector3D(v.point.x, v.point.y, v.point.z, 1);
     color.r = v.color.r;
     color.g = v.color.g;
     color.b = v.color.b;
     onePerZ = 1;
     this.u = v.u;
     this.v = v.v;
 }
 public CVertex(float x, float y, float z, float u, float v, float r, float g, float b)
 {
     point = new CVector3D(x, y, z, 1);
     color.r = r;
     color.g = g;
     color.b = b;
     onePerZ = 1;
     this.u = u;
     this.v = v;
 }
        /// <summary>
        /// 获取视矩阵
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="lookAt"></param>
        /// <param name="up"></param>
        /// <returns></returns>
        public static CMatrix4x4 GetView(CVector3D pos, CVector3D lookAt, CVector3D up)
        {
            //视线方向
            CVector3D dir   = lookAt - pos;
            CVector3D right = CVector3D.Cross(up, dir);

            right.Normalize();
            //平移部分
            CMatrix4x4 t = new CMatrix4x4(1, 0, 0, 0,
                                          0, 1, 0, 0,
                                          0, 0, 1, 0,
                                          -pos.x, -pos.y, -pos.z, 1);
            //旋转部分
            CMatrix4x4 r = new CMatrix4x4(right.x, up.x, dir.x, 0,
                                          right.y, up.y, dir.y, 0,
                                          right.z, up.z, dir.z, 0,
                                          0, 0, 0, 1);

            return(t * r);
        }
        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);
        }
 /// <summary>
 /// 点乘
 /// </summary>
 /// <param name="lhs"></param>
 /// <param name="rhs"></param>
 /// <returns></returns>
 public static float Dot(CVector3D lhs,CVector3D rhs)
 {
     return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
 }
 /// <summary>
 /// 叉乘
 /// </summary>
 /// <param name="lhs"></param>
 /// <param name="rhs"></param>
 /// <returns></returns>
 public static CVector3D Cross(CVector3D lhs, CVector3D rhs)
 {
     float x = lhs.y * rhs.z - lhs.z * rhs.y;
     float y = lhs.z * rhs.x - lhs.x * rhs.z;
     float z = lhs.x * rhs.y - lhs.y * rhs.x;
     return new CVector3D(x, y, z, 1);
 }
 /// <summary>
 /// 背面消隐
 /// </summary>
 /// <returns>是否通关背面消隐测试</returns>
 private bool BackFaceCulling(CVertex p1, CVertex p2, CVertex p3)
 {
     CVector3D v1 = p2.point - p1.point;
     CVector3D v2 = p3.point - p2.point;
     CVector3D normal = CVector3D.Cross(v1, v2);
     CVector3D viewDir = new CVector3D(0,0,1);//由于在剪裁空间,视线一点是这个方向
     if (CVector3D.Dot(normal,viewDir) > 0)
     {
         return true;
     }
     return false;
 }
Beispiel #16
0
 /// <summary>
 /// 点乘
 /// </summary>
 /// <param name="lhs"></param>
 /// <param name="rhs"></param>
 /// <returns></returns>
 public static float Dot(CVector3D lhs, CVector3D rhs)
 {
     return(lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z);
 }
 /// <summary>
 /// 获取视矩阵
 /// </summary>
 /// <param name="pos"></param>
 /// <param name="lookAt"></param>
 /// <param name="up"></param>
 /// <returns></returns>
 public static CMatrix4x4 GetView(CVector3D pos, CVector3D lookAt, CVector3D up)
 {
     //视线方向
     CVector3D dir = lookAt - pos;
     CVector3D right = CVector3D.Cross(up, dir);
     right.Normalize();
     //平移部分
     CMatrix4x4 t = new CMatrix4x4(1,0,0,0,
                                    0,1,0,0,
                                    0,0,1,0,
                                    -pos.x, -pos.y, -pos.z, 1);
     //旋转部分
     CMatrix4x4 r= new CMatrix4x4(right.x,up.x,dir.x,0,
                                    right.y,up.y,dir.y,0,
                                    right.z,up.z,dir.z,0,
                                    0, 0, 0, 1);
     return t * r;
 }
 public CVertex()
 {
     point = new CVector3D();
 }
 public CVertex()
 {
     point = new CVector3D();
 }