Beispiel #1
0
        public static Vector3 GetBBCenter(this IHitable hitable)
        {
            var bb = hitable.GetGenericBoundingBox();

            return(new Vector3(
                       (bb.Min.X + bb.Max.X) / 2,
                       (bb.Min.Y + bb.Max.Y) / 2,
                       (bb.Min.Z + bb.Max.Z) / 2
                       ));
        }
Beispiel #2
0
        public BVHNode(params IHitable[] children)
        {
            if (children.Length == 0)
            {
                return;
            }

            AxisSelector.Axis      sortingAxis    = AxisSelector.GetMaxVarianceAxis(children);
            IEnumerable <IHitable> sortedChildren = children.OrderBy(child =>
            {
                var bbCenter = child.GetBBCenter();
                switch (sortingAxis)
                {
                case AxisSelector.Axis.X:
                    return(bbCenter.X);

                case AxisSelector.Axis.Y:
                    return(bbCenter.Y);

                case AxisSelector.Axis.Z:
                    return(bbCenter.Z);

                default:
                    return(0);
                }
            });

            switch (children.Length)
            {
            case 1:
                Left = Right = children[0];
                break;

            case 2:
                Left  = children[0];
                Right = children[1];
                break;

            default:
                int firstHalfSize = children.Length / 2;
                Left  = new BVHNode(sortedChildren.Take(firstHalfSize).ToArray());
                Right = new BVHNode(sortedChildren.Skip(firstHalfSize).ToArray());
                break;
            }

            _boundingBox = new BoundingBox(Left.GetGenericBoundingBox(), Right.GetGenericBoundingBox());
        }