Example #1
0
        bool ReadBones()
        {
            Advance("numbones");
            int boneNum = int.Parse(NextToken);

            bones = new MD5Bone[boneNum];

            for (int i = 0; i < boneNum; i++)
            {
                Advance("bone");

                // sanity check
                int num = int.Parse(NextToken);
                System.Diagnostics.Debug.Assert(num == i, "Invalid bone num!");

                // read bone data - name, bindpos, bindmat and [parent]
                Advance("name");
                string name = NextToken;
                Advance("bindpos");
                Math.Vector3 position = new Math.Vector3(
                    NextFloat, NextFloat, NextFloat);
                Advance("bindmat");
                Math.Matrix4 md5matrix = new Math.Matrix4(
                    NextFloat, NextFloat, NextFloat, 0,
                    NextFloat, NextFloat, NextFloat, 0,
                    NextFloat, NextFloat, NextFloat, 0,
                    position.X, position.Y, position.Z, 0);
                Math.Matrix4 matrix = Math.Matrix4.Zero;
                matrix.Column1 = md5matrix.Column1;
                matrix.Column2 = md5matrix.Column3;
                matrix.Column3 = md5matrix.Column2;
                matrix.Column4 = md5matrix.Column4;
                Advance("parent", "bone");
                string parentName = null;
                if (CurrentToken == "parent")
                {
                    parentName = NextToken;
                }

                bones[i] = new MD5Bone(name, matrix, parentName);
            }

            return(true);
        }
Example #2
0
        bool ReadMeshes()
        {
            Advance("nummeshes");
            int meshNum = int.Parse(NextToken);

            mesh = new Mesh();

            for (int i = 0; i < 1 /*meshNum*/; i++)
            {
                Advance("mesh");

                // sanity check
                int num = int.Parse(NextToken);
                System.Diagnostics.Debug.Assert(num == i, "Invalid mesh num!");

                // read mesh data - shader, verts
                Advance("shader");
                string   shaderPath  = NextToken;
                FileInfo info        = new FileInfo(shaderPath);
                string   localPath   = shaderPath.Insert(shaderPath.Length - info.Extension.Length, "_local");
                string   diffusePath = shaderPath.Insert(shaderPath.Length - info.Extension.Length, "_d");
                Textures textures    = new Textures();
                ITexture texture     = null;
                try {
                    texture = TextureManager.Instance.Load(shaderPath);
                } catch {
                    texture = TextureManager.Instance.Load(diffusePath);
                }
                ITexture normalMap = TextureManager.Instance.Load(localPath);
                textures["color"]  = texture;
                textures["normal"] = normalMap;

                if (!ReadVertices())
                {
                    return(false);
                }

                if (!ReadTriangles())
                {
                    return(false);
                }

                if (!ReadWeights())
                {
                    return(false);
                }

                // let's test it
                VertexUnit     vertexUnit     = new VertexUnit(VertexFormat.PositionTexture2Tangent, vertices.Length);
                PositionStream positionStream = (PositionStream)vertexUnit[0];
                TextureStream  textureStream  = (TextureStream)vertexUnit[1];
                TextureStream  normalStream   = (TextureStream)vertexUnit[2];
                for (int j = 0; j < vertices.Length; j++)
                {
                    Vector3   pos    = Vector3.Zero;
                    MD5Vertex vertex = vertices[j];
                    for (int k = 0; k < vertex.WeightCount; k++)
                    {
                        MD5Weight weight = weights[vertex.WeightIndex + k];
                        MD5Bone   bone   = bones[weight.BoneIndex];

                        pos += weight.Vector * bone.Matrix * weight.BiasFactor;
                    }
                    positionStream[j] = pos;
                    textureStream[j]  = vertex.UV;
                    normalStream[j]   = vertex.UV;
                }

                // add tangent space stuff
                IGraphicsStream[] streams = Util.CalcTangentSpaceStreams(positionStream, textureStream, indexStream);

                Array.Copy(streams[0].Data, vertexUnit[3].Data, vertices.Length);
                Array.Copy(streams[1].Data, vertexUnit[4].Data, vertices.Length);
                Array.Copy(streams[2].Data, vertexUnit[5].Data, vertices.Length);
                //mesh.SubSets.Add( new SubSet(vertexUnit, indexStream, material));
            }

            return(true);
        }