private void TransformVertices(Mesh mesh, Bone bone) { //var x = mesh.BoneBindings; var boneBinding = mesh.BoneBindings.FirstOrDefault(x => mesh.BoneNames[x.BoneIndex] == bone.Name); if (boneBinding != null){ for (var i = 0; i < boneBinding.RealVertexCount; i++) { var vertexIndex = boneBinding.FirstRealVertex + i; var transformedVertex = mesh.TransformedVertex[vertexIndex]; var relativeVertex = mesh.Vertex[vertexIndex]; var translatedMatrix = Matrix.CreateTranslation(new Vector3(relativeVertex.Vertex.Coord.X, relativeVertex.Vertex.Coord.Y, relativeVertex.Vertex.Coord.Z)) * bone.AbsoluteMatrix; transformedVertex.Vertex.Coord = Vector3.Transform(Vector3.Zero, translatedMatrix); //var translatedMatrix = bone.AbsoluteMatrix * Matrix.CreateTranslation(relativeVertex.Vertex.Coord); //transformedVertex.Vertex.Coord = Vector3.Transform(Vector3.Zero, translatedMatrix); } for (var i = 0; i < boneBinding.BlendVertexCount; i++) { var vertexIndex = boneBinding.FirstBlendVertex + i; var transformedVertex = mesh.TransformedVertex[vertexIndex]; var relativeVertex = mesh.Vertex[vertexIndex]; } //for (var i = 0; i < boneBinding.BlendVertexCount; i++) //{ // var vertexIndex = boneBinding.FirstBlendVertex + i; // var transformedVertex = mesh.TransformedVertex[vertexIndex]; // var relativeVertex = mesh.Vertex[vertexIndex]; // //var translatedMatrix = bone.AbsoluteMatrix * Matrix.CreateTranslation(relativeVertex.Vertex.Coord); // //transformedVertex.Vertex.Coord = Vector3.Transform(Vector3.Zero, translatedMatrix); // var translatedMatrix = Matrix.CreateTranslation(new Vector3(relativeVertex.Vertex.Coord.X, relativeVertex.Vertex.Coord.Y, relativeVertex.Vertex.Coord.Z)) * bone.AbsoluteMatrix; // transformedVertex.Vertex.Coord = Vector3.Transform(Vector3.Zero, translatedMatrix); //} } foreach (var child in bone.Children){ TransformVertices(mesh, child); } }
private void BlendVertices(Mesh mesh) { for (var i = 0; i < mesh.BlendVertexCount; i++) { var blendVertex = mesh.TransformedVertex[mesh.RealVertexCount + i]; var weight = blendVertex.BlendData.Weight; var realVertex = mesh.TransformedVertex[blendVertex.BlendData.OtherVertex]; realVertex.Vertex.Coord.X = weight * blendVertex.Vertex.Coord.X + (1 - weight) * realVertex.Vertex.Coord.X; realVertex.Vertex.Coord.Y = weight * blendVertex.Vertex.Coord.Y + (1 - weight) * realVertex.Vertex.Coord.Y; realVertex.Vertex.Coord.Z = weight * blendVertex.Vertex.Coord.Z + (1 - weight) * realVertex.Vertex.Coord.Z; } }
private void DrawMesh(Mesh mesh, Texture2D texture) { //glTranslatef(Character.Translation.x, Character.Translation.y, zoom + Character.Translation.z); //glRotatef(Character.Rotation.x, 1.0f, 0.0f, 0.0f); //glRotatef(Character.Rotation.y, 0.0f, 1.0f, 0.0f); //glRotatef(Character.Rotation.z, 0.0f, 0.0f, 1.0f); var device = GraphicsDevice; device.VertexDeclaration = new VertexDeclaration(GraphicsDevice, MeshVertex.VertexElements); device.RenderState.CullMode = CullMode.None; var effect = new BasicEffect(GraphicsDevice, null); effect.Texture = texture; effect.TextureEnabled = true; effect.VertexColorEnabled = false; effect.World = World; effect.View = View; effect.Projection = Projection; effect.CommitChanges(); effect.Begin(); foreach (var pass in effect.Techniques[0].Passes) { pass.Begin(); foreach (var face in mesh.FaceData) { var vertexA = mesh.TransformedVertex[face.VertexA]; var vertexB = mesh.TransformedVertex[face.VertexB]; var vertexC = mesh.TransformedVertex[face.VertexC]; var vertexList = new MeshVertex[3] { vertexA.Vertex, vertexB.Vertex, vertexC.Vertex }; device.DrawUserPrimitives(PrimitiveType.TriangleList, vertexList, 0, 1); } //device.DrawUserPrimitives(PrimitiveType.TriangleList, mesh.TransformedVertexData, 0, mesh.TransformedVertexData.Length / 3); pass.End(); } effect.End(); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 4.0f, GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height, 1.0f, 2000.0f); View = Matrix.CreateLookAt(new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up); World = Matrix.Identity; Head = new Mesh(); Head.Read(File.ReadAllBytes("head.mesh")); Body = new Mesh(); Body.Read(File.ReadAllBytes("body.mesh")); HeadTexture = Texture2D.FromFile(GraphicsDevice, "head.jpg"); BodyTexture = Texture2D.FromFile(GraphicsDevice, "body.jpg"); RightHand = new Mesh(); RightHand.Read(File.ReadAllBytes("rhand.mesh")); LeftHand = new Mesh(); LeftHand.Read(File.ReadAllBytes("lhand.mesh")); RightHandTexture = Texture2D.FromFile(GraphicsDevice, "hand.jpg"); LeftHandTexture = Texture2D.FromFile(GraphicsDevice, "hand.jpg"); Skelenton = new Skelenton(); Skelenton.Read(File.ReadAllBytes("skeleton.skel")); }