コード例 #1
0
        /// <summary>
        /// Generates a index for a triangle soup quad with the specified shape
        /// </summary>
        /// <param name="lod">Level of detail</param>
        /// <param name="bufferShape">Buffer shape</param>
        /// <param name="triangles">Triangle count</param>
        /// <returns>Returns the generated index list</returns>
        public static uint[] GenerateIndices(LevelOfDetail lod, IndexBufferShapes bufferShape, int triangles)
        {
            uint offset   = (uint)lod;
            uint fullSide = (uint)Math.Sqrt(triangles / 2f);

            int tris = triangles / (int)Math.Pow(offset, 2);

            int  nodes    = tris / 2;
            uint side     = (uint)Math.Sqrt(nodes);
            uint sideLoss = side / 2;

            bool topSide =
                bufferShape == IndexBufferShapes.CornerTopLeft ||
                bufferShape == IndexBufferShapes.CornerTopRight ||
                bufferShape == IndexBufferShapes.SideTop;

            bool bottomSide =
                bufferShape == IndexBufferShapes.CornerBottomLeft ||
                bufferShape == IndexBufferShapes.CornerBottomRight ||
                bufferShape == IndexBufferShapes.SideBottom;

            bool leftSide =
                bufferShape == IndexBufferShapes.CornerBottomLeft ||
                bufferShape == IndexBufferShapes.CornerTopLeft ||
                bufferShape == IndexBufferShapes.SideLeft;

            bool rightSide =
                bufferShape == IndexBufferShapes.CornerBottomRight ||
                bufferShape == IndexBufferShapes.CornerTopRight ||
                bufferShape == IndexBufferShapes.SideRight;

            uint totalTriangles = (uint)tris;

            if (topSide)
            {
                totalTriangles -= sideLoss;
            }
            if (bottomSide)
            {
                totalTriangles -= sideLoss;
            }
            if (leftSide)
            {
                totalTriangles -= sideLoss;
            }
            if (rightSide)
            {
                totalTriangles -= sideLoss;
            }

            List <uint> indices = new List <uint>((int)totalTriangles * 3);

            for (uint y = 1; y < side; y += 2)
            {
                for (uint x = 1; x < side; x += 2)
                {
                    uint indexPRow = (((y - 1) * offset) * (fullSide + 1)) + (x * offset);
                    uint indexCRow = (((y + 0) * offset) * (fullSide + 1)) + (x * offset);
                    uint indexNRow = (((y + 1) * offset) * (fullSide + 1)) + (x * offset);

                    bool firstRow    = y == 1;
                    bool lastRow     = y == side - 1;
                    bool firstColumn = x == 1;
                    bool lastColumn  = x == side - 1;

                    //Top side
                    var top = ComputeTopSide(firstRow, topSide, offset, indexPRow, indexCRow);

                    //Bottom side
                    var bottom = ComputeBottomSide(lastRow, bottomSide, offset, indexCRow, indexNRow);

                    //Left side
                    var left = ComputeLeftSide(firstColumn, leftSide, offset, indexPRow, indexCRow, indexNRow);

                    //Right side
                    var right = ComputeRightSide(lastColumn, rightSide, offset, indexPRow, indexCRow, indexNRow);

                    indices.AddRange(top);
                    indices.AddRange(bottom);
                    indices.AddRange(left);
                    indices.AddRange(right);
                }
            }

            return(indices.ToArray());
        }
コード例 #2
0
 /// <summary>
 /// Generates a index for a triangle soup quad with the specified shape
 /// </summary>
 /// <param name="bufferShape">Buffer shape</param>
 /// <param name="triangles">Triangle count</param>
 /// <returns>Returns the generated index list</returns>
 public static uint[] GenerateIndices(IndexBufferShapes bufferShape, int triangles)
 {
     return(GenerateIndices(LevelOfDetail.High, bufferShape, triangles));
 }