Example #1
0
        public float Trace(Ray ray)
        {
            List <ShapeNode> allShapeNodes = new List <ShapeNode>();

            _scene.Root.GetChildren(allShapeNodes, true);

            float bestDist = -1;

            foreach (var v in allShapeNodes)
            {
                float dist = v.Trace(ray);
                if (dist >= 0)
                {
                    if (bestDist < 0)
                    {
                        bestDist = dist;
                    }
                    else
                    {
                        bestDist = SMath.Min(bestDist, dist);
                    }
                }
            }
            return(bestDist);
        }
Example #2
0
        public override float Trace(Ray ray)
        {
            //{ { t1 = - o.d - Sqrt[r ^ 2 + (o.d) ^ 2 - o.o]},{ t2 = - o.d + Sqrt[r ^ 2 + (o.d) ^ 2 - o.o]} }
            // o = ray's origin
            // d = ray's direction
            // r = sphere radius

            float o_dot_d = ray._origin.DotProduct(ray._direction);
            float o_dot_o = ray._origin.DotProduct(ray._origin);
            float r2      = _radius * _radius;

            if (o_dot_d <= 0)
            {
                float delta = r2 + o_dot_d * o_dot_d - o_dot_o;
                if (delta >= 0)
                {
                    float sqrtDelta = SMath.Sqrt(delta);
                    float t1        = -o_dot_d - sqrtDelta;

                    if (t1 < 0)
                    {
                        float t2 = -o_dot_d + sqrtDelta;
                        if (t2 >= 0)
                        {
                            return(t2);
                        }
                    }
                    return(t1);
                }
            }

            return(-1.0f);
        }
Example #3
0
        public bool Normalize(float tolerance = SMath.SmallNumber)
        {
            float squareSum = DotProduct(this);

            if (squareSum > tolerance)
            {
                float scale = SMath.InvSqrt(squareSum);
                x *= scale; y *= scale; z *= scale;
                return(true);
            }
            return(false);
        }
Example #4
0
        public Ray GenerateRay(float x, float y)
        {
            float fovHalfRadianY = SMath.ToRadian(_camera.FOV * 0.5f);
            float fovHalfRadianX = fovHalfRadianY * _camera.Aspect;
            float sx             = SMath.Tan(fovHalfRadianX) * _camera.Near * x;
            float sy             = SMath.Tan(fovHalfRadianY) * _camera.Near * y;

            Vector3 o       = _cameraTransform.Translation;
            Vector3 right   = _cameraTransform.RightVector;
            Vector3 forward = _cameraTransform.ForwardVector;
            Vector3 up      = _cameraTransform.UpVector;
            float   d       = _camera.Near;
            Vector3 dir     = right * sx + up * sy + forward * d;

            dir.Normalize();
            return(new Ray(o, dir));
        }