Esempio n. 1
0
        /*
         * Subdivides or unsubdivides this quad based on the current
         * viewer distance to this quad, relatively to its size. This
         * method uses the current viewer position provided by the
         * TerrainNode to which this quadtree belongs.
         */
        public void Update()
        {
            Frustum.VISIBILTY v = (m_parent == null) ? Frustum.VISIBILTY.PARTIALLY : m_parent.GetVisible();

            if (v == Frustum.VISIBILTY.PARTIALLY)
            {
                m_visible = m_owner.GetVisibility(m_localBox);
            }
            else
            {
                m_visible = v;
            }

            // here we reuse the occlusion test from the previous frame:
            // if the quad was found unoccluded in the previous frame, we suppose it is
            // still unoccluded at this frame. If it was found occluded, we perform
            // an occlusion test to check if it is still occluded.
            if (m_visible != Frustum.VISIBILTY.INVISIBLE && m_occluded)
            {
                m_occluded = m_owner.IsOccluded(m_localBox);

                if (m_occluded)
                {
                    m_visible = Frustum.VISIBILTY.INVISIBLE;
                }
            }

            double ground = m_owner.GetView().GetGroundHeight();
            double dist   = m_owner.GetCameraDist(new Box3d(m_ox, m_ox + m_length, m_oy, m_oy + m_length, Math.Min(0.0, ground), Math.Max(0.0, ground)));

            if ((m_owner.GetSplitInvisibleQuads() || m_visible != Frustum.VISIBILTY.INVISIBLE) && dist < m_length * m_owner.GetSplitDist() && m_level < m_owner.GetMaxLevel())
            {
                if (IsLeaf())
                {
                    Subdivide();
                }

                int[]  order = new int[4];
                double ox    = m_owner.GetLocalCameraPos().x;
                double oy    = m_owner.GetLocalCameraPos().y;
                double cx    = m_ox + m_length / 2.0;
                double cy    = m_oy + m_length / 2.0;

                if (oy < cy)
                {
                    if (ox < cx)
                    {
                        order[0] = 0;
                        order[1] = 1;
                        order[2] = 2;
                        order[3] = 3;
                    }
                    else
                    {
                        order[0] = 1;
                        order[1] = 0;
                        order[2] = 3;
                        order[3] = 2;
                    }
                }
                else
                {
                    if (ox < cx)
                    {
                        order[0] = 2;
                        order[1] = 0;
                        order[2] = 3;
                        order[3] = 1;
                    }
                    else
                    {
                        order[0] = 3;
                        order[1] = 1;
                        order[2] = 2;
                        order[3] = 0;
                    }
                }

                m_children[order[0]].Update();
                m_children[order[1]].Update();
                m_children[order[2]].Update();
                m_children[order[3]].Update();

                // we compute a more precise occlusion for the next frame (see above),
                // by combining the occlusion status of the child nodes
                m_occluded = (m_children[0].GetOccluded() && m_children[1].GetOccluded() && m_children[2].GetOccluded() && m_children[3].GetOccluded());
            }
            else
            {
                if (m_visible != Frustum.VISIBILTY.INVISIBLE)
                {
                    // we add the bounding box of this quad to the occluders list
                    m_occluded = m_owner.AddOccluder(m_localBox);
                    if (m_occluded)
                    {
                        m_visible = Frustum.VISIBILTY.INVISIBLE;
                    }
                }

                if (!IsLeaf())
                {
                    Release();
                }
            }
        }