Ejemplo n.º 1
0
            private static void ParseVertices(StreamReader reader, MqoObject mqoObject)
            {
                MqoVertex[] vertices = null;
                string      line;

                while ((line = reader.ReadLine()) != null)
                {
                    int countStart = line.IndexOf("visible");
                    if (countStart >= 0)
                    {
                        int visible = Int32.Parse(line.Substring(countStart + 7 + 1));
                        if (visible > 0)
                        {
                            mqoObject.visible = true;
                        }
                        continue;
                    }
                    countStart = line.IndexOf("vertex");
                    if (countStart >= 0)
                    {
                        countStart += 7;
                        int countEnd    = line.IndexOf(' ', countStart);
                        int vertexCount = Int32.Parse(line.Substring(countStart, countEnd - countStart));
                        vertices = new MqoVertex[vertexCount];

                        for (int i = 0; i < vertexCount; i++)
                        {
                            MqoVertex vertex = new MqoVertex();
                            line = reader.ReadLine();
                            string[] sArray = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            float[]  coords = new float[] { Utility.ParseFloat(sArray[0]), Utility.ParseFloat(sArray[1]), Utility.ParseFloat(sArray[2]) };

                            for (int j = 0; j < 3; j++)
                            {
                                coords[j] /= 10f;
                                if (coords[j].Equals(Single.NaN))
                                {
                                    throw new Exception("vertex " + i + " has invalid coordinates in mesh object " + mqoObject.fullname);
                                }
                            }
                            vertex.coords = new Vector3(coords[0], coords[1], coords[2]);
                            vertices[i]   = vertex;
                        }
                        break;
                    }
                }
                mqoObject.vertices = vertices;
            }
Ejemplo n.º 2
0
            private static void ParseFaces(StreamReader reader, MqoObject mqoObject)
            {
                List <MqoFace> faceList = null;
                string         line;

                while ((line = reader.ReadLine()) != null)
                {
                    int countStart = line.IndexOf("face");
                    if (countStart >= 0)
                    {
                        countStart += 5;
                        int countEnd  = line.IndexOf(' ', countStart);
                        int faceCount = Int32.Parse(line.Substring(countStart, countEnd - countStart));
                        faceList = new List <MqoFace>(faceCount);

                        for (int i = 0; i < faceCount; i++)
                        {
                            // get vertex indices & uv
                            line = reader.ReadLine();
                            string[] sArray      = line.Split(new char[] { '\t', ' ', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                            int      numVertices = Int32.Parse(sArray[0]);
                            if (numVertices > 3)
                            {
                                throw new Exception("Face " + i + " in mesh object " + mqoObject.fullname + " has more than 3 vertices. Triangulate the meshes");
                            }
                            else if (numVertices < 3)
                            {
                                Report.ReportLog("Warning: Skipping face " + i + " in mesh object " + mqoObject.fullname + " because it has a less than 3 vertices");
                            }
                            else
                            {
                                MqoFace face = new MqoFace();
                                faceList.Add(face);

                                for (int j = 1; j < sArray.Length; j++)
                                {
                                    if (sArray[j].ToUpper() == "V")
                                    {
                                        for (int k = 0; k < face.vertexIndices.Length; k++)
                                        {
                                            face.vertexIndices[k] = Int32.Parse(sArray[++j]);
                                        }
                                    }
                                    else if (sArray[j].ToUpper() == "M")
                                    {
                                        face.materialIndex = Int32.Parse(sArray[++j]);
                                    }
                                    else if (sArray[j].ToUpper() == "UV")
                                    {
                                        for (int k = 0; k < face.UVs.Length; k++)
                                        {
                                            face.UVs[k] = new float[2] {
                                                Utility.ParseFloat(sArray[++j]), Utility.ParseFloat(sArray[++j])
                                            };
                                        }
                                    }
                                    else if (sArray[j].ToUpper() == "COL")
                                    {
                                        for (int k = 0; k < face.vertexIndices.Length; k++)
                                        {
                                            j++;
                                            if (!(mqoObject.vertices[face.vertexIndices[k]] is MqoVertexWithColour))
                                            {
                                                MqoVertexWithColour vertCol = new MqoVertexWithColour();
                                                vertCol.coords = mqoObject.vertices[face.vertexIndices[k]].coords;
                                                uint abgr = uint.Parse(sArray[j]);
                                                int  argb = (int)(abgr & 0xFF00FF00) | (int)((abgr & 0x00FF0000) >> 16) | (int)((abgr & 0x000000FF) << 16);
                                                vertCol.colour = new Color4(argb);
                                                mqoObject.vertices[face.vertexIndices[k]] = vertCol;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
                mqoObject.faces = faceList.ToArray();
            }