void CModelConverter::ReadDAESceneTree(FCDSceneNode* pNode, CConversionSceneNode* pScene) { size_t iTransforms = pNode->GetTransformCount(); Matrix4x4 mTransformations; for (size_t t = 0; t < iTransforms; t++) { FCDTransform* pTransform = pNode->GetTransform(t); FMMatrix44 m = pTransform->ToMatrix(); mTransformations *= Matrix4x4(m); } pScene->SetTransformations(mTransformations); size_t iInstances = pNode->GetInstanceCount(); for (size_t i = 0; i < iInstances; i++) { FCDEntityInstance* pInstance = pNode->GetInstance(i); switch (pInstance->GetType()) { case FCDEntityInstance::GEOMETRY: { FCDGeometryInstance* pGeometryInstance = dynamic_cast<FCDGeometryInstance*>(pInstance); FCDEntity* pEntity = pGeometryInstance->GetEntity(); size_t iMesh = pScene->m_pScene->FindMesh(convert_from_fstring(pEntity->GetName())); size_t iMeshInstance = pScene->AddMeshInstance(iMesh); size_t iMaterialInstances = pGeometryInstance->GetMaterialInstanceCount(); for (size_t m = 0; m < iMaterialInstances; m++) { FCDMaterialInstance* pMaterialInstance = pGeometryInstance->GetMaterialInstance(m); FCDMaterial* pMaterial = pMaterialInstance->GetMaterial(); tstring sMaterial = pMaterial?convert_from_fstring(pMaterialInstance->GetMaterial()->GetName()):""; tstring sMaterialStub = convert_from_fstring(pMaterialInstance->GetSemantic()); size_t iMaterial = pScene->m_pScene->FindMaterial(sMaterial); size_t iMaterialStub = pScene->m_pScene->GetMesh(iMesh)->FindMaterialStub(sMaterialStub); pScene->GetMeshInstance(iMeshInstance)->AddMappedMaterial(iMaterialStub, iMaterial); } } } } size_t iChildren = pNode->GetChildrenCount(); for (size_t j = 0; j < iChildren; j++) { FCDSceneNode* pChildNode = pNode->GetChild(j); size_t iNode = pScene->AddChild(convert_from_fstring(pChildNode->GetName())); ReadDAESceneTree(pChildNode, pScene->GetChild(iNode)); } }
/** * Converts a COLLADA XML document into the PMD mesh format. * * @param input XML document to parse * @param output callback for writing the PMD data; called lots of times * with small strings * @param xmlErrors output - errors reported by the XML parser * @throws ColladaException on failure */ static void ColladaToPMD(const char* input, OutputCB& output, std::string& xmlErrors) { CommonConvert converter(input, xmlErrors); if (converter.GetInstance().GetEntity()->GetType() == FCDEntity::GEOMETRY) { Log(LOG_INFO, "Found static geometry"); FCDGeometryPolygons* polys = GetPolysFromGeometry((FCDGeometry*)converter.GetInstance().GetEntity()); // Convert the geometry into a suitable form for the game ReindexGeometry(polys); std::vector<VertexBlend> boneWeights; // unused std::vector<BoneTransform> boneTransforms; // unused std::vector<PropPoint> propPoints; // Get the raw vertex data FCDGeometryPolygonsInput* inputPosition = polys->FindInput(FUDaeGeometryInput::POSITION); FCDGeometryPolygonsInput* inputNormal = polys->FindInput(FUDaeGeometryInput::NORMAL); FCDGeometryPolygonsInput* inputTexcoord = polys->FindInput(FUDaeGeometryInput::TEXCOORD); const uint32* indicesCombined = inputPosition->GetIndices(); size_t indicesCombinedCount = inputPosition->GetIndexCount(); // (ReindexGeometry guarantees position/normal/texcoord have the same indexes) FCDGeometrySource* sourcePosition = inputPosition->GetSource(); FCDGeometrySource* sourceNormal = inputNormal ->GetSource(); FCDGeometrySource* sourceTexcoord = inputTexcoord->GetSource(); float* dataPosition = sourcePosition->GetData(); float* dataNormal = sourceNormal ->GetData(); float* dataTexcoord = sourceTexcoord->GetData(); size_t vertexCount = sourcePosition->GetDataCount() / 3; assert(sourcePosition->GetDataCount() == vertexCount*3); assert(sourceNormal ->GetDataCount() == vertexCount*3); assert(sourceTexcoord->GetDataCount() == vertexCount*2); // Transform mesh coordinate system to game coordinates // (doesn't modify prop points) TransformStaticModel(dataPosition, dataNormal, vertexCount, converter.GetEntityTransform(), converter.IsYUp()); // Add static prop points // which are empty child nodes of the main parent // Default prop points are already given in game coordinates AddDefaultPropPoints(propPoints); // Calculate transform to convert from COLLADA-defined up_axis to Z-up because // it's relatively straightforward to convert that to game coordinates FMMatrix44 upAxisTransform = FMMatrix44_Identity; if (converter.IsYUp()) { // Prop points are rotated -90 degrees about the X-axis, reverse that rotation // (do this once now because it's easier than messing with quaternions later) upAxisTransform = FMMatrix44::XAxisRotationMatrix(1.57f); } AddStaticPropPoints(propPoints, upAxisTransform, converter.GetInstance().GetParent()); WritePMD(output, indicesCombined, indicesCombinedCount, dataPosition, dataNormal, dataTexcoord, vertexCount, boneWeights, boneTransforms, propPoints); } else if (converter.GetInstance().GetType() == FCDEntityInstance::CONTROLLER) { Log(LOG_INFO, "Found skinned geometry"); FCDControllerInstance& controllerInstance = static_cast<FCDControllerInstance&>(converter.GetInstance()); // (NB: GetType is deprecated and should be replaced with HasType, // except that has irritating linker errors when using a DLL, so don't // bother) assert(converter.GetInstance().GetEntity()->GetType() == FCDEntity::CONTROLLER); // assume this is always true? FCDController* controller = static_cast<FCDController*>(converter.GetInstance().GetEntity()); FCDSkinController* skin = controller->GetSkinController(); REQUIRE(skin != NULL, "is skin controller"); FixSkeletonRoots(controllerInstance); // Data for joints is stored in two places - avoid overflows by limiting // to the minimum of the two sizes, and warn if they're different (which // happens in practice for slightly-broken meshes) size_t jointCount = std::min(skin->GetJointCount(), controllerInstance.GetJointCount()); if (skin->GetJointCount() != controllerInstance.GetJointCount()) { Log(LOG_WARNING, "Mismatched bone counts (skin has %d, skeleton has %d)", skin->GetJointCount(), controllerInstance.GetJointCount()); for (size_t i = 0; i < skin->GetJointCount(); ++i) Log(LOG_INFO, "Skin joint %d: %s", i, skin->GetJoint(i)->GetId().c_str()); for (size_t i = 0; i < controllerInstance.GetJointCount(); ++i) Log(LOG_INFO, "Skeleton joint %d: %s", i, controllerInstance.GetJoint(i)->GetName().c_str()); } // Get the skinned mesh for this entity FCDGeometry* baseGeometry = controller->GetBaseGeometry(); REQUIRE(baseGeometry != NULL, "controller has base geometry"); FCDGeometryPolygons* polys = GetPolysFromGeometry(baseGeometry); // Make sure it doesn't use more bones per vertex than the game can handle SkinReduceInfluences(skin, maxInfluences, 0.001f); // Convert the geometry into a suitable form for the game ReindexGeometry(polys, skin); const Skeleton& skeleton = FindSkeleton(controllerInstance); // Convert the bone influences into VertexBlend structures for the PMD: bool hasComplainedAboutNonexistentJoints = false; // because we want to emit a warning only once std::vector<VertexBlend> boneWeights; // one per vertex const FCDSkinControllerVertex* vertexInfluences = skin->GetVertexInfluences(); for (size_t i = 0; i < skin->GetInfluenceCount(); ++i) { VertexBlend influences = defaultInfluences; assert(vertexInfluences[i].GetPairCount() <= maxInfluences); // guaranteed by ReduceInfluences; necessary for avoiding // out-of-bounds writes to the VertexBlend for (size_t j = 0; j < vertexInfluences[i].GetPairCount(); ++j) { uint32 jointIdx = vertexInfluences[i].GetPair(j)->jointIndex; REQUIRE(jointIdx <= 0xFF, "sensible number of joints (<256)"); // because we only have a u8 to store them in // Find the joint on the skeleton, after checking it really exists FCDSceneNode* joint = NULL; if (jointIdx < controllerInstance.GetJointCount()) joint = controllerInstance.GetJoint(jointIdx); // Complain on error if (! joint) { if (! hasComplainedAboutNonexistentJoints) { Log(LOG_WARNING, "Vertexes influenced by nonexistent joint"); hasComplainedAboutNonexistentJoints = true; } continue; } // Store into the VertexBlend int boneId = skeleton.GetBoneID(joint->GetName().c_str()); if (boneId < 0) { // The relevant joint does exist, but it's not a recognised // bone in our chosen skeleton structure Log(LOG_ERROR, "Vertex influenced by unrecognised bone '%s'", joint->GetName().c_str()); continue; } influences.bones[j] = (uint8)boneId; influences.weights[j] = vertexInfluences[i].GetPair(j)->weight; } boneWeights.push_back(influences); } // Convert the bind pose into BoneTransform structures for the PMD: BoneTransform boneDefault = { { 0, 0, 0 }, { 0, 0, 0, 1 } }; // identity transform std::vector<BoneTransform> boneTransforms (skeleton.GetBoneCount(), boneDefault); for (size_t i = 0; i < jointCount; ++i) { FCDSceneNode* joint = controllerInstance.GetJoint(i); int boneId = skeleton.GetRealBoneID(joint->GetName().c_str()); if (boneId < 0) { // unrecognised joint - it's probably just a prop point // or something, so ignore it continue; } FMMatrix44 bindPose = skin->GetJoint(i)->GetBindPoseInverse().Inverted(); HMatrix matrix; memcpy(matrix, bindPose.Transposed().m, sizeof(matrix)); // set matrix = bindPose^T, to match what decomp_affine wants AffineParts parts; decomp_affine(matrix, &parts); BoneTransform b = { { parts.t.x, parts.t.y, parts.t.z }, { parts.q.x, parts.q.y, parts.q.z, parts.q.w } }; boneTransforms[boneId] = b; } // Construct the list of prop points. // Currently takes all objects that are directly attached to a // standard bone, and whose name begins with "prop-" or "prop_". std::vector<PropPoint> propPoints; AddDefaultPropPoints(propPoints); for (size_t i = 0; i < jointCount; ++i) { FCDSceneNode* joint = controllerInstance.GetJoint(i); int boneId = skeleton.GetBoneID(joint->GetName().c_str()); if (boneId < 0) { // unrecognised joint name - ignore, same as before continue; } // Check all the objects attached to this bone for (size_t j = 0; j < joint->GetChildrenCount(); ++j) { FCDSceneNode* child = joint->GetChild(j); if (child->GetName().find("prop-") != 0 && child->GetName().find("prop_") != 0) { // doesn't begin with "prop-", so skip it continue; } // Strip off the "prop-" from the name std::string propPointName (child->GetName().substr(5)); Log(LOG_INFO, "Adding prop point %s", propPointName.c_str()); // Get translation and orientation of local transform FMMatrix44 localTransform = child->ToMatrix(); HMatrix matrix; memcpy(matrix, localTransform.Transposed().m, sizeof(matrix)); AffineParts parts; decomp_affine(matrix, &parts); // Add prop point to list PropPoint p = { propPointName, { parts.t.x, parts.t.y, parts.t.z }, { parts.q.x, parts.q.y, parts.q.z, parts.q.w }, (uint8)boneId }; propPoints.push_back(p); } } // Get the raw vertex data FCDGeometryPolygonsInput* inputPosition = polys->FindInput(FUDaeGeometryInput::POSITION); FCDGeometryPolygonsInput* inputNormal = polys->FindInput(FUDaeGeometryInput::NORMAL); FCDGeometryPolygonsInput* inputTexcoord = polys->FindInput(FUDaeGeometryInput::TEXCOORD); const uint32* indicesCombined = inputPosition->GetIndices(); size_t indicesCombinedCount = inputPosition->GetIndexCount(); // (ReindexGeometry guarantees position/normal/texcoord have the same indexes) FCDGeometrySource* sourcePosition = inputPosition->GetSource(); FCDGeometrySource* sourceNormal = inputNormal ->GetSource(); FCDGeometrySource* sourceTexcoord = inputTexcoord->GetSource(); float* dataPosition = sourcePosition->GetData(); float* dataNormal = sourceNormal ->GetData(); float* dataTexcoord = sourceTexcoord->GetData(); size_t vertexCount = sourcePosition->GetDataCount() / 3; assert(sourcePosition->GetDataCount() == vertexCount*3); assert(sourceNormal ->GetDataCount() == vertexCount*3); assert(sourceTexcoord->GetDataCount() == vertexCount*2); // Transform model coordinate system to game coordinates TransformSkinnedModel(dataPosition, dataNormal, vertexCount, boneTransforms, propPoints, converter.GetEntityTransform(), skin->GetBindShapeTransform(), converter.IsYUp(), converter.IsXSI()); WritePMD(output, indicesCombined, indicesCombinedCount, dataPosition, dataNormal, dataTexcoord, vertexCount, boneWeights, boneTransforms, propPoints); } else { throw ColladaException("Unrecognised object type"); } }
bool CModelConverter::ReadDAE(const tstring& sFilename) { if (m_pWorkListener) m_pWorkListener->BeginProgress(); FCollada::Initialize(); FCDocument* pDoc = FCollada::NewTopDocument(); if (m_pWorkListener) m_pWorkListener->SetAction("Reading file", 0); if (FCollada::LoadDocumentFromFile(pDoc, convert_to_fstring(sFilename))) { size_t i; FCDocumentTools::StandardizeUpAxisAndLength(pDoc, FMVector3(0, 1, 0)); FCDMaterialLibrary* pMatLib = pDoc->GetMaterialLibrary(); size_t iEntities = pMatLib->GetEntityCount(); if (m_pWorkListener) m_pWorkListener->SetAction("Reading materials", iEntities); for (i = 0; i < iEntities; ++i) { FCDMaterial* pColladaMaterial = pMatLib->GetEntity(i); FCDEffect* pEffect = pColladaMaterial->GetEffect(); size_t iMaterial = m_pScene->AddMaterial(convert_from_fstring(pColladaMaterial->GetName())); CConversionMaterial* pMaterial = m_pScene->GetMaterial(iMaterial); if (pEffect->GetProfileCount() < 1) continue; FCDEffectProfile* pEffectProfile = pEffect->GetProfile(0); FUDaeProfileType::Type eProfileType = pEffectProfile->GetType(); if (eProfileType == FUDaeProfileType::COMMON) { FCDEffectStandard* pStandardProfile = dynamic_cast<FCDEffectStandard*>(pEffectProfile); if (pStandardProfile) { pMaterial->m_vecAmbient = Vector((float*)pStandardProfile->GetAmbientColor()); pMaterial->m_vecDiffuse = Vector((float*)pStandardProfile->GetDiffuseColor()); pMaterial->m_vecSpecular = Vector((float*)pStandardProfile->GetSpecularColor()); pMaterial->m_vecEmissive = Vector((float*)pStandardProfile->GetEmissionColor()); pMaterial->m_flShininess = pStandardProfile->GetShininess(); } } for (size_t j = 0; j < pEffectProfile->GetEffectParameterCount(); j++) { FCDEffectParameter* pEffectParameter = pEffectProfile->GetEffectParameter(j); FCDEffectParameter::Type eType = pEffectParameter->GetType(); if (eType == FCDEffectParameter::SAMPLER) { FCDEffectParameterSampler* pSampler = dynamic_cast<FCDEffectParameterSampler*>(pEffectParameter); if (pSampler) { FCDEffectParameterSurface* pSurface = pSampler->GetSurface(); if (pSurface) { if (pSurface->GetImageCount()) { // Christ Collada why do you have to make things so damn complicated? FCDImage* pImage = pSurface->GetImage(0); pMaterial->m_sDiffuseTexture = convert_from_fstring(pImage->GetFilename()); // Fix up a bug in the Max Collada exporter if (pMaterial->m_sDiffuseTexture.startswith("\\\\C\\")) pMaterial->m_sDiffuseTexture = "C:\\" + pMaterial->m_sDiffuseTexture.substr(4); else if (pMaterial->m_sDiffuseTexture.startswith("\\\\D\\")) pMaterial->m_sDiffuseTexture = "D:\\" + pMaterial->m_sDiffuseTexture.substr(4); } } } } } if (m_pWorkListener) m_pWorkListener->WorkProgress(i+1); } FCDGeometryLibrary* pGeoLib = pDoc->GetGeometryLibrary(); iEntities = pGeoLib->GetEntityCount(); if (m_pWorkListener) m_pWorkListener->SetAction("Loading entities", iEntities); for (i = 0; i < iEntities; ++i) { FCDGeometry* pGeometry = pGeoLib->GetEntity(i); if (pGeometry->IsMesh()) { size_t j; size_t iMesh = m_pScene->AddMesh(convert_from_fstring(pGeometry->GetName())); CConversionMesh* pMesh = m_pScene->GetMesh(iMesh); pMesh->AddBone(convert_from_fstring(pGeometry->GetName())); FCDGeometryMesh* pGeoMesh = pGeometry->GetMesh(); FCDGeometrySource* pPositionSource = pGeoMesh->GetPositionSource(); size_t iVertexCount = pPositionSource->GetValueCount(); for (j = 0; j < iVertexCount; j++) { const float* pflValues = pPositionSource->GetValue(j); pMesh->AddVertex(pflValues[0], pflValues[1], pflValues[2]); } FCDGeometrySource* pNormalSource = pGeoMesh->FindSourceByType(FUDaeGeometryInput::NORMAL); if (pNormalSource) { iVertexCount = pNormalSource->GetValueCount(); for (j = 0; j < iVertexCount; j++) { const float* pflValues = pNormalSource->GetValue(j); pMesh->AddNormal(pflValues[0], pflValues[1], pflValues[2]); } } FCDGeometrySource* pUVSource = pGeoMesh->FindSourceByType(FUDaeGeometryInput::TEXCOORD); if (pUVSource) { iVertexCount = pUVSource->GetValueCount(); for (j = 0; j < iVertexCount; j++) { const float* pflValues = pUVSource->GetValue(j); pMesh->AddUV(pflValues[0], pflValues[1]); } } for (j = 0; j < pGeoMesh->GetPolygonsCount(); j++) { FCDGeometryPolygons* pPolygons = pGeoMesh->GetPolygons(j); FCDGeometryPolygonsInput* pPositionInput = pPolygons->FindInput(FUDaeGeometryInput::POSITION); FCDGeometryPolygonsInput* pNormalInput = pPolygons->FindInput(FUDaeGeometryInput::NORMAL); FCDGeometryPolygonsInput* pUVInput = pPolygons->FindInput(FUDaeGeometryInput::TEXCOORD); size_t iPositionCount = pPositionInput->GetIndexCount(); uint32* pPositions = pPositionInput->GetIndices(); size_t iNormalCount = 0; uint32* pNormals = NULL; if (pNormalInput) { iNormalCount = pNormalInput->GetIndexCount(); pNormals = pNormalInput->GetIndices(); } size_t iUVCount = 0; uint32* pUVs = NULL; if (pUVInput) { iUVCount = pUVInput->GetIndexCount(); pUVs = pUVInput->GetIndices(); } fm::stringT<fchar> sMaterial = pPolygons->GetMaterialSemantic(); size_t iCurrentMaterial = pMesh->AddMaterialStub(convert_from_fstring(sMaterial)); if (pPolygons->TestPolyType() == 3) { // All triangles! for (size_t k = 0; k < iPositionCount; k+=3) { size_t iFace = pMesh->AddFace(iCurrentMaterial); pMesh->AddVertexToFace(iFace, pPositions[k+0], pUVs?pUVs[k+0]:~0, pNormals?pNormals[k+0]:~0); pMesh->AddVertexToFace(iFace, pPositions[k+1], pUVs?pUVs[k+1]:~0, pNormals?pNormals[k+1]:~0); pMesh->AddVertexToFace(iFace, pPositions[k+2], pUVs?pUVs[k+2]:~0, pNormals?pNormals[k+2]:~0); } } else if (pPolygons->TestPolyType() == 4) { // All quads! for (size_t k = 0; k < iPositionCount; k+=4) { size_t iFace = pMesh->AddFace(iCurrentMaterial); pMesh->AddVertexToFace(iFace, pPositions[k+0], pUVs?pUVs[k+0]:~0, pNormals?pNormals[k+0]:~0); pMesh->AddVertexToFace(iFace, pPositions[k+1], pUVs?pUVs[k+1]:~0, pNormals?pNormals[k+1]:~0); pMesh->AddVertexToFace(iFace, pPositions[k+2], pUVs?pUVs[k+2]:~0, pNormals?pNormals[k+2]:~0); pMesh->AddVertexToFace(iFace, pPositions[k+3], pUVs?pUVs[k+3]:~0, pNormals?pNormals[k+3]:~0); } } else { size_t iFaces = pPolygons->GetFaceCount(); for (size_t k = 0; k < iFaces; k++) { size_t iFace = pMesh->AddFace(iCurrentMaterial); size_t o = pPolygons->GetFaceVertexOffset(k); size_t iFaceVertexCount = pPolygons->GetFaceVertexCount(k); for (size_t v = 0; v < iFaceVertexCount; v++) pMesh->AddVertexToFace(iFace, pPositions[o+v], pUVs?pUVs[o+v]:~0, pNormals?pNormals[o+v]:~0); } } } } if (m_pWorkListener) m_pWorkListener->WorkProgress(i+1); } FCDVisualSceneNodeLibrary* pVisualScenes = pDoc->GetVisualSceneLibrary(); iEntities = pVisualScenes->GetEntityCount(); for (i = 0; i < iEntities; ++i) { FCDSceneNode* pNode = pVisualScenes->GetEntity(i); size_t iScene = m_pScene->AddScene(convert_from_fstring(pNode->GetName())); ReadDAESceneTree(pNode, m_pScene->GetScene(iScene)); } } else { printf("Oops! Some kind of error happened!\n"); return false; } m_pScene->SetWorkListener(m_pWorkListener); m_pScene->CalculateExtends(); for (size_t i = 0; i < m_pScene->GetNumMeshes(); i++) { if (m_bWantEdges) m_pScene->GetMesh(i)->CalculateEdgeData(); if (m_pScene->GetMesh(i)->GetNumNormals() == 0) m_pScene->GetMesh(i)->CalculateVertexNormals(); m_pScene->GetMesh(i)->CalculateVertexTangents(); } pDoc->Release(); FCollada::Release(); if (m_pWorkListener) m_pWorkListener->EndProgress(); return true; }