public static void showVector3(Vector3D v)
 {
     if (isEnable)
     {
         Console.WriteLine("[{0},{1},{2},{3}]", v.x, v.y, v.z, v.w);
     }
 }
 public static Vector3D operator +(Vector3D lhs, Vector3D rhs)
 {
     Vector3D v = new Vector3D();
     v.x = lhs.x + rhs.x;
     v.y = lhs.y + rhs.y;
     v.z = lhs.z + rhs.z;
     v.w = 0;
     return v;
 }
 /// <summary>
 /// 向量变换
 /// </summary>
 /// <param name="lhs"></param>
 /// <param name="rhs"></param>
 /// <returns></returns>
 public static Vector3D operator *(Vector3D lhs, Matrix4x4 rhs)
 {
     Vector3D v = new Vector3D();
     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 void Test()
        {
            Vector3D a = new Vector3D(1, 2, 1,1);
            Vector3D b = new Vector3D(5, 6, 0,1);
            Vector3D c = new Vector3D(1, 2, 3, 1);

            float r1 = Vector3D.Dot(a,b);
            Vector3D r2 = a - b;
            Vector3D r3 = Vector3D.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);
            //
            Matrix4x4 mat1 = new Matrix4x4(1,2,3,4,
                                            1,2,3,4,
                                            1,2,3,4,
                                            0,0,0,1);
            Matrix4x4 mat2 = new Matrix4x4(1, 2, 3, 4,
                                            1, 2, 3, 4,
                                            1, 2, 3, 4,
                                            1, 2, 3, 4);
            Matrix4x4 mat3 = new Matrix4x4();
            mat3.Identity();
            Matrix4x4 mat4 = new Matrix4x4(1, 0, 0, 0,
                                           0, 1, 0, 0,
                                           0, 0, 1, 0,
                                           1, 2, 3, 1);

            Matrix4x4 mat5 = new Matrix4x4(1, 2, 3, 4,
                                           4, 3, 2, 1,
                                           0, -1, 2, 0,
                                           1, 6, 4, -2);
            Console.WriteLine("mat5   Determinate:" + mat5.Determinate());
            Console.WriteLine("mat5   GetAdjoint:");
            showMat(mat5.GetAdjoint());

            Console.WriteLine("mat5   Inverse:" );
            showMat(mat5.Inverse());

            Console.WriteLine("mat5 *  mat5 Inverse:");
            showMat(mat5 * mat5.Inverse());

            Console.WriteLine("mat2   Transpose:");
            showMat(mat2.Transpose());

            Matrix4x4 matr1 = mat1 * mat3;
            Console.WriteLine("mat1 * mat3:");
            showMat(matr1);
            Matrix4x4 matr2 = mat1 * mat2;
            Console.WriteLine("mat1 * mat2:");
            showMat(matr2);
            Vector3D r4 = a * mat1;
            Console.WriteLine("a * mat1:({0},{1},{2},{3})", r4.x, r4.y, r4.z, r4.w);

            Vector3D r5 = a * mat4;
            Console.WriteLine("a * mat4:({0},{1},{2},{3})", r5.x, r5.y, r5.z, r5.w);
        }
Пример #5
0
 public Camera(Vector3D pos, Vector3D lookAt, Vector3D up, float fov, float aspect, float zn, float zf)
 {
     this.pos = pos;
     this.lookAt = lookAt;
     this.up = up;
     this.fov = fov;
     this.aspect = aspect;
     this.zn = zn;
     this.zf = zf;
 }
Пример #6
0
 public Vertex(Vertex v)
 {
     point = v.point;
     normal = v.normal;
     this.vcolor = v.vcolor;
     onePerZ = 1;
     this.u = v.u;
     this.v = v.v;
     this.lightingColor = v.lightingColor;
 }
Пример #7
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="pointList">顶点位置列表</param>
 /// <param name="indexs">顶点索引列表</param>
 /// <param name="Uvs">uv坐标列表</param>
 /// <param name="vertColor">顶点色列表</param>
 public Mesh(Vector3D[] pointList, int[] indexs, Point2D[] Uvs, Vector3D[] vertColors, Vector3D[] normals, Material mat)
 {
     _verts = new Vertex[indexs.Length];
     //生成顶点列表
     for (int i = 0; i < indexs.Length; i++)
     {
         int pointIndex = indexs[i];
         Vector3D point = pointList[pointIndex];
         _verts[i] = new Vertex(point, normals[i], Uvs[i].x, Uvs[i].y, vertColors[i].x, vertColors[i].y, vertColors[i].z);
     }
     _mat = mat;
 }
Пример #8
0
 public Vertex(Vector3D point, Vector3D normal,float u, float v, float r, float g, float b)
 {
     this.point = point;
     this.normal = normal;
     this.point.w = 1;
     vcolor = new Color();
     vcolor.r = r;
     vcolor.g = g;
     vcolor.b = b;
     onePerZ = 1;
     this.u = u;
     this.v = v;
     lightingColor = new Color();
     lightingColor.r = 1;
     lightingColor.g = 1;
     lightingColor.b = 1;
 }
Пример #9
0
        /// <summary>
        /// 实现了“基础光照模型”,在世界空间进行顶点光照处理
        /// </summary>
        /// <param name="v"></param>
        private void Lighting(Matrix4x4 m, Vector3D worldEyePositon ,ref Vertex v)
        {
            Vector3D worldPoint = v.point * m;//世界空间顶点位置
            Vector3D normal = v.normal * m.Inverse().Transpose();//模型空间法线乘以世界矩阵的逆转置得到世界空间法线
            normal = normal.Normalize();
            SoftRenderer.RenderData.Color emissiveColor = _mesh.material.emissive;//自发光
            SoftRenderer.RenderData.Color ambientColor = _ambientColor * _mesh.material.ka;//环境光

            Vector3D inLightDir = (_light.worldPosition - worldPoint).Normalize();
            float diffuse = Vector3D.Dot(normal, inLightDir);
            if(diffuse < 0)
            {
                diffuse = 0;
            }
            SoftRenderer.RenderData.Color diffuseColor = _mesh.material.diffuse * diffuse * _light.lightColor;//漫反射
            //
            Vector3D inViewDir = (worldEyePositon - worldPoint).Normalize();
            Vector3D h = (inViewDir + inLightDir).Normalize();
            float specular = 0;
            if(diffuse != 0)
            {//防止出现光源在物体背面产生高光的情况
                specular = (float)System.Math.Pow(MathUntil.Range(Vector3D.Dot(h, normal), 0, 1), _mesh.material.shininess);
            }
            SoftRenderer.RenderData.Color specularColor = _mesh.material.specular * specular * _light.lightColor;//镜面高光
            //
            v.lightingColor = emissiveColor + ambientColor + diffuseColor + specularColor;
        }
Пример #10
0
 /// <summary>
 /// 点乘
 /// </summary>
 /// <param name="lhs"></param>
 /// <param name="rhs"></param>
 /// <returns></returns>
 public static float Dot(Vector3D lhs,Vector3D rhs)
 {
     return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
 }
Пример #11
0
 /// <summary>
 /// 叉乘
 /// </summary>
 /// <param name="lhs"></param>
 /// <param name="rhs"></param>
 /// <returns></returns>
 public static Vector3D Cross(Vector3D lhs, Vector3D 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 Vector3D(x, y, z, 0);
 }
Пример #12
0
 public Light(Vector3D worldPosition, Color lightColor)
 {
     this.worldPosition = worldPosition;
     this.lightColor = lightColor;
 }