Exemplo n.º 1
0
            /// <summary>
            /// Create bone
            /// </summary>
            /// <param name="setMatrix">Set matrix</param>
            /// <param name="setParentBone">Set parent bone</param>
            /// <param name="setNum">Set number</param>
            /// <param name="setId">Set id name</param>
            public RuntimeBone(Matrix setMatrix, RuntimeBone setParentBone,
                               int setNum, string setId)
            {
                initialMatrix = setMatrix;
                pos           = initialMatrix.Translation;
                parent        = setParentBone;
                id            = setId;

                invBoneSkinMatrix = Matrix.Identity;
            } // Bone(setMatrix, setParentBone, setNum)
Exemplo n.º 2
0
        }         // Save(writer)

        #endregion

        #region WriteBoneRef
        void WriteBoneRef(RuntimeBone bone, BinaryWriter writer)
        {
            for (int i = 0; i < bones.Length; i++)
            {
                if (bones[i] == bone)
                {
                    writer.Write(i);
                    return;
                }
            }

            writer.Write((int)-1);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Renders a single bone and all its childs using lines.
        /// </summary>
        private void RenderBone(Matrix worldMatrix, RuntimeBone bone)
        {
            for (int childIdx = 0; childIdx < bone.children.Length; childIdx++)
            {
                // Draw line to child
                // TODO!!!

                /*PrimitiveRenderer.DrawLine(
                 *                      worldMatrix,
                 *                      bone.finalMatrix.Translation,
                 *                      bone.children[childIdx].finalMatrix.Translation,
                 *                      new Vector4(1, 0, 0, 1));*/

                // Render childs of child
                RenderBone(worldMatrix, bone.children[childIdx]);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Update animation. Will do nothing if animation stayed the same since
        /// last time we called this method.
        /// </summary>
        private void UpdateAnimation()
        {
            for (int i = 0; i < bones.Length; i++)
            {
                RuntimeBone bone = bones[i];

                // Just assign the final matrix from the animation matrices.
                bone.finalMatrix = bone.animationMatrices[currentAnimationNum];

                // Also use parent matrix if we got one
                // This will always work because all the bones are in order.
                if (bone.parent != null)
                {
                    bone.finalMatrix *= bone.parent.finalMatrix;
                }
            } // foreach
        }     // UpdateAnimation()
Exemplo n.º 5
0
        /*private void RenderMeshPart(SkeletalMeshPart part,
         *                          RenderManager renderManager,
         *                          Graphics2D.IRenderConfiguration renderConfig,
         *                          Material materialToUse)
         * {
         *  SkinnedEffect skinEff = part.Material.Effect as SkinnedEffect;
         *  if (skinEff == null)
         *  {
         *      return;
         *  }
         *
         *  skinEff.WeightsPerVertex = 1;
         *
         *  skinEff.AmbientLightColor = Vector3.One;
         *  skinEff.FogEnabled = false;
         *
         *  skinEff.SetBoneTransforms(part.BoneMatrics);
         *
         *  skinEff.World = WorldMatrix;
         *  skinEff.View = renderManager.ViewTarget.ViewMatrix;
         *  skinEff.Projection = renderManager.ViewTarget.ProjectionMatrix;
         *
         *  skinEff.Parameters["WorldViewProj"].SetValue(WorldMatrix *
         *                                               renderManager.ViewTarget.ViewMatrix *
         *                                               renderManager.ViewTarget.ProjectionMatrix);
         *  part.Material.Effect.CurrentTechnique.Passes[0].Apply();
         *
         *  if (part.DiffuseTexture != null)
         *  {
         *      part.Material.Effect.Parameters["Texture"].SetValue(part.DiffuseTexture);
         *      part.Material.Effect.CurrentTechnique.Passes[0].Apply();
         *  }
         *
         *  renderConfig.GraphicsDevice.SetVertexBuffer(part.VertexBuffer);
         *  renderConfig.GraphicsDevice.DrawPrimitives(part.PrimitiveType, 0, part.PrimitiveCount);
         * }*/

        private void DrawBone(RuntimeBone bone, RenderManager renderManager, IRenderConfiguration renderConfig)
        {
            /*if (effect == null)
             * {
             *  effect = new BasicEffect(renderConfig.GraphicsDevice);
             * }
             *
             * VertexPositionColor v1 = new VertexPositionColor();
             * v1.Color = Color.Red;
             * v1.Position = bone.CombinedTransform.Translation;
             *
             * VertexPositionColor v2 = new VertexPositionColor();
             * v2.Color = Color.Blue;
             * v2.Position = bone.ParentBone.CombinedTransform.Translation;
             *
             * Vector3 midPoint = v1.Position + ((v2.Position - v1.Position) * 0.5f);
             * Vector3 screenPos = renderConfig.GraphicsDevice.Viewport.Project(midPoint,
             *                                                               renderManager.ViewTarget.ProjectionMatrix,
             *                                                               renderManager.ViewTarget.ViewMatrix,
             *                                                               WorldMatrix);
             * renderConfig.PrimitiveRenderManager.SpriteBatch.Begin();
             * renderConfig.PrimitiveRenderManager.SpriteBatch.DrawString(renderConfig.PrimitiveRenderManager.EngineFont,
             *                                                bone.BoneName,
             *                                                new Vector2(screenPos.X, screenPos.Y),
             *                                                Color.Yellow);
             * renderConfig.PrimitiveRenderManager.SpriteBatch.End();
             *
             * effect.TextureEnabled = false;
             * effect.VertexColorEnabled = true;
             * effect.FogEnabled = false;
             * effect.LightingEnabled = false;
             *
             * effect.Parameters["WorldViewProj"].SetValue(WorldMatrix *
             *                                          renderManager.ViewTarget.ViewMatrix *
             *                                          renderManager.ViewTarget.ProjectionMatrix);
             *
             * effect.CurrentTechnique.Passes[0].Apply();
             *
             * renderConfig.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList,
             *                                                                  new VertexPositionColor[] { v1, v2 }, 0, 1);
             *
             * for (int i = 0; i < bone.ChildBones.Length; i++)
             * {
             *  DrawBone(bone.ChildBones[i], renderManager, renderConfig);
             * }*/
        }
Exemplo n.º 6
0
        /// <summary>
        /// Load
        /// </summary>
        public override void Deserialize(BinaryReader reader)
        {
            base.Deserialize(reader);

            // Read mesh data
            numOfVertices = reader.ReadInt32();
            vertices      = new SkinnedTangentVertex[numOfVertices];
            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i] = new SkinnedTangentVertex(
                    new Vector3(reader.ReadSingle(), reader.ReadSingle(),
                                reader.ReadSingle()),
                    new Vector2(reader.ReadSingle(), reader.ReadSingle()),
                    new Vector3(reader.ReadSingle(), reader.ReadSingle(),
                                reader.ReadSingle()),
                    new Vector3(reader.ReadSingle(), reader.ReadSingle(),
                                reader.ReadSingle()),
                    new Vector3(reader.ReadSingle(), reader.ReadSingle(),
                                reader.ReadSingle()),
                    new Vector4(reader.ReadSingle(), reader.ReadSingle(),
                                reader.ReadSingle(), reader.ReadSingle()),
                    new Vector4(reader.ReadSingle(), reader.ReadSingle(),
                                reader.ReadSingle(), reader.ReadSingle()));
            } // for (int)

            numOfIndices  = reader.ReadInt32();
            objectIndices = new uint[numOfIndices];
            for (int i = 0; i < objectIndices.Length; i++)
            {
                objectIndices[i] = reader.ReadUInt32();
            } // for (int)

            boundingSphere = new BoundingSphere(
                new Vector3(reader.ReadSingle(), reader.ReadSingle(),
                            reader.ReadSingle()),
                reader.ReadSingle());

            // Read animation data
            runtimeAnimations.Clear();

            int numAnim = reader.ReadInt32();

            for (int i = 0; i < numAnim; i++)
            {
                RuntimeAnimation anim = new RuntimeAnimation();
                anim.Name  = reader.ReadString();
                anim.Start = reader.ReadInt32();
                anim.End   = reader.ReadInt32();
                runtimeAnimations.Add(anim);
            } // for (int)

            string semantic = reader.ReadString();
            string defaultDiffuseTexture = reader.ReadString();

            semantic = reader.ReadString();
            string defaultNormalTexture = reader.ReadString();

            objectMatrix = ReadMatrixHelper(reader);

            numOfAnimations = reader.ReadInt32();

            int boneCnt = reader.ReadInt32();

            bones = new RuntimeBone[boneCnt];
            // Precreate empty bones
            for (int i = 0; i < boneCnt; i++)
            {
                bones[i] = new RuntimeBone();
            } // for (int)
              // Now read bones
            for (int i = 0; i < boneCnt; i++)
            {
                bones[i].id     = reader.ReadString();
                bones[i].parent = ReadBoneRef(reader);
                int childCnt = reader.ReadInt32();
                bones[i].children = new RuntimeBone[childCnt];
                for (int j = 0; j < childCnt; j++)
                {
                    bones[i].children[j] = ReadBoneRef(reader);
                } // for (int)
                bones[i].initialMatrix     = ReadMatrixHelper(reader);
                bones[i].invBoneSkinMatrix = ReadMatrixHelper(reader);
                int aninMatCnt = reader.ReadInt32();
                bones[i].animationMatrices = new Matrix[aninMatCnt];
                for (int j = 0; j < aninMatCnt; j++)
                {
                    bones[i].animationMatrices[j] =
                        ReadMatrixHelper(reader);
                } // for (int)
            }     // for (int)

            for (int i = 0; i < bones.Length; i++)
            {
                RuntimeBone bone = bones[i];

                // Just assign the final matrix from the animation matrices.
                bone.finalMatrix = bone.animationMatrices[0];

                // Also use parent matrix if we got one
                // This will always work because all the bones are in order.
                if (bone.parent != null)
                {
                    bone.finalMatrix *=
                        bone.parent.finalMatrix;
                }
            } // for (int)

            sockets = new RuntimeSocket[reader.ReadInt32()];
            for (int i = 0; i < sockets.Length; i++)
            {
                sockets[i] = new RuntimeSocket();
                sockets[i].LoadFromStream(reader, bones);
            } // for (int)

            GenerateVertexAndIndexBuffers();

            CreateBoundingSphere();

            ValidateAnimations();
        } // Load(packageName, assetName, stream)