public virtual void Init(byte[] hMap)
 {
     Patch_cpp patch;
     m_HeightMap = hMap;
     for (int y = 0; y < NUM_PATCHES_PER_SIDE; y++)
         for (int x = 0; x < NUM_PATCHES_PER_SIDE; x++)
         {
             patch = new Patch_cpp();
             m_Patches[y, x] = patch;
             patch.Init(x * PATCH_SIZE, y * PATCH_SIZE, x * PATCH_SIZE, y * PATCH_SIZE, hMap);
             patch.ComputeVariance();
         }
     for (int i = 0; i < m_TriPool.Length; i++)
         m_TriPool[i] = new TriTreeNode_cpp();
 }
        // The static half of the Patch Class
        public virtual void Init(int heightX, int heightY, int worldX, int worldY, byte[] hMap)
        {
            m_BaseLeft = Land.AllocateTri();
            m_BaseRight = Land.AllocateTri();

            m_BaseLeft.BaseNeighbour = m_BaseRight;
            m_BaseRight.BaseNeighbour = m_BaseLeft;

            m_WorldX = worldX;
            m_WorldY = worldY;

            m_HeightMap = hMap;

            m_VarianceDirty = true;
            m_isVisible = false;
        }
        public virtual void RecursTessellate(TriTreeNode_cpp tri, int leftX, int leftY, int rightX, int rightY, int apexX, int apexY, int node)
        {
            float TriVariance = 0;
            int centreX = (leftX + rightX) >> 1;
            int centreY = (leftY + rightY) >> 1;

            if (node < (1 << VARIANCE_DEPTH))
            {
                float distance = (new Vector3((float)centreX, 0, (float)centreY) - Landscape_cpp.gViewPosition).Length();
                TriVariance = ((float)m_CurrentVariance[node] * Landscape_cpp.MAP_SIZE * 2) / distance;
            }

            if((node >= (1<<VARIANCE_DEPTH)) ||
            (TriVariance > Landscape_cpp.gFrameVariance))
            {
                Split(tri);

                if (tri.LeftChild != null &&
                    ((Math.Abs(leftX - rightX) >= 3) || (Math.Abs(leftY - rightY) >= 3)))
                {
                    RecursTessellate(tri.LeftChild, apexX, apexY, leftX, leftY, centreX, centreY, node << 1);
                    RecursTessellate(tri.RightChild, rightX, rightX, apexX, apexY, centreX, centreY, 1 + (node << 1));
                }
            }
        }
        // The recursive half of the Patch Class
        public virtual void Split(TriTreeNode_cpp tri)
        {
            if (tri.LeftChild != null)
                return;

            if (tri.BaseNeighbour != null && (tri.BaseNeighbour.BaseNeighbour != tri))
                Split(tri.BaseNeighbour);

            //tri.LeftChild = Landscape.AllocateTri();
            //tri.RightChild = Landscape.AllocateTri();
            tri.LeftChild = new TriTreeNode_cpp();
            tri.RightChild = new TriTreeNode_cpp();

            if (tri.RightChild == null)
                return;

            tri.LeftChild.BaseNeighbour = tri.LeftNeighbour;
            tri.LeftNeighbour.LeftNeighbour = tri.RightChild;

            tri.RightChild.BaseNeighbour = tri.RightNeighbour;
            tri.RightChild.RightNeighbour = tri.LeftChild;

            if (tri.LeftNeighbour != null)
            {
                if (tri.LeftNeighbour.BaseNeighbour == tri)
                    tri.LeftNeighbour.BaseNeighbour = tri.LeftChild;
                else if (tri.LeftNeighbour.LeftNeighbour == tri)
                    tri.LeftNeighbour.LeftNeighbour = tri.LeftChild;
                else if (tri.LeftNeighbour.RightNeighbour == tri)
                    tri.LeftNeighbour.RightNeighbour = tri.LeftChild;
                else
                    throw new Exception("Illegal Left Neighbour");
            }
            if (tri.RightNeighbour != null)
            {
                if (tri.RightNeighbour.BaseNeighbour == tri)
                    tri.RightNeighbour.BaseNeighbour = tri.RightChild;
                else if (tri.RightNeighbour.RightNeighbour == tri)
                    tri.RightNeighbour.RightNeighbour = tri.RightChild;
                else if (tri.RightNeighbour.LeftNeighbour == tri)
                    tri.RightNeighbour.LeftNeighbour = tri.RightChild;
                else
                    throw new Exception("Illegal Right Neighbour");
            }

            if (tri.BaseNeighbour != null)
            {
                if (tri.BaseNeighbour.LeftChild != null)
                {
                    tri.BaseNeighbour.LeftChild.RightNeighbour = tri.RightChild;
                    tri.BaseNeighbour.RightChild.LeftNeighbour = tri.LeftChild;
                    tri.LeftChild.RightNeighbour = tri.BaseNeighbour.RightChild;
                    tri.RightChild.LeftNeighbour = tri.BaseNeighbour.LeftChild;
                }
                else
                    Split(tri.BaseNeighbour); // Base neighbour, in a diamond with us. Split it.
            }
            else
            {
                // Edge.
                tri.LeftChild.RightNeighbour = null;
                tri.RightChild.LeftNeighbour = null;
            }
        }
 public virtual void RecursRender(TriTreeNode_cpp tri, int leftX, int leftY, int rightX, int rightY, int apexX, int apexY)
 {
     throw new NotImplementedException();
 }