void MeshImporter::print_index_list(COLLADAFW::IndexList& index_list)
{
	fprintf(stderr, "Index list for \"%s\":\n", index_list.getName().c_str());
	for (int i = 0; i < index_list.getIndicesCount(); i += 2) {
		fprintf(stderr, "%u, %u\n", index_list.getIndex(i), index_list.getIndex(i + 1));
	}
	fprintf(stderr, "\n");
}
void MeshImporter::set_vcol(MLoopCol *mlc, VCOLDataWrapper &vob, int loop_index, COLLADAFW::IndexList &index_list, int count)
{
	int index;
	for (index = 0; index < count; index++, mlc++) {
		int v_index = index_list.getIndex(index + loop_index);
		vob.get_vcol(v_index,mlc);
	}
}
void MeshImporter::set_vcol(MLoopCol *mlc, VCOLDataWrapper &vob, int loop_index, COLLADAFW::IndexList &index_list, int count)
{
	COLLADAFW::UIntValuesArray& indices =index_list.getIndices();
	int index;
	for(index = 0; index < count; index++,mlc++)
	{
		int v_index = indices[index+loop_index];
		vob.get_vcol(v_index,mlc);
	}
}
void MeshImporter::set_face_uv(MLoopUV *mloopuv, UVDataWrapper &uvs,
                               int start_index, COLLADAFW::IndexList& index_list, int count)
{
	// per face vertex indices, this means for quad we have 4 indices, not 8
	COLLADAFW::UIntValuesArray& indices = index_list.getIndices();

	for (int index = 0; index < count; index++) {
		int uv_index = indices[index+start_index];
		uvs.getUV(uv_index, mloopuv[index].uv);
	}
}
SubMesh* ColladaSubMeshReader::Load( void )
{  
	COLLADAFW::MeshPrimitiveArray& primitives = m_pMesh->getMeshPrimitives();

	//若没有几何图元信息
	if( 0 == primitives.getCount() )
	{
		return NULL;
	}

	COLLADAFW::MeshPrimitive* pPrimitive = primitives[0]; 
 
	if(  COLLADAFW::MeshPrimitive::TRIANGLES != pPrimitive->getPrimitiveType()  )
	{
		return NULL;
	}

	COLLADAFW::UIntValuesArray& positionIndices = pPrimitive->getPositionIndices(); 
	//若没有位置索引 则直接退出
	if( 0 == positionIndices.getCount() )
	{
		return NULL;
	} 

	COLLADAFW::UIntValuesArray& normalIndices = pPrimitive->getNormalIndices(); 
	m_bHasNormals = ( normalIndices.getCount() != 0 );  
	COLLADAFW::UIntValuesArray& tangentIndices = pPrimitive->getTangentIndices(); 
	m_bHasTangents = ( tangentIndices.getCount() != 0 ); 
	COLLADAFW::UIntValuesArray& binormalIndices = pPrimitive->getBinormalIndices(); 
	m_bHasBinormals = ( binormalIndices.getCount() != 0 );
	COLLADAFW::IndexList* pUVs = NULL;
	if( pPrimitive->getUVCoordIndicesArray().getCount() )
	{
		pUVs = pPrimitive->getUVCoordIndicesArray()[0]; 
		m_bHasUVs = ( pUVs->getIndices().getCount() != 0 );
	} 

	SubMesh* pSubMesh = new SubMesh;
	pSubMesh->SetName( m_pMesh->getName() );  
	Resource::Material* pMaterial = Resource::MaterialManager::GetInstance()->CreateOrRetrieveMaterial( m_pMesh->getName() );
	pSubMesh->SetMaterial( pMaterial );

	Render::VertexBuffer& vertexBuf =  pSubMesh->RenderData().VertexBuf();
	pSubMesh->RenderData().PrimitiveType( Render::TYPE_TRIANGLES );

	vertexBuf.ChannelFlag() = Render::POSITION_CH;
	if( m_bHasNormals )
	{
		vertexBuf.ChannelFlag() |= Render::NORMAL_CH;
	}
	if( m_bHasUVs )
	{
		vertexBuf.ChannelFlag() |= Render::TEXCOORD_CH;
	}
	if( m_bHasTangents )
	{
		vertexBuf.ChannelFlag() |= Render::TANGENT_CH;
	}
	if( m_bHasBinormals )
	{
		vertexBuf.ChannelFlag() |= Render::BINORMAL_CH;
	}

	unsigned int uiCurrIndex = 0;
	unsigned int uiIndicesCount = positionIndices.getCount();

	for( unsigned int uiIndex = 0 ; uiIndex < uiIndicesCount ; uiIndex++ )
	{
		Tuple tuple;
		tuple.m_posIndex = positionIndices[uiIndex];
		if( m_bHasNormals )
		{
			tuple.m_normIndex = normalIndices[uiIndex];
		}
		if( m_bHasUVs )
		{
			tuple.m_uvIndex = pUVs->getIndices()[uiIndex];
		}
		if( m_bHasTangents )
		{
			tuple.m_tangentIndex = tangentIndices[uiIndex];
		}
		if( m_bHasBinormals )
		{
			tuple.m_binormalIndex = binormalIndices[uiIndex];
		}

		indicesTable_t::iterator itIndex = m_indicesMap.find(tuple); 
		if( itIndex == m_indicesMap.end() )
		{
			Render::Vertex newVert;
			LoadVertexByTuple( tuple , newVert );
			vertexBuf.AppendVertex( newVert );
			m_indicesMap.insert( std::make_pair( tuple , uiCurrIndex ) );
			pSubMesh->RenderData().AppendIndex( uiCurrIndex );
			uiCurrIndex++;
		}else{
			pSubMesh->RenderData().AppendIndex( itIndex->second );
		}

	}//for( unsigned int uiIndex = 0 ; uiIndex < uiIndicesCount ; uiIndex++ )

	return pSubMesh;
}