Example #1
0
        private OBJIndex ParseOBJIndex(String token)
        {
            String[] values = token.Split('/');

            OBJIndex result = new OBJIndex();

            result.SetVertexIndex(Utils.ParseInt(values[0]) - 1);

            if (values.Length > 1)
            {
                if (!String.IsNullOrEmpty(values[1]))
                {
                    m_hasTexCoords = true;
                    result.SetTexCoordIndex(Utils.ParseInt(values[1]) - 1);
                }

                if (values.Length > 2)
                {
                    m_hasNormals = true;
                    result.SetNormalIndex(Utils.ParseInt(values[2]) - 1);
                }
            }

            return(result);
        }
Example #2
0
    Mesh ToMesh()
    {
        var result   = new Mesh();
        var indexMap = new Dictionary <OBJIndex, int>();

        for (int i = 0; i < indices.Count; ++i)
        {
            OBJIndex index = indices[i];

            Vector4 position = positions[index.VertexIndex];

            Vector4 texCoord = hasTexCoords ?
                               texCoords[index.TexCoordIndex]
                : new Vector4(0, 0, 0, 0);

            Vector4 normal = hasNormals ?
                             normals[index.NormalIndex]
                : new Vector4(0, 0, 0, 0);

            bool found = indexMap.TryGetValue(index, out int vertexIndex);

            if (!found)
            {
                vertexIndex     = result.Vertices.Count;
                indexMap[index] = vertexIndex;
                result.Vertices.Add(new Vertex(position, texCoord, normal));
            }

            result.Indices.Add(vertexIndex);
        }

        return(result);
    }
Example #3
0
            override public bool Equals(Object obj)
            {
                OBJIndex index = (OBJIndex)obj;

                return(m_vertexIndex == index.m_vertexIndex &&
                       m_texCoordIndex == index.m_texCoordIndex &&
                       m_normalIndex == index.m_normalIndex);
            }
Example #4
0
        public IndexedModel ToIndexedModel()
        {
            IndexedModel result      = new IndexedModel();
            IndexedModel normalModel = new IndexedModel();
            Dictionary <OBJIndex, int> resultIndexMap = new Dictionary <OBJIndex, int>();
            Dictionary <int, int>      normalIndexMap = new Dictionary <int, int>();
            Dictionary <int, int>      indexMap       = new Dictionary <int, int>();

            for (int i = 0; i < m_indices.Count(); i++)
            {
                OBJIndex currentIndex = m_indices[i];

                Vector currentPosition = m_positions[currentIndex.GetVertexIndex()];
                Vector currentTexCoord;
                Vector currentNormal;

                if (m_hasTexCoords)
                {
                    currentTexCoord = m_texCoords[currentIndex.GetTexCoordIndex()];
                }
                else
                {
                    currentTexCoord = new Vector(0, 0, 0, 0);
                }

                if (m_hasNormals)
                {
                    currentNormal = m_normals[currentIndex.GetNormalIndex()];
                }
                else
                {
                    currentNormal = new Vector(0, 0, 0, 0);
                }

                int modelVertexIndex = 0;

                if (!resultIndexMap.ContainsKey(currentIndex))
                {
                    modelVertexIndex = result.GetPositions().Count();
                    resultIndexMap.Add(currentIndex, modelVertexIndex);

                    result.GetPositions().Add(currentPosition);
                    result.GetTexCoords().Add(currentTexCoord);
                    if (m_hasNormals)
                    {
                        result.GetNormals().Add(currentNormal);
                    }
                }
                else
                {
                    modelVertexIndex = resultIndexMap[currentIndex];
                }

                int normalModelIndex = 0;

                if (!normalIndexMap.ContainsKey(currentIndex.GetVertexIndex()))
                {
                    normalModelIndex = normalModel.GetPositions().Count();
                    normalIndexMap.Add(currentIndex.GetVertexIndex(), normalModelIndex);

                    normalModel.GetPositions().Add(currentPosition);
                    normalModel.GetTexCoords().Add(currentTexCoord);
                    normalModel.GetNormals().Add(currentNormal);
                    normalModel.GetTangents().Add(new Vector(0, 0, 0, 0));
                }
                else
                {
                    normalModelIndex = normalIndexMap[currentIndex.GetVertexIndex()];
                }

                result.GetIndices().Add(modelVertexIndex);
                normalModel.GetIndices().Add(normalModelIndex);

                if (!indexMap.ContainsKey(modelVertexIndex))
                {
                    indexMap.Add(modelVertexIndex, normalModelIndex);
                }
            }

            if (!m_hasNormals)
            {
                normalModel.CalcNormals();

                for (int i = 0; i < result.GetPositions().Count; i++)
                {
                    result.GetNormals().Add(normalModel.GetNormals()[indexMap[i]]);
                }
            }

            normalModel.CalcTangents();

            for (int i = 0; i < result.GetPositions().Count; i++)
            {
                result.GetTangents().Add(normalModel.GetTangents()[indexMap[i]]);
            }

            return(result);
        }