protected unsafe void MakeTriangles()
{
// Our vertex buffer is subdivided to the highest level, we need to generate tris
// which step over the vertices we don't need for this level of detail.
// Calculate steps
int vStep = 1 << ( maxVLevel - vLevel );
int uStep = 1 << ( maxULevel - uLevel );
int currentWidth = ( LevelWidth( uLevel ) - 1 ) * ( ( controlWidth - 1 ) / 2 ) + 1;
int currentHeight = ( LevelWidth( vLevel ) - 1 ) * ( ( controlHeight - 1 ) / 2 ) + 1;
bool use32bitindexes = ( indexBuffer.Type == IndexType.Size32 );
// The mesh is built, just make a list of indexes to spit out the triangles
int vInc, uInc;
int vCount, uCount, v, u, iterations;
if ( side == VisibleSide.Both )
{
iterations = 2;
vInc = vStep;
v = 0; // Start with front
}
else
{
iterations = 1;
if ( side == VisibleSide.Front )
{
vInc = vStep;
v = 0;
}
else
{
vInc = -vStep;
v = meshHeight - 1;
}
}
// Calc num indexes
currentIndexCount = ( currentWidth - 1 ) * ( currentHeight - 1 ) * 6 * iterations;
int v1, v2, v3;
int count = 0;
// Lock just the section of the buffer we need
IntPtr shortBuffer = IntPtr.Zero;
IntPtr intBuffer = IntPtr.Zero;
short* p16 = null;
int* p32 = null;
if ( use32bitindexes )
{
intBuffer = indexBuffer.Lock(
indexOffset * sizeof( int ),
requiredIndexCount * sizeof( int ),
BufferLocking.NoOverwrite );
p32 = (int*)intBuffer;
}
else
{
shortBuffer = indexBuffer.Lock(
indexOffset * sizeof( short ),
requiredIndexCount * sizeof( short ),
BufferLocking.NoOverwrite );
p16 = (short*)shortBuffer;
}
while ( iterations-- > 0 )
{
// Make tris in a zigzag pattern (compatible with strips)
u = 0;
uInc = uStep; // Start with moving +u
vCount = currentHeight - 1;
while ( vCount-- > 0 )
{
uCount = currentWidth - 1;
while ( uCount-- > 0 )
{
// First Tri in cell
// -----------------
v1 = ( ( v + vInc ) * meshWidth ) + u;
v2 = ( v * meshWidth ) + u;
v3 = ( ( v + vInc ) * meshWidth ) + ( u + uInc );
// Output indexes
if ( use32bitindexes )
{
p32[ count++ ] = v1;
p32[ count++ ] = v2;
p32[ count++ ] = v3;
}
else
{
p16[ count++ ] = (short)v1;
p16[ count++ ] = (short)v2;
p16[ count++ ] = (short)v3;
}
// Second Tri in cell
// ------------------
v1 = ( ( v + vInc ) * meshWidth ) + ( u + uInc );
v2 = ( v * meshWidth ) + u;
v3 = ( v * meshWidth ) + ( u + uInc );
// Output indexes
if ( use32bitindexes )
{
p32[ count++ ] = v1;
p32[ count++ ] = v2;
p32[ count++ ] = v3;
}
else
{
p16[ count++ ] = (short)v1;
p16[ count++ ] = (short)v2;
p16[ count++ ] = (short)v3;
}
// Next column
u += uInc;
}
// Next row
v += vInc;
u = 0;
}
// Reverse vInc for double sided
v = meshHeight - 1;
vInc = -vInc;
}
// don't forget to unlock!
indexBuffer.Unlock();
}