2012-08-16 14:18:21 +00:00
#region Using Statements
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using ANX.Framework.Content.Pipeline.Processors ;
2015-04-08 14:50:03 +02:00
using ANX.Framework.Graphics ;
using System.Runtime.InteropServices ;
2012-08-16 14:18:21 +00:00
#endregion
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
namespace ANX.Framework.Content.Pipeline.Graphics
{
public sealed class VertexContent
{
public VertexChannelCollection Channels
{
get ;
private set ;
}
public VertexChannel < int > PositionIndices
{
get ;
private set ;
}
public IndirectPositionCollection Positions
{
get ;
private set ;
}
public int VertexCount
{
2012-08-27 21:24:20 +00:00
get { return PositionIndices . Count ; }
}
public VertexContent ( GeometryContent content )
{
2015-04-08 14:50:03 +02:00
Channels = new VertexChannelCollection ( this ) ;
2012-08-27 21:24:20 +00:00
PositionIndices = new VertexChannel < int > ( "PositionIndices" ) ;
Positions = new IndirectPositionCollection ( content , PositionIndices ) ;
2012-08-16 14:18:21 +00:00
}
public int Add ( int positionIndex )
{
2015-04-08 14:50:03 +02:00
int vertexCount = this . VertexCount ;
this . Insert ( vertexCount , positionIndex ) ;
return vertexCount ;
2012-08-16 14:18:21 +00:00
}
public void AddRange ( IEnumerable < int > positionIndexCollection )
{
2015-04-08 14:50:03 +02:00
this . InsertRange ( this . VertexCount , positionIndexCollection ) ;
2012-08-16 14:18:21 +00:00
}
public VertexBufferContent CreateVertexBuffer ( )
{
2015-04-08 14:50:03 +02:00
int vector3Size = Marshal . SizeOf ( typeof ( Vector3 ) ) ;
VertexDeclarationContent vertexDeclaration = new VertexDeclarationContent ( ) ;
//first add a declaration for the usual positions.
vertexDeclaration . VertexElements . Add ( new VertexElement ( 0 , VertexElementFormat . Vector3 , VertexElementUsage . Position , 0 ) ) ;
int vertexStride = vector3Size ;
//Now add all custom channels.
foreach ( var channel in Channels )
{
VertexElementFormat format ;
if ( ! VectorConverter . TryGetVertexElementFormat ( channel . ElementType , out format ) )
{
throw new InvalidContentException ( string . Format ( "The element type of the channel \"{0}\" does not correspond to a {1}." , channel . Name , typeof ( VertexElementFormat ) . Name ) ) ;
}
VertexElementUsage usage ;
if ( ! VertexChannelNames . TryDecodeUsage ( channel . Name , out usage ) )
{
throw new InvalidContentException ( string . Format ( "Can't decode the {0} of the channel \"{1}\"." , typeof ( VertexElementUsage ) , channel . Name ) ) ;
}
vertexDeclaration . VertexElements . Add ( new VertexElement ( vertexStride , format , usage , VertexChannelNames . DecodeUsageIndex ( channel . Name ) ) ) ;
vertexStride + = Marshal . SizeOf ( channel . ElementType ) ;
}
vertexDeclaration . VertexStride = vertexStride ;
VertexBufferContent vertexBuffer = new VertexBufferContent ( this . VertexCount * vertexStride ) ;
vertexBuffer . VertexDeclaration = vertexDeclaration ;
vertexBuffer . Write < Vector3 > ( 0 , vertexStride , this . Positions ) ;
for ( int i = 0 ; i < Channels . Count ; i + + )
{
var channel = Channels [ i ] ;
//First VertexElements are the vertex positions.
vertexBuffer . Write ( vertexDeclaration . VertexElements [ i + 1 ] . Offset , vertexStride , channel . ElementType , channel ) ;
}
return vertexBuffer ;
2012-08-16 14:18:21 +00:00
}
public void Insert ( int index , int positionIndex )
{
2015-04-08 14:50:03 +02:00
this . PositionIndices . Insert ( index , positionIndex ) ;
2012-08-16 14:18:21 +00:00
}
public void InsertRange ( int index , IEnumerable < int > positionIndexCollection )
{
2015-04-08 14:50:03 +02:00
this . PositionIndices . InsertRange ( index , positionIndexCollection ) ;
2012-08-16 14:18:21 +00:00
}
public void RemoveAt ( int index )
{
2015-04-08 14:50:03 +02:00
this . RemoveRange ( index , 1 ) ;
2012-08-16 14:18:21 +00:00
}
public void RemoveRange ( int index , int count )
{
throw new NotImplementedException ( ) ;
}
}
}