예제 #1
0
파일: BVH.cs 프로젝트: ByteKay/Nullspace
        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]);
            }
        }
예제 #2
0
파일: BVH.cs 프로젝트: ByteKay/Nullspace
        /// <summary>
        ///
        /// </summary>
        /// <param name="ray">射线</param>
        /// <param name="intersection">交点信息</param>
        /// <param name="occlusion">是否找到最短的。  true if 找到交叉就行; false if 找到最短的 </param>
        /// <returns></returns>
        public bool GetIntersection(BVHRay ray, ref BVHIntersectionInfo intersection, bool occlusion)
        {
            intersection.mLength = 999999999.0f;
            intersection.mObject = null;
            float[] bbhits = new float[4];
            int     closer, other;

            BVHTraversal[] todo = new BVHTraversal[64];
            todo[0] = new BVHTraversal();
            int stackptr = 0;

            todo[stackptr].mIndex  = 0;
            todo[stackptr].mLength = -9999999.0f;
            while (stackptr >= 0)
            {
                int   ni   = todo[stackptr].mIndex;
                float near = todo[stackptr].mLength;
                stackptr--;
                BVHFlatNode node = mFlatTreeList[ni];
                // 对叶节点做相交测试
                if (node.mRightOffset == 0)
                {
                    bool hit = false;
                    for (int o = 0; o < node.mLeafCount; ++o)
                    {
                        BVHIntersectionInfo current = new BVHIntersectionInfo();
                        BVHObject           obj     = mBuildPrims[(int)node.mStartIndex + o];
                        hit = obj.GetIntersection(ref ray, ref current);
                        if (hit)
                        {
                            if (occlusion)
                            {
                                return(true);
                            }
                            if (current.mLength < intersection.mLength)
                            {
                                intersection = current;
                            }
                        }
                    }
                }
                else
                {
                    // 对父结点做测试
                    bool hitc0 = mFlatTreeList[ni + 1].mBox.Intersect(ray, ref bbhits[0], ref bbhits[1]);
                    bool hitc1 = mFlatTreeList[ni + (int)node.mRightOffset].mBox.Intersect(ray, ref bbhits[2], ref bbhits[3]);
                    if (hitc0 && hitc1)
                    {
                        closer = ni + 1;
                        other  = ni + (int)node.mRightOffset;
                        if (bbhits[2] < bbhits[0])
                        {
                            float temp = bbhits[0];
                            bbhits[0] = bbhits[2];
                            bbhits[2] = temp;
                            temp      = bbhits[1];
                            bbhits[1] = bbhits[3];
                            bbhits[3] = temp;
                            int itemp = closer;
                            closer = other;
                            other  = itemp;
                        }
                        todo[++stackptr] = new BVHTraversal(other, bbhits[2]);
                        todo[++stackptr] = new BVHTraversal(closer, bbhits[0]);
                    }
                    else if (hitc0)
                    {
                        todo[++stackptr] = new BVHTraversal(ni + 1, bbhits[0]);
                    }
                    else if (hitc1)
                    {
                        todo[++stackptr] = new BVHTraversal(ni + (int)node.mRightOffset, bbhits[2]);
                    }
                }
            }
            if (intersection.mObject != null)
            {
                intersection.mHitPoint = ray.mOrigin + ray.mDirection * intersection.mLength;
            }
            return(intersection.mObject != null);
        }
예제 #3
0
파일: BVH.cs 프로젝트: Hengle/NullspaceKay
        private void Build()
        {
            int stackptr = 0;

            mNumNodes = mNumLeafs = 0;
            uint Untouched    = 0xffffffff;
            uint TouchedTwice = 0xfffffffd;

            PREALLOC[stackptr].Start  = 0;
            PREALLOC[stackptr].End    = (uint)mBuildPrims.Count;
            PREALLOC[stackptr].Parent = 0xfffffffc;
            stackptr++;
            List <BVHFlatNode> buildnodes = new List <BVHFlatNode>(mBuildPrims.Count * 2);

            while (stackptr > 0)
            {
                BVHBuildEntry bnode  = PREALLOC[--stackptr];
                uint          start  = bnode.Start;
                uint          end    = bnode.End;
                uint          nPrims = end - start;
                BVHFlatNode   node   = new BVHFlatNode();
                node.StartIndex  = start;
                node.LeafCount   = nPrims;
                node.RightOffset = Untouched;
                BVHBox bb = new BVHBox(mBuildPrims[(int)start].GetBBox().Min, mBuildPrims[(int)start].GetBBox().Max);
                BVHBox bc = new BVHBox(mBuildPrims[(int)start].GetCentroid());
                // 计算 bnode 的 BBox
                for (uint p = start + 1; p < end; ++p)
                {
                    bb.ExpandToInclude(mBuildPrims[(int)p].GetBBox());
                    bc.ExpandToInclude(mBuildPrims[(int)p].GetCentroid());
                }
                node.Box = bb;
                // 节点数小于叶节点设置的数量,不继续划分,叶节点数量增1
                if (nPrims <= mNodeMaxLeafSize)
                {
                    node.RightOffset = 0;
                    mNumLeafs++;
                }
                // 添加
                mNumNodes++; // 实际上为 buildnodes 的Count
                buildnodes.Add(node);
                // 记录父节点关于右孩子结点相对父结点的偏移值mRightOffset
                // 第一次为左孩子,相对父结点的偏移值为1
                // 每个父节点最多被两次 hit
                // 后面入栈:先右孩子再左孩子
                // 执行出站:先出左孩子再出右孩子
                // 父节点最开始设置为 RightOffset = Untouched = 0xffffffff
                // 执行到 左孩子时 RightOffset -= 1 = 0xfffffffe
                // 执行到 右孩子时 RightOffset -= 1 = 0xfffffffd = TouchedTwice
                if (bnode.Parent != 0xfffffffc)
                {
                    buildnodes[(int)bnode.Parent].RightOffset--;
                    if (buildnodes[(int)bnode.Parent].RightOffset == TouchedTwice)
                    {
                        // 此时处理的是右孩子,记录右孩子的起始索引
                        buildnodes[(int)bnode.Parent].RightOffset = (uint)mNumNodes - 1 - bnode.Parent;
                    }
                }

                // 叶节点
                if (node.RightOffset == 0)
                {
                    continue;
                }
                // 选择合适的分割维度
                uint split_dim = (uint)bc.MaxDimension();
                // 计算分割值
                float split_coord = 0.5f * (bc.Min[(int)split_dim] + bc.Max[(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].Start  = mid;
                PREALLOC[stackptr].End    = end;
                PREALLOC[stackptr].Parent = (uint)mNumNodes - 1;
                stackptr++;
                // 左孩子
                PREALLOC[stackptr].Start  = start;
                PREALLOC[stackptr].End    = mid;
                PREALLOC[stackptr].Parent = (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]);
            }
        }