FUBoundingSphere FUBoundingSphere::Transform(const FMMatrix44& transform) const { if (!IsValid()) return (*this); FMVector3 transformedCenter = transform.TransformCoordinate(center); FUBoundingSphere transformedSphere(transformedCenter, 0.0f); // Calculate the transformed bounding sphere radius using three sample points. FMVector3 testPoints[3] = { FMVector3(radius, 0.0f, 0.0f), FMVector3(0.0f, radius, 0.0f), FMVector3(0.0f, 0.0f, radius) }; for (size_t i = 0; i < 6; ++i) { testPoints[i] = transform.TransformVector(testPoints[i]); float lengthSquared = testPoints[i].LengthSquared(); if (lengthSquared > transformedSphere.radius * transformedSphere.radius) { transformedSphere.radius = sqrtf(lengthSquared); } } return transformedSphere; }
static void AddStaticPropPoints(std::vector<PropPoint> &propPoints, const FMMatrix44& upAxisTransform, FCDSceneNode* node) { if (node->GetName().find("prop-") == 0 || node->GetName().find("prop_") == 0) { // Strip off the "prop-" from the name std::string propPointName (node->GetName().substr(5)); Log(LOG_INFO, "Adding prop point %s", propPointName.c_str()); // CalculateWorldTransform applies transformations recursively for all parents of this node // upAxisTransform transforms this node to right-handed Z_UP coordinates FMMatrix44 transform = upAxisTransform * node->CalculateWorldTransform(); HMatrix matrix; memcpy(matrix, transform.Transposed().m, sizeof(matrix)); AffineParts parts; decomp_affine(matrix, &parts); // Add prop point in game coordinates PropPoint p = { propPointName, // Flip translation across the x-axis by swapping y and z { parts.t.x, parts.t.z, parts.t.y }, // To convert the quaternions: imagine you're using the axis/angle // representation, then swap the y,z basis vectors and change the // direction of rotation by negating the angle ( => negating sin(angle) // => negating x,y,z => changing (x,y,z,w) to (-x,-z,-y,w) // but then (-x,-z,-y,w) == (x,z,y,-w) so do that instead) { parts.q.x, parts.q.z, parts.q.y, -parts.q.w }, 0xff }; propPoints.push_back(p); } // Search children for prop points for (size_t i = 0; i < node->GetChildrenCount(); ++i) AddStaticPropPoints(propPoints, upAxisTransform, node->GetChild(i)); }
/** * Applies world-space transform to vertex data and transforms Collada's right-handed * Y-up / Z-up coordinates to the game's left-handed Y-up coordinate system */ static void TransformStaticModel(float* position, float* normal, size_t vertexCount, const FMMatrix44& transform, bool yUp) { for (size_t i = 0; i < vertexCount; ++i) { FMVector3 pos (&position[i*3], 0); FMVector3 norm (&normal[i*3], 0); // Apply the scene-node transforms pos = transform.TransformCoordinate(pos); norm = FMVector3_Normalize(transform.TransformVector(norm)); // Convert from right-handed Y_UP or Z_UP to the game's coordinate system (left-handed Y-up) if (yUp) { pos.z = -pos.z; norm.z = -norm.z; } else { std::swap(pos.y, pos.z); std::swap(norm.y, norm.z); } // Copy back to array position[i*3] = pos.x; position[i*3+1] = pos.y; position[i*3+2] = pos.z; normal[i*3] = norm.x; normal[i*3+1] = norm.y; normal[i*3+2] = norm.z; } }
/** * Applies world-space transform to vertex data and transforms Collada's right-handed * Y-up / Z-up coordinates to the game's left-handed Y-up coordinate system */ static void TransformSkinnedModel(float* position, float* normal, size_t vertexCount, std::vector<BoneTransform>& bones, std::vector<PropPoint>& propPoints, const FMMatrix44& transform, const FMMatrix44& bindTransform, bool yUp, bool isXSI) { FMMatrix44 scaledTransform; // for vertexes FMMatrix44 scaleMatrix; // for bones // HACK: see comment in PSAConvert::TransformVertices if (isXSI) { scaleMatrix = DecomposeToScaleMatrix(transform); scaledTransform = DecomposeToScaleMatrix(bindTransform) * transform; } else { scaleMatrix = FMMatrix44_Identity; scaledTransform = bindTransform; } // Update the vertex positions and normals for (size_t i = 0; i < vertexCount; ++i) { FMVector3 pos (&position[i*3], 0); FMVector3 norm (&normal[i*3], 0); // Apply the scene-node transforms pos = scaledTransform.TransformCoordinate(pos); norm = FMVector3_Normalize(scaledTransform.TransformVector(norm)); // Convert from right-handed Y_UP or Z_UP to the game's coordinate system (left-handed Y-up) if (yUp) { pos.z = -pos.z; norm.z = -norm.z; } else { std::swap(pos.y, pos.z); std::swap(norm.y, norm.z); } // and copy back into the original array position[i*3] = pos.x; position[i*3+1] = pos.y; position[i*3+2] = pos.z; normal[i*3] = norm.x; normal[i*3+1] = norm.y; normal[i*3+2] = norm.z; } TransformBones(bones, scaleMatrix, yUp); // And do the same for prop points for (size_t i = 0; i < propPoints.size(); ++i) { if (yUp) { propPoints[i].translation[0] = -propPoints[i].translation[0]; propPoints[i].orientation[0] = -propPoints[i].orientation[0]; propPoints[i].orientation[3] = -propPoints[i].orientation[3]; } else { // Flip translation across the x-axis by swapping y and z std::swap(propPoints[i].translation[1], propPoints[i].translation[2]); // To convert the quaternions: imagine you're using the axis/angle // representation, then swap the y,z basis vectors and change the // direction of rotation by negating the angle ( => negating sin(angle) // => negating x,y,z => changing (x,y,z,w) to (-x,-z,-y,w) // but then (-x,-z,-y,w) == (x,z,y,-w) so do that instead) std::swap(propPoints[i].orientation[1], propPoints[i].orientation[2]); propPoints[i].orientation[3] = -propPoints[i].orientation[3]; } } }
/** * 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"); } }