Example #1
0
        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();
        }
Example #2
0
        public void Read(byte[] data)
        {
            using (var reader = new VBFile(new MemoryStream(data)))
            {
                System.Diagnostics.Debug.WriteLine("========== Mesh ==========");

                Version = reader.ReadInt32();
                BoneCount = reader.ReadInt32();

                System.Diagnostics.Debug.WriteLine("Version: " + Version);
                System.Diagnostics.Debug.WriteLine("BoneCount: " + BoneCount);

                /** Read bone names |str_len|str_body| **/
                BoneNames = new string[BoneCount];
                for (var i = 0; i < BoneCount; i++){
                    BoneNames[i] = reader.ReadPascalString();

                    System.Diagnostics.Debug.WriteLine("| Bone " + (i + 1) + ": " + BoneNames[i]);
                }

                /** Faces **/
                FaceCount = reader.ReadInt32();
                System.Diagnostics.Debug.WriteLine("FaceCount: " + FaceCount);

                FaceData = new Face[FaceCount];
                for (var i = 0; i < FaceCount; i++){
                    FaceData[i] = new Face {
                        VertexA = reader.ReadInt32(),
                        VertexB = reader.ReadInt32(),
                        VertexC = reader.ReadInt32()
                    };
                }

                /** Bone bindings **/
                BindingCount = reader.ReadInt32();
                BoneBindings = new BoneBinding[BindingCount];
                for (var i = 0; i < BindingCount; i++){
                    BoneBindings[i] = new BoneBinding {
                        BoneIndex = reader.ReadInt32(),
                        FirstRealVertex = reader.ReadInt32(),
                        RealVertexCount = reader.ReadInt32(),
                        FirstBlendVertex = reader.ReadInt32(),
                        BlendVertexCount = reader.ReadInt32()
                    };
                }

                /** Texture vertex data **/
                RealVertexCount = reader.ReadInt32();
                //VertexData = new MeshVertex[RealVertexCount];

                var textureData = new Vector2[RealVertexCount];
                for (var i = 0; i < RealVertexCount; i++){
                    textureData[i] = new Vector2(
                        reader.ReadFloat(),
                        reader.ReadFloat()
                    );
                }

                /** Blend data **/
                BlendVertexCount = reader.ReadInt32();
                var blend = new BlendData[BlendVertexCount];
                for (var i = 0; i < BlendVertexCount; i++)
                {
                    blend[i] = new BlendData {
                        Weight = (float)reader.ReadInt32()/0x8000,
                        OtherVertex = reader.ReadInt32()
                    };
                }

                TotalVertexCount = reader.ReadInt32();
                //VertexData = new MeshVertex[TotalVertexCount];
                //TransformedVertexData = new MeshVertex[TotalVertexCount];

                Vertex = new MeshVertexData[TotalVertexCount];
                TransformedVertex = new MeshVertexData[TotalVertexCount];

                for (var i = 0; i < TotalVertexCount; i++){
                    var vertexData = new MeshVertex {
                        Coord = new Vector3(
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                        )
                    };
                    var tVertexData = new MeshVertex
                    {
                        Coord = vertexData.Coord,
                        NormalCoord = new Vector3(
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                        )
                    };

                    var vertex = new MeshVertexData {
                        Vertex = vertexData
                    };

                    var tVertex = new MeshVertexData{
                        Vertex = tVertexData
                    };

                    if (i < RealVertexCount)
                    {
                        tVertex.Vertex.TextureCoord = textureData[i];
                    }
                    else
                    {
                        tVertex.BlendData = blend[i - RealVertexCount];
                    }

                    //VertexData[i] = vertexData;
                    //TransformedVertexData[i] = tVertexData;

                    Vertex[i] = vertex;
                    TransformedVertex[i] = tVertex;
                }

            }
        }
        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();
        }
Example #4
0
        public void Read(byte[] data)
        {
            using (var reader = new VBFile(new MemoryStream(data)))
            {
                System.Diagnostics.Debug.WriteLine("========== Mesh ==========");

                Version   = reader.ReadInt32();
                BoneCount = reader.ReadInt32();

                System.Diagnostics.Debug.WriteLine("Version: " + Version);
                System.Diagnostics.Debug.WriteLine("BoneCount: " + BoneCount);

                /** Read bone names |str_len|str_body| **/
                BoneNames = new string[BoneCount];
                for (var i = 0; i < BoneCount; i++)
                {
                    BoneNames[i] = reader.ReadPascalString();

                    System.Diagnostics.Debug.WriteLine("| Bone " + (i + 1) + ": " + BoneNames[i]);
                }

                /** Faces **/
                FaceCount = reader.ReadInt32();
                System.Diagnostics.Debug.WriteLine("FaceCount: " + FaceCount);

                FaceData = new Face[FaceCount];
                for (var i = 0; i < FaceCount; i++)
                {
                    FaceData[i] = new Face {
                        VertexA = reader.ReadInt32(),
                        VertexB = reader.ReadInt32(),
                        VertexC = reader.ReadInt32()
                    };
                }

                /** Bone bindings **/
                BindingCount = reader.ReadInt32();
                BoneBindings = new BoneBinding[BindingCount];
                for (var i = 0; i < BindingCount; i++)
                {
                    BoneBindings[i] = new BoneBinding {
                        BoneIndex        = reader.ReadInt32(),
                        FirstRealVertex  = reader.ReadInt32(),
                        RealVertexCount  = reader.ReadInt32(),
                        FirstBlendVertex = reader.ReadInt32(),
                        BlendVertexCount = reader.ReadInt32()
                    };
                }

                /** Texture vertex data **/
                RealVertexCount = reader.ReadInt32();
                //VertexData = new MeshVertex[RealVertexCount];

                var textureData = new Vector2[RealVertexCount];
                for (var i = 0; i < RealVertexCount; i++)
                {
                    textureData[i] = new Vector2(
                        reader.ReadFloat(),
                        reader.ReadFloat()
                        );
                }

                /** Blend data **/
                BlendVertexCount = reader.ReadInt32();
                var blend = new BlendData[BlendVertexCount];
                for (var i = 0; i < BlendVertexCount; i++)
                {
                    blend[i] = new BlendData {
                        Weight      = (float)reader.ReadInt32() / 0x8000,
                        OtherVertex = reader.ReadInt32()
                    };
                }


                TotalVertexCount = reader.ReadInt32();
                //VertexData = new MeshVertex[TotalVertexCount];
                //TransformedVertexData = new MeshVertex[TotalVertexCount];

                Vertex            = new MeshVertexData[TotalVertexCount];
                TransformedVertex = new MeshVertexData[TotalVertexCount];

                for (var i = 0; i < TotalVertexCount; i++)
                {
                    var vertexData = new MeshVertex {
                        Coord = new Vector3(
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                            )
                    };
                    var tVertexData = new MeshVertex
                    {
                        Coord       = vertexData.Coord,
                        NormalCoord = new Vector3(
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                            )
                    };

                    var vertex = new MeshVertexData {
                        Vertex = vertexData
                    };

                    var tVertex = new MeshVertexData {
                        Vertex = tVertexData
                    };


                    if (i < RealVertexCount)
                    {
                        tVertex.Vertex.TextureCoord = textureData[i];
                    }
                    else
                    {
                        tVertex.BlendData = blend[i - RealVertexCount];
                    }



                    //VertexData[i] = vertexData;
                    //TransformedVertexData[i] = tVertexData;

                    Vertex[i]            = vertex;
                    TransformedVertex[i] = tVertex;
                }
            }
        }