Пример #1
0
        public AnimationRenderer([NotNull] Game game,
                                 [NotNull] PrettyMesh bodyMesh, [NotNull] PrettyAvatar bodyAvatar, [NotNull, ItemNotNull] IReadOnlyList <BoneNode> bodyBoneList,
                                 [NotNull] PrettyMesh headMesh, [NotNull] PrettyAvatar headAvatar, [NotNull, ItemNotNull] IReadOnlyList <BoneNode> headBoneList,
                                 [NotNull] BodyAnimation animation)
        {
            _game         = game;
            _bodyMesh     = bodyMesh;
            _bodyAvatar   = bodyAvatar;
            _bodyBoneList = bodyBoneList;
            _headMesh     = headMesh;
            _headAvatar   = headAvatar;
            _headBoneList = headBoneList;
            _animation    = animation;

            #region Body

            var bodyVertexBuffer = new VertexBuffer();
            var bodyIndexBuffer  = new IndexBuffer();

            var bodyVertices = new PosNorm[bodyMesh.Vertices.Length];
            var bodyIndices  = new uint[bodyMesh.Indices.Length];

            for (var k = 0; k < bodyVertices.Length; ++k)
            {
                bodyVertices[k] = new PosNorm {
                    Position = bodyMesh.Vertices[k].ToOpenTK().FixCoordSystem(),
                    Normal   = bodyMesh.Normals[k].ToOpenTK().FixCoordSystem()
                };
            }

            _originalBodyVertices = bodyVertices;
            _bodyVertices         = (PosNorm[])bodyVertices.Clone();

            for (var k = 0; k < bodyIndices.Length; ++k)
            {
                bodyIndices[k] = bodyMesh.Indices[k];
            }

            bodyVertexBuffer.BufferData(bodyVertices, BufferUsageHint.StreamDraw);
            bodyIndexBuffer.BufferData(bodyIndices, BufferUsageHint.StaticDraw);

            _bodyVertexBuffer = bodyVertexBuffer;
            _bodyIndexBuffer  = bodyIndexBuffer;

            #endregion

            #region Head

            var headVertexBuffer = new VertexBuffer();
            var headIndexBuffer  = new IndexBuffer();

            var headVertices = new PosNorm[headMesh.Vertices.Length];
            var headIndices  = new uint[headMesh.Indices.Length];

            for (var k = 0; k < headVertices.Length; ++k)
            {
                headVertices[k] = new PosNorm {
                    Position = headMesh.Vertices[k].ToOpenTK().FixCoordSystem(),
                    Normal   = headMesh.Normals[k].ToOpenTK().FixCoordSystem()
                };
            }

            _originalHeadVertices = headVertices;
            _headVertices         = (PosNorm[])headVertices.Clone();

            for (var k = 0; k < headIndices.Length; ++k)
            {
                headIndices[k] = headMesh.Indices[k];
            }

            headVertexBuffer.BufferData(headVertices, BufferUsageHint.StreamDraw);
            headIndexBuffer.BufferData(headIndices, BufferUsageHint.StaticDraw);

            _headVertexBuffer = headVertexBuffer;
            _headIndexBuffer  = headIndexBuffer;

            #endregion
        }
Пример #2
0
        public static IReadOnlyList <BoneNode> BuildBoneHierachy([NotNull] PrettyAvatar avatar, [NotNull] PrettyMesh mesh)
        {
            var boneList = new List <BoneNode>();

            for (var i = 0; i < avatar.AvatarSkeleton.Nodes.Length; i++)
            {
                var n = avatar.AvatarSkeleton.Nodes[i];

                var parent   = n.ParentIndex >= 0 ? boneList[n.ParentIndex] : null;
                var boneId   = avatar.AvatarSkeleton.NodeIDs[i];
                var bonePath = avatar.BoneNamesMap[boneId];

                var boneIndex = avatar.AvatarSkeleton.NodeIDs.FindIndex(boneId);

                if (boneIndex < 0)
                {
                    throw new IndexOutOfRangeException();
                }

                var initialPose = avatar.AvatarSkeletonPose.Transforms[boneIndex];

                var bone = new BoneNode(parent, i, bonePath, initialPose.LocalPosition.ToOpenTK(), initialPose.LocalRotation.ToOpenTK());

                boneList.Add(bone);
            }

            foreach (var bone in boneList)
            {
                bone.Initialize();
            }

#if DEBUG
            Debug.Print("Model bones:");

            for (var i = 0; i < boneList.Count; i++)
            {
                var bone = boneList[i];
                Debug.Print("[{0}]: {1}", i, bone.ToString());
            }
#endif

            return(boneList);
        }