public int GetOrAddTextureCoordinate(TextureCoordinate textureCoordinate)
        {
            int index = m_TextureCoordinates.IndexOf(textureCoordinate);

            if (index < 0)
            {
                m_TextureCoordinates.Add(textureCoordinate);
                return(m_TextureCoordinates.Count - 1);
            }

            return(index);
        }
Esempio n. 2
0
        public bool DeserializeEMAP(ModelObject modelObject, string data, int breaklineLength)
        {
            if (modelObject == null)
            {
                return(false);
            }

            List <int> indices = new List <int>();
            List <int> uvs     = new List <int>();

            //----------------------------
            // Indices (Vertex Index)
            //----------------------------
            string indiceListName = "points=";

            //Indice start index
            int vertexListStartIndex = data.IndexOf(indiceListName);

            vertexListStartIndex += indiceListName.Length;

            //Find indice end index (first breakline character)
            int vertexListEndIndex = 0;

            for (int i = vertexListStartIndex; i < data.Length; ++i)
            {
                if (data[i] == '\n' || data[i] == '\r')
                {
                    vertexListEndIndex = i;
                    break;
                }
            }

            //Split into indiceStrings
            string indiceListString = data.Substring(vertexListStartIndex, vertexListEndIndex - vertexListStartIndex);

            string[] indiceStrings = indiceListString.Split(';');

            //Loop trough them and finally deserialize
            for (int i = 0; i < indiceStrings.Length; ++i)
            {
                int  indiceIndex = 0;
                bool success     = int.TryParse(indiceStrings[i], out indiceIndex);
                if (success == false)
                {
                    Logger.LogMessage(Logger.LogType.Error, "Trying to Parse invalid Face (vertex index is not an integer)");
                    return(false);
                }

                indices.Add(indiceIndex);
            }

            //----------------------------
            // UVS
            //----------------------------
            string uvsListName = "uvs=";

            //UV start index
            int uvListStartIndex = data.IndexOf(uvsListName);

            uvListStartIndex += uvsListName.Length;

            //Find uv end index (first breakline character)
            int uvListEndIndex = 0;

            for (int i = uvListStartIndex; i < data.Length; ++i)
            {
                if (data[i] == '\n' || data[i] == '\r')
                {
                    uvListEndIndex = i;
                    break;
                }
            }

            //Split into uvStrings
            string uvsListString = data.Substring(uvListStartIndex, uvListEndIndex - uvListStartIndex);

            string[] uvStrings = uvsListString.Split(';');

            //Loop trough them and finally deserialize
            for (int i = 0; i < uvStrings.Length; ++i)
            {
                TextureCoordinate newTextureCoordinate = new TextureCoordinate();
                bool success = newTextureCoordinate.Deserialize(uvStrings[i], ',');

                if (success == false)
                {
                    return(false);
                }

                //As they are stored in the ModelObject, we need to send them there and use their index here
                int textureCoordinateIndex = modelObject.GetOrAddTextureCoordinate(newTextureCoordinate);
                uvs.Add(textureCoordinateIndex);
            }

            //----------------------------
            // Create FaceVertices
            //----------------------------
            if (indices.Count != uvs.Count)
            {
                Logger.LogMessage(Logger.LogType.Error, "Face has different amount of indices & uvs");
                return(false);
            }

            for (int i = 0; i < indices.Count; ++i)
            {
                FaceVertex newFaceVertex = new FaceVertex();
                bool       success       = newFaceVertex.DeserializeEMAP(indices[i], uvs[i]);

                if (success == false)
                {
                    return(false);
                }

                m_FaceVertices.Add(newFaceVertex);
            }

            return(true);
        }
