Example #1
0
    public void SolveQEF()
    {
        if (P == null)
        {
            return;
        }

        if (P.Count < 4)
        {
            qef = null;
            return;
        }

        qef = new QEF(P, N);
    }
Example #2
0
        public void GenerateAt(int x, int y)
        {
            int corners = 0;

            for (int i = 0; i < 4; i++)
            {
                if (map[x + i / 2, y + i % 2] < 0)
                {
                    corners |= 1 << i;
                }
            }

            if (corners == 0 || corners == 15)
            {
                return;
            }

            VertexPositionColor[] vs = new VertexPositionColor[10];
            Color c = Color.LightSteelBlue;

            vs[0] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 0) * Size, 0), c);
            vs[1] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 0) * Size, 0), c);
            vs[2] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 0) * Size, 0), c);
            vs[3] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 1) * Size, 0), c);
            vs[4] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 1) * Size, 0), c);
            vs[5] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 1) * Size, 0), c);
            vs[6] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 1) * Size, 0), c);
            vs[7] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 0) * Size, 0), c);
            OutlineBuffer.SetData <VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 8, VertexPositionColor.VertexDeclaration.VertexStride);
            OutlineLocation += 8;

            QEF     qef            = new QEF();
            Vector3 average_normal = new Vector3();

            for (int i = 0; i < 4; i++)
            {
                int c1 = edges[i, 0];
                int c2 = edges[i, 1];

                int m1 = (corners >> c1) & 1;
                int m2 = (corners >> c2) & 1;
                if (m1 == m2)
                {
                    continue;
                }

                float d1 = map[x + c1 / 2, y + c1 % 2];
                float d2 = map[x + c2 / 2, y + c2 % 2];

                Vector2 p1 = new Vector2((float)((c1 / 2)), (float)((c1 % 2)));
                Vector2 p2 = new Vector2((float)((c2 / 2)), (float)((c2 % 2)));

                Vector2 intersection = Sampler.GetIntersection(p1, p2, d1, d2);
                Vector2 normal       = Sampler.GetNormal(intersection + new Vector2(x, y));          //GetNormal(x, y);
                average_normal += new Vector3(normal, 0);

                qef.Add(intersection, normal);
            }

            average_normal /= (float)qef.Intersections.Count;
            average_normal.Normalize();

            vertices[x, y] = qef.Solve2(0, 16, 0);

            Vector2 p   = vertices[x, y];
            Color   n_c = new Color(average_normal * 0.5f + Vector3.One * 0.5f);
            VertexPositionColorNormal v = new VertexPositionColorNormal(new Vector3((p.X + x) * Size, (p.Y + y) * Size, 0), n_c, average_normal);

            Vertices.Add(v);
            vertex_indexes[x, y] = VertexCount;
            VertexCount++;
        }
Example #3
0
        public bool ConstructLeaf(ref int v_index, List<VertexPositionColorNormal> vertices, int grid_size)
        {
            int corners = 0;
            float[,] samples = new float[2, 2];
            for (int i = 0; i < 4; i++)
            {
                if ((samples[i / 2, i % 2] = Sampler.Sample(position + new Vector2(i / 2, i % 2))) < 0)
                    corners |= 1 << i;
            }

            if (corners == 0 || corners == 15)
                return false;

            QEF qef = new QEF();
            Vector3 average_normal = new Vector3();
            for (int i = 0; i < 4; i++)
            {
                int c1 = Sampler.Edges[i, 0];
                int c2 = Sampler.Edges[i, 1];

                int m1 = (corners >> c1) & 1;
                int m2 = (corners >> c2) & 1;
                if (m1 == m2)
                    continue;

                float d1 = samples[c1 / 2, c1 % 2];
                float d2 = samples[c2 / 2, c2 % 2];

                Vector2 p1 = new Vector2((float)((c1 / 2)), (float)((c1 % 2)));
                Vector2 p2 = new Vector2((float)((c2 / 2)), (float)((c2 % 2)));

                Vector2 intersection = Sampler.GetIntersection(p1, p2, d1, d2);
                Vector2 normal = Sampler.GetNormal(intersection + position);//GetNormal(x, y);
                average_normal += new Vector3(normal, 0);

                qef.Add(intersection, normal);
            }

            average_normal /= (float)qef.Intersections.Count;
            average_normal.Normalize();

            draw_info = new QuadtreeDrawInfo();
            draw_info.position = qef.Solve2(0, 0, 0);
            draw_info.averageNormal = average_normal;
            draw_info.corners = corners;
            draw_info.index = v_index++;
            Color n_c = new Color(average_normal * 0.5f + Vector3.One * 0.5f);
            vertices.Add(new VertexPositionColorNormal(new Vector3(position * grid_size + draw_info.position * size * grid_size, 0), n_c, average_normal));

            type = QuadtreeNodeType.Leaf;
            return true;
        }
