// utility routines for converting draw primitive format from list of strips to tris
        void _ConvertToTris(ushort[] primitiveDataIn, int[] primitiveMatIn, short[] indicesIn, int numPrimIn,
            out int numPrimOut, out int numIndicesOut,
            DrawPrimitive[] primitivesOut, short[] indicesOut)
        {
            numPrimOut = -1;
            numIndicesOut = 0;

            int prevMaterial = -99999;
            for (int i = 0; i < numPrimIn; i++)
            {
                uint newMat = (uint)primitiveMatIn[i];
                newMat &= ~DrawPrimitive.TypeMask;
                if (newMat != prevMaterial)
                {
                    numPrimOut++;
                    if (primitivesOut != null)
                    {
                        primitivesOut[numPrimOut].Start = (ushort)numIndicesOut;
                        primitivesOut[numPrimOut].NumElements = 0;
                        primitivesOut[numPrimOut].MaterialIndex = newMat | DrawPrimitive.Triangles;
                    }
                    prevMaterial = (int)newMat;
                }
                ushort start = primitiveDataIn[i * 2];
                ushort numElements = primitiveDataIn[i * 2 + 1];

                // gonna depend on what kind of primitive it is...
                if ((primitiveMatIn[i] & DrawPrimitive.TypeMask) == DrawPrimitive.Triangles)
                {
                    for (int j = 0; j < numElements; j += 3)
                    {
                        if (indicesOut != null)
                        {
                            indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
                            indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
                            indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
                        }
                        if (primitivesOut != null)
                            primitivesOut[numPrimOut].NumElements += 3;
                        numIndicesOut += 3;
                    }
                }
                else
                {
                    short[] idx = { indicesIn[start + 0], 0, indicesIn[start + 1] };
                    uint nextIdx = 1;
                    for (int j = 2; j < numElements; j++)
                    {
                        idx[nextIdx] = idx[2];
                        nextIdx = nextIdx ^ 1;
                        idx[2] = indicesIn[start + j];
                        if (idx[0] == idx[1] || idx[1] == idx[2] || idx[2] == idx[0])
                            continue;
                        if (indicesOut != null)
                        {
                            indicesOut[numIndicesOut + 0] = idx[0];
                            indicesOut[numIndicesOut + 1] = idx[1];
                            indicesOut[numIndicesOut + 2] = idx[2];
                        }
                        if (primitivesOut != null)
                            primitivesOut[numPrimOut].NumElements += 3;
                        numIndicesOut += 3;
                    }
                }
            }
            numPrimOut++;
        }
        // utility routines for converting draw primitive format from list of strips...list of strips
        void _LeaveAsMultipleStrips(ushort[] primitiveDataIn, int[] primitiveMatIn, short[] indicesIn, int numPrimIn,
            out int numPrimOut, out int numIndicesOut,
            DrawPrimitive[] primitivesOut, short[] indicesOut)
        {
            numPrimOut = -1;
            numIndicesOut = 0;

            int prevMaterial = -99999;
            for (int i = 0; i < numPrimIn; i++)
            {
                int newMat = primitiveMatIn[i];

                ushort start = primitiveDataIn[i * 2];
                ushort numElements = primitiveDataIn[i * 2 + 1];

                numPrimOut++;
                if (primitivesOut != null)
                {
                    primitivesOut[numPrimOut].Start = (ushort)numIndicesOut;
                    primitivesOut[numPrimOut].NumElements = 0;
                    primitivesOut[numPrimOut].MaterialIndex = (uint)newMat;
                }
                prevMaterial = newMat;

                // gonna depend on what kind of primitive it is...
                // from above we know it's the same kind as the one we're building...
                if ((primitiveMatIn[i] & DrawPrimitive.TypeMask) == DrawPrimitive.Triangles)
                {
                    // triangles primitive...add to it
                    for (int j = 0; j < numElements; j += 3)
                    {
                        if (indicesOut != null)
                        {
                            indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
                            indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
                            indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
                        }
                        if (primitivesOut != null)
                            primitivesOut[numPrimOut].NumElements += 3;
                        numIndicesOut += 3;
                    }
                }
                else
                {
                    // strip primitive...add to it
                    if (indicesOut != null)
                    {
                        for (int j = 0; j < numElements; j++)
                            indicesOut[numIndicesOut + j] = indicesIn[start + j];
                    }
                    if (primitivesOut != null)
                        primitivesOut[numPrimOut].NumElements = numElements;
                    numIndicesOut += numElements;
                }
            }
            numPrimOut++;
        }
        // utility routines for converting draw primitive format from list of strips to one big uber strip
        void _ConvertToSingleStrip(ushort[] primitiveDataIn, int[] primitiveMatIn, short[] indicesIn, int numPrimIn,
            out int numPrimOut, out int numIndicesOut,
            DrawPrimitive[] primitivesOut, short[] indicesOut)
        {
            numPrimOut = -1;
            numIndicesOut = 0;

            int prevMaterial = -99999;
            int curDrawOut = 0;
            for (int i = 0; i < numPrimIn; i++)
            {
                int newMat = primitiveMatIn[i];
                if (newMat != prevMaterial)
                {
                    numPrimOut++;
                    if (primitivesOut != null)
                    {
                        primitivesOut[numPrimOut].Start = (ushort)numIndicesOut;
                        primitivesOut[numPrimOut].NumElements = 0;
                        primitivesOut[numPrimOut].MaterialIndex = (uint)newMat;
                    }
                    curDrawOut = 0;
                    prevMaterial = newMat;
                }
                ushort start = primitiveDataIn[i * 2];
                ushort numElements = primitiveDataIn[i * 2 + 1];

                // gonna depend on what kind of primitive it is...
                // from above we know it's the same kind as the one we're building...
                if ((primitiveMatIn[i] & DrawPrimitive.TypeMask) == DrawPrimitive.Triangles)
                {
                    // triangles primitive...add to it
                    for (int j = 0; j < numElements; j += 3)
                    {
                        if (indicesOut != null)
                        {
                            indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
                            indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
                            indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
                        }
                        if (primitivesOut != null)
                            primitivesOut[numPrimOut].NumElements += 3;
                        numIndicesOut += 3;
                    }
                }
                else
                {
                    // strip primitive...add to it
                    if (indicesOut != null)
                    {
                        if ((curDrawOut & 1) != 0)
                        {
                            indicesOut[numIndicesOut + 0] = indicesOut[numIndicesOut - 1];
                            indicesOut[numIndicesOut + 1] = indicesOut[numIndicesOut - 1];
                            indicesOut[numIndicesOut + 2] = indicesIn[start];
                            for (int j = 0; j < numElements; j++)
                                indicesOut[numIndicesOut + 3 + j] = indicesIn[start + j];
                        }
                        else if (curDrawOut != 0)
                        {
                            indicesOut[numIndicesOut + 0] = indicesOut[numIndicesOut - 1];
                            indicesOut[numIndicesOut + 1] = indicesIn[start];
                            for (int j = 0; j < numElements; j++)
                                indicesOut[numIndicesOut + 2 + j] = indicesIn[start + j];
                        }
                        else
                        {
                            for (int j = 0; j < numElements; j++)
                                indicesOut[numIndicesOut + j] = indicesIn[start + j];
                        }
                    }
                    ushort added = numElements;
                    added += (ushort)((curDrawOut != 0) ? (((curDrawOut & 1) != 0) ? 3 : 2) : 0);
                    if (primitivesOut != null)
                        primitivesOut[numPrimOut].NumElements += added;
                    numIndicesOut += added;
                    curDrawOut += added;
                }
            }
            numPrimOut++;
        }