示例#1
0
        protected float LoadObj()
        {
            Positions.Clear();
            Normals.Clear();
            Texcoords.Clear();

            Groups.Clear();
            mMtrls.Clear();

            mCurrentGroup    = null;
            mCurrentMtrlName = null;
            mCurrentMtrl     = default(ObjMaterial);

            float start = 0.0f;

            mDirectory = Path.GetDirectoryName(FileName) + "\\";

            mCurrentGroup = new ObjGroup("default", "default");
            Groups.Add(mCurrentGroup);

            ParseFile(ParseObjToken, FileName);
            foreach (var objGroup in Groups)
            {
                if (objGroup.TexCoords.Any())
                {
                    Texcoords.AddRange(objGroup.TexCoords);//NormalizeTexCoords(
                }
            }
            CreateNormals();
            return(start);
        }
示例#2
0
        protected bool ValidateGroup(ObjGroup group)
        {
            for (int i = 0; i < group.VIndices.Count; i++)
            {
                System.Diagnostics.Debug.Assert(group.VIndices[i].X < Positions.Count);
                System.Diagnostics.Debug.Assert(group.VIndices[i].Y < Positions.Count);
                System.Diagnostics.Debug.Assert(group.VIndices[i].Z < Positions.Count);

                if (i < group.NIndices.Count && group.NIndices.Count > 0 && Normals.Count > 0)
                {
                    System.Diagnostics.Debug.Assert(group.NIndices[i].X < Normals.Count);
                    System.Diagnostics.Debug.Assert(group.NIndices[i].Y < Normals.Count);
                    System.Diagnostics.Debug.Assert(group.NIndices[i].Z < Normals.Count);
                }

                if (i < group.TIndices.Count && group.TIndices.Count > 0 && Texcoords.Count > 0)
                {
                    System.Diagnostics.Debug.Assert(group.TIndices[i].X < Texcoords.Count);
                    System.Diagnostics.Debug.Assert(group.TIndices[i].Y < Texcoords.Count);
                    System.Diagnostics.Debug.Assert(group.TIndices[i].Z < Texcoords.Count);
                }
            }
            return(true);
        }
示例#3
0
        protected void ParseObjToken(string line, string[] tokens)
        {
            string token           = tokens[0].ToLower();
            int    lastObjTexCoord = 0;

            try
            {
                switch (token)
                {
                case "v":
                {
                    float x = float.Parse(tokens[1], CultureInfo.InvariantCulture);
                    float y = float.Parse(tokens[2], CultureInfo.InvariantCulture);
                    float z = float.Parse(tokens[3], CultureInfo.InvariantCulture);

                    Vector3 p = new Vector3(x * Scale, y * Scale, z * Scale);
                    BBox.AddFloat3(p);

                    Positions.Add(p);
                    break;
                }

                case "vn":
                {
                    if (ParseNormals)
                    {
                        float x = float.Parse(tokens[1], CultureInfo.InvariantCulture);
                        float y = float.Parse(tokens[2], CultureInfo.InvariantCulture);
                        float z = float.Parse(tokens[3], CultureInfo.InvariantCulture);

                        Normals.Add(new Vector3(x, y, z));
                    }
                    break;
                }

                case "vt":
                {
                    float x = float.Parse(tokens[1], CultureInfo.InvariantCulture);
                    float y = float.Parse(tokens[2], CultureInfo.InvariantCulture);

                    //Texcoords.Add(new Vector2(x,y));
                    mCurrentGroup.TexCoords.Add(new Vector2(x, y));
                    break;
                }

                case "f":
                {
                    int delta = 1;
                    int v0 = 0, v1 = 0, v2 = 0;

                    if (tokens.Length >= 10)
                    {
                        //full v, vn, vt indices
                        v0 = int.Parse(tokens[1]);
                        v1 = int.Parse(tokens[4]);
                        v2 = int.Parse(tokens[7]);

                        mCurrentGroup.VIndices.Add(new Int3(v0 - delta, v1 - delta, v2 - delta));

                        v0 = int.Parse(tokens[2]);
                        v1 = int.Parse(tokens[5]);
                        v2 = int.Parse(tokens[8]);

                        mCurrentGroup.TIndices.Add(new Int3(v0 - delta, v1 - delta, v2 - delta));

                        if (ParseNormals)
                        {
                            v0 = int.Parse(tokens[3]);
                            v1 = int.Parse(tokens[6]);
                            v2 = int.Parse(tokens[9]);

                            mCurrentGroup.NIndices.Add(new Int3(v0 - delta, v1 - delta, v2 - delta));
                        }
                    }
                    else if (tokens.Length >= 7)
                    {
                        v0 = int.Parse(tokens[1]);
                        v1 = int.Parse(tokens[3]);
                        v2 = int.Parse(tokens[5]);

                        mCurrentGroup.VIndices.Add(new Int3(v0 - delta, v1 - delta, v2 - delta));

                        v0 = int.Parse(tokens[2]);
                        v1 = int.Parse(tokens[4]);
                        v2 = int.Parse(tokens[6]);

                        if (line.Contains("//"))
                        {
                            if (ParseNormals)
                            {
                                mCurrentGroup.NIndices.Add(new Int3(v0 - delta, v1 - delta, v2 - delta));
                            }
                        }
                        else
                        {
                            mCurrentGroup.TIndices.Add(new Int3(v0 - delta, v1 - delta, v2 - delta));
                        }
                    }
                    else if (tokens.Length >= 4)
                    {
                        //v indices
                        v0 = int.Parse(tokens[1]);
                        v1 = int.Parse(tokens[2]);
                        v2 = int.Parse(tokens[3]);

                        mCurrentGroup.VIndices.Add(new Int3(v0 - delta, v1 - delta, v2 - delta));
                    }

                    break;
                }

                case "g":
                {
                    if (!ParseMaterials)
                    {
                        break;
                    }

                    string name = tokens[1];
                    mCurrentGroupName = name;
                    lastObjTexCoord   = Texcoords.Count;
                    mCurrentGroup     = new ObjGroup(name, name)
                    {
                        mtrl = mCurrentMtrlName
                    };
                    Groups.Add(mCurrentGroup);
                    break;
                }

                case "usemtl":
                {
                    if (!ParseMaterials)
                    {
                        break;
                    }

                    string name = tokens[1];
                    //if (mCurrentGroup != null){ Texcoords.AddRange(NormalizeTexCoords(mCurrentGroup.TexCoords));}
                    //string groupname = mCurrentGroupName + name;

                    //mCurrentGroup = new ObjGroup( groupname, mCurrentGroupName );
                    //Groups.Add( mCurrentGroup );

                    mCurrentMtrlName   = name;
                    mCurrentGroup.mtrl = name;

                    break;
                }

                case "mtllib":
                {
                    if (!ParseMaterials)
                    {
                        break;
                    }

                    mCurrentMtrl = default(ObjMaterial);

                    string name = mDirectory + tokens[1];
                    ParseFile(ParseMtrlToken, name);

                    if (mCurrentMtrlName != null)
                    {
                        mMtrls.Add(mCurrentMtrlName, mCurrentMtrl);
                    }
                    mCurrentMtrlName = null;
                    break;
                }

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error parsing obj token: " + ex.Message);
            }
        }