protected void Initialize()
{
//create geometry
int nvertices = mSlices * 4;
int elemsize = 3 * 3;
int dsize = elemsize * nvertices;
int x = 0;
iData = new IndexData();
vData = new VertexData();
float[] vertices = new float[ dsize ];
float[,] coords = new float[ 4 ,2]
{
{ 0.0f, 0.0f },
{ 0.0f, 1.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
};
for ( x = 0; x < mSlices; x++ )
{
for ( int y = 0; y < 4; y++ )
{
float xcoord = coords[ y , 0 ] - 0.5f;
float ycoord = coords[ y , 1 ] - 0.5f;
float zcoord = -( x / ( mSlices - 1.0f ) - 0.5f );
// 1.0f .. a/(a+1)
// coordinate
vertices[ x * 4 * elemsize + y * elemsize + 0 ] = xcoord * ( mSize / 2.0f );
vertices[ x * 4 * elemsize + y * elemsize + 1 ] = ycoord * ( mSize / 2.0f );
vertices[ x * 4 * elemsize + y * elemsize + 2 ] = zcoord * ( mSize / 2.0f );
// normal
vertices[ x * 4 * elemsize + y * elemsize + 3 ] = 0.0f;
vertices[ x * 4 * elemsize + y * elemsize + 4 ] = 0.0f;
vertices[ x * 4 * elemsize + y * elemsize + 5 ] = 1.0f;
// tex
vertices[ x * 4 * elemsize + y * elemsize + 6 ] = xcoord * Utility.Sqrt( 3.0f );
vertices[ x * 4 * elemsize + y * elemsize + 7 ] = ycoord * Utility.Sqrt( 3.0f );
vertices[ x * 4 * elemsize + y * elemsize + 8 ] = zcoord * Utility.Sqrt( 3.0f );
}
}
int[] faces = new int[ mSlices * 6 ];
for ( x = 0; x < mSlices; x++ )
{
faces[ x * 6 + 0 ] = ( x * 4 + 0 );
faces[ x * 6 + 1 ] = ( x * 4 + 1 );
faces[ x * 6 + 2 ] = ( x * 4 + 2 );
faces[ x * 6 + 3 ] = ( x * 4 + 1 );
faces[ x * 6 + 4 ] = ( x * 4 + 2 );
faces[ x * 6 + 5 ] = ( x * 4 + 3 );
}
//setup buffers
vData.vertexStart = 0;
vData.vertexCount = nvertices;
VertexDeclaration vdecl = vData.vertexDeclaration;
VertexBufferBinding vbuf = vData.vertexBufferBinding;
int offset = 0;
vdecl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.Position );
offset += VertexElement.GetTypeSize( VertexElementType.Float3 );
vdecl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.Normal );
offset += VertexElement.GetTypeSize( VertexElementType.Float3 );
vdecl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.TexCoords );
offset += VertexElement.GetTypeSize( VertexElementType.Float3 );
HardwareVertexBuffer vbuffer = HardwareBufferManager.Instance
.CreateVertexBuffer( offset, nvertices, BufferUsage.StaticWriteOnly );
vbuf.SetBinding( 0, vbuffer );
vbuffer.WriteData( 0, vbuffer.Size, vertices, true );
HardwareIndexBuffer ibuf = HardwareBufferManager.Instance
.CreateIndexBuffer( IndexType.Size16, mSlices * 6,
BufferUsage.StaticWriteOnly );
iData.indexBuffer = ibuf;
iData.indexCount = mSlices * 6;
iData.indexStart = 0;
ibuf.WriteData( 0, ibuf.Size, faces, true );
vertices = null;
faces = null;
// Now make the render operation
renderOperation.operationType = OperationType.TriangleList;
renderOperation.indexData = iData;
renderOperation.vertexData = vData;
renderOperation.useIndices = true;
Material material = (Material)MaterialManager.Instance.Create( mTexture, "General" );
material.RemoveAllTechniques();
Technique technique = material.CreateTechnique();
Pass pass = technique.CreatePass();
TextureUnitState textureUnit = pass.CreateTextureUnitState();
pass.SetSceneBlending( SceneBlendType.TransparentAlpha );
pass.DepthWrite = false;
pass.CullingMode = CullingMode.None;
pass.LightingEnabled = false;
textureUnit.SetTextureAddressingMode( TextureAddressing.Clamp );
textureUnit.SetTextureName( mTexture, TextureType.ThreeD );
textureUnit.SetTextureFiltering( TextureFiltering.Trilinear );
mUnit = textureUnit;
this.Material = material;
}