public void CreateSphere( float radius, int rings, int segments )
{
renderOperation.operationType = OperationType.TriangleStrip;
renderOperation.vertexData = new VertexData();
renderOperation.indexData = new IndexData();
renderOperation.useIndices = false;
VertexData vertexData = renderOperation.vertexData;
IndexData indexData = renderOperation.indexData;
// define the vertex format
VertexDeclaration vertexDecl = vertexData.vertexDeclaration;
int currOffset = 0;
// only generate positions
vertexDecl.AddElement( 0, currOffset, VertexElementType.Float3, VertexElementSemantic.Position );
currOffset += VertexElement.GetTypeSize( VertexElementType.Float3 );
// allocate the vertex buffer
vertexData.vertexCount = ( rings + 1 ) * ( segments + 1 );
HardwareVertexBuffer vBuf = HardwareBufferManager.Instance.CreateVertexBuffer( vertexDecl.GetVertexSize( 0 ), vertexData.vertexCount, BufferUsage.StaticWriteOnly, false );
VertexBufferBinding binding = vertexData.vertexBufferBinding;
binding.SetBinding( 0, vBuf );
// allocate index buffer
indexData.indexCount = 6 * rings * ( segments + 1 );
indexData.indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer( IndexType.Size16, indexData.indexCount, BufferUsage.StaticWriteOnly, false );
HardwareIndexBuffer iBuf = indexData.indexBuffer;
float fDeltaRingAngle = ( Math.Utility.PI / rings );
float fDeltaSegAngle = ( 2 * Math.Utility.PI / segments );
ushort wVerticeIndex = 0;
unsafe
{
float* pVertex = (float*)( vBuf.Lock( BufferLocking.Discard ).ToPointer() );
ushort* pIndices = (ushort*)( iBuf.Lock( BufferLocking.Discard ).ToPointer() );
// Generate the group of rings for the sphere
for ( int ring = 0; ring <= rings; ring++ )
{
float r0 = radius * Math.Utility.Sin( ring * fDeltaRingAngle );
float y0 = radius * Math.Utility.Cos( ring * fDeltaRingAngle );
// Generate the group of segments for the current ring
for ( int seg = 0; seg <= segments; seg++ )
{
float x0 = r0 * Math.Utility.Sin( seg * fDeltaSegAngle );
float z0 = r0 * Math.Utility.Cos( seg * fDeltaSegAngle );
// Add one vertex to the strip which makes up the sphere
*pVertex++ = x0;
*pVertex++ = y0;
*pVertex++ = z0;
if ( ring != rings )
{
// each vertex (except the last) has six indicies pointing to it
*pIndices++ = (ushort)(wVerticeIndex + segments + 1 );
*pIndices++ = wVerticeIndex;
*pIndices++ = (ushort)(wVerticeIndex + segments);
*pIndices++ = (ushort)(wVerticeIndex + segments + 1);
*pIndices++ = (ushort)(wVerticeIndex + 1);
*pIndices++ = wVerticeIndex;
wVerticeIndex++;
}
}; // end for seg
} // end for ring
}
// Unlock
vBuf.Unlock();
iBuf.Unlock();
// Set bounding box and sphere
this.box = new AxisAlignedBox( new Vector3( -radius, -radius, -radius ), new Vector3( radius, radius, radius ) );
this._radius = 15000;
this._ignoreWorld = false;
}