Esempio n. 3
0
        public string SerializeEMAP(ref int seed, List <TextureCoordinate> textureCoordinates)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("Face{");

            //Default surface settings
            stringBuilder.AppendLine("surf={");
            stringBuilder.AppendLine("localMapping=False");
            stringBuilder.AppendLine("mappingType=5"); //Todo: now only local
            stringBuilder.AppendLine("material=0");
            stringBuilder.AppendLine("color=0");
            stringBuilder.AppendLine("colorEmissive=0");
            stringBuilder.AppendLine("seed=" + seed);
            stringBuilder.AppendLine("halfRes=False");
            stringBuilder.AppendLine("uvScaleBias=1,1,0,0");
            stringBuilder.AppendLine("uvScroll=0,0");
            stringBuilder.AppendLine("localOffset=0,0,0");
            stringBuilder.AppendLine("worldOffset=0,0,0");
            stringBuilder.AppendLine("}");

            //Vertices
            StringBuilder pointStringBuilder = new StringBuilder("points=");

            for (int i = 0; i < m_FaceVertices.Count; ++i)
            {
                if (i != 0)
                {
                    pointStringBuilder.Append(";");
                }
                pointStringBuilder.Append(m_FaceVertices[i].VertexIndex);
            }
            stringBuilder.AppendLine(pointStringBuilder.ToString());

            //UV
            StringBuilder uvStringBuilder = new StringBuilder("uvs=");

            for (int i = 0; i < m_FaceVertices.Count; ++i)
            {
                if (i != 0)
                {
                    uvStringBuilder.Append(";");
                }

                //In case there are no texture coordinates in the file
                if (textureCoordinates.Count == 0 || m_FaceVertices[i].TextureCoordinateIndex < 0)
                {
                    uvStringBuilder.Append("0,0");
                }
                else
                {
                    TextureCoordinate textureCoordinate = textureCoordinates[m_FaceVertices[i].TextureCoordinateIndex]; //0 based!
                    uvStringBuilder.Append(textureCoordinate.Serialize(','));
                }
            }
            stringBuilder.AppendLine(uvStringBuilder.ToString());

            stringBuilder.AppendLine("}");

            seed += 1;
            return(stringBuilder.ToString());
        }
        //Serialization
        public bool DeserializeOBJPart(string data, int vertexIndexOffset, int textureCoordinateIndexOffset, int normalIndexOffset)
        {
            //NOTE: We are only parsing a very small selection of OBJ features!
            //Full list of features: https://en.wikipedia.org/wiki/Wavefront_.obj_file

            //Vertex
            if (data.StartsWith("v "))
            {
                //Remove the "v " so Vector4f can parse it (w is optional)
                data = data.Substring(2, data.Length - 2);
                Vertex newVertex = new Vertex();
                bool   success   = newVertex.Deserialize(data);

                if (success == true)
                {
                    m_Vertices.Add(newVertex);
                }

                return(success);
            }

            //Texture coordinate
            else if (data.StartsWith("vt "))
            {
                //Remove the "vt " so Vector3f can parse it (w is optional)
                data = data.Substring(3, data.Length - 3);
                TextureCoordinate newTextureCoordinate = new TextureCoordinate();
                bool success = newTextureCoordinate.Deserialize(data);

                if (success == true)
                {
                    m_TextureCoordinates.Add(newTextureCoordinate);
                }

                return(success);
            }

            //Normal (currently not used by Prodeus, but here for the sake of completeness)
            else if (data.StartsWith("vn "))
            {
                //Remove the "vn " so Vector3f can parse it
                data = data.Substring(3, data.Length - 3);
                Normal newNormal = new Normal();
                bool   success   = newNormal.Deserialize(data);

                if (success == true)
                {
                    m_Normals.Add(newNormal);
                }

                return(success);
            }

            //Face
            else if (data.StartsWith("f "))
            {
                Face newFace = new Face();
                bool success = newFace.DeserializeOBJ(data, vertexIndexOffset, textureCoordinateIndexOffset, normalIndexOffset);

                if (success == true)
                {
                    m_Faces.Add(newFace);
                }

                return(success);
            }

            return(true);
        }