static Matrix4x4 GetMatrix4x4FromIMatrix(spMatrix4x4 src) { float[] srcElements = new float[16]; src.GetElements(srcElements); return(new Matrix4x4(srcElements)); }
static void bend_geometry(ISimplygonSDK sdk, spScene scene) { // The two bones that influence the vertices //spMatrix4x4 sp_bone1; spGeometryData geom = Utils.SimplygonCast <spSceneMesh>(scene.GetRootNode().GetChild(0)).GetGeometry(); // get the bone weights field and ids spRealArray boneWeights = geom.GetBoneWeights(); spRidArray boneIds = geom.GetBoneIds(); // get the Coordinates field spRealArray Coords = geom.GetCoords(); // now, transform all vertices' coordinates using the bones for (int v = 0; v < Coords.GetTupleCount(); ++v) { Vector3D vtx = new Vector3D(Coords.GetItem(v * 3 + 0), Coords.GetItem(v * 3 + 1), Coords.GetItem(v * 3 + 2)); uint no = boneIds.GetItemCount(); int bone_id_1 = boneIds.GetItem(v * 2 + 0); int bone_id_2 = boneIds.GetItem(v * 2 + 1); spSceneBone bone_1; spSceneBone bone_2; Matrix4x4 b1gtMat; Matrix4x4 b2gtMat; Vector3D vtx1 = new Vector3D(vtx); Vector3D vtx2 = new Vector3D(vtx); if (bone_id_1 != -1) { bone_1 = scene.GetBoneTable().GetBone(bone_id_1); //retrieve the global transform of the bone in bind space spMatrix4x4 rootGlobalTransform = sdk.CreateMatrix4x4(); bone_1.EvaluateDefaultGlobalTransformation(rootGlobalTransform); //apply transfrom to animate bone spTransform3 bone1_transform = sdk.CreateTransform3(); bone1_transform.AddTransformation(rootGlobalTransform); bone1_transform.AddRotation(GetRadFromDegrees(-30), 1, 0, 0); rootGlobalTransform = bone1_transform.GetMatrix(); //apply transform b1gtMat = GetMatrix4x4FromIMatrix(rootGlobalTransform); vtx1 = b1gtMat.MultiplyPointVector(vtx); } if (bone_id_2 != -1) { bone_2 = scene.GetBoneTable().GetBone(bone_id_2); spMatrix4x4 boneGlobalTransform = sdk.CreateMatrix4x4(); bone_2.EvaluateDefaultGlobalTransformation(boneGlobalTransform); spTransform3 bone2_transform = sdk.CreateTransform3(); //transform into bone2's local space and apply transform bone2_transform.PreMultiply(); boneGlobalTransform.Invert(); bone2_transform.AddTransformation(boneGlobalTransform); bone2_transform.AddRotation(GetRadFromDegrees(-30), 1, 0, 0); bone2_transform.AddTranslation(0.0f, 17.5f, 0.0f); //apply transform of the first bone bone2_transform.AddRotation(GetRadFromDegrees(-30), 1, 0, 0); //get the global transform matrix for the animation pose boneGlobalTransform = bone2_transform.GetMatrix(); b2gtMat = GetMatrix4x4FromIMatrix(boneGlobalTransform); //apply transform to the vertex vtx2 = b2gtMat.MultiplyPointVector(vtx); } //get the bone weights from the geometry data float blend1 = boneWeights.GetItem(v * 2 + 0); float blend2 = boneWeights.GetItem(v * 2 + 1); // normalize the blend float blend_scale = 1.0f / (blend1 + blend2); blend1 *= blend_scale; blend2 *= blend_scale; // do a linear blend vtx = vtx1 * blend1 + vtx2 * blend2; // store in coordinates Coords.SetItem(v * 3 + 0, vtx.x); Coords.SetItem(v * 3 + 1, vtx.y); Coords.SetItem(v * 3 + 2, vtx.z); } }