Ejemplo n.º 1
0
        public void TrySolveSimpleQuadraticEquation()
        {
            var mat = new Matrix4X4()
            {
                Cells = new double[4, 4]
                {
                    { -1, 0, 0, 0 },
                    { 0, -1, 0, 0 },
                    { 0, 0, 1, 0 },
                    { 0, 0, 0, -2 }
                }
            };

            var v = new Vector4D(1, 1, 0, 1);

            var z = ImplicitEllipsoidEquationSolver.SolveZ(v, mat);

            Assert.IsTrue(z.HasValue);
            Assert.AreEqual(-2.0, z.Value, 0.05);
        }
Ejemplo n.º 2
0
        private Color ShootRay(int x, int y, int maxX, int maxY)
        {
            var aspect = (double)maxX / maxY;

            var rayx = aspect * (2.0 * x / maxX - 1.0);
            var rayy = (double)y / maxY * 2.0 - 1.0;

            var a2 = EllipsoidA * EllipsoidA;
            var b2 = EllipsoidB * EllipsoidB;
            var c2 = EllipsoidC * EllipsoidC;

            var matrix = new Matrix4X4()
            {
                Cells = new double[4, 4]
                {
                    { 1 / a2, 0, 0, 0 },
                    { 0, 1 / b2, 0, 0 },
                    { 0, 0, 1 / c2, 0 },
                    { 0, 0, 0, -1 }
                }
            };

            var inversedWorld = _worldTransformation.GaussianInverse();
            var dm            = inversedWorld.Transpose() * matrix * inversedWorld;

            var vec = new Vector4D(rayx, rayy, 0.0, 1.0);

            var foundZ = ImplicitEllipsoidEquationSolver.SolveZ(vec, dm);

            if (!foundZ.HasValue)
            {
                return(Colors.Black);
            }

            var crossPoint = new Vector4D(rayx, rayy, foundZ.Value, 1.0);
            var normal     = ImplicitEllipsoidEquationSolver.CalculateNormal(crossPoint, dm);
            var lightVec   = new Vector4D(0.0, 0.0, -1.0, 0.0);
            var dot        = Math.Max(0, normal.DotProduct(lightVec));

            dot = Math.Pow(dot, Exponent);

            var material = Color.FromRgb(255, 255, 0);

            //var foundzcolor = (byte) ((foundZ.Value + 1.0)*0.5*255.0);
            //return Color.FromRgb(foundzcolor, foundzcolor, foundzcolor);

            if (DisplayMode == DisplayMode.OnlyNormals)
            {
                var nx = (normal.X + 1.0) * 0.5;
                var ny = (normal.Y + 1.0) * 0.5;
                var nz = (normal.Z + 1.0) * 0.5;
                return(Color.FromRgb((byte)(255.0 * nx), (byte)(255.0 * ny), (byte)(255.0 * nz)));
            }

            if (DisplayMode == DisplayMode.LightingOnly)
            {
                material = Colors.White;
            }

            return(Color.FromRgb((byte)(dot * material.R), (byte)(dot * material.G), (byte)(dot * material.B)));
        }