Пример #1
0
        public BVHTriangleObject(Vector3 p1, Vector3 p2, Vector3 p3)
        {
            mP1     = p1;
            mP2     = p2;
            mP3     = p3;
            mCenter = (mP1 + mP2 + mP3) * 0.33333f;
            Vector3 min = Vector3.Min(Vector3.Min(mP1, mP2), mP3);
            Vector3 max = Vector3.Max(Vector3.Max(mP1, mP2), mP3);

            mBox    = new BVHBox(min, max);
            mNormal = Vector3.Cross(mP2 - mP1, mP3 - mP1);
            mNormal.Normalize();
        }
Пример #2
0
 public void ExpandToInclude(BVHBox b)
 {
     mMin        = Vector3.Min(mMin, b.mMin);
     mMax        = Vector3.Max(mMax, b.mMax);
     mExtentSize = mMax - mMin;
 }
Пример #3
0
 public BVHAABBObject(Vector3 min, Vector3 max)
 {
     mAABB = new BVHBox(min, max);
 }
Пример #4
0
        private void Build()
        {
            int  stackptr     = 0;
            uint Untouched    = 0xffffffff;
            uint TouchedTwice = 0xfffffffd;

            PREALLOC[stackptr].mStart  = 0;
            PREALLOC[stackptr].mEnd    = (uint)mBuildPrims.Count;
            PREALLOC[stackptr].mParent = 0xfffffffc;
            stackptr++;
            List <BVHFlatNode> buildnodes = new List <BVHFlatNode>(mBuildPrims.Count * 2);

            while (stackptr > 0)
            {
                BVHBuildEntry bnode  = PREALLOC[--stackptr];
                uint          start  = bnode.mStart;
                uint          end    = bnode.mEnd;
                uint          nPrims = end - start;
                mNumNodes++;
                BVHFlatNode node = new BVHFlatNode();
                node.mStartIndex  = start;
                node.mLeafCount   = nPrims;
                node.mRightOffset = Untouched;
                BVHBox bb = new BVHBox(mBuildPrims[(int)start].GetBBox().mMin, mBuildPrims[(int)start].GetBBox().mMax);
                BVHBox bc = new BVHBox(mBuildPrims[(int)start].GetCentroid());
                for (uint p = start + 1; p < end; ++p)
                {
                    bb.ExpandToInclude(mBuildPrims[(int)p].GetBBox());
                    bc.ExpandToInclude(mBuildPrims[(int)p].GetCentroid());
                }
                node.mBox = bb;
                if (nPrims <= mNodeMaxLeafSize)
                {
                    node.mRightOffset = 0;
                    mNumLeafs++;
                }

                buildnodes.Add(node);
                // 记录父节点关于右孩子结点相对父结点的偏移值mRightOffset
                // 第一次为左孩子,相对父结点的偏移值为1
                // 每个父节点最多被两次 hit
                if (bnode.mParent != 0xfffffffc)
                {
                    buildnodes[(int)bnode.mParent].mRightOffset--;
                    if (buildnodes[(int)bnode.mParent].mRightOffset == TouchedTwice)
                    {
                        buildnodes[(int)bnode.mParent].mRightOffset = (uint)mNumNodes - 1 - bnode.mParent;
                    }
                }
                if (node.mRightOffset == 0)
                {
                    continue;
                }
                // 选择合适的分割维度
                uint  split_dim   = (uint)bc.MaxDimension();
                float split_coord = 0.5f * (bc.mMin[(int)split_dim] + bc.mMax[(int)split_dim]);
                uint  mid         = start;
                // 交换 start 和 end 之间 的数据
                for (uint i = start; i < end; ++i)
                {
                    if (mBuildPrims[(int)i].GetCentroid()[(int)split_dim] < split_coord)
                    {
                        BVHObject temp = mBuildPrims[(int)i];
                        mBuildPrims[(int)i]   = mBuildPrims[(int)mid];
                        mBuildPrims[(int)mid] = temp;
                        ++mid;
                    }
                }
                if (mid == start || mid == end)
                {
                    mid = start + (end - start) / 2;
                }
                // 右孩子
                PREALLOC[stackptr].mStart  = mid;
                PREALLOC[stackptr].mEnd    = end;
                PREALLOC[stackptr].mParent = (uint)mNumNodes - 1;
                stackptr++;
                // 左孩子
                PREALLOC[stackptr].mStart  = start;
                PREALLOC[stackptr].mEnd    = mid;
                PREALLOC[stackptr].mParent = (uint)mNumNodes - 1;
                stackptr++;
            }
            if (mFlatTreeList != null)
            {
                mFlatTreeList.Clear();
            }
            mFlatTreeList = new List <BVHFlatNode>(mNumNodes);
            for (uint n = 0; n < mNumNodes; ++n)
            {
                mFlatTreeList.Add(buildnodes[(int)n]);
            }
        }