public static void LinkLeftAndRightPatches( TerrainPatch leftPatch, TerrainPatch rightPatch )
 {
     if ( leftPatch != null )
     {
         leftPatch.RightPatch = rightPatch;
     }
     if ( rightPatch != null )
     {
         rightPatch.LeftPatch = leftPatch;
     }
 }
 public static void LinkTopAndBottomPatches( TerrainPatch topPatch, TerrainPatch bottomPatch )
 {
     if ( topPatch != null )
     {
         topPatch.BottomPatch = bottomPatch;
     }
     if ( bottomPatch != null )
     {
         bottomPatch.TopPatch = topPatch;
     }
 }
        private bool BuildConnectingStripIndexBuffer( TerrainPatch neighbour, Side side, ICollection<int> indices )
        {
            if ( neighbour == null )
            {
                return false;
            }

            if ( LodLevel <= neighbour.LodLevel )
            {
                //	Current patch has higher detail than neighbour patch
                return false;
            }
            int index;
            int nextIndexOffset;
            int neighbourIndex;
            int neighbourNextIndexOffset;

            GetSideVars( side, out index, out nextIndexOffset, 1 );
            neighbour.GetSideVars( OppositeSide( side ), out neighbourIndex, out neighbourNextIndexOffset, 0 );

            int startError = ( int )Functions.Pow( 2, LodLevel - neighbour.LodLevel );
            int error = startError;
            for ( int count = 1; count < neighbour.m_Size; ++count )
            {
                if ( error == 0 )
                {
                    indices.Add( neighbourIndex );
                    indices.Add( index );
                    indices.Add( index + nextIndexOffset );

                    index += nextIndexOffset;
                    error = startError;
                }
                indices.Add( neighbourIndex );
                indices.Add( index );
                indices.Add( neighbourIndex + neighbourNextIndexOffset );

                neighbourIndex += neighbourNextIndexOffset;
                --error;
            }
            indices.Add( neighbourIndex );
            indices.Add( index );
            indices.Add( index + nextIndexOffset );

            return true;
        }
            /// <summary>
            /// Setup constructor
            /// </summary>
            public CubeSide( int res, Point3 topLeft, Point3 topRight, Point3 bottomLeft, int defaultPatchLodLevel, bool defaultVisibility )
            {
                if ( ( res % 2 ) == 0 )
                {
                    throw new ArgumentException( "Cube side resolution must be odd" );
                }

                m_Resolution = res;
                m_Patches = new TerrainPatch[ res, res ];

                Point3 rowStart = topLeft;
                Vector3 xInc = ( topRight - topLeft ) / res;
                Vector3 yInc = ( bottomLeft - topLeft ) / res;

                //	Create all the terrain patches and set their bounds
                for ( int row = 0; row < res; ++row )
                {
                    Point3 curPos = rowStart;
                    for ( int col = 0; col < res; ++col )
                    {
                        TerrainPatch newPatch = new TerrainPatch( );
                        newPatch.LodLevel = defaultPatchLodLevel;
                        newPatch.SetBounds( curPos, curPos + xInc, curPos + yInc );
                        m_Patches[ col, row ] = newPatch;
                        curPos += xInc;
                    }
                    rowStart += yInc;
                }

                //	Link patches (this does not link edge patches to other cube side patches - this is done
                //	by calling LinkToSide())
                int finalCol = res - 1;
                int finalRow = res - 1;
                for ( int row = 0; row < res; ++row )
                {
                    for ( int col = 0; col < res; ++col )
                    {
                        TerrainPatch curPatch = m_Patches[ col, row ];
                        curPatch.LeftPatch = col == 0 ? null : m_Patches[ col - 1, row ];
                        curPatch.TopPatch = row == 0 ? null : m_Patches[ col, row - 1 ];
                        curPatch.RightPatch = col == finalCol ? null : m_Patches[ col + 1, row ];
                        curPatch.BottomPatch = row == finalRow ? null : m_Patches[ col, row + 1 ];
                    }
                }

                Visible = defaultVisibility;
            }