public void GenerateFractualSlice(Vector3 globalRot, float[,] border = null, Fractal.BorderType borders = Fractal.BorderType.None)
        {
            Fractal fracH = new Fractal(this.size, border, borders);
            this.heightMaps = fracH.heights;

            this.GlobalRot = new Vector4(globalRot, 0);

            createVertices();
            createIndices();
            genNormals();

            //vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
            //indexBuffer.SetData<int>(indices);
        }
        private List<float> savePosBorder(Fractal.BorderType border)
        {
            List<float> result = new List<float>();

            //ltrb
            int x = (border == Fractal.BorderType.Right ? this.Size - 2 : 0);
            x = (border == Fractal.BorderType.Left ? 1 : x);
            int y = (border == Fractal.BorderType.Bottom ? this.Size - 2 : 0);
            y = (border == Fractal.BorderType.Top ? 1 : y);

            int size = this.Size;
            bool m = border == Fractal.BorderType.Top || border == Fractal.BorderType.Bottom;
            for (int c = 0; c < size; c++)
            {
                float a = this.heightMaps[x, y];
                result.Add(a);

                if (m)
                    x++;
                else
                    y++;
            }

            return result;
        }
        public void GenerateFractual()
        {
            Fractal fracH = new Fractal(this.size);
            this.heightMaps = fracH.heights;

            createVertices();
            createIndices();
            genNormals();

            //vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
            //indexBuffer.SetData<int>(indices);
        }
        public void loadPosBorders(List<float> poss, Fractal.BorderType border, bool reverse)
        {
            if (reverse)
                poss.Reverse();

            if (border == Fractal.BorderType.Left)
            {
                this.OtherRawHeightBorders[0] = new List<float>(poss);
                this.neighbourBorders |= Fractal.BorderType.Left;
            }
            else if (border == Fractal.BorderType.Top)
            {
                this.OtherRawHeightBorders[1] = new List<float>(poss);
                this.neighbourBorders |= Fractal.BorderType.Top;
            }
            else if (border == Fractal.BorderType.Right)
            {
                this.OtherRawHeightBorders[2] = new List<float>(poss);
                this.neighbourBorders |= Fractal.BorderType.Right;
            }
            else if (border == Fractal.BorderType.Bottom)
            {
                this.OtherRawHeightBorders[3] = new List<float>(poss);
                this.neighbourBorders |= Fractal.BorderType.Bottom;
            }

            this.AdjustNormals(border);
        }
        public void GenerateFractualSlice(Vector3 globalRot, float[][] border = null, Fractal.BorderType borders = Fractal.BorderType.None)
        {
            Fractal fracH = new Fractal(this.Size, border, borders);
            this.heightMaps = fracH.heights;

            this.MyHeightBorders[0] = this.saveFracBorder(Fractal.BorderType.Left);
            this.MyHeightBorders[1] = this.saveFracBorder(Fractal.BorderType.Top);
            this.MyHeightBorders[2] = this.saveFracBorder(Fractal.BorderType.Right);
            this.MyHeightBorders[3] = this.saveFracBorder(Fractal.BorderType.Bottom);

            this.GlobalRot = new Vector4(globalRot, 0);
            this.InitialRot = globalRot;

            createVertices();
            createIndices();
            genNormals();

            setRawHeights();
        }
        public void GenerateFractual()
        {
            Fractal fracH = new Fractal(this.Size);
            this.heightMaps = fracH.heights;

            this.MyHeightBorders[0] = this.saveFracBorder(Fractal.BorderType.Left);
            this.MyHeightBorders[1] = this.saveFracBorder(Fractal.BorderType.Top);
            this.MyHeightBorders[2] = this.saveFracBorder(Fractal.BorderType.Right);
            this.MyHeightBorders[3] = this.saveFracBorder(Fractal.BorderType.Bottom);

            createVertices();
            createIndices();
            genNormals();

            setRawHeights();
        }
        public void AdjustNormals(Fractal.BorderType border)
        {
            //ltrb
            int x = (border == Fractal.BorderType.Right ? this.Size - 1 : 0);
            int y = (border == Fractal.BorderType.Bottom ? this.Size - 1 : 0);

            int bb = 0;
            if (border == Fractal.BorderType.Top)
                bb = 1;
            else if (border == Fractal.BorderType.Right)
                bb = 2;
            else if (border == Fractal.BorderType.Bottom)
                bb = 3;

            int size = this.Size;
            bool m = border == Fractal.BorderType.Top || border == Fractal.BorderType.Bottom;
            for (int c = 0; c < size - 1; c++)
            {
                Vector3 p0 = this.getSpherePos(x, y, this.heightMaps[x, y]);

                float h2 = this.OtherRawHeightBorders[bb][c];
                float h3 = this.OtherRawHeightBorders[bb][c + 1];

                Vector3 normal = new Vector3();
                if (border == Fractal.BorderType.Left)
                {
                    Vector3 p1 = this.getSpherePos(x, y + 1, this.heightMaps[x, y + 1]);
                    Vector3 p2 = this.getSpherePos(this.Size - 2, y, h2);
                    Vector3 p3 = this.getSpherePos(this.Size - 2, y + 1, h3);

                    Matrix mat = Matrix.CreateRotationX(MathHelper.ToRadians(0)) *
                                 Matrix.CreateRotationY(MathHelper.ToRadians(0)) *
                                 Matrix.CreateRotationZ(MathHelper.ToRadians(90));
                    p2 = Vector3.Transform(p2, mat);
                    p3 = Vector3.Transform(p3, mat);

                    normal = Vector3.Cross(p2 - p3, p2 - p0);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;

                    normal = Vector3.Cross(p3 - p1, p3 - p0);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x, y + 1)].Normal += normal;
                }
                else if (border == Fractal.BorderType.Top)
                {
                    Vector3 p1 = this.getSpherePos(x + 1, y, this.heightMaps[x + 1, y]);
                    Vector3 p2 = this.getSpherePos(x, this.Size - 2, h2);
                    Vector3 p3 = this.getSpherePos(x + 1, this.Size - 2, h3);

                    Matrix mat = Matrix.CreateRotationX(MathHelper.ToRadians(-90)) *
                                 Matrix.CreateRotationY(MathHelper.ToRadians(0)) *
                                 Matrix.CreateRotationZ(MathHelper.ToRadians(0));
                    p2 = Vector3.Transform(p2, mat);
                    p3 = Vector3.Transform(p3, mat);

                    normal = Vector3.Cross(p2 - p0, p2 - p3);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;

                    normal = Vector3.Cross(p0 - p1, p0 - p3);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x + 1, y)].Normal += normal;
                }
                else if (border == Fractal.BorderType.Right)
                {
                    Vector3 p1 = this.getSpherePos(x, y + 1, this.heightMaps[x, y + 1]);
                    Vector3 p2 = this.getSpherePos(1, y, h2);
                    Vector3 p3 = this.getSpherePos(1, y + 1, h3);

                    Matrix mat = Matrix.CreateRotationX(MathHelper.ToRadians(0)) *
                                 Matrix.CreateRotationY(MathHelper.ToRadians(0)) *
                                 Matrix.CreateRotationZ(MathHelper.ToRadians(-90));
                    p2 = Vector3.Transform(p2, mat);
                    p3 = Vector3.Transform(p3, mat);

                    normal = Vector3.Cross(p0 - p1, p0 - p2);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x, y + 1)].Normal += normal;

                    normal = Vector3.Cross(p1 - p3, p1 - p2);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y + 1)].Normal += normal;
                }
                else if (border == Fractal.BorderType.Bottom)
                {
                    Vector3 p1 = this.getSpherePos(x + 1, y, this.heightMaps[x + 1, y]);
                    Vector3 p2 = this.getSpherePos(x, 1, h2);
                    Vector3 p3 = this.getSpherePos(x + 1, 1, h3);

                    Matrix mat = Matrix.CreateRotationX(MathHelper.ToRadians(90)) *
                                 Matrix.CreateRotationY(MathHelper.ToRadians(0)) *
                                 Matrix.CreateRotationZ(MathHelper.ToRadians(0));
                    p2 = Vector3.Transform(p2, mat);
                    p3 = Vector3.Transform(p3, mat);

                    normal = Vector3.Cross(p0 - p2, p0 - p1);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x + 1, y)].Normal += normal;

                    normal = Vector3.Cross(p2 - p3, p2 - p1);
                    normal.Normalize();
                    this.vertices[convertFromXY(x + 1, y)].Normal += normal;
                }

                if (m)
                    x++;
                else
                    y++;
            }
        }
        public void AdjustChildNormals(Fractal.BorderType border)
        {
            //ltrb
            int x = (border == Fractal.BorderType.Right ? this.Size - 1 : 0);
            int y = (border == Fractal.BorderType.Bottom ? this.Size - 1 : 0);

            int bb = 0;
            if (border == Fractal.BorderType.Top)
                bb = 1;
            else if (border == Fractal.BorderType.Right)
                bb = 2;
            else if (border == Fractal.BorderType.Bottom)
                bb = 3;

            if (this.OtherRawHeightBorders[bb] == null)
                return;

            if(this.OtherRawHeightBorders[bb].Count != this.Size)
                this.OtherRawHeightBorders[bb] = this.estimateBoundary(this.OtherRawHeightBorders[bb]);

            int size = this.Size;
            bool m = border == Fractal.BorderType.Top || border == Fractal.BorderType.Bottom;

            for (int c = 0; c < size - 1; c++)
            {
                float h2 = this.OtherRawHeightBorders[bb][c];
                float h3 = this.OtherRawHeightBorders[bb][c + 1];

                Vector3 normal = new Vector3();
                if (border == Fractal.BorderType.Left)
                {
                    Vector3 p0 = this.getSpherePos(x, y, this.heightMaps[x, y]);
                    Vector3 p1 = this.getSpherePos(x, y + 1, this.heightMaps[x, y + 1]);
                    Vector3 p2 = this.getSpherePos(x - 2, y, h2);
                    Vector3 p3 = this.getSpherePos(x - 2, y + 1, h3);

                    normal = Vector3.Cross(p2 - p3, p2 - p0);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;

                    normal = Vector3.Cross(p3 - p1, p3 - p0);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x, y + 1)].Normal += normal;
                }
                else if (border == Fractal.BorderType.Top)
                {
                    Vector3 p0 = this.getSpherePos(x, y, this.heightMaps[x, y]);
                    Vector3 p1 = this.getSpherePos(x + 1, y, this.heightMaps[x + 1, y]);
                    Vector3 p2 = this.getSpherePos(x, y - 2, h2);
                    Vector3 p3 = this.getSpherePos(x + 1, y - 2, h3);

                    normal = Vector3.Cross(p2 - p0, p2 - p3);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;

                    normal = Vector3.Cross(p0 - p1, p0 - p3);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x + 1, y)].Normal += normal;
                }
                else if (border == Fractal.BorderType.Right)
                {
                    Vector3 p0 = this.getSpherePos(x, y, this.heightMaps[x, y]);
                    Vector3 p1 = this.getSpherePos(x, y + 1, this.heightMaps[x, y + 1]);
                    Vector3 p2 = this.getSpherePos(x + 2, y, h2);
                    Vector3 p3 = this.getSpherePos(x + 2, y + 1, h3);

                    normal = Vector3.Cross(p0 - p1, p0 - p2);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x, y + 1)].Normal += normal;

                    normal = Vector3.Cross(p1 - p3, p1 - p2);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y + 1)].Normal += normal;
                }
                else if (border == Fractal.BorderType.Bottom)
                {
                    Vector3 p0 = this.getSpherePos(x, y, this.heightMaps[x, y]);
                    Vector3 p1 = this.getSpherePos(x + 1, y, this.heightMaps[x + 1, y]);
                    Vector3 p2 = this.getSpherePos(x, y + 2, h2);
                    Vector3 p3 = this.getSpherePos(x + 1, y + 2, h3);

                    normal = Vector3.Cross(p0 - p2, p0 - p1);
                    normal.Normalize();
                    this.vertices[convertFromXY(x, y)].Normal += normal;
                    this.vertices[convertFromXY(x + 1, y)].Normal += normal;

                    normal = Vector3.Cross(p2 - p3, p2 - p1);
                    normal.Normalize();
                    this.vertices[convertFromXY(x + 1, y)].Normal += normal;
                }

                if (m)
                    x++;
                else
                    y++;
            }
        }