/** * メッシュ情報を読み込む * * @param mesh メッシュ情報 */ void FbxFileLoader::load_mesh( FbxMesh* mesh ) { if ( ! mesh ) { return; } if ( ! get_model()->get_mesh() ) { get_model()->set_mesh( create_mesh() ); } VertexIndexMap vertex_index_map; VertexList vertex_list; Mesh::PositionList position_list; Mesh::VertexWeightList vertex_weight_list; // load_mesh_vertex() for ( int n = 0; n < mesh->GetControlPointsCount(); n++ ) { FbxVector4 v = mesh->GetControlPointAt( n ); position_list.push_back( Mesh::Position( static_cast< float >( v[ 0 ] ), static_cast< float >( v[ 1 ] ), static_cast< float >( v[ 2 ] ) ) ); } // load_mesh_vertex_weight() { int skin_count = mesh->GetDeformerCount( FbxDeformer::eSkin ); if ( skin_count > 0 ) { assert( skin_count == 1 ); vertex_weight_list.resize( position_list.size() ); FbxSkin* skin = FbxCast< FbxSkin >( mesh->GetDeformer( 0, FbxDeformer::eSkin ) ); load_mesh_vertex_weight( skin, vertex_weight_list ); } } FbxLayerElementSmoothing* smoothing = 0; // load_mesh_smoothing_info() { FbxLayer* layer = mesh->GetLayer( 0 ); smoothing = layer->GetSmoothing(); } if ( ! smoothing ) { COMMON_THROW_EXCEPTION_MESSAGE( "this FBX format is not supported. ( no smoothing info )" ); } // load_mesh_plygon() FbxLayerElementArrayTemplate< int >* material_indices; mesh->GetMaterialIndices( & material_indices ); for ( int n = 0; n < mesh->GetPolygonCount(); n++ ) { Mesh::VertexGroup* vertex_group = get_model()->get_mesh()->get_vertex_group_at( material_indices->GetAt( n ) ); bool is_smooth = smoothing->GetDirectArray().GetAt( n ) != 0; FbxVector4 polygon_normal( 0.f, 0.f, 0.f ); if ( ! is_smooth ) { // ポリゴンの法線を計算する Mesh::Position p1( position_list.at( mesh->GetPolygonVertex( n, 0 ) ) ); Mesh::Position p2( position_list.at( mesh->GetPolygonVertex( n, 1 ) ) ); Mesh::Position p3( position_list.at( mesh->GetPolygonVertex( n, 2 ) ) ); FbxVector4 a = FbxVector4( p1.x(), p1.y(), p1.z() ); FbxVector4 b = FbxVector4( p2.x(), p2.y(), p2.z() ); FbxVector4 c = FbxVector4( p3.x(), p3.y(), p3.z() ); FbxVector4 ab( b - a ); FbxVector4 bc( c - b ); polygon_normal = ab.CrossProduct( bc ); polygon_normal.Normalize(); } for ( int m = 0; m < mesh->GetPolygonSize( n ); m++ ) { int position_index = mesh->GetPolygonVertex( n, m ); Mesh::Vertex v; v.Position = Mesh::Position( position_list.at( position_index ) ); FbxVector2 uv_vector; bool unmapped; if ( mesh->GetPolygonVertexUV( n, m, "UVMap", uv_vector, unmapped) ) { v.TexCoord = Mesh::TexCoord( static_cast< float >( uv_vector[ 0 ] ), 1.f - static_cast< float >( uv_vector[ 1 ] ) ); } if ( is_smooth ) { FbxVector4 normal_vector; // 頂点の法線 if ( mesh->GetPolygonVertexNormal( n, m, normal_vector ) ) { v.Normal = Mesh::Normal( static_cast< float >( normal_vector[ 0 ] ), static_cast< float >( normal_vector[ 1 ] ), static_cast< float >( normal_vector[ 2 ] ) ); } } else { // ポリゴンの法線 v.Normal = Mesh::Normal( static_cast< float >( polygon_normal[ 0 ] ), static_cast< float >( polygon_normal[ 1 ] ), static_cast< float >( polygon_normal[ 2 ] ) ); } // 頂点の一覧に追加 { VertexIndexMap::iterator i = vertex_index_map.find( v ); if ( i != vertex_index_map.end() ) { vertex_group->add_index( i->second ); } else { Mesh::Index vertex_index = static_cast< Mesh::Index >( get_model()->get_mesh()->get_vertex_count() ); get_model()->get_mesh()->add_vertex( v ); if ( ! vertex_weight_list.empty() ) { get_model()->get_mesh()->add_vertex_weight( vertex_weight_list.at( position_index ) ); } vertex_group->add_index( vertex_index ); vertex_index_map[ v ] = vertex_index; } } } } }