コード例 #1
0
        private void RasterizeTriangle(List <RecastVertex> vertexes, int v0, int v1, int v2, short area, float ics, float ich, int flagMergeThr)
        {
            int          w = Width;
            int          h = Height;
            RecastVertex tempMin, tempMax;

            float by = Bmax[1] - Bmin[1];

            tempMin = new RecastVertex(vertexes[v0]);
            tempMax = new RecastVertex(vertexes[v0]);
            tempMin = RecastVertex.Min(tempMin, vertexes[v1]);
            tempMin = RecastVertex.Min(tempMin, vertexes[v2]);
            tempMax = RecastVertex.Max(tempMax, vertexes[v1]);
            tempMax = RecastVertex.Max(tempMax, vertexes[v2]);

            if (!OverlapBounds(Bmin, Bmax, tempMin.ToArray(), tempMax.ToArray()))
            {
                return;
            }

            int x0 = (int)((tempMin[0] - Bmin[0]) * ics);
            int y0 = (int)((tempMin[2] - Bmin[2]) * ics);
            int x1 = (int)((tempMax[0] - Bmin[0]) * ics);
            int y1 = (int)((tempMax[2] - Bmin[2]) * ics);

            x0 = Math.Max(0, Math.Min(x0, w - 1));
            y0 = Math.Max(0, Math.Min(y0, h - 1));
            x1 = Math.Max(0, Math.Min(x1, w - 1));
            y1 = Math.Max(0, Math.Min(y1, h - 1));

            float[] inArray = new float[7 * 3], outArray = new float[7 * 3], inrowArray = new float[7 * 3];

            for (int y = y0; y <= y1; y++)
            {
                Array.Copy(vertexes[v0].ToArray(), 0, inArray, 0, 3);
                Array.Copy(vertexes[v1].ToArray(), 0, inArray, 1 * 3, 3);
                Array.Copy(vertexes[v2].ToArray(), 0, inArray, 2 * 3, 3);
                int   nvrow = 3;
                float cz    = Bmin[2] + y * Cs;
                nvrow = ClipPoly(inArray, nvrow, ref outArray, 0, 1, -cz);
                if (nvrow < 3)
                {
                    continue;
                }
                nvrow = ClipPoly(outArray, nvrow, ref inrowArray, 0, -1, cz + Cs);
                if (nvrow < 3)
                {
                    continue;
                }

                for (int x = x0; x <= x1; x++)
                {
                    int   nv = nvrow;
                    float cx = Bmin[0] + x * Cs;
                    nv = ClipPoly(inrowArray, nv, ref outArray, 1, 0, -cx);
                    if (nv < 3)
                    {
                        continue;
                    }
                    nv = ClipPoly(outArray, nv, ref inArray, -1, 0, cx + Cs);
                    if (nv < 3)
                    {
                        continue;
                    }

                    float smin = inArray[1], smax = inArray[1];
                    for (int i = 1; i < nv; i++)
                    {
                        smin = Math.Min(smin, inArray[i * 3 + 1]);
                        smax = Math.Max(smax, inArray[i * 3 + 1]);
                    }
                    smin -= Bmin[1];
                    smax -= Bmin[1];

                    if (smax.CompareTo(0.0f) < 0 || smin.CompareTo(by) > 0)
                    {
                        continue;
                    }
                    if (smin.CompareTo(0.0f) < 0)
                    {
                        smin = 0;
                    }
                    if (smax.CompareTo(by) > 0)
                    {
                        smax = by;
                    }

                    int ismin = Math.Max(0, Math.Min((int)Math.Floor(smin * ich), short.MaxValue));
                    int ismax = Math.Max(ismin + 1, Math.Min((int)Math.Floor(smax * ich), short.MaxValue));

                    AddSpan(x, y, ismin, ismax, area, flagMergeThr);
                }
            }
        }