float CalculatePatchDistance(Patch p)
 {
     int centerX = (p.worldX + p.worldX+PATCH_SIZE) >> 1;
     int centerY = (p.worldY + p.worldX + PATCH_SIZE) >> 1;
     int centerZ = FetchFunction(centerX, centerY);
     Vector3 center = new Vector3(centerX, centerZ, centerY);
     float distance = (center - CameraPos).Length();
     return distance;
 }
        bool IsPatchVisible(Patch p)
        {
            bool b = VisibleFunction(p.worldX, p.worldY + this.PATCH_SIZE,
                                p.worldX + this.PATCH_SIZE, p.worldY,
                                p.worldX, p.worldY);
            b |= VisibleFunction(p.worldX + this.PATCH_SIZE, p.worldY,
                                p.worldX, p.worldY + this.PATCH_SIZE,
                                p.worldX + this.PATCH_SIZE, p.worldY + this.PATCH_SIZE);

            return b;
        }
        public void Init()
        {
            MaxTris = NUM_PATCHES_PER_SIDE * NUM_PATCHES_PER_SIDE * 4;
            nodeList = new TriTreeNode[MaxTris];

            for (int i = 0; i < nodeList.Length; i++)
            {
                nodeList[i] = new TriTreeNode();
                nodeList[i].Self = i;
            }

            Patches = new Patch[NUM_PATCHES_PER_SIDE, NUM_PATCHES_PER_SIDE];

            for(int y = 0; y < NUM_PATCHES_PER_SIDE; y++)
                for (int x = 0; x < NUM_PATCHES_PER_SIDE; x++)
                {
                    Patch p = new Patch(this);
                    p.Init(x*PATCH_SIZE, y * PATCH_SIZE, x * PATCH_SIZE, y * PATCH_SIZE);
                    Patches[y, x] = p;
                }
            this.Reset();
            this.RecreateVertices();
        }