Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        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;
            }
        }
Esempio n. 3
0
        public static void Scale(ModelBoneCollection bones, Matrix4D scale, bool applyToHierarchy = false)
        {
            List <string> processed = new List <string>();

            foreach (ModelBone bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    ModelMesh mesh = bone.Mesh;

                    foreach (ModelMeshPart meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.VertexCount; i++)
                        {
                            Vector3 position = Vector3.TransformVector(meshpart.VertexBuffer.Data[i].Position, scale);
                            meshpart.VertexBuffer.ModifyVertexPosition(i, position);
                        }

                        meshpart.VertexBuffer.Initialise();
                    }

                    processed.Add(mesh.Name);
                }

                if (applyToHierarchy)
                {
                    Matrix4D transform = bone.Transform;
                    Vector3  position  = Vector3.TransformPosition(transform.ExtractTranslation(), scale);

                    transform.M41 = position.X;
                    transform.M42 = position.Y;
                    transform.M43 = position.Z;

                    bone.Transform = transform;
                }
            }
        }
        /// <inheritdoc/>
        public override void CreateObject(Vector3 Position, Transformation BaseTransformation, Transformation AuxTransformation,
                                          int SectionIndex, bool AccurateObjectDisposal, double StartingDistance, double EndingDistance, double BlockLength,
                                          double TrackPosition, double Brightness, bool DuplicateMaterials)
        {
            bool[] free    = new bool[Objects.Length];
            bool   anyfree = false;
            bool   allfree = true;

            for (int i = 0; i < Objects.Length; i++)
            {
                free[i] = Objects[i].IsFreeOfFunctions();
                if (free[i])
                {
                    anyfree = true;
                }
                else
                {
                    allfree = false;
                }
            }
            if (anyfree && !allfree && Objects.Length > 1)
            {
                //Optimise a little: If *all* are free of functions, this can safely be converted into a static object without regard to below
                if (AuxTransformation.X != Vector3.Right || AuxTransformation.Y != Vector3.Down || AuxTransformation.Z != Vector3.Forward)
                {
                    //HACK:
                    //An animated object containing a mix of functions and non-functions and using yaw, pitch or roll must not be converted into a mix
                    //of animated and static objects, as this causes rounding differences....
                    anyfree = false;
                }
            }
            if (anyfree)
            {
                for (int i = 0; i < Objects.Length; i++)
                {
                    if (Objects[i].States.Length != 0)
                    {
                        if (free[i])
                        {
                            Transformation FinalTransformation = new Transformation(BaseTransformation, AuxTransformation);
                            Matrix4D       mat = Matrix4D.Identity;
                            mat *= Objects[i].States[0].Translation;
                            mat *= (Matrix4D) new Transformation(FinalTransformation.Z, FinalTransformation.Y, FinalTransformation.X);
                            double zOffset = mat.ExtractTranslation().Z * -1.0;

                            currentHost.CreateStaticObject(Objects[i].States[0].Prototype, Position, AuxTransformation, mat, Matrix4D.CreateTranslation(Position.X, Position.Y, -Position.Z), AccurateObjectDisposal, zOffset, StartingDistance, EndingDistance, BlockLength, TrackPosition, Brightness);
                        }
                        else
                        {
                            Objects[i].CreateObject(Position, BaseTransformation, AuxTransformation, SectionIndex, TrackPosition, Brightness);
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < Objects.Length; i++)
                {
                    if (Objects[i].States.Length != 0)
                    {
                        Objects[i].CreateObject(Position, BaseTransformation, AuxTransformation, SectionIndex, TrackPosition, Brightness);
                    }
                }
            }
            if (this.Sounds == null)
            {
                return;
            }
            for (int i = 0; i < Sounds.Length; i++)
            {
                if (this.Sounds[i] == null)
                {
                    continue;
                }
                var snd = this.Sounds[i] as WorldSound;
                if (snd != null)
                {
                    snd.CreateSound(Sounds[i].Position, BaseTransformation, AuxTransformation, SectionIndex, TrackPosition);
                }
                var snd2 = this.Sounds[i] as AnimatedWorldObjectStateSound;
                if (snd2 != null)
                {
                    snd2.Create(Position, BaseTransformation, AuxTransformation, SectionIndex, TrackPosition, Brightness);
                }
            }
        }
Esempio n. 5
0
File: Icon.cs Progetto: q4a/Flummery
        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();
        }