Example #4
0
        public void GenerateAt(int x, int y)
        {
            int corners = 0;
            for (int i = 0; i < 4; i++)
            {
                if (map[x + i / 2, y + i % 2] < 0)
                    corners |= 1 << i;
            }

            if (corners == 0 || corners == 15)
                return;

            VertexPositionColor[] vs = new VertexPositionColor[10];
            Color c = Color.LightSteelBlue;
            vs[0] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 0) * Size, 0), c);
            vs[1] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 0) * Size, 0), c);
            vs[2] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 0) * Size, 0), c);
            vs[3] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 1) * Size, 0), c);
            vs[4] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 1) * Size, 0), c);
            vs[5] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 1) * Size, 0), c);
            vs[6] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 1) * Size, 0), c);
            vs[7] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 0) * Size, 0), c);
            OutlineBuffer.SetData<VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 8, VertexPositionColor.VertexDeclaration.VertexStride);
            OutlineLocation += 8;

            QEF qef = new QEF();
            Vector3 average_normal = new Vector3();
            for (int i = 0; i < 4; i++)
            {
                int c1 = edges[i, 0];
                int c2 = edges[i, 1];

                int m1 = (corners >> c1) & 1;
                int m2 = (corners >> c2) & 1;
                if (m1 == m2)
                    continue;

                float d1 = map[x + c1 / 2, y + c1 % 2];
                float d2 = map[x + c2 / 2, y + c2 % 2];

                Vector2 p1 = new Vector2((float)((c1 / 2)), (float)((c1 % 2)));
                Vector2 p2 = new Vector2((float)((c2 / 2)), (float)((c2 % 2)));

                Vector2 intersection = Sampler.GetIntersection(p1, p2, d1, d2);
                Vector2 normal = Sampler.GetNormal(intersection + new Vector2(x, y));//GetNormal(x, y);
                average_normal += new Vector3(normal, 0);

                qef.Add(intersection, normal);
            }

            average_normal /= (float)qef.Intersections.Count;
            average_normal.Normalize();

            vertices[x, y] = qef.Solve2(0, 16, 0);

            Vector2 p = vertices[x, y];
            Color n_c = new Color(average_normal * 0.5f + Vector3.One * 0.5f);
            VertexPositionColorNormal v = new VertexPositionColorNormal(new Vector3((p.X + x) * Size, (p.Y + y) * Size, 0), n_c, average_normal);
            Vertices.Add(v);
            vertex_indexes[x, y] = VertexCount;
            VertexCount++;
        }
Example #5
0
        public bool ConstructLeaf(ref int v_index, List <VertexPositionColorNormal> vertices, int grid_size)
        {
            int corners = 0;

            float[,] samples = new float[2, 2];
            for (int i = 0; i < 4; i++)
            {
                if ((samples[i / 2, i % 2] = Sampler.Sample(position + new Vector2(i / 2, i % 2))) < 0)
                {
                    corners |= 1 << i;
                }
            }

            if (corners == 0 || corners == 15)
            {
                return(false);
            }

            QEF     qef            = new QEF();
            Vector3 average_normal = new Vector3();

            for (int i = 0; i < 4; i++)
            {
                int c1 = Sampler.Edges[i, 0];
                int c2 = Sampler.Edges[i, 1];

                int m1 = (corners >> c1) & 1;
                int m2 = (corners >> c2) & 1;
                if (m1 == m2)
                {
                    continue;
                }

                float d1 = samples[c1 / 2, c1 % 2];
                float d2 = samples[c2 / 2, c2 % 2];

                Vector2 p1 = new Vector2((float)((c1 / 2)), (float)((c1 % 2)));
                Vector2 p2 = new Vector2((float)((c2 / 2)), (float)((c2 % 2)));

                Vector2 intersection = Sampler.GetIntersection(p1, p2, d1, d2);
                Vector2 normal       = Sampler.GetNormal(intersection + position);          //GetNormal(x, y);
                average_normal += new Vector3(normal, 0);

                qef.Add(intersection, normal);
            }

            average_normal /= (float)qef.Intersections.Count;
            average_normal.Normalize();

            draw_info               = new QuadtreeDrawInfo();
            draw_info.position      = qef.Solve2(0, 0, 0);
            draw_info.averageNormal = average_normal;
            draw_info.corners       = corners;
            draw_info.index         = v_index++;
            Color n_c = new Color(average_normal * 0.5f + Vector3.One * 0.5f);

            vertices.Add(new VertexPositionColorNormal(new Vector3(position * grid_size + draw_info.position * size * grid_size, 0), n_c, average_normal));

            type = QuadtreeNodeType.Leaf;
            return(true);
        }