public void UpdateTransforms(ModelNode pNode, Matrix4 transf) { string nodename = pNode.Name; Matrix4 nodeTransf = Matrix4.Identity; double time; SingleAnimationNode pNodeAnim = FindNodeAnim(nodename, pNode.Mode, out time); if (pNodeAnim != null) { BEPUutilities.Vector3 vec = pNodeAnim.lerpPos(time); BEPUutilities.Quaternion quat = pNodeAnim.lerpRotate(time); OpenTK.Quaternion oquat = new OpenTK.Quaternion(quat.X, quat.Y, quat.Z, quat.W); Matrix4 trans; Matrix4.CreateTranslation(vec.X, vec.Y, vec.Z, out trans); trans.Transpose(); Matrix4 rot; Matrix4.CreateFromQuaternion(ref oquat, out rot); rot.Transpose(); Matrix4.Mult(ref trans, ref rot, out nodeTransf); } Matrix4 global; Matrix4.Mult(ref transf, ref nodeTransf, out global); for (int i = 0; i < pNode.Bones.Count; i++) { //Matrix4 modded; //Matrix4.Mult(ref globalInverse, ref global, out modded); Matrix4.Mult(ref global, ref pNode.Bones[i].Offset, out pNode.Bones[i].Transform); } for (int i = 0; i < pNode.Children.Count; i++) { UpdateTransforms(pNode.Children[i], global); } }
//Animation frame data collection methods public static Quaternion fetchRotQuaternion(TkAnimNodeData node, TkAnimMetadata animMeta, int frameCounter) { //Load Frames //Console.WriteLine("Setting Frame Index {0}", frameIndex); TkAnimNodeFrameData frame = animMeta.AnimFrameData[frameCounter]; TkAnimNodeFrameData stillframe = animMeta.StillFrameData; OpenTK.Quaternion q; //Check if there is a rotation for that node if (node.RotIndex < frame.Rotations.Count) { int rotindex = node.RotIndex; q = new OpenTK.Quaternion(frame.Rotations[rotindex].x, frame.Rotations[rotindex].y, frame.Rotations[rotindex].z, frame.Rotations[rotindex].w); } else //Load stillframedata { int rotindex = node.RotIndex - frame.Rotations.Count; q = new OpenTK.Quaternion(stillframe.Rotations[rotindex].x, stillframe.Rotations[rotindex].y, stillframe.Rotations[rotindex].z, stillframe.Rotations[rotindex].w); } return(q); }
/// <summary> /// Gets the forward vector of this controller. /// </summary> /// <returns></returns> public Location ForwardVector() { OpenTK.Quaternion loquat = Position.ExtractRotation(true); BEPUutilities.Quaternion lquat = new BEPUutilities.Quaternion(loquat.X, loquat.Y, loquat.Z, loquat.W); BEPUutilities.Vector3 lforw = -BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitZ, lquat); return(new Location(lforw)); }
public static OpenTK.Matrix4 GLSRT(OpenTK.Vector3 pos, OpenTK.Quaternion qua, OpenTK.Vector3 scale) { OpenTK.Matrix4 t = OpenTK.Matrix4.CreateTranslation(pos); OpenTK.Matrix4 r = OpenTK.Matrix4.CreateFromQuaternion(qua); OpenTK.Matrix4 s = OpenTK.Matrix4.CreateScale(scale); return(s * r * t); }
public override void applyPoses(Dictionary <string, Matrix4> poseMatrices) { foreach (KeyValuePair <string, Matrix4> kp in poseMatrices) { string node_name = kp.Key; if (jointDict.ContainsKey(node_name)) { Joint j = jointDict[node_name]; //j.localPoseMatrix = kp.Value; //Vector3 tr = kp.Value.ExtractTranslation(); Vector3 sc = kp.Value.ExtractScale(); OpenTK.Quaternion q = kp.Value.ExtractRotation(); //j.localRotation = Matrix4.CreateFromQuaternion(q); //j.localPosition = tr; j.localRotation = Matrix4.CreateFromQuaternion(q) * j.__localRotation; j.localScale = sc; //j.localPoseMatrix = kp.Value; } } update(); }
public void Update(float dt) { m_fDeltaTime = dt; m_v3LinearAcceleration = m_fGravity + (m_v3Force / m_fMass); m_v3LinearVelocity += m_v3LinearAcceleration * m_fDeltaTime; m_v3Position += m_v3LinearVelocity * m_fDeltaTime; Matrix4 m4Translate = Matrix4.CreateTranslation(m_v3Position); m_v3AngularAcceleration = (m_v3Torque / m_fMass); m_v3AngularVelocity += m_v3AngularAcceleration * m_fDeltaTime; m_v3Rotate += m_v3AngularVelocity * m_fDeltaTime; // damping m_v3LinearVelocity *= (float)Math.Pow(m_fLinearDamping, m_fDeltaTime); m_v3AngularVelocity *= (float)Math.Pow(m_fAngularDamping, m_fDeltaTime); //Matrix4 m4RotX = Matrix4.CreateRotationX(m_v3Rotate.X); //Matrix4 m4RotY = Matrix4.CreateRotationY(m_v3Rotate.Y); //Matrix4 m4RotZ = Matrix4.CreateRotationZ(m_v3Rotate.Z); //Matrix4 m4RotXYZ = Matrix4.Mult(Matrix4.Mult(m4RotX, m4RotY), m4RotZ); OpenTK.Quaternion quat = ToQuaternion(m_v3Rotate.X, m_v3Rotate.Y, m_v3Rotate.Z); Matrix4 m4Rotate = Matrix4.CreateFromQuaternion(quat); m_m4World = Matrix4.Mult(m4Rotate, m4Translate); }
public static void AssertEquals(TK.Quaternion q1, Quaternion q2, String msg) { AssertEquals(q1.X, q2.X, msg + String.Format(" => checking X component ({0} == {1}", q1.X, q2.X)); AssertEquals(q1.Y, q2.Y, msg + String.Format(" => checking Y component ({0} == {1}", q1.Y, q2.Y)); AssertEquals(q1.Z, q2.Z, msg + String.Format(" => checking Z component ({0} == {1}", q1.Z, q2.Z)); AssertEquals(q1.W, q2.W, msg + String.Format(" => checking W component ({0} == {1}", q1.W, q2.W)); }
//No longer previewing protected override void LeftMouseUpResizing(Viewport2D viewport, ViewportEvent e) { if (_currentTool == null) { base.LeftMouseUpResizing(viewport, e); return; } var transformation = GetTransformMatrix(viewport, e); if (transformation.HasValue) { Matrix4 m = (Matrix4)transformation; Vector3 c = m.ExtractTranslation(); OpenTK.Quaternion q = m.ExtractRotation(); if (_currentTool.GetType() == typeof(MoveTool)) { TotalTranslation += new Coordinate((decimal)c.X, (decimal)c.Y, (decimal)c.Z); } else if (_currentTool.GetType() == typeof(RotateTool)) { q.Invert(); TotalRotation *= q; } _form.KeyFrameEdit(-1, TotalTranslation, ToGeometric(TotalRotation)); //var createClone = KeyboardState.Shift && State.Handle == ResizeHandle.Center; //ExecuteTransform(_currentTool.GetTransformName(), CreateMatrixMultTransformation(transformation.Value), createClone); } Document.EndSelectionTransform(); State.ActiveViewport = null; State.Action = BoxAction.Drawn; SelectionChanged(); }
/// <summary> /// Function to blend from one keyframe to another. /// </summary> public void ChangeLocalFixedDataBlend(ActionState st) { Debug.Assert(0 <= st.KfBlend && st.KfBlend <= 1); foreach (NodeAnimationChannel channel in _action.NodeAnimationChannels) { BoneNode bone_nd = _scene.GetBoneNode(channel.NodeName); // now rotation tk.Quaternion target_roto = tk.Quaternion.Identity; if (channel.RotationKeyCount > st.TargetKeyframe) { target_roto = channel.RotationKeys[st.TargetKeyframe].Value.eToOpenTK(); } tk.Quaternion start_frame_roto = channel.RotationKeys[st.OriginKeyframe].Value.eToOpenTK(); tk.Quaternion result_roto = tk.Quaternion.Slerp(start_frame_roto, target_roto, (float)st.KfBlend); // now translation tk.Vector3 target_trans = tk.Vector3.Zero; if (channel.PositionKeyCount > st.TargetKeyframe) { target_trans = channel.PositionKeys[st.TargetKeyframe].Value.eToOpenTK(); } tk.Vector3 cur_trans = channel.PositionKeys[st.OriginKeyframe].Value.eToOpenTK(); tk.Vector3 result_trans = cur_trans + tk.Vector3.Multiply(target_trans - cur_trans, (float)st.KfBlend); // combine rotation and translation tk.Matrix4 result = tk.Matrix4.CreateFromQuaternion(result_roto); result.Row3.Xyz = result_trans; bone_nd.LocTrans = result.eToAssimp(); } }
public static Assimp.Quaternion convertQuaternion(OpenTK.Quaternion localQuat) { Assimp.Quaternion q = new Assimp.Quaternion(); q.X = localQuat.X; q.Y = localQuat.Y; q.Z = localQuat.Z; q.W = localQuat.W; return(q); }
private OpenTK.Quaternion TKQuaternion(Assimp.Quaternion rot) { OpenTK.Quaternion quat = new OpenTK.Quaternion(); quat.X = rot.X; quat.Y = rot.Y; quat.Z = rot.Z; quat.W = rot.W; return quat; }
public static Matrix4 ConvertGLTFTRSToOpenTKMatrix(float[] scale, float[] rotation, float[] translation) { OpenTK.Quaternion r = new OpenTK.Quaternion(rotation[0], rotation[1], rotation[2], rotation[3]); Matrix4 rotationMatrix = Matrix4.CreateFromQuaternion(r); Matrix4 scaleMatrix = Matrix4.CreateScale(scale[0], scale[1], scale[2]); Matrix4 translationMatrix = Matrix4.CreateTranslation(translation[0], translation[1], translation[2]); return(scaleMatrix * rotationMatrix * translationMatrix); }
public static OpenTK.Quaternion TKQuaternion(Assimp.Quaternion rot) { OpenTK.Quaternion quat = new OpenTK.Quaternion(); quat.X = rot.X; quat.Y = rot.Y; quat.Z = rot.Z; quat.W = rot.W; return(quat); }
private DataStructures.Geometric.Quaternion ToGeometric(OpenTK.Quaternion q) { DataStructures.Geometric.Quaternion ret; ret = new DataStructures.Geometric.Quaternion((decimal)q.X, (decimal)q.Y, (decimal)q.Z, (decimal)q.W); return(ret); }
public void TestGetMatrix() { TK.Quaternion tkQ = TK.Quaternion.FromAxisAngle(new TK.Vector3(.25f, .5f, 0.0f), TK.MathHelper.PiOver2); Quaternion q = new Quaternion(tkQ.W, tkQ.X, tkQ.Y, tkQ.Z); TK.Matrix4 tkM = TK.Matrix4.CreateFromAxisAngle(new TK.Vector3(.25f, .5f, 0.0f), TK.MathHelper.PiOver2); Matrix4x4 m = q.GetMatrix(); TestHelper.AssertEquals(tkM, m, "Testing GetMatrix"); }
public void TestConjugate() { TK.Quaternion tkQ = TK.Quaternion.FromAxisAngle(TK.Vector3.UnitY, TK.MathHelper.PiOver2); Quaternion q = new Quaternion(tkQ.W, tkQ.X, tkQ.Y, tkQ.Z); tkQ.Conjugate(); q.Conjugate(); TestHelper.AssertEquals(tkQ.X, tkQ.Y, tkQ.Z, tkQ.W, q, "Testing conjugate"); }
internal RenderModelMarkerBlockBase(BinaryReader binaryReader) { this.regionIndex = binaryReader.ReadByte(); this.permutationIndex = binaryReader.ReadByte(); this.nodeIndex = binaryReader.ReadByte(); this.invalidName_ = binaryReader.ReadBytes(1); this.translation = binaryReader.ReadVector3(); this.rotation = binaryReader.ReadQuaternion(); this.scale = binaryReader.ReadSingle(); }
public void TestNormalize() { TK.Quaternion tkQ = TK.Quaternion.FromAxisAngle(new TK.Vector3(.25f, .5f, 0.0f), TK.MathHelper.PiOver2); Quaternion q = new Quaternion(tkQ.W, tkQ.X, tkQ.Y, tkQ.Z); tkQ.Normalize(); q.Normalize(); TestHelper.AssertEquals(tkQ.X, tkQ.Y, tkQ.Z, tkQ.W, q, "Testing normalize"); }
internal StructureBspEnvironmentObjectBlockBase(BinaryReader binaryReader) { this.name = binaryReader.ReadString32(); this.rotation = binaryReader.ReadQuaternion(); this.translation = binaryReader.ReadVector3(); this.paletteIndex = binaryReader.ReadShortBlockIndex1(); this.invalidName_ = binaryReader.ReadBytes(2); this.uniqueID = binaryReader.ReadInt32(); this.exportedObjectType = binaryReader.ReadTagClass(); this.scenarioObjectName = binaryReader.ReadString32(); }
public override void ToolSelected(bool preventHistory) { _form.Show(Editor.Instance); Editor.Instance.Focus(); TotalTranslation = Coordinate.Zero; TotalRotation = new OpenTK.Quaternion(0, 0, 0, 1); _currentTool = null; _lastTool = null; _form.OnShow(); }
// Change standard T-pose to TDA T-pose private static void FixTdaBonesAndVertices([NotNull, ItemNotNull] IReadOnlyList <PmxBone> bones, [NotNull, ItemNotNull] IReadOnlyList <PmxVertex> vertices) { var defRotRight = Quaternion.FromEulerAngles(0, 0, MathHelper.DegreesToRadians(34.5f)); var defRotLeft = Quaternion.FromEulerAngles(0, 0, MathHelper.DegreesToRadians(-34.5f)); var leftArm = bones.SingleOrDefault(b => b.Name == "左腕"); var rightArm = bones.SingleOrDefault(b => b.Name == "右腕"); Debug.Assert(leftArm != null, nameof(leftArm) + " != null"); Debug.Assert(rightArm != null, nameof(rightArm) + " != null"); leftArm.AnimatedRotation = defRotLeft; rightArm.AnimatedRotation = defRotRight; foreach (var bone in bones) { if (bone.ParentIndex >= 0) { bone.Parent = bones[bone.ParentIndex]; } } foreach (var bone in bones) { bone.SetToVmdPose(true); } foreach (var vertex in vertices) { var m = Matrix4.Zero; for (var j = 0; j < 4; ++j) { var boneWeight = vertex.BoneWeights[j]; if (!boneWeight.IsValid) { continue; } m = m + bones[boneWeight.BoneIndex].SkinMatrix * boneWeight.Weight; } vertex.Position = Vector3.TransformPosition(vertex.Position, m); vertex.Normal = Vector3.TransformNormal(vertex.Normal, m); } foreach (var bone in bones) { bone.InitialPosition = bone.CurrentPosition; } }
public void TestRotate() { TK.Vector3 tkV1 = new TK.Vector3(0, 5, 10); Vector3D v1 = new Vector3D(0, 5, 10); TK.Quaternion tkQ = TK.Quaternion.FromAxisAngle(TK.Vector3.UnitY, TK.MathHelper.PiOver2); Quaternion q = new Quaternion(tkQ.W, tkQ.X, tkQ.Y, tkQ.Z); TK.Vector3 tkV2 = TK.Vector3.Transform(tkV1, tkQ); Vector3D v2 = Quaternion.Rotate(v1, q); TestHelper.AssertEquals(tkV2.X, tkV2.Y, tkV2.Z, v2, "Testing rotate"); }
public void TestOpMultiply() { TK.Quaternion tkQ1 = TK.Quaternion.FromAxisAngle(TK.Vector3.UnitY, TK.MathHelper.PiOver2); Quaternion q1 = new Quaternion(tkQ1.W, tkQ1.X, tkQ1.Y, tkQ1.Z); TK.Quaternion tkQ2 = TK.Quaternion.FromAxisAngle(TK.Vector3.UnitY, TK.MathHelper.Pi); Quaternion q2 = new Quaternion(tkQ2.W, tkQ2.X, tkQ2.Y, tkQ2.Z); Quaternion q = q1 * q2; TK.Quaternion tkQ = tkQ1 * tkQ2; TestHelper.AssertEquals(tkQ.X, tkQ.Y, tkQ.Z, tkQ.W, q, "Testing Op multiply"); }
public void TestSlerp() { TK.Quaternion tkQ1 = TK.Quaternion.FromAxisAngle(TK.Vector3.UnitY, TK.MathHelper.PiOver2); Quaternion q1 = new Quaternion(tkQ1.W, tkQ1.X, tkQ1.Y, tkQ1.Z); TK.Quaternion tkQ2 = TK.Quaternion.FromAxisAngle(TK.Vector3.UnitY, TK.MathHelper.Pi); Quaternion q2 = new Quaternion(tkQ2.W, tkQ2.X, tkQ2.Y, tkQ2.Z); Quaternion q = Quaternion.Slerp(q1, q2, .5f); TK.Quaternion tkQ = TK.Quaternion.Slerp(tkQ1, tkQ2, .5f); TestHelper.AssertEquals(tkQ.X, tkQ.Y, tkQ.Z, tkQ.W, q, "Testing slerp"); }
protected void UpdateRotation(object sender, EventArgs e) { // Event may fire multiple times per render. Don't do unnecessary updates. TimeSpan nextRenderTime = ((RenderingEventArgs)e).RenderingTime; if (nextRenderTime != lastRenderTime) { lastRenderTime = nextRenderTime; OpenTK.Quaternion q = rift.PredictedOrientation; RawRotation = new System.Windows.Media.Media3D.Quaternion(q.X, -q.Y, q.Z, -q.W); UpdatePositionAndRotation(); } }
public void Load(Stream fs) { //Binary Reader BinaryReader br = new BinaryReader(fs); //Lamest way to read a matrix invBindMatrix.M11 = br.ReadSingle(); invBindMatrix.M12 = br.ReadSingle(); invBindMatrix.M13 = br.ReadSingle(); invBindMatrix.M14 = br.ReadSingle(); invBindMatrix.M21 = br.ReadSingle(); invBindMatrix.M22 = br.ReadSingle(); invBindMatrix.M23 = br.ReadSingle(); invBindMatrix.M24 = br.ReadSingle(); invBindMatrix.M31 = br.ReadSingle(); invBindMatrix.M32 = br.ReadSingle(); invBindMatrix.M33 = br.ReadSingle(); invBindMatrix.M34 = br.ReadSingle(); invBindMatrix.M41 = br.ReadSingle(); invBindMatrix.M42 = br.ReadSingle(); invBindMatrix.M43 = br.ReadSingle(); invBindMatrix.M44 = br.ReadSingle(); //Calculate Binding Matrix Vector3 BindTranslate, BindScale; OpenTK.Quaternion BindRotation = new OpenTK.Quaternion(); //Get Translate BindTranslate.X = br.ReadSingle(); BindTranslate.Y = br.ReadSingle(); BindTranslate.Z = br.ReadSingle(); //Get Quaternion BindRotation.X = br.ReadSingle(); BindRotation.Y = br.ReadSingle(); BindRotation.Z = br.ReadSingle(); BindRotation.W = br.ReadSingle(); //Get Scale BindScale.X = br.ReadSingle(); BindScale.Y = br.ReadSingle(); BindScale.Z = br.ReadSingle(); //Generate Matrix BindMatrix = Matrix4.CreateScale(BindScale) * Matrix4.CreateFromQuaternion(BindRotation) * Matrix4.CreateTranslation(BindTranslate); //Check Results [Except from Joint 0, the determinant of the multiplication is always 1, // transforms should be good] //Console.WriteLine((BindMatrix * invBindMatrix).Determinant); }
internal ModelNodeBlockBase(BinaryReader binaryReader) { this.name = binaryReader.ReadStringID(); this.parentNode = binaryReader.ReadShortBlockIndex1(); this.firstChildNode = binaryReader.ReadShortBlockIndex1(); this.nextSiblingNode = binaryReader.ReadShortBlockIndex1(); this.invalidName_ = binaryReader.ReadBytes(2); this.defaultTranslation = binaryReader.ReadVector3(); this.defaultRotation = binaryReader.ReadQuaternion(); this.defaultInverseScale = binaryReader.ReadSingle(); this.defaultInverseForward = binaryReader.ReadVector3(); this.defaultInverseLeft = binaryReader.ReadVector3(); this.defaultInverseUp = binaryReader.ReadVector3(); this.defaultInversePosition = binaryReader.ReadVector3(); }
public void LoadRotations(FileStream fs, int count) { BinaryReader br = new BinaryReader(fs); for (int i = 0; i < count; i++) { OpenTK.Quaternion q = new OpenTK.Quaternion(); q.X = br.ReadSingle(); q.Y = br.ReadSingle(); q.Z = br.ReadSingle(); q.W = br.ReadSingle(); this.rotations.Add(q); } }
/// <summary> /// Initializes a new instance of the <see cref="RenderableWorldModel"/> class. /// </summary> public RenderableWorldModel(WMO inModel, PackageGroup inPackageGroup) { this.Model = inModel; this.ModelPackageGroup = inPackageGroup; this.ActorTransform = new Transform ( new Vector3(0.0f, 0.0f, 0.0f), Quaternion.FromAxisAngle(Vector3.UnitX, MathHelper.Pi), new Vector3(1.0f, 1.0f, 1.0f) ); this.IsInitialized = false; Initialize(); }
internal RenderModelNodeBlockBase(BinaryReader binaryReader) { this.name = binaryReader.ReadStringID(); this.parentNode = binaryReader.ReadShortBlockIndex1(); this.firstChildNode = binaryReader.ReadShortBlockIndex1(); this.nextSiblingNode = binaryReader.ReadShortBlockIndex1(); this.importNodeIndex = binaryReader.ReadInt16(); this.defaultTranslation = binaryReader.ReadVector3(); this.defaultRotation = binaryReader.ReadQuaternion(); this.inverseForward = binaryReader.ReadVector3(); this.inverseLeft = binaryReader.ReadVector3(); this.inverseUp = binaryReader.ReadVector3(); this.inversePosition = binaryReader.ReadVector3(); this.inverseScale = binaryReader.ReadSingle(); this.distanceFromParent = binaryReader.ReadSingle(); }
/// <summary> /// Creates a new quaternion from the specified OpenTK quaternion object. /// </summary> /// <param name="q">The OpenTK quaternion object.</param> internal Quaternion(OTK.Quaternion q) { _quaternion = q; }