void sb7fbxmodel::ProcessMesh(FbxNode* pNode) { fbxsdk::FbxMesh* pMesh = pNode->GetMesh(); if(pMesh == NULL) { return; } int triangleCount = pMesh->GetPolygonCount(); int vertexCounter = 0; sub_mesh sm; sm.count = 3 * triangleCount; sm.va = (vetex_attr*)malloc(sizeof(vetex_attr) * sm.count); m_vass.push_back(sm); for(int i = 0 ; i < triangleCount ; ++i) { for(int j = 0 ; j < 3 ; j++) { vetex_attr va; int ctrlPointIndex = pMesh->GetPolygonVertex(i , j); // Read the vertex ReadVertex(pMesh , ctrlPointIndex , va.vertex); // Read the color of each vertex ReadColor(pMesh , ctrlPointIndex , vertexCounter , va.color); // Read the UV of each vertex for(int k = 0 ; k < 2 ; ++k) { ReadUV(pMesh , ctrlPointIndex , pMesh->GetTextureUVIndex(i, j) , k , va.uv[k]); } // Read the normal of each vertex ReadNormal(pMesh , ctrlPointIndex , vertexCounter , va.normal); // Read the tangent of each vertex ReadTangent(pMesh , ctrlPointIndex , vertexCounter , va.tangent); sm.va[vertexCounter] = va; vertexCounter++; } // 根据读入的信息组装三角形,并以某种方式使用即可,比如存入到列表中、保存到文件等... } }
void FbxLoader::ProcessMesh(FbxNode* node, Node& meshNode) { FbxMesh* currMesh = node->GetMesh(); if(!currMesh) return; FbxLayerElementTangent* tangents = nullptr; if(useNormalMap) { if(currMesh->GetElementTangentCount() < 1) { currMesh->GenerateTangentsDataForAllUVSets(); tangents = currMesh->GetElementTangent(); } } std::map<int, std::vector<Vertex>> subMeshes; int vertCounter = 0; const int polygonCount = currMesh->GetPolygonCount(); for(int i = 0; i < polygonCount; i++) { const int polySize = currMesh->GetPolygonSize(i); int nMaterials = node->GetMaterialCount(); auto elementMaterial = currMesh->GetElementMaterial(); int mi = 0; if(elementMaterial) mi = currMesh->GetElementMaterial()->GetIndexArray()[i]; for(int j = 2; j >= 0; --j) { int ctrlIndex = currMesh->GetPolygonVertex(i, j); auto& currCtrlPoint = meshNode.controlPoints[ctrlIndex]; FbxVector4 v4; auto& pos = currCtrlPoint.position * factor; currMesh->GetPolygonVertexNormal(i, j, v4); Vector3 normal = { (float)v4[0], (float)v4[1], (float)v4[2] }; Vector3 tangent = { 0, 0, 0 }; if(useNormalMap) { ReadTangent(tangents, ctrlIndex, vertCounter, v4); tangent = { (float)v4[0], (float)v4[1], (float)v4[2] }; } Vector2 uv; FbxStringList uvSetNames; currMesh->GetUVSetNames(uvSetNames); bool unmapped = false; // supports one uv set only if(uvSetNames.GetCount() > 0) { FbxVector2 UV; currMesh->GetPolygonVertexUV(i, j, uvSetNames[0], UV, unmapped); uv = { (float)UV[0], (float)UV[1] }; } if(axismode == eLeftHanded) { pos.x *= -1; uv.y = 1 - uv.y; normal.x *= -1; tangent.x *= -1; } Vector4 weights = { 0, 0, 0, 0 }; Byte4 indices = { 0, 0, 0, 0 }; int blendCount = (int)min(currCtrlPoint.blendWeigths.size(), 4); if(blendCount > 0) { meshNode.useSkinnedMesh = true; if(currCtrlPoint.blendWeigths.size() > 4) sort(currCtrlPoint.blendWeigths.begin(), currCtrlPoint.blendWeigths.end()); for(int i = 0; i < blendCount; i++) { weights[i] = currCtrlPoint.blendWeigths[i].weight; indices.m[i] = currCtrlPoint.blendWeigths[i].boneIndex; } } Vertex temp = { pos, uv, normal, tangent, indices, weights }; subMeshes[mi].push_back(temp); } ++vertCounter; } if(subMeshes.size() > 0) { int index = 0; meshNode.meshes.reserve(vertCounter); meshNode.vertIndices.reserve(vertCounter); meshNode.vertexCountOfSubMesh.reserve(subMeshes.size()); for(auto& pair : subMeshes) { auto& m = pair.second; for(int i = 0; i < m.size(); ++i) meshNode.vertIndices.emplace_back(index++); meshNode.vertexCountOfSubMesh.push_back((int)m.size()); meshNode.meshes.insert(meshNode.meshes.end(), m.begin(), m.end()); } } subMeshes.clear(); }