D3D Mesh to COLLADA

First let me thank those people who are taking the time to read my post as it is kind of long (with code).

Secondly, if the formatting does not appear right then my apologies. I would appreciate it if you could tell me how to keep my formatting on this board.

I haven’t quite put all the pieces together in my mind yet, and I am having some problem understanding the COLLADA layout with a mesh. I am trying to save a D3D mesh using COLLADA DOM with C++. The two areas that I am having problems with are the EFFECTS and MATERIALS.

When I add the TRIANGLES I am also assigning a MATERIAL for each subset. Is this correct?

I am assigning material ID’s as such:

		sprintf( tmp, "mtl%d", AttribId );
		triangles->setMaterial( tmp );

Now comes the confusing part. I add an EFFECT using the below code. Could someone tell me what is needed to finish this off, assuming that I am trying to use the Fixed-Function pipeline with a simple texture-material associated with the TRIANGLES.


bool CColladaParser::AddEffect( daeElement* parentNode, CXMesh* theModel )
{
	if( parentNode == NULL  ||  theModel == NULL )
		return false;


	domLibrary_effects* libEffects = daeSafeCast<domLibrary_effects>( parentNode->add( "library_effects" ) );
	if( libEffects == NULL )
		return false;


	domEffect* effect = daeSafeCast<domEffect>( libEffects->add( "effect" ) );
	if( effect == NULL )
		return false;

	effect->setId( "ModelEffect" );


	domFx_profile_abstract* profile = daeSafeCast<domFx_profile_abstract>( effect->add( "profile_COMMON" ) );
	if( profile == NULL )
		return false;


	// Add a surface - what is a surface?
	domFx_newparam_common* newparam = daeSafeCast<domFx_newparam_common>( profile->add( "newparam" ) );
	if( newparam == NULL )
		return false;

	newparam->setSid( "surface" );

	domFx_surface_common* surface = daeSafeCast<domFx_surface_common>( newparam->add( "surface" ) );
	if( surface == NULL )
		return false;

	surface->setType( FX_SURFACE_TYPE_ENUM_2D );

	//surface->add( "init_from" )->setCharData( tmpName.c_str() );



	// Add a <sampler2D>
	return true;
}

And finally, the MATERIAL setup is lacking too.


bool CColladaParser::AddMaterial( daeElement* parentNode, CXMesh* theModel )
{
	if( parentNode == NULL  ||  theModel == NULL )
		return false;


	domLibrary_materials* libMaterials = daeSafeCast<domLibrary_materials>( parentNode->add( "library_materials" ) );
	if( libMaterials == NULL )
		return false;

	UINT matCount = theModel->GetNumMaterials();
	for( UINT matID = 0;  matID < matCount;  matID++ )
	{
		CXMaterial* theMaterial = theModel->GetMaterial( matID );
		if( theMaterial == NULL )
			continue;

		domMaterial* material = daeSafeCast<domMaterial>( libMaterials->add( "material" ) );
		if( material == NULL )
			continue;

		char tmpName[ 100 ];
		memset( tmpName, 0, sizeof(tmpName) );

		sprintf( tmpName, "mtl%d", matID );
		material->setId( tmpName );
		material->add( "instance_effect" )->setAttribute( "url", "#ModelEffect" );
	}


    return true;
}

Mesh primitives (e.g. <triangles>) have a symbolic-name “material” attribute. This is not a URI, ID, or SID. See <bind_material> in the spec for binding details.

Read the COLLADA specification Chapter 7: Getting Started with FX, for details on the structures and relationships involved between geometry, materials and effects.

Thanks.

I am printing out both Ch 7 and Ch 8.

Since you’re using the DOM you can also review the rt and fx samples for coding examples. The COLLADA Refinery (also on sourceforge) is also built on the DOM, as are other tools and plug-ins like OpenSceneGraph plugin, CgToGLSL, etc.

Also for DX vertex buffer creation, have a look at the COLLADA for XNA C# source.