Beispiel #1
0
        // IVec3ArrayIterator
        public void addVec3Array(Vec3[] points, int numPoints)
        {
            ObjFace f = new ObjFace();

            f.setFirstVertex(faceVerts.Count);
            f.setNumVerts(numPoints);
            faces.Add(f);
            for (int i = 0; i < numPoints; i++)
            {
                faceVerts.Add(new ObjFaceVertex(xyzs.Count + i));
            }
            for (int i = 0; i < numPoints; i++)
            {
                xyzs.Add(points[i]);
            }
        }
Beispiel #2
0
        public void addIndexedMesh(Vec3[] mPoints, int numPoints, short numFaces, short[] fIndices, short[] fOffsets, short[] fCounts)
        {
            int firstXyz = xyzs.Count;

            for (int i = 0; i < numPoints; i++)
            {
                xyzs.Add(mPoints[i]);
            }
            for (int i = 0; i < numFaces; i++)
            {
                int     numFaceVerts = fCounts[i];
                ObjFace f            = new ObjFace();
                f.setFirstVertex(faceVerts.Count);
                f.setNumVerts(numFaceVerts);
                for (int j = 0; j < numFaceVerts; j++)
                {
                    int xyzIndex = firstXyz + fIndices[fOffsets[i] + j];
                    faceVerts.Add(new ObjFaceVertex(xyzIndex));
                }
                faces.Add(f);
            }
        }
Beispiel #3
0
        public bool loadObjModel(string fileName)
        {
            StreamReader r          = new StreamReader(fileName);
            string       fileData   = r.ReadToEnd();
            int          lineNumber = 0;
            ObjObject    lastObject = null;
            ObjGroup     lastGroup  = null;

            foreach (string line in fileData.Split('\n'))
            {
                lineNumber++;
                // skip comments
                int    commentStart = line.IndexOf('#');
                string sl;
                if (commentStart >= 0)
                {
                    sl = line.Substring(0, commentStart);
                }
                else
                {
                    sl = line;
                }
                // remove extra whitespaces
                sl.Trim();
                // tokenize
                string[] tokens = sl.Split(lineSplitChars, StringSplitOptions.RemoveEmptyEntries);
                if (tokens == null || tokens.Length == 0)
                {
                    continue;
                }
                switch (tokens[0])
                {
                case "mtllib":
                {
                    if (tokens.Length < 2)
                    {
                        showOBJParseError("Missing materialName after 'mtllib' token at line " + lineNumber);
                        return(true);
                    }
                    else
                    {
                        string mtlLibName = tokens[1];
                    }
                }
                break;

                case "o":
                {
                    if (tokens.Length < 2)
                    {
                        showOBJParseError("Missing objectName after 'o' token at line " + lineNumber);
                        return(true);
                    }
                    else
                    {
                        if (lastObject != null)
                        {
                            lastObject.setNumFaces(faces.Count - lastObject.getFirstFace());
                        }
                        string    objectName = tokens[1];
                        ObjObject no         = new ObjObject();
                        no.setName(objectName);
                        no.setFirstFace(faces.Count);
                        objects.Add(no);
                        lastObject = no;
                    }
                }
                break;

                case "g":
                {
                    if (tokens.Length < 2)
                    {
                        showOBJParseError("Missing groupName after 'g' token at line " + lineNumber);
                        return(true);
                    }
                    else
                    {
                        if (lastGroup != null)
                        {
                            lastGroup.setNumFaces(faces.Count - lastGroup.getFirstFace());
                        }
                        string   groupName = tokens[1];
                        ObjGroup no        = new ObjGroup();
                        no.setName(groupName);
                        no.setFirstFace(faces.Count);
                        groups.Add(no);
                        lastObject = no;
                    }
                }
                break;

                case "v":
                {
                    if (tokens.Length < 4)
                    {
                        showOBJParseError("Missing groupName after 'g' token at line " + lineNumber);
                        return(true);
                    }
                    else
                    {
                        float x, y, z;
                        if (float.TryParse(tokens[1].Replace('.', ','), out x) == false)
                        {
                            showOBJParseError("'X' component of 'v' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        if (float.TryParse(tokens[2].Replace('.', ','), out y) == false)
                        {
                            showOBJParseError("'Y' component of 'v' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        if (float.TryParse(tokens[3].Replace('.', ','), out z) == false)
                        {
                            showOBJParseError("'Z' component of 'v' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        xyzs.Add(new Vec3(x, y, z));
                    }
                }
                break;

                case "vt":
                {
                    if (tokens.Length < 3)
                    {
                        showOBJParseError("Missing texcoord values after 'vt' token at line " + lineNumber);
                        return(true);
                    }
                    else
                    {
                        float x, y;
                        if (float.TryParse(tokens[1].Replace('.', ','), out x) == false)
                        {
                            showOBJParseError("'X' component of 'vt' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        if (float.TryParse(tokens[2].Replace('.', ','), out y) == false)
                        {
                            showOBJParseError("'Y' component of 'v' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        texCoords.Add(new Vec2(x, y));
                    }
                }
                break;

                case "vn":
                {
                    if (tokens.Length < 4)
                    {
                        showOBJParseError("Missing normal values after 'vn' token at line " + lineNumber);
                        return(true);
                    }
                    else
                    {
                        float x, y, z;
                        if (float.TryParse(tokens[1].Replace('.', ','), out x) == false)
                        {
                            showOBJParseError("'X' component of 'vn' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        if (float.TryParse(tokens[2].Replace('.', ','), out y) == false)
                        {
                            showOBJParseError("'Y' component of 'vn' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        if (float.TryParse(tokens[3].Replace('.', ','), out z) == false)
                        {
                            showOBJParseError("'Z' component of 'vn' is not a valid float at line " + lineNumber);
                            return(true);
                        }
                        normals.Add(new Vec3(x, y, z));
                    }
                }
                break;

                case "f":
                {
                    if (tokens.Length < 4)
                    {
                        showOBJParseError("Missing face vertex indices after 'f' token at line " + lineNumber);
                        return(true);
                    }
                    else
                    {
                        int     numFaceVerts = tokens.Length - 1;
                        ObjFace face         = new ObjFace();
                        face.setFirstVertex(faceVerts.Count);
                        face.setNumVerts(numFaceVerts);
                        for (int i = 0; i < numFaceVerts; i++)
                        {
                            string        faceVertexString = tokens[1 + i];
                            ObjFaceVertex faceVert         = new ObjFaceVertex();
                            if (faceVert.setFromString(faceVertexString))
                            {
                                showOBJParseError("Vertex string '" + faceVertexString + "' is not a valid at line " + lineNumber);
                                return(true);
                            }
                            faceVerts.Add(faceVert);
                        }
                        faces.Add(face);
                    }
                }
                break;

                default:
                {
                }
                break;
                }
            }
            if (lastObject != null)
            {
                lastObject.setNumFaces(faces.Count - lastObject.getFirstFace());
            }
            if (lastGroup != null)
            {
                lastGroup.setNumFaces(faces.Count - lastGroup.getFirstFace());
            }

            /* if (objects.Count == 0)
             * {
             *   ObjObject obj = new ObjObject();
             *   obj.setName("default_object");
             *   obj.setFirstFace(0);
             *   obj.setNumFaces(faces.Count);
             *   objects.Add(obj);
             * }
             * if (groups.Count == 0)
             * {
             *   ObjGroup obj = new ObjGroup();
             *   obj.setName("default_group");
             *   obj.setFirstFace(0);
             *   obj.setNumFaces(faces.Count);
             *   groups.Add(obj);
             * }*/
            return(false);
        }