public static void Freeze(Model model, Matrix4D matrix) { foreach (ModelMesh mesh in model.Meshes) { Matrix4D o = mesh.Parent.Transform; Vector3 p = Vector3.TransformVector(o.ExtractTranslation(), matrix); Vector3 e = o.ExtractRotation().ToEuler(Quaternion.RotationOrder.OrderXYZ); o = Matrix4D.CreateFromQuaternion( Quaternion.FromAxisAngle(Vector3.UnitX, Maths.DegreesToRadians(e.X)) * Quaternion.FromAxisAngle(Vector3.UnitZ, Maths.DegreesToRadians(-e.Y)) * Quaternion.FromAxisAngle(Vector3.UnitY, Maths.DegreesToRadians(-e.Z)) ); o.M41 = p.X; o.M42 = p.Y; o.M43 = p.Z; mesh.Parent.Transform = matrix; Freeze(mesh, FreezeComponents.Rotation); mesh.Parent.Transform = o; } foreach (ModelBone bone in model.Bones) { if (bone.Mesh != null) { continue; } Matrix4D o = bone.Transform; Vector3 p = Vector3.TransformVector(o.ExtractTranslation(), matrix); Vector3 e = o.ExtractRotation().ToEuler(Quaternion.RotationOrder.OrderXYZ); o = Matrix4D.CreateFromQuaternion( Quaternion.FromAxisAngle(Vector3.UnitX, Maths.DegreesToRadians(e.X)) * Quaternion.FromAxisAngle(Vector3.UnitZ, Maths.DegreesToRadians(-e.Y)) * Quaternion.FromAxisAngle(Vector3.UnitY, Maths.DegreesToRadians(-e.Z)) ); o.M41 = p.X; o.M42 = p.Y; o.M43 = p.Z; bone.Transform = o; } }
public void Draw() { if (model == null) { model = new Model(); Sphere sphere = new Sphere(0.025f, 7, 7); ModelManipulator.SetVertexColour(sphere, 0, 255, 0, 255); model.AddMesh(sphere); model.SetRenderStyle(RenderStyle.Wireframe); } Matrix4D parentTransform = LinkedBone.CombinedTransform; Transform = Matrix4D.Identity; Vector3 v = parentTransform.ExtractTranslation(); parentTransform.Normalise(); parentTransform.M41 = v.X; parentTransform.M42 = v.Y; parentTransform.M43 = v.Z; Transform *= Matrix4D.CreateFromQuaternion(parentTransform.ExtractRotation()); Transform *= Matrix4D.CreateTranslation(parentTransform.ExtractTranslation()); Matrix4D mS = SceneManager.Current.Transform; Matrix4D mT = Transform; SceneManager.Current.Renderer.PushMatrix(); SceneManager.Current.Renderer.MultMatrix(ref mS); SceneManager.Current.Renderer.MultMatrix(ref mT); model.Draw(); SceneManager.Current.Renderer.PopMatrix(); }
public void Draw() { if (asset == null) { ModelMeshPart sprite = new ModelMeshPart(); sprite.AddVertex(new Vector3(-0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 1)); sprite.AddVertex(new Vector3(-0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 0)); sprite.AddVertex(new Vector3(0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 0)); sprite.AddVertex(new Vector3(0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 1)); sprite.AddVertex(new Vector3(0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 1)); sprite.AddVertex(new Vector3(0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 0)); sprite.AddVertex(new Vector3(-0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 0)); sprite.AddVertex(new Vector3(-0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 1)); sprite.IndexBuffer.Initialise(); sprite.VertexBuffer.Initialise(); sprite.Material = new Material { Name = "Entity.Asset", Texture = SceneManager.Current.Content.Load <Texture, PNGImporter>(IconFilename) }; sprite.PrimitiveType = PrimitiveType.Quads; ModelMesh spritemesh = new ModelMesh(); spritemesh.AddModelMeshPart(sprite); Model spritemodel = new Model(); spritemodel.AddMesh(spritemesh); asset = spritemodel; } if (Linked) { Matrix4D parentTransform = ((ModelBone)Link).CombinedTransform; if (LinkType == LinkType.All) { Transform = parentTransform; } else { Transform = Matrix4D.Identity; Vector3 v = parentTransform.ExtractTranslation(); parentTransform.Normalise(); parentTransform.M41 = v.X; parentTransform.M42 = v.Y; parentTransform.M43 = v.Z; if (LinkType.HasFlag(LinkType.Rotation)) { Transform *= Matrix4D.CreateFromQuaternion(parentTransform.ExtractRotation()); } if (LinkType.HasFlag(LinkType.Scale)) { Transform *= Matrix4D.CreateScale(parentTransform.ExtractScale()); } if (LinkType.HasFlag(LinkType.Position)) { Transform *= Matrix4D.CreateTranslation(parentTransform.ExtractTranslation()); } } } Matrix4D mS = SceneManager.Current.Transform; Matrix4D mT = Transform; SceneManager.Current.Renderer.PushMatrix(); Matrix4D position = Matrix4D.CreateTranslation(mT.ExtractTranslation()); SceneManager.Current.Renderer.MultMatrix(ref mS); SceneManager.Current.Renderer.MultMatrix(ref position); if (Lollipop) { Matrix4D rotation = ViewportManager.Current.Active.Camera.Rotation; Matrix4D scale = Matrix4D.CreateScale(0.1f); SceneManager.Current.Renderer.MultMatrix(ref rotation); SceneManager.Current.Renderer.MultMatrix(ref scale); } SceneManager.Current.Renderer.Enable("Blend"); SceneManager.Current.Renderer.BlendFunc("SrcAlpha", "OneMinusSrcAlpha"); ((Model)asset).Draw(); SceneManager.Current.Renderer.Disable("Blend"); SceneManager.Current.Renderer.PopMatrix(); }