private void _tesselate2DMesh( SubMesh subMesh, int width, int height, bool doubleSided, BufferUsage indexBufferUsage, bool indexShadowBuffer )
{
int vInc, uInc, v, u, iterations;
int vCount, uCount;
vInc = 1;
v = 0;
iterations = doubleSided ? 2 : 1;
// setup index count
subMesh.indexData.indexCount = ( width - 1 ) * ( height - 1 ) * 2 * iterations * 3;
// create the index buffer using the current API
subMesh.indexData.indexBuffer =
HardwareBufferManager.Instance.CreateIndexBuffer( IndexType.Size16, subMesh.indexData.indexCount, indexBufferUsage, indexShadowBuffer );
short v1, v2, v3;
// grab a reference for easy access
HardwareIndexBuffer idxBuffer = subMesh.indexData.indexBuffer;
// lock the whole index buffer
IntPtr data = idxBuffer.Lock( BufferLocking.Discard );
unsafe
{
short* pIndex = (short*)data.ToPointer();
while ( 0 < iterations-- )
{
// make tris in a zigzag pattern (strip compatible)
u = 0;
uInc = 1;
vCount = height - 1;
while ( 0 < vCount-- )
{
uCount = width - 1;
while ( 0 < uCount-- )
{
// First Tri in cell
// -----------------
v1 = (short)( ( ( v + vInc ) * width ) + u );
v2 = (short)( ( v * width ) + u );
v3 = (short)( ( ( v + vInc ) * width ) + ( u + uInc ) );
// Output indexes
*pIndex++ = v1;
*pIndex++ = v2;
*pIndex++ = v3;
// Second Tri in cell
// ------------------
v1 = (short)( ( ( v + vInc ) * width ) + ( u + uInc ) );
v2 = (short)( ( v * width ) + u );
v3 = (short)( ( v * width ) + ( u + uInc ) );
// Output indexes
*pIndex++ = v1;
*pIndex++ = v2;
*pIndex++ = v3;
// Next column
u += uInc;
} // while uCount
v += vInc;
u = 0;
} // while vCount
v = height - 1;
vInc = -vInc;
} // while iterations
}// unsafe
// unlock the buffer
idxBuffer.Unlock();
}