Exemple #1
0
    ///Geometry
    private void SetGeometryData(string data)
    {
        data = data.Replace("\r\n", "\n");
        string[] lines = data.Split("\n".ToCharArray());
        Vector3  v;

        mainso = new scaleoffset();
        for (int i = 0; i < lines.Length; i++)
        {
            string l = lines[i];
            l = Regex.Replace(l, @"# object", "o");        //tomekkie ALTERATION
            if (l.IndexOf("#") != -1)
            {
                l = l.Substring(0, l.IndexOf("#"));
            }
            l = Regex.Replace(l, @"\s+", " ");
            l = l.Trim();
            string[] p = l.Split(" ".ToCharArray());
            switch (p[0])
            {
            case O:
                if (!EnforceSingleObj)
                {
                    if (currentso != null)
                    {
                        currentso.vlast = vcount;
                        objso.Add(currentso);
                    }
                    buffer.PushObject(p[1].Trim());
                    currentso        = new scaleoffset();
                    currentso.vfirst = vcount;
                    currentso.name   = p[1].Trim();
                }
                break;

            case G:
                buffer.PushGroup(p[1].Trim());
                break;

            case V:
                if (p.Length >= 3)
                {
                    v = new Vector3(cf(p[1]), cf(p[2]), 0 - cf(p[3]));
                    buffer.PushVertex(v);                                //Any 0- Flipping should match normals
                    vcount++;
                    pushscaleoffset(mainso, v);
                    if (currentso != null)
                    {
                        pushscaleoffset(currentso, v);
                    }
                }
                break;

            case VT:
                if (p.Length >= 2)
                {
                    buffer.PushUV(new Vector2(cf(p[1]), cf(p[2])));
                }
                break;

            case VN:
                if (p.Length >= 3)
                {
                    buffer.PushNormal(new Vector3(cf(p[1]), cf(p[2]), 0 - cf(p[3])));                         //Any 0- Flipping should match vertex
                }
                break;

            case F:
                if (p.Length >= 4)
                {
                    if (p.Length <= 5)                            //is triangle or quad
                    {
                        string[] c;
                        for (int j = 0; j < p.Length - 3; j++)                                  //get all possible triangles from line
                        {
                            FaceIndices fi = new FaceIndices();                                 //1 vert
                            c = p[1].Trim().Split("/".ToCharArray());
                            if (c.Length > 0 && c[0] != string.Empty)
                            {
                                fi.vi = ci(c[0]) - 1;
                            }
                            if (c.Length > 1 && c[1] != string.Empty)
                            {
                                fi.vu = ci(c[1]) - 1;
                            }
                            if (c.Length > 2 && c[2] != string.Empty)
                            {
                                fi.vn = ci(c[2]) - 1;
                            }
                            buffer.PushFace(fi);
                            for (int k = 0; k < 2; k++)                                 //2nd and 3rd vert
                            {
                                fi = new FaceIndices();
                                int no = 3 - k + j;                                           //		To invert faces replace with : int no=2+k+j;
                                c = p[no].Trim().Split("/".ToCharArray());
                                if (c.Length > 0 && c[0] != string.Empty)
                                {
                                    fi.vi = ci(c[0]) - 1;
                                }
                                if (c.Length > 1 && c[1] != string.Empty)
                                {
                                    fi.vu = ci(c[1]) - 1;
                                }
                                if (c.Length > 2 && c[2] != string.Empty)
                                {
                                    fi.vn = ci(c[2]) - 1;
                                }
                                buffer.PushFace(fi);
                            }
                        }
                    }
                    else                                 //is poly try triangulate, see TriPoly script
                    {
                        TriPoly triangulation;
                        triangulation = new TriPoly();
                        Vector3[] pointlist = new Vector3[p.Length - 1];
                        //Vector3[] normallist=new Vector3[p.Length-1];
                        string[] c;
                        for (int j = 1; j < p.Length; j++)                             //go through each faceindex in poly list and pull relevant vertice from geometrybuffer add to vector[]
                        {
                            c = p[j].Trim().Split("/".ToCharArray());
                            if (c.Length > 0 && c[0] != string.Empty)
                            {
                                pointlist[j - 1] = buffer.vertices[ci(c[0]) - 1];
                            }
                            //if (c.Length > 2 && c[2] != string.Empty) {normallist[j-1] = buffer.normals[ci(c[2])-1];}
                        }
                        int[] indices;
                        //if (normallist!=null) {
                        //	indices=triangulation.Patch(pointlist, normallist);
                        //} else {
                        indices = triangulation.Patch(pointlist);                                       //, normallist
                        //}
                        if (indices.Length > 2)
                        {
                            for (int j = 0; j < indices.Length; ++j)                                 //may need to reverse this?
                            {
                                FaceIndices fi = new FaceIndices();
                                c = p[indices[j] + 1].Trim().Split("/".ToCharArray());
                                if (c.Length > 0 && c[0] != string.Empty)
                                {
                                    fi.vi = ci(c[0]) - 1;
                                }
                                if (c.Length > 1 && c[1] != string.Empty)
                                {
                                    fi.vu = ci(c[1]) - 1;
                                }
                                if (c.Length > 2 && c[2] != string.Empty)
                                {
                                    fi.vn = ci(c[2]) - 1;
                                }
                                buffer.PushFace(fi);
                            }
                        }
                    }
                }
                break;

            case MTL:
                mtllib = p[1].Trim();
                break;

            case UML:
                buffer.PushMaterialGroup(p[1].Trim());                         //hello
                break;
            }
        }
        if (currentso != null)
        {
            currentso.vlast = vcount;
            objso.Add(currentso);
        }
    }