//  Use this constructor to build the octree
        public MyModelOctree(MyModel model)
        {
            // we can't use performance timer, because octree now loaded in parallel tasks
            //MyPerformanceTimer.OctreeBuilding.Start();

            m_model = model;

            //  Bounding box for the root node - get it from model (we need to trust the model that it will be good)
            m_rootNode = new MyModelOctreeNode(model.BoundingBox);

            //  Add model triangles into octree
            for (int i = 0; i < m_model.Triangles.Length; i++)
            {
                //  Add triangleVertexes to octree
                m_rootNode.AddTriangle(model, i, 0);
            }

            //  This method will look if node has all its childs null, and if yes, destroy childs array (saving memory + making traversal faster, because we don't need to traverse whole array)
            m_rootNode.OptimizeChilds();

            // we can't use performance timer, because octree now loaded in parallel tasks
            //MyPerformanceTimer.OctreeBuilding.End();
        }
Beispiel #2
0
        //  Use this constructor to build the octree
        public MyModelOctree(MyModel model)
        {
            // we can't use performance timer, because octree now loaded in parallel tasks
            //MyPerformanceTimer.OctreeBuilding.Start();

            m_model = model;

            //  Bounding box for the root node - get it from model (we need to trust the model that it will be good)
            m_rootNode = new MyModelOctreeNode(model.BoundingBox);            

            //  Add model triangles into octree
            for (int i = 0; i < m_model.Triangles.Length; i++)
            {
                //  Add triangleVertexes to octree
                m_rootNode.AddTriangle(model, i, 0);
            }

            //  This method will look if node has all its childs null, and if yes, destroy childs array (saving memory + making traversal faster, because we don't need to traverse whole array)
            m_rootNode.OptimizeChilds();

            // we can't use performance timer, because octree now loaded in parallel tasks
            //MyPerformanceTimer.OctreeBuilding.End();
        }
        //  Add triangleVertexes into this node or its child or child's child...
        public void AddTriangle(MyModel model, int triangleIndex, int recursiveLevel)
        {
            BoundingBox triangleBoundingBox = new BoundingBox();

            model.GetTriangleBoundingBox(triangleIndex, ref triangleBoundingBox);

            if (recursiveLevel != MAX_RECURSIVE_LEVEL)
            {
                //  If we didn't reach max recursive level, we look for child where triangleVertexes can be completely contained
                for (int i = 0; i < OCTREE_CHILDS_COUNT; i++)
                {
                    BoundingBox childBoundingBox = GetChildBoundingBox(m_boundingBox, i);

                    //  If child completely contains the triangleVertexes, we add it to this child (or its child...child).
                    if (childBoundingBox.Contains(triangleBoundingBox) == ContainmentType.Contains)
                    {
                        if (m_childs[i] == null)
                        {
                            m_childs[i] = new MyModelOctreeNode(childBoundingBox);
                        }

                        m_childs[i].AddTriangle(model, triangleIndex, recursiveLevel + 1);

                        // Child completely contains triangle, so also current bounding box must contain that triangle
                        m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Min);
                        m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Max);
                        return;
                    }
                }
            }

            //  If we get here, it was because we reached max recursive level or no child completely contained the triangleVertexes, so we add triangleVertexes to this node
            m_triangleIndices.Add(triangleIndex);
            m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Min);
            m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Max);
        }
        //  Add triangleVertexes into this node or its child or child's child...
        public void AddTriangle(MyModel model, int triangleIndex, int recursiveLevel)
        {
            BoundingBox triangleBoundingBox = new BoundingBox();
            model.GetTriangleBoundingBox(triangleIndex, ref triangleBoundingBox);

            if (recursiveLevel != MAX_RECURSIVE_LEVEL)
            {
                //  If we didn't reach max recursive level, we look for child where triangleVertexes can be completely contained
                for (int i = 0; i < OCTREE_CHILDS_COUNT; i++)
                {
                    BoundingBox childBoundingBox = GetChildBoundingBox(m_boundingBox, i);

                    //  If child completely contains the triangleVertexes, we add it to this child (or its child...child).
                    if (childBoundingBox.Contains(triangleBoundingBox) == ContainmentType.Contains)
                    {
                        if (m_childs[i] == null) m_childs[i] = new MyModelOctreeNode(childBoundingBox);

                        m_childs[i].AddTriangle(model, triangleIndex, recursiveLevel + 1);

                        // Child completely contains triangle, so also current bounding box must contain that triangle
                        m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Min);
                        m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Max);
                        return;
                    }
                }
            }

            //  If we get here, it was because we reached max recursive level or no child completely contained the triangleVertexes, so we add triangleVertexes to this node
            m_triangleIndices.Add(triangleIndex);
            m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Min);
            m_realBoundingBox = m_realBoundingBox.Include(ref triangleBoundingBox.Max);
        }