Example #1
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);
        }