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);
        }
예제 #2
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;
        }