Sign in to follow this  
Maxim Skachkov

[MDX] Dream code for CustomVertex

Recommended Posts

I want that in the code of creation and initialisation of VertexBuffer the type's structure of the CustomVertex was not duplicated! Code i want to refactoring:
public override void InitGraphVB( Device ADevice )
{
	int count = 100;
	System.Type vertType = typeof( CustomVertex.PositionNormalColored );

	VertexFormats vertFormat = CustomVertex.PositionNormalColored.Format;

			
	VertexBuffer vb = new VertexBuffer( vertType, count, ADevice, Usage.WriteOnly, vertFormat, Pool.Managed );

	CustomVertex.PositionNormalColored[] verts = ( CustomVertex.PositionNormalColored[] ) vb.Lock( 0, 0 ); 
	
	for ( int i = 0; i < count; i++ )
	{	
		Vector3 pos = ...;
		Vector3 norm = ...;
		System.Drawing.Color color = ...;
		verts[ i ] = new CustomVertex.PositionNormalColored( pos, norm, color );
	}
	vb.Unlock();
}


Dream code to illustrate the idea:
public override void InitGraphVB( Device ADevice )
{
	int count = 100;
	System.Type vertType = typeof( CustomVertex.PositionNormalColored );

	VertexFormats vertFormat = ( VertexFormats ) ( classof( vertType ).GetField( "Format" ) );

			
	VertexBuffer vb = new VertexBuffer( vertType, count, ADevice, Usage.WriteOnly, vertFormat, Pool.Managed );

	classof( vertType )[] verts = ( classof( vertType )[] ) vb.Lock( 0, 0 ); 

	for ( int i = 0; i < count ; i++ )
	{	
		Vector3 pos = ...;
		Vector3 norm = ...;
		System.Drawing.Color color = ...;

		object[] constrParams = { ( object )pos, ( object )norm , ( object )color };

		verts[ i ] = Activator.CreateInstance( vertType, constrParams );
	}
	vb.Unlock();
}


The main difficulty for me how get class by his type: classof( vertType )? Also i don't know how get access to class members knowing his name like this: classof( vertType ).GetField( "Format" )? [Edited by - Maxim Skachkov on April 4, 2006 2:06:01 PM]

Share this post


Link to post
Share on other sites
Hi,duhroach!
No problem in the Constructor(). The constructors for a vertex buffer are identical for both samples of the code.

I mean the "CustomVertex.PositionNormalColored" code fragment should appeared only once.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this