public Line3d( Vector3 startPoint, Vector3 direction, float length, ColorEx color )
{
// normalize the direction vector to ensure all elements fall in [0,1] range.
direction.Normalize();
// calculate the actual endpoint
Vector3 endPoint = startPoint + ( direction * length );
vertexData = new VertexData();
renderOperation.vertexData = vertexData;
renderOperation.vertexData.vertexCount = 2;
renderOperation.vertexData.vertexStart = 0;
renderOperation.indexData = null;
renderOperation.operationType = OperationType.LineList;
renderOperation.useIndices = false;
VertexDeclaration decl = vertexData.vertexDeclaration;
VertexBufferBinding binding = vertexData.vertexBufferBinding;
// add a position and color element to the declaration
decl.AddElement( POSITION, 0, VertexElementType.Float3, VertexElementSemantic.Position );
decl.AddElement( COLOR, 0, VertexElementType.Color, VertexElementSemantic.Diffuse );
// create a vertex buffer for the position
HardwareVertexBuffer buffer =
HardwareBufferManager.Instance.CreateVertexBuffer( decl.Clone( POSITION ), vertexData.vertexCount, BufferUsage.StaticWriteOnly );
Vector3[] pos = new Vector3[] { startPoint, endPoint };
// write the data to the position buffer
buffer.WriteData( 0, buffer.Size, pos, true );
// bind the position buffer
binding.SetBinding( POSITION, buffer );
// create a color buffer
buffer = HardwareBufferManager.Instance.CreateVertexBuffer( decl.Clone( COLOR ), vertexData.vertexCount, BufferUsage.StaticWriteOnly );
int colorValue = Root.Instance.RenderSystem.ConvertColor( color );
int[] colors = new int[] { colorValue, colorValue };
// write the data to the position buffer
buffer.WriteData( 0, buffer.Size, colors, true );
// bind the color buffer
binding.SetBinding( COLOR, buffer );
// MATERIAL
// grab a copy of the BaseWhite material for our use
Material material = (Material)MaterialManager.Instance.GetByName( "BaseWhite" );
material = material.Clone( "LineMat" );
// disable lighting to vertex colors are used
material.Lighting = false;
// set culling to none so the triangle is drawn 2 sided
material.CullingMode = CullingMode.None;
this.Material = material;
// set the bounding box of the line
this.box = new AxisAlignedBox( startPoint, endPoint );
}