Esempio n. 1
0
        private void GetVertInfo(OBJFile file, int vertexIndex, out Vector3 o_pos, out Vector4 o_colour, out Vector2 o_texCoord, out Vector3?o_normal)
        {
            var vertex = file.Verts[vertexIndex];
            var objPos = file.Positions[vertex.PositionIndex - 1];

            o_pos = objPos;

            var objColour = file.Colours[vertex.PositionIndex - 1];

            o_colour = objColour;

            if (vertex.TexCoordIndex > 0)
            {
                var objTexCoord = file.TexCoords[vertex.TexCoordIndex - 1];
                o_texCoord = new Vector2(objTexCoord.X, 1.0f - objTexCoord.Y);
            }
            else
            {
                o_texCoord = new Vector2(0.5f, 0.5f);
            }

            if (vertex.NormalIndex > 0)
            {
                var objNormal = file.Normals[vertex.NormalIndex - 1];
                o_normal = objNormal;
                o_normal.Value.Normalize();
            }
            else
            {
                o_normal = null;
            }
        }
Esempio n. 2
0
        private void Load(IFileStore store)
        {
            // Load
            var objFile = new OBJFile();

            using (var reader = store.OpenTextFile(m_path))
            {
                objFile.Parse(reader);
            }

            // Interpret
            var dir = AssetPath.GetDirectoryName(m_path);

            m_materialFile = AssetPath.Combine(dir, objFile.MTLLib);
            m_groups       = new List <Group>();
            m_groupLookup  = new Dictionary <string, int>();
            foreach (var objGroup in objFile.Groups)
            {
                // Skip empty groups
                if (objGroup.Faces.Count == 0)
                {
                    continue;
                }

                // Load geometry
                Geometry geometry = new Geometry(Primitive.Triangles);
                for (int i = 0; i < objGroup.Faces.Count; ++i)
                {
                    var objFace = objGroup.Faces[i];
                    if (objFace.VertexCount >= 3)
                    {
                        Vector3 posA, posB, posC;
                        Vector4 colourA, colourB, colourC;
                        Vector3?normA, normB, normC;
                        Vector2 texCoordA, texCoordB, texCoordC;
                        GetVertInfo(objFile, objFace.FirstVertex, out posA, out colourA, out texCoordA, out normA);
                        GetVertInfo(objFile, objFace.FirstVertex + 1, out posB, out colourB, out texCoordB, out normB);
                        GetVertInfo(objFile, objFace.FirstVertex + 2, out posC, out colourC, out texCoordC, out normC);

                        Vector3 faceNormal, faceTangent;
                        MathUtils.GenerateNormalAndTangent(
                            posA, posB, posC,
                            texCoordA, texCoordB, texCoordC,
                            out faceNormal, out faceTangent
                            );

                        // Add the verts
                        int firstVertIndex = geometry.VertexCount;
                        geometry.AddVertex(posA, normA.HasValue ? normA.Value : faceNormal, faceTangent, texCoordA, colourA);
                        geometry.AddVertex(posB, normB.HasValue ? normB.Value : faceNormal, faceTangent, texCoordB, colourB);
                        geometry.AddVertex(posC, normC.HasValue ? normC.Value : faceNormal, faceTangent, texCoordC, colourC);
                        for (int j = 3; j < objFace.VertexCount; ++j)
                        {
                            Vector3 posN;
                            Vector4 colourN;
                            Vector3?normN;
                            Vector2 texCoordN;
                            GetVertInfo(objFile, objFace.FirstVertex + j, out posN, out colourN, out texCoordN, out normN);
                            geometry.AddVertex(posN, normN.HasValue ? normN.Value : faceNormal, faceTangent, texCoordN, colourN);
                        }

                        // Add the indexes
                        for (int k = 2; k < objFace.VertexCount; ++k)
                        {
                            geometry.AddIndex(firstVertIndex);
                            geometry.AddIndex(firstVertIndex + k - 1);
                            geometry.AddIndex(firstVertIndex + k);
                        }
                    }
                }
                geometry.Rebuild();

                // Build shadow geometry
                var shadowGeometry = new Geometry(Primitive.Triangles);
                shadowGeometry.AddShadowGeometry(geometry, ref Matrix4.Identity);
                shadowGeometry.Rebuild();

                // Prepare the group
                var group = new Group();
                group.Name           = objGroup.Name;
                group.MaterialName   = objGroup.MaterialName;
                group.Geometry       = geometry;
                group.ShadowGeometry = shadowGeometry;

                m_groupLookup.Add(group.Name, m_groups.Count);
                m_groups.Add(group);
            }
